Search in sources :

Example 6 with ColumnSource

use of com.robotoworks.mechanoid.db.sqliteModel.ColumnSource in project mechanoid by robotoworks.

the class ContentProviderContractGenerator method _generateBuilderSetters.

protected CharSequence _generateBuilderSetters(final CreateViewStatement stmt) {
    StringConcatenation _builder = new StringConcatenation();
    ArrayList<ColumnSource> cols = ModelUtil.getViewResultColumns(stmt);
    _builder.newLineIfNotEmpty();
    {
        final Function1<ColumnSource, Boolean> _function = new Function1<ColumnSource, Boolean>() {

            public Boolean apply(final ColumnSource it) {
                String _name = it.getName();
                boolean _equals = _name.equals("_id");
                return Boolean.valueOf((!_equals));
            }
        };
        Iterable<ColumnSource> _filter = IterableExtensions.<ColumnSource>filter(cols, _function);
        for (final ColumnSource item : _filter) {
            ResultColumn col = ((ResultColumn) item);
            _builder.newLineIfNotEmpty();
            ColumnType type = ModelUtil.getInferredColumnType(col);
            _builder.newLineIfNotEmpty();
            _builder.append("public Builder set");
            String _name = col.getName();
            String _pascalize = Strings.pascalize(_name);
            _builder.append(_pascalize, "");
            _builder.append("(");
            String _javaTypeName = ModelUtil.toJavaTypeName(type);
            _builder.append(_javaTypeName, "");
            _builder.append(" value) {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("mValues.put(");
            String _name_1 = stmt.getName();
            String _pascalize_1 = Strings.pascalize(_name_1);
            _builder.append(_pascalize_1, "\t");
            _builder.append(".");
            String _name_2 = col.getName();
            String _underscore = Strings.underscore(_name_2);
            String _upperCase = _underscore.toUpperCase();
            _builder.append(_upperCase, "\t");
            _builder.append(", value);");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("return this;");
            _builder.newLine();
            _builder.append("}");
            _builder.newLine();
        }
    }
    return _builder;
}
Also used : ColumnType(com.robotoworks.mechanoid.db.sqliteModel.ColumnType) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) ResultColumn(com.robotoworks.mechanoid.db.sqliteModel.ResultColumn) ColumnSource(com.robotoworks.mechanoid.db.sqliteModel.ColumnSource)

Example 7 with ColumnSource

use of com.robotoworks.mechanoid.db.sqliteModel.ColumnSource in project mechanoid by robotoworks.

the class ContentProviderContractGenerator method generate.

public CharSequence generate(final Model model, final SqliteDatabaseSnapshot snapshot) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/*");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* Generated by Robotoworks Mechanoid");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("package ");
    String _packageName = model.getPackageName();
    _builder.append(_packageName, "");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("import android.net.Uri;");
    _builder.newLine();
    _builder.append("import android.provider.BaseColumns;");
    _builder.newLine();
    _builder.append("import com.robotoworks.mechanoid.Mechanoid;");
    _builder.newLine();
    _builder.append("import com.robotoworks.mechanoid.db.AbstractValuesBuilder;");
    _builder.newLine();
    _builder.append("import java.lang.reflect.Field;\t\t\t");
    _builder.newLine();
    _builder.append("import java.util.Collections;");
    _builder.newLine();
    _builder.append("import java.util.HashSet;");
    _builder.newLine();
    _builder.append("import java.util.HashMap;");
    _builder.newLine();
    _builder.append("import java.util.Set;");
    _builder.newLine();
    _builder.append("import java.util.Map;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("public class ");
    DatabaseBlock _database = model.getDatabase();
    String _name = _database.getName();
    String _pascalize = Strings.pascalize(_name);
    _builder.append(_pascalize, "");
    _builder.append("Contract  {");
    _builder.newLineIfNotEmpty();
    _builder.append("    ");
    _builder.append("public static final String CONTENT_AUTHORITY = initAuthority();");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("private static String initAuthority() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("String authority = \"");
    String _packageName_1 = model.getPackageName();
    _builder.append(_packageName_1, "\t\t");
    _builder.append(".");
    DatabaseBlock _database_1 = model.getDatabase();
    String _name_1 = _database_1.getName();
    String _lowerCase = _name_1.toLowerCase();
    _builder.append(_lowerCase, "\t\t");
    _builder.append("\";");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("try {");
    _builder.newLine();
    _builder.append("    \t\t");
    _builder.newLine();
    _builder.append("    \t\t");
    _builder.append("ClassLoader loader = ");
    DatabaseBlock _database_2 = model.getDatabase();
    String _name_2 = _database_2.getName();
    String _pascalize_1 = Strings.pascalize(_name_2);
    _builder.append(_pascalize_1, "    \t\t");
    _builder.append("Contract.class.getClassLoader();");
    _builder.newLineIfNotEmpty();
    _builder.append("    \t\t");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("Class<?> clz = loader.loadClass(\"");
    String _packageName_2 = model.getPackageName();
    _builder.append(_packageName_2, "\t\t\t");
    _builder.append(".");
    DatabaseBlock _database_3 = model.getDatabase();
    String _name_3 = _database_3.getName();
    String _pascalize_2 = Strings.pascalize(_name_3);
    _builder.append(_pascalize_2, "\t\t\t");
    _builder.append("ContentProviderAuthority\");");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t\t");
    _builder.append("Field declaredField = clz.getDeclaredField(\"CONTENT_AUTHORITY\");");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("authority = declaredField.get(null).toString();");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("} catch (ClassNotFoundException e) {} ");
    _builder.newLine();
    _builder.append("    \t");
    _builder.append("catch (NoSuchFieldException e) {} ");
    _builder.newLine();
    _builder.append("    \t");
    _builder.append("catch (IllegalArgumentException e) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("} catch (IllegalAccessException e) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return authority;");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("private static final Uri BASE_CONTENT_URI = Uri.parse(\"content://\" + CONTENT_AUTHORITY);");
    _builder.newLine();
    _builder.newLine();
    {
        Collection<CreateTableStatement> _tables = snapshot.getTables();
        final Function1<CreateTableStatement, String> _function = new Function1<CreateTableStatement, String>() {

            public String apply(final CreateTableStatement x) {
                return x.getName();
            }
        };
        List<CreateTableStatement> _sortBy = IterableExtensions.<CreateTableStatement, String>sortBy(_tables, _function);
        for (final CreateTableStatement tbl : _sortBy) {
            _builder.append("\t");
            _builder.append("interface ");
            String _name_4 = tbl.getName();
            String _pascalize_3 = Strings.pascalize(_name_4);
            _builder.append(_pascalize_3, "\t");
            _builder.append("Columns {");
            _builder.newLineIfNotEmpty();
            {
                EList<ColumnSource> _columnDefs = tbl.getColumnDefs();
                final Function1<ColumnSource, Boolean> _function_1 = new Function1<ColumnSource, Boolean>() {

                    public Boolean apply(final ColumnSource it) {
                        String _name = it.getName();
                        boolean _equals = _name.equals("_id");
                        return Boolean.valueOf((!_equals));
                    }
                };
                Iterable<ColumnSource> _filter = IterableExtensions.<ColumnSource>filter(_columnDefs, _function_1);
                for (final ColumnSource col : _filter) {
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("String ");
                    String _name_5 = col.getName();
                    String _underscore = Strings.underscore(_name_5);
                    String _upperCase = _underscore.toUpperCase();
                    _builder.append(_upperCase, "\t\t");
                    _builder.append(" = \"");
                    String _name_6 = col.getName();
                    _builder.append(_name_6, "\t\t");
                    _builder.append("\";");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
        }
    }
    {
        Collection<CreateViewStatement> _views = snapshot.getViews();
        final Function1<CreateViewStatement, String> _function_2 = new Function1<CreateViewStatement, String>() {

            public String apply(final CreateViewStatement x) {
                return x.getName();
            }
        };
        List<CreateViewStatement> _sortBy_1 = IterableExtensions.<CreateViewStatement, String>sortBy(_views, _function_2);
        for (final CreateViewStatement vw : _sortBy_1) {
            _builder.append("\t");
            _builder.append("interface ");
            String _name_7 = vw.getName();
            String _pascalize_4 = Strings.pascalize(_name_7);
            _builder.append(_pascalize_4, "\t");
            _builder.append("Columns {");
            _builder.newLineIfNotEmpty();
            {
                ArrayList<ColumnSource> _viewResultColumns = ModelUtil.getViewResultColumns(vw);
                final Function1<ColumnSource, Boolean> _function_3 = new Function1<ColumnSource, Boolean>() {

                    public Boolean apply(final ColumnSource it) {
                        String _name = it.getName();
                        boolean _equals = _name.equals("_id");
                        return Boolean.valueOf((!_equals));
                    }
                };
                Iterable<ColumnSource> _filter_1 = IterableExtensions.<ColumnSource>filter(_viewResultColumns, _function_3);
                for (final ColumnSource col_1 : _filter_1) {
                    _builder.append("\t");
                    _builder.append("\t");
                    CharSequence _generateInterfaceMemberForResultColumn = this.generateInterfaceMemberForResultColumn(col_1);
                    _builder.append(_generateInterfaceMemberForResultColumn, "\t\t");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
        }
    }
    {
        Collection<CreateTableStatement> _configInitTables = ModelUtil.getConfigInitTables(model);
        final Function1<CreateTableStatement, String> _function_4 = new Function1<CreateTableStatement, String>() {

            public String apply(final CreateTableStatement x) {
                return x.getName();
            }
        };
        List<CreateTableStatement> _sortBy_2 = IterableExtensions.<CreateTableStatement, String>sortBy(_configInitTables, _function_4);
        for (final CreateTableStatement tbl_1 : _sortBy_2) {
            _builder.append("\t");
            _builder.append("interface ");
            String _name_8 = tbl_1.getName();
            String _pascalize_5 = Strings.pascalize(_name_8);
            _builder.append(_pascalize_5, "\t");
            _builder.append("Columns {");
            _builder.newLineIfNotEmpty();
            {
                EList<ColumnSource> _columnDefs_1 = tbl_1.getColumnDefs();
                final Function1<ColumnSource, Boolean> _function_5 = new Function1<ColumnSource, Boolean>() {

                    public Boolean apply(final ColumnSource it) {
                        String _name = it.getName();
                        boolean _equals = _name.equals("_id");
                        return Boolean.valueOf((!_equals));
                    }
                };
                Iterable<ColumnSource> _filter_2 = IterableExtensions.<ColumnSource>filter(_columnDefs_1, _function_5);
                for (final ColumnSource col_2 : _filter_2) {
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("String ");
                    String _name_9 = col_2.getName();
                    String _underscore_1 = Strings.underscore(_name_9);
                    String _upperCase_1 = _underscore_1.toUpperCase();
                    _builder.append(_upperCase_1, "\t\t");
                    _builder.append(" = \"");
                    String _name_10 = col_2.getName();
                    _builder.append(_name_10, "\t\t");
                    _builder.append("\";");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
        }
    }
    {
        Collection<CreateViewStatement> _configInitViews = ModelUtil.getConfigInitViews(model);
        final Function1<CreateViewStatement, String> _function_6 = new Function1<CreateViewStatement, String>() {

            public String apply(final CreateViewStatement x) {
                return x.getName();
            }
        };
        List<CreateViewStatement> _sortBy_3 = IterableExtensions.<CreateViewStatement, String>sortBy(_configInitViews, _function_6);
        for (final CreateViewStatement vw_1 : _sortBy_3) {
            _builder.append("\t");
            _builder.append("interface ");
            String _name_11 = vw_1.getName();
            String _pascalize_6 = Strings.pascalize(_name_11);
            _builder.append(_pascalize_6, "\t");
            _builder.append("Columns {");
            _builder.newLineIfNotEmpty();
            {
                ArrayList<ColumnSource> _viewResultColumns_1 = ModelUtil.getViewResultColumns(vw_1);
                final Function1<ColumnSource, Boolean> _function_7 = new Function1<ColumnSource, Boolean>() {

                    public Boolean apply(final ColumnSource it) {
                        String _name = it.getName();
                        boolean _equals = _name.equals("_id");
                        return Boolean.valueOf((!_equals));
                    }
                };
                Iterable<ColumnSource> _filter_3 = IterableExtensions.<ColumnSource>filter(_viewResultColumns_1, _function_7);
                for (final ColumnSource col_3 : _filter_3) {
                    _builder.append("\t");
                    _builder.append("\t");
                    CharSequence _generateInterfaceMemberForResultColumn_1 = this.generateInterfaceMemberForResultColumn(col_3);
                    _builder.append(_generateInterfaceMemberForResultColumn_1, "\t\t");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
        }
    }
    _builder.append("\t\t\t");
    _builder.newLine();
    {
        Collection<CreateTableStatement> _tables_1 = snapshot.getTables();
        final Function1<CreateTableStatement, String> _function_8 = new Function1<CreateTableStatement, String>() {

            public String apply(final CreateTableStatement x) {
                return x.getName();
            }
        };
        List<CreateTableStatement> _sortBy_4 = IterableExtensions.<CreateTableStatement, String>sortBy(_tables_1, _function_8);
        for (final CreateTableStatement tbl_2 : _sortBy_4) {
            _builder.append("\t");
            CharSequence _generateContractItem = this.generateContractItem(model, snapshot, tbl_2);
            _builder.append(_generateContractItem, "\t");
            _builder.newLineIfNotEmpty();
        }
    }
    _builder.newLine();
    {
        Collection<CreateViewStatement> _views_1 = snapshot.getViews();
        final Function1<CreateViewStatement, String> _function_9 = new Function1<CreateViewStatement, String>() {

            public String apply(final CreateViewStatement x) {
                return x.getName();
            }
        };
        List<CreateViewStatement> _sortBy_5 = IterableExtensions.<CreateViewStatement, String>sortBy(_views_1, _function_9);
        for (final CreateViewStatement vw_2 : _sortBy_5) {
            _builder.append("\t");
            CharSequence _generateContractItem_1 = this.generateContractItem(model, snapshot, vw_2);
            _builder.append(_generateContractItem_1, "\t");
            _builder.newLineIfNotEmpty();
        }
    }
    _builder.append("\t");
    _builder.newLine();
    {
        Collection<CreateTableStatement> _configInitTables_1 = ModelUtil.getConfigInitTables(model);
        final Function1<CreateTableStatement, String> _function_10 = new Function1<CreateTableStatement, String>() {

            public String apply(final CreateTableStatement x) {
                return x.getName();
            }
        };
        List<CreateTableStatement> _sortBy_6 = IterableExtensions.<CreateTableStatement, String>sortBy(_configInitTables_1, _function_10);
        for (final CreateTableStatement tbl_3 : _sortBy_6) {
            _builder.append("\t");
            CharSequence _generateContractItem_2 = this.generateContractItem(model, snapshot, tbl_3);
            _builder.append(_generateContractItem_2, "\t");
            _builder.newLineIfNotEmpty();
        }
    }
    _builder.newLine();
    {
        Collection<CreateViewStatement> _configInitViews_1 = ModelUtil.getConfigInitViews(model);
        final Function1<CreateViewStatement, String> _function_11 = new Function1<CreateViewStatement, String>() {

            public String apply(final CreateViewStatement x) {
                return x.getName();
            }
        };
        List<CreateViewStatement> _sortBy_7 = IterableExtensions.<CreateViewStatement, String>sortBy(_configInitViews_1, _function_11);
        for (final CreateViewStatement vw_3 : _sortBy_7) {
            _builder.append("\t");
            CharSequence _generateContractItem_3 = this.generateContractItem(model, snapshot, vw_3);
            _builder.append(_generateContractItem_3, "\t");
            _builder.newLineIfNotEmpty();
        }
    }
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("static Map<Uri, Set<Uri>> REFERENCING_VIEWS;");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("static {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("Map<Uri, Set<Uri>> map = new HashMap<Uri, Set<Uri>>();");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    {
        Collection<CreateTableStatement> _tables_2 = snapshot.getTables();
        final Function1<CreateTableStatement, String> _function_12 = new Function1<CreateTableStatement, String>() {

            public String apply(final CreateTableStatement x) {
                return x.getName();
            }
        };
        List<CreateTableStatement> _sortBy_8 = IterableExtensions.<CreateTableStatement, String>sortBy(_tables_2, _function_12);
        for (final CreateTableStatement tbl_4 : _sortBy_8) {
            _builder.append("\t\t");
            _builder.append("map.put(");
            String _name_12 = tbl_4.getName();
            String _pascalize_7 = Strings.pascalize(_name_12);
            _builder.append(_pascalize_7, "\t\t");
            _builder.append(".CONTENT_URI, ");
            String _name_13 = tbl_4.getName();
            String _pascalize_8 = Strings.pascalize(_name_13);
            _builder.append(_pascalize_8, "\t\t");
            _builder.append(".VIEW_URIS);");
            _builder.newLineIfNotEmpty();
        }
    }
    {
        Collection<CreateViewStatement> _views_2 = snapshot.getViews();
        final Function1<CreateViewStatement, String> _function_13 = new Function1<CreateViewStatement, String>() {

            public String apply(final CreateViewStatement x) {
                return x.getName();
            }
        };
        List<CreateViewStatement> _sortBy_9 = IterableExtensions.<CreateViewStatement, String>sortBy(_views_2, _function_13);
        for (final CreateViewStatement vw_4 : _sortBy_9) {
            _builder.append("\t\t");
            _builder.append("map.put(");
            String _name_14 = vw_4.getName();
            String _pascalize_9 = Strings.pascalize(_name_14);
            _builder.append(_pascalize_9, "\t\t");
            _builder.append(".CONTENT_URI, ");
            String _name_15 = vw_4.getName();
            String _pascalize_10 = Strings.pascalize(_name_15);
            _builder.append(_pascalize_10, "\t\t");
            _builder.append(".VIEW_URIS);");
            _builder.newLineIfNotEmpty();
        }
    }
    {
        Collection<CreateTableStatement> _configInitTables_2 = ModelUtil.getConfigInitTables(model);
        final Function1<CreateTableStatement, String> _function_14 = new Function1<CreateTableStatement, String>() {

            public String apply(final CreateTableStatement x) {
                return x.getName();
            }
        };
        List<CreateTableStatement> _sortBy_10 = IterableExtensions.<CreateTableStatement, String>sortBy(_configInitTables_2, _function_14);
        for (final CreateTableStatement tbl_5 : _sortBy_10) {
            _builder.append("\t\t");
            _builder.append("map.put(");
            String _name_16 = tbl_5.getName();
            String _pascalize_11 = Strings.pascalize(_name_16);
            _builder.append(_pascalize_11, "\t\t");
            _builder.append(".CONTENT_URI, ");
            String _name_17 = tbl_5.getName();
            String _pascalize_12 = Strings.pascalize(_name_17);
            _builder.append(_pascalize_12, "\t\t");
            _builder.append(".VIEW_URIS);");
            _builder.newLineIfNotEmpty();
        }
    }
    {
        Collection<CreateViewStatement> _configInitViews_2 = ModelUtil.getConfigInitViews(model);
        final Function1<CreateViewStatement, String> _function_15 = new Function1<CreateViewStatement, String>() {

            public String apply(final CreateViewStatement x) {
                return x.getName();
            }
        };
        List<CreateViewStatement> _sortBy_11 = IterableExtensions.<CreateViewStatement, String>sortBy(_configInitViews_2, _function_15);
        for (final CreateViewStatement vw_5 : _sortBy_11) {
            _builder.append("\t\t");
            _builder.append("map.put(");
            String _name_18 = vw_5.getName();
            String _pascalize_13 = Strings.pascalize(_name_18);
            _builder.append(_pascalize_13, "\t\t");
            _builder.append(".CONTENT_URI, ");
            String _name_19 = vw_5.getName();
            String _pascalize_14 = Strings.pascalize(_name_19);
            _builder.append(_pascalize_14, "\t\t");
            _builder.append(".VIEW_URIS);");
            _builder.newLineIfNotEmpty();
        }
    }
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("REFERENCING_VIEWS = Collections.unmodifiableMap(map);");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    CharSequence _generateContractItemsForActions = this.generateContractItemsForActions(model, snapshot);
    _builder.append(_generateContractItemsForActions, "\t");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("private ");
    DatabaseBlock _database_4 = model.getDatabase();
    String _name_20 = _database_4.getName();
    String _pascalize_15 = Strings.pascalize(_name_20);
    _builder.append(_pascalize_15, "\t");
    _builder.append("Contract(){}");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* <p>Delete all rows from all tables</p>");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("*/\t\t\t\t\t\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public static void deleteAll() {");
    _builder.newLine();
    {
        Collection<CreateTableStatement> _tables_3 = snapshot.getTables();
        final Function1<CreateTableStatement, String> _function_16 = new Function1<CreateTableStatement, String>() {

            public String apply(final CreateTableStatement x) {
                return x.getName();
            }
        };
        List<CreateTableStatement> _sortBy_12 = IterableExtensions.<CreateTableStatement, String>sortBy(_tables_3, _function_16);
        for (final CreateTableStatement tbl_6 : _sortBy_12) {
            _builder.append("\t\t");
            String _name_21 = tbl_6.getName();
            String _pascalize_16 = Strings.pascalize(_name_21);
            _builder.append(_pascalize_16, "\t\t");
            _builder.append(".delete();");
            _builder.newLineIfNotEmpty();
        }
    }
    {
        Collection<CreateTableStatement> _configInitTables_3 = ModelUtil.getConfigInitTables(model);
        final Function1<CreateTableStatement, String> _function_17 = new Function1<CreateTableStatement, String>() {

            public String apply(final CreateTableStatement x) {
                return x.getName();
            }
        };
        List<CreateTableStatement> _sortBy_13 = IterableExtensions.<CreateTableStatement, String>sortBy(_configInitTables_3, _function_17);
        for (final CreateTableStatement tbl_7 : _sortBy_13) {
            _builder.append("\t\t");
            String _name_22 = tbl_7.getName();
            String _pascalize_17 = Strings.pascalize(_name_22);
            _builder.append(_pascalize_17, "\t\t");
            _builder.append(".delete();");
            _builder.newLineIfNotEmpty();
        }
    }
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
}
Also used : DatabaseBlock(com.robotoworks.mechanoid.db.sqliteModel.DatabaseBlock) CreateViewStatement(com.robotoworks.mechanoid.db.sqliteModel.CreateViewStatement) CreateTableStatement(com.robotoworks.mechanoid.db.sqliteModel.CreateTableStatement) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Collection(java.util.Collection) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) List(java.util.List) ColumnSource(com.robotoworks.mechanoid.db.sqliteModel.ColumnSource)

Example 8 with ColumnSource

use of com.robotoworks.mechanoid.db.sqliteModel.ColumnSource in project mechanoid by robotoworks.

the class SqliteModelJavaValidator method checkUniqueResultColumnAliases.

@Check
public void checkUniqueResultColumnAliases(SelectList list) {
    EList<ColumnSource> cols = list.getResultColumns();
    for (int i = 0; i < cols.size(); i++) {
        ColumnSource subject = cols.get(i);
        int matches = 0;
        for (int j = 0; j < cols.size(); j++) {
            ColumnSource target = cols.get(j);
            if (subject.getName() != null && target.getName() != null && subject.getName().equalsIgnoreCase(target.getName())) {
                matches++;
            }
            if (matches > 1) {
                error("Duplicate alias not allowed", target, SqliteModelPackage.Literals.COLUMN_SOURCE__NAME, -1);
                return;
            }
        }
    }
}
Also used : ColumnSource(com.robotoworks.mechanoid.db.sqliteModel.ColumnSource) Check(org.eclipse.xtext.validation.Check)

Example 9 with ColumnSource

use of com.robotoworks.mechanoid.db.sqliteModel.ColumnSource in project mechanoid by robotoworks.

the class ModelUtil method getViewResultColumns.

public static ArrayList<ColumnSource> getViewResultColumns(final CreateViewStatement stmt) {
    ArrayList<ColumnSource> result = new ArrayList<ColumnSource>();
    SelectStatement _selectStatement = stmt.getSelectStatement();
    SelectCoreExpression coreExpr = _selectStatement.getCore();
    if ((coreExpr instanceof SelectCore)) {
        SelectCore core = ((SelectCore) coreExpr);
        SelectCoreExpression _right = core.getRight();
        SelectList selectList = ((SelectExpression) _right).getSelectList();
        boolean _notEquals = (!Objects.equal(selectList, null));
        if (_notEquals) {
            EList<ColumnSource> _resultColumns = selectList.getResultColumns();
            final Function1<ColumnSource, Boolean> _function = new Function1<ColumnSource, Boolean>() {

                public Boolean apply(final ColumnSource it) {
                    boolean _and = false;
                    String _name = it.getName();
                    boolean _notEquals = (!Objects.equal(_name, null));
                    if (!_notEquals) {
                        _and = false;
                    } else {
                        String _name_1 = it.getName();
                        boolean _equals = _name_1.equals("");
                        boolean _not = (!_equals);
                        _and = _not;
                    }
                    return Boolean.valueOf(_and);
                }
            };
            Iterable<ColumnSource> _filter = IterableExtensions.<ColumnSource>filter(_resultColumns, _function);
            Iterables.<ColumnSource>addAll(result, _filter);
        }
    } else {
        SelectList selectList_1 = ((SelectExpression) coreExpr).getSelectList();
        boolean _notEquals_1 = (!Objects.equal(selectList_1, null));
        if (_notEquals_1) {
            EList<ColumnSource> _resultColumns_1 = selectList_1.getResultColumns();
            final Function1<ColumnSource, Boolean> _function_1 = new Function1<ColumnSource, Boolean>() {

                public Boolean apply(final ColumnSource it) {
                    boolean _and = false;
                    String _name = it.getName();
                    boolean _notEquals = (!Objects.equal(_name, null));
                    if (!_notEquals) {
                        _and = false;
                    } else {
                        String _name_1 = it.getName();
                        boolean _equals = _name_1.equals("");
                        boolean _not = (!_equals);
                        _and = _not;
                    }
                    return Boolean.valueOf(_and);
                }
            };
            Iterable<ColumnSource> _filter_1 = IterableExtensions.<ColumnSource>filter(_resultColumns_1, _function_1);
            Iterables.<ColumnSource>addAll(result, _filter_1);
        }
    }
    return result;
}
Also used : SelectList(com.robotoworks.mechanoid.db.sqliteModel.SelectList) SelectCoreExpression(com.robotoworks.mechanoid.db.sqliteModel.SelectCoreExpression) ArrayList(java.util.ArrayList) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) SelectExpression(com.robotoworks.mechanoid.db.sqliteModel.SelectExpression) SelectCore(com.robotoworks.mechanoid.db.sqliteModel.SelectCore) SelectStatement(com.robotoworks.mechanoid.db.sqliteModel.SelectStatement) ColumnSource(com.robotoworks.mechanoid.db.sqliteModel.ColumnSource)

Example 10 with ColumnSource

use of com.robotoworks.mechanoid.db.sqliteModel.ColumnSource in project mechanoid by robotoworks.

the class ModelUtil method getInferredColumnType.

public static ColumnType getInferredColumnType(final ResultColumn col) {
    Expression expr = col.getExpression();
    boolean _matched = false;
    if (!_matched) {
        if (expr instanceof CastExpression) {
            _matched = true;
            SqliteDataType _type = ((CastExpression) expr).getType();
            return ModelUtil.toColumnType(_type);
        }
    }
    if (!_matched) {
        if (expr instanceof ColumnSourceRef) {
            _matched = true;
            ColumnSource _column = ((ColumnSourceRef) expr).getColumn();
            if ((_column instanceof ResultColumn)) {
                ColumnSource _column_1 = ((ColumnSourceRef) expr).getColumn();
                ColumnType _inferredColumnType = ModelUtil.getInferredColumnType(((ResultColumn) _column_1));
                return ((ColumnType) _inferredColumnType);
            } else {
                ColumnSource _column_2 = ((ColumnSourceRef) expr).getColumn();
                return ((ColumnDef) _column_2).getType();
            }
        }
    }
    if (!_matched) {
        if (expr instanceof ExprConcat) {
            _matched = true;
            return ColumnType.TEXT;
        }
    }
    if (!_matched) {
        if (expr instanceof Function) {
            _matched = true;
            boolean _or = false;
            boolean _or_1 = false;
            boolean _or_2 = false;
            boolean _or_3 = false;
            boolean _or_4 = false;
            String _name = ((Function) expr).getName();
            boolean _equalsIgnoreCase = _name.equalsIgnoreCase("count");
            if (_equalsIgnoreCase) {
                _or_4 = true;
            } else {
                String _name_1 = ((Function) expr).getName();
                boolean _equalsIgnoreCase_1 = _name_1.equalsIgnoreCase("length");
                _or_4 = _equalsIgnoreCase_1;
            }
            if (_or_4) {
                _or_3 = true;
            } else {
                String _name_2 = ((Function) expr).getName();
                boolean _equalsIgnoreCase_2 = _name_2.equalsIgnoreCase("random");
                _or_3 = _equalsIgnoreCase_2;
            }
            if (_or_3) {
                _or_2 = true;
            } else {
                String _name_3 = ((Function) expr).getName();
                boolean _equalsIgnoreCase_3 = _name_3.equalsIgnoreCase("last_insert_rowid");
                _or_2 = _equalsIgnoreCase_3;
            }
            if (_or_2) {
                _or_1 = true;
            } else {
                String _name_4 = ((Function) expr).getName();
                boolean _equalsIgnoreCase_4 = _name_4.equalsIgnoreCase("changes");
                _or_1 = _equalsIgnoreCase_4;
            }
            if (_or_1) {
                _or = true;
            } else {
                String _name_5 = ((Function) expr).getName();
                boolean _equalsIgnoreCase_5 = _name_5.equalsIgnoreCase("total_changes");
                _or = _equalsIgnoreCase_5;
            }
            if (_or) {
                return ColumnType.INTEGER;
            } else {
                boolean _or_5 = false;
                boolean _or_6 = false;
                boolean _or_7 = false;
                boolean _or_8 = false;
                boolean _or_9 = false;
                String _name_6 = ((Function) expr).getName();
                boolean _equalsIgnoreCase_6 = _name_6.equalsIgnoreCase("abs");
                if (_equalsIgnoreCase_6) {
                    _or_9 = true;
                } else {
                    String _name_7 = ((Function) expr).getName();
                    boolean _equalsIgnoreCase_7 = _name_7.equalsIgnoreCase("avg");
                    _or_9 = _equalsIgnoreCase_7;
                }
                if (_or_9) {
                    _or_8 = true;
                } else {
                    String _name_8 = ((Function) expr).getName();
                    boolean _equalsIgnoreCase_8 = _name_8.equalsIgnoreCase("round");
                    _or_8 = _equalsIgnoreCase_8;
                }
                if (_or_8) {
                    _or_7 = true;
                } else {
                    String _name_9 = ((Function) expr).getName();
                    boolean _equalsIgnoreCase_9 = _name_9.equalsIgnoreCase("sum");
                    _or_7 = _equalsIgnoreCase_9;
                }
                if (_or_7) {
                    _or_6 = true;
                } else {
                    String _name_10 = ((Function) expr).getName();
                    boolean _equalsIgnoreCase_10 = _name_10.equalsIgnoreCase("total");
                    _or_6 = _equalsIgnoreCase_10;
                }
                if (_or_6) {
                    _or_5 = true;
                } else {
                    String _name_11 = ((Function) expr).getName();
                    boolean _equalsIgnoreCase_11 = _name_11.equalsIgnoreCase("likelihood");
                    _or_5 = _equalsIgnoreCase_11;
                }
                if (_or_5) {
                    return ColumnType.REAL;
                } else {
                    boolean _or_10 = false;
                    String _name_12 = ((Function) expr).getName();
                    boolean _equalsIgnoreCase_12 = _name_12.equalsIgnoreCase("randomblob");
                    if (_equalsIgnoreCase_12) {
                        _or_10 = true;
                    } else {
                        String _name_13 = ((Function) expr).getName();
                        boolean _equalsIgnoreCase_13 = _name_13.equalsIgnoreCase("zeroblob");
                        _or_10 = _equalsIgnoreCase_13;
                    }
                    if (_or_10) {
                        return ColumnType.BLOB;
                    }
                }
            }
            return ColumnType.TEXT;
        }
    }
    return ColumnType.TEXT;
}
Also used : SqliteDataType(com.robotoworks.mechanoid.db.sqliteModel.SqliteDataType) ColumnSourceRef(com.robotoworks.mechanoid.db.sqliteModel.ColumnSourceRef) Function(com.robotoworks.mechanoid.db.sqliteModel.Function) ColumnType(com.robotoworks.mechanoid.db.sqliteModel.ColumnType) ExprConcat(com.robotoworks.mechanoid.db.sqliteModel.ExprConcat) SelectCoreExpression(com.robotoworks.mechanoid.db.sqliteModel.SelectCoreExpression) SelectExpression(com.robotoworks.mechanoid.db.sqliteModel.SelectExpression) Expression(com.robotoworks.mechanoid.db.sqliteModel.Expression) CastExpression(com.robotoworks.mechanoid.db.sqliteModel.CastExpression) ResultColumn(com.robotoworks.mechanoid.db.sqliteModel.ResultColumn) ColumnDef(com.robotoworks.mechanoid.db.sqliteModel.ColumnDef) CastExpression(com.robotoworks.mechanoid.db.sqliteModel.CastExpression) ColumnSource(com.robotoworks.mechanoid.db.sqliteModel.ColumnSource)

Aggregations

ColumnSource (com.robotoworks.mechanoid.db.sqliteModel.ColumnSource)21 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)13 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)12 ColumnType (com.robotoworks.mechanoid.db.sqliteModel.ColumnType)8 ResultColumn (com.robotoworks.mechanoid.db.sqliteModel.ResultColumn)5 EList (org.eclipse.emf.common.util.EList)5 ColumnDef (com.robotoworks.mechanoid.db.sqliteModel.ColumnDef)4 ArrayList (java.util.ArrayList)4 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)4 CreateTableStatement (com.robotoworks.mechanoid.db.sqliteModel.CreateTableStatement)2 CreateViewStatement (com.robotoworks.mechanoid.db.sqliteModel.CreateViewStatement)2 SelectCoreExpression (com.robotoworks.mechanoid.db.sqliteModel.SelectCoreExpression)2 SelectExpression (com.robotoworks.mechanoid.db.sqliteModel.SelectExpression)2 SelectList (com.robotoworks.mechanoid.db.sqliteModel.SelectList)2 TableDefinition (com.robotoworks.mechanoid.db.sqliteModel.TableDefinition)2 Consumer (java.util.function.Consumer)2 EObject (org.eclipse.emf.ecore.EObject)2 AlterTableAddColumnStatement (com.robotoworks.mechanoid.db.sqliteModel.AlterTableAddColumnStatement)1 CastExpression (com.robotoworks.mechanoid.db.sqliteModel.CastExpression)1 ColumnSourceRef (com.robotoworks.mechanoid.db.sqliteModel.ColumnSourceRef)1