Search in sources :

Example 1 with InitBlock

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

the class SqliteOpenHelperGenerator 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.content.Context;");
    _builder.newLine();
    _builder.append("import android.database.sqlite.SQLiteDatabase;");
    _builder.newLine();
    _builder.append("import com.robotoworks.mechanoid.db.MechanoidSQLiteOpenHelper;");
    _builder.newLine();
    _builder.append("import com.robotoworks.mechanoid.db.SQLiteMigration;");
    _builder.newLine();
    _builder.newLine();
    {
        DatabaseBlock _database = model.getDatabase();
        EList<MigrationBlock> _migrations = _database.getMigrations();
        int _size = _migrations.size();
        boolean _greaterThan = (_size > 0);
        if (_greaterThan) {
            int version = 0;
            _builder.newLineIfNotEmpty();
            {
                DatabaseBlock _database_1 = model.getDatabase();
                EList<MigrationBlock> _migrations_1 = _database_1.getMigrations();
                for (final MigrationBlock migration : _migrations_1) {
                    _builder.append("import ");
                    String _packageName_1 = model.getPackageName();
                    _builder.append(_packageName_1, "");
                    _builder.append(".migrations.Default");
                    DatabaseBlock _database_2 = model.getDatabase();
                    String _name = _database_2.getName();
                    String _pascalize = Strings.pascalize(_name);
                    _builder.append(_pascalize, "");
                    _builder.append("MigrationV");
                    _builder.append(version = (version + 1), "");
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                }
            }
        }
    }
    _builder.newLine();
    _builder.append("public abstract class Abstract");
    DatabaseBlock _database_3 = model.getDatabase();
    String _name_1 = _database_3.getName();
    String _pascalize_1 = Strings.pascalize(_name_1);
    _builder.append(_pascalize_1, "");
    _builder.append("OpenHelper extends MechanoidSQLiteOpenHelper {");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("private static final String DATABASE_NAME = \"");
    DatabaseBlock _database_4 = model.getDatabase();
    String _name_2 = _database_4.getName();
    _builder.append(_name_2, "\t");
    _builder.append(".db\";");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public static final int VERSION = ");
    DatabaseBlock _database_5 = model.getDatabase();
    EList<MigrationBlock> _migrations_2 = _database_5.getMigrations();
    int _size_1 = _migrations_2.size();
    _builder.append(_size_1, "\t");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public interface Sources {");
    _builder.newLine();
    {
        Collection<CreateTableStatement> _tables = snapshot.getTables();
        for (final CreateTableStatement table : _tables) {
            _builder.append("\t\t");
            _builder.append("String ");
            String _name_3 = table.getName();
            String _underscore = Strings.underscore(_name_3);
            String _upperCase = _underscore.toUpperCase();
            _builder.append(_upperCase, "\t\t");
            _builder.append(" = \"");
            String _name_4 = table.getName();
            _builder.append(_name_4, "\t\t");
            _builder.append("\";");
            _builder.newLineIfNotEmpty();
        }
    }
    {
        Collection<CreateViewStatement> _views = snapshot.getViews();
        for (final CreateViewStatement view : _views) {
            _builder.append("\t\t");
            _builder.append("String ");
            String _name_5 = view.getName();
            String _underscore_1 = Strings.underscore(_name_5);
            String _upperCase_1 = _underscore_1.toUpperCase();
            _builder.append(_upperCase_1, "\t\t");
            _builder.append(" = \"");
            String _name_6 = view.getName();
            _builder.append(_name_6, "\t\t");
            _builder.append("\";");
            _builder.newLineIfNotEmpty();
        }
    }
    {
        Collection<CreateTableStatement> _configInitTables = ModelUtil.getConfigInitTables(model);
        for (final CreateTableStatement table_1 : _configInitTables) {
            _builder.append("\t\t");
            _builder.append("String ");
            String _name_7 = table_1.getName();
            String _underscore_2 = Strings.underscore(_name_7);
            String _upperCase_2 = _underscore_2.toUpperCase();
            _builder.append(_upperCase_2, "\t\t");
            _builder.append(" = \"");
            String _name_8 = table_1.getName();
            _builder.append(_name_8, "\t\t");
            _builder.append("\";");
            _builder.newLineIfNotEmpty();
        }
    }
    {
        Collection<CreateViewStatement> _configInitViews = ModelUtil.getConfigInitViews(model);
        for (final CreateViewStatement view_1 : _configInitViews) {
            _builder.append("\t\t");
            _builder.append("String ");
            String _name_9 = view_1.getName();
            String _underscore_3 = Strings.underscore(_name_9);
            String _upperCase_3 = _underscore_3.toUpperCase();
            _builder.append(_upperCase_3, "\t\t");
            _builder.append(" = \"");
            String _name_10 = view_1.getName();
            _builder.append(_name_10, "\t\t");
            _builder.append("\";");
            _builder.newLineIfNotEmpty();
        }
    }
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public Abstract");
    DatabaseBlock _database_6 = model.getDatabase();
    String _name_11 = _database_6.getName();
    String _pascalize_2 = Strings.pascalize(_name_11);
    _builder.append(_pascalize_2, "\t");
    _builder.append("OpenHelper(Context context) {");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("super(context, DATABASE_NAME, null, VERSION);");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public Abstract");
    DatabaseBlock _database_7 = model.getDatabase();
    String _name_12 = _database_7.getName();
    String _pascalize_3 = Strings.pascalize(_name_12);
    _builder.append(_pascalize_3, "\t");
    _builder.append("OpenHelper(Context context, String name) {");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("super(context, name, null, VERSION);");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@Override");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public void onCreate(SQLiteDatabase db) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("applyMigrations(db, 0, VERSION);");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    {
        DatabaseBlock _database_8 = model.getDatabase();
        InitBlock _init = _database_8.getInit();
        boolean _notEquals = (!Objects.equal(_init, null));
        if (_notEquals) {
            _builder.append("\t");
            _builder.append("@Override");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("public void onOpen(SQLiteDatabase db) {");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("\t");
            _builder.append("super.onOpen(db);");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("\t");
            DatabaseBlock _database_9 = model.getDatabase();
            InitBlock _init_1 = _database_9.getInit();
            EList<DDLStatement> _statements = _init_1.getStatements();
            CharSequence _generateStatements = this._sqliteDatabaseStatementGenerator.generateStatements(_statements);
            _builder.append(_generateStatements, "\t\t");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
        }
    }
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@Override");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("protected SQLiteMigration createMigration(int version) {");
    _builder.newLine();
    {
        DatabaseBlock _database_10 = model.getDatabase();
        EList<MigrationBlock> _migrations_3 = _database_10.getMigrations();
        int _size_2 = _migrations_3.size();
        boolean _greaterThan_1 = (_size_2 > 0);
        if (_greaterThan_1) {
            _builder.append("\t\t");
            int version_1 = (-1);
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("switch(version) {");
            _builder.newLine();
            {
                DatabaseBlock _database_11 = model.getDatabase();
                EList<MigrationBlock> _migrations_4 = _database_11.getMigrations();
                for (final MigrationBlock migration_1 : _migrations_4) {
                    _builder.append("\t\t");
                    _builder.append("\t");
                    _builder.append("case ");
                    _builder.append(version_1 = (version_1 + 1), "\t\t\t");
                    _builder.append(":");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t\t");
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("return create");
                    DatabaseBlock _database_12 = model.getDatabase();
                    String _name_13 = _database_12.getName();
                    String _pascalize_4 = Strings.pascalize(_name_13);
                    _builder.append(_pascalize_4, "\t\t\t\t");
                    _builder.append("MigrationV");
                    _builder.append((version_1 + 1), "\t\t\t\t");
                    _builder.append("();");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t\t");
            _builder.append("\t");
            _builder.append("default:");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("\t\t");
            _builder.append("throw new IllegalStateException(\"No migration for version \" + version);");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("}");
            _builder.newLine();
        } else {
            _builder.append("\t\t");
            _builder.append("throw new IllegalStateException(\"No migrations for any version\");");
            _builder.newLine();
        }
    }
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    {
        DatabaseBlock _database_13 = model.getDatabase();
        EList<MigrationBlock> _migrations_5 = _database_13.getMigrations();
        int _size_3 = _migrations_5.size();
        boolean _greaterThan_2 = (_size_3 > 0);
        if (_greaterThan_2) {
            _builder.append("\t");
            int version_2 = 0;
            _builder.newLineIfNotEmpty();
            {
                DatabaseBlock _database_14 = model.getDatabase();
                EList<MigrationBlock> _migrations_6 = _database_14.getMigrations();
                for (final MigrationBlock migration_2 : _migrations_6) {
                    _builder.append("\t");
                    _builder.append("protected SQLiteMigration create");
                    DatabaseBlock _database_15 = model.getDatabase();
                    String _name_14 = _database_15.getName();
                    String _pascalize_5 = Strings.pascalize(_name_14);
                    _builder.append(_pascalize_5, "\t");
                    _builder.append("MigrationV");
                    _builder.append(version_2 = (version_2 + 1), "\t");
                    _builder.append("() {");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("return new Default");
                    DatabaseBlock _database_16 = model.getDatabase();
                    String _name_15 = _database_16.getName();
                    String _pascalize_6 = Strings.pascalize(_name_15);
                    _builder.append(_pascalize_6, "\t\t");
                    _builder.append("MigrationV");
                    _builder.append(version_2, "\t\t");
                    _builder.append("();");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("}");
                    _builder.newLine();
                }
            }
        }
    }
    _builder.append("}");
    _builder.newLine();
    return _builder;
}
Also used : DatabaseBlock(com.robotoworks.mechanoid.db.sqliteModel.DatabaseBlock) EList(org.eclipse.emf.common.util.EList) CreateViewStatement(com.robotoworks.mechanoid.db.sqliteModel.CreateViewStatement) CreateTableStatement(com.robotoworks.mechanoid.db.sqliteModel.CreateTableStatement) InitBlock(com.robotoworks.mechanoid.db.sqliteModel.InitBlock) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Collection(java.util.Collection) MigrationBlock(com.robotoworks.mechanoid.db.sqliteModel.MigrationBlock)

Example 2 with InitBlock

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

the class ModelUtil method findPreviousStatementsOfType.

public static <T extends DDLStatement> ArrayList<T> findPreviousStatementsOfType(final DDLStatement stmt, final Class<T> statementType, final boolean inclusive) {
    ArrayList<T> list = new ArrayList<T>();
    DatabaseBlock db = ModelUtil.<DatabaseBlock>getAncestorOfType(stmt, DatabaseBlock.class);
    EList<MigrationBlock> _migrations = db.getMigrations();
    for (final MigrationBlock migration : _migrations) {
        EList<DDLStatement> _statements = migration.getStatements();
        for (final DDLStatement ddl : _statements) {
            {
                if ((!inclusive)) {
                    boolean _equals = Objects.equal(ddl, stmt);
                    if (_equals) {
                        return list;
                    }
                }
                Class<? extends DDLStatement> _class = ddl.getClass();
                boolean _isAssignableFrom = statementType.isAssignableFrom(_class);
                if (_isAssignableFrom) {
                    list.add(((T) ddl));
                }
                boolean _equals_1 = Objects.equal(ddl, stmt);
                if (_equals_1) {
                    return list;
                }
            }
        }
    }
    InitBlock _ancestorOfType = ModelUtil.<InitBlock>getAncestorOfType(stmt, InitBlock.class);
    boolean _notEquals = (!Objects.equal(_ancestorOfType, null));
    if (_notEquals) {
        InitBlock _init = db.getInit();
        EList<DDLStatement> _statements_1 = _init.getStatements();
        for (final DDLStatement ddl_1 : _statements_1) {
            {
                if ((!inclusive)) {
                    boolean _equals = Objects.equal(ddl_1, stmt);
                    if (_equals) {
                        return list;
                    }
                }
                Class<? extends DDLStatement> _class = ddl_1.getClass();
                boolean _isAssignableFrom = statementType.isAssignableFrom(_class);
                if (_isAssignableFrom) {
                    list.add(((T) ddl_1));
                }
                boolean _equals_1 = Objects.equal(ddl_1, stmt);
                if (_equals_1) {
                    return list;
                }
            }
        }
    }
    return list;
}
Also used : DatabaseBlock(com.robotoworks.mechanoid.db.sqliteModel.DatabaseBlock) DDLStatement(com.robotoworks.mechanoid.db.sqliteModel.DDLStatement) InitBlock(com.robotoworks.mechanoid.db.sqliteModel.InitBlock) ArrayList(java.util.ArrayList) MigrationBlock(com.robotoworks.mechanoid.db.sqliteModel.MigrationBlock)

Example 3 with InitBlock

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

the class ModelUtil method getConfigInitTables.

public static Collection<CreateTableStatement> getConfigInitTables(final Model model) {
    final ArrayList<CreateTableStatement> items = Lists.<CreateTableStatement>newArrayList();
    DatabaseBlock _database = model.getDatabase();
    InitBlock _init = _database.getInit();
    boolean _notEquals = (!Objects.equal(_init, null));
    if (_notEquals) {
        DatabaseBlock _database_1 = model.getDatabase();
        InitBlock _init_1 = _database_1.getInit();
        EList<DDLStatement> _statements = _init_1.getStatements();
        Iterable<CreateTableStatement> _filter = Iterables.<CreateTableStatement>filter(_statements, CreateTableStatement.class);
        Iterables.<CreateTableStatement>addAll(items, _filter);
    }
    return items;
}
Also used : DatabaseBlock(com.robotoworks.mechanoid.db.sqliteModel.DatabaseBlock) DDLStatement(com.robotoworks.mechanoid.db.sqliteModel.DDLStatement) CreateTableStatement(com.robotoworks.mechanoid.db.sqliteModel.CreateTableStatement) InitBlock(com.robotoworks.mechanoid.db.sqliteModel.InitBlock)

Example 4 with InitBlock

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

the class ModelUtil method findPreviousStatementsOfType.

public static <T extends DDLStatement> ArrayList<T> findPreviousStatementsOfType(final DatabaseBlock db, final DDLStatement stmt, final Class<T> statementType, final boolean inclusive) {
    ArrayList<T> list = new ArrayList<T>();
    EList<MigrationBlock> _migrations = db.getMigrations();
    for (final MigrationBlock migration : _migrations) {
        EList<DDLStatement> _statements = migration.getStatements();
        for (final DDLStatement ddl : _statements) {
            {
                if ((!inclusive)) {
                    boolean _equals = Objects.equal(ddl, stmt);
                    if (_equals) {
                        return list;
                    }
                }
                Class<? extends DDLStatement> _class = ddl.getClass();
                boolean _isAssignableFrom = statementType.isAssignableFrom(_class);
                if (_isAssignableFrom) {
                    list.add(((T) ddl));
                }
                boolean _equals_1 = Objects.equal(ddl, stmt);
                if (_equals_1) {
                    return list;
                }
            }
        }
    }
    InitBlock _init = db.getInit();
    boolean _notEquals = (!Objects.equal(_init, null));
    if (_notEquals) {
        InitBlock _init_1 = db.getInit();
        EList<DDLStatement> _statements_1 = _init_1.getStatements();
        for (final DDLStatement ddl_1 : _statements_1) {
            {
                if ((!inclusive)) {
                    boolean _equals = Objects.equal(ddl_1, stmt);
                    if (_equals) {
                        return list;
                    }
                }
                Class<? extends DDLStatement> _class = ddl_1.getClass();
                boolean _isAssignableFrom = statementType.isAssignableFrom(_class);
                if (_isAssignableFrom) {
                    list.add(((T) ddl_1));
                }
                boolean _equals_1 = Objects.equal(ddl_1, stmt);
                if (_equals_1) {
                    return list;
                }
            }
        }
    }
    return list;
}
Also used : DDLStatement(com.robotoworks.mechanoid.db.sqliteModel.DDLStatement) InitBlock(com.robotoworks.mechanoid.db.sqliteModel.InitBlock) ArrayList(java.util.ArrayList) MigrationBlock(com.robotoworks.mechanoid.db.sqliteModel.MigrationBlock)

Example 5 with InitBlock

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

the class ModelUtil method getConfigInitViews.

public static Collection<CreateViewStatement> getConfigInitViews(final Model model) {
    final ArrayList<CreateViewStatement> items = Lists.<CreateViewStatement>newArrayList();
    DatabaseBlock _database = model.getDatabase();
    InitBlock _init = _database.getInit();
    boolean _notEquals = (!Objects.equal(_init, null));
    if (_notEquals) {
        DatabaseBlock _database_1 = model.getDatabase();
        InitBlock _init_1 = _database_1.getInit();
        EList<DDLStatement> _statements = _init_1.getStatements();
        Iterable<CreateViewStatement> _filter = Iterables.<CreateViewStatement>filter(_statements, CreateViewStatement.class);
        Iterables.<CreateViewStatement>addAll(items, _filter);
    }
    return items;
}
Also used : CreateViewStatement(com.robotoworks.mechanoid.db.sqliteModel.CreateViewStatement) DatabaseBlock(com.robotoworks.mechanoid.db.sqliteModel.DatabaseBlock) DDLStatement(com.robotoworks.mechanoid.db.sqliteModel.DDLStatement) InitBlock(com.robotoworks.mechanoid.db.sqliteModel.InitBlock)

Aggregations

InitBlock (com.robotoworks.mechanoid.db.sqliteModel.InitBlock)6 DDLStatement (com.robotoworks.mechanoid.db.sqliteModel.DDLStatement)4 DatabaseBlock (com.robotoworks.mechanoid.db.sqliteModel.DatabaseBlock)4 MigrationBlock (com.robotoworks.mechanoid.db.sqliteModel.MigrationBlock)3 CreateTableStatement (com.robotoworks.mechanoid.db.sqliteModel.CreateTableStatement)2 CreateViewStatement (com.robotoworks.mechanoid.db.sqliteModel.CreateViewStatement)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)1 EList (org.eclipse.emf.common.util.EList)1 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)1 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1