Exemplo n.º 1
0
  @Check
  public void checkInitBlockStatementTypes(InitBlock initBlock) {
    for (DDLStatement stmt : initBlock.getStatements()) {
      if (!((stmt instanceof CreateViewStatement) || (stmt instanceof CreateTableStatement))) {
        error(
            "Only tables and view statements allowed in the init block",
            stmt,
            SqliteModelPackage.Literals.DDL_STATEMENT.getEStructuralFeature(
                SqliteModelPackage.DDL_STATEMENT),
            -1);
      }

      if (stmt instanceof CreateViewStatement) {
        CreateViewStatement v = (CreateViewStatement) stmt;

        if (!v.isTemporary()) {
          error(
              "Only temporary views allowed in the init block",
              stmt,
              SqliteModelPackage.Literals.TABLE_DEFINITION__NAME);
        }
      }
      if (stmt instanceof CreateTableStatement) {
        CreateTableStatement t = (CreateTableStatement) stmt;

        if (!t.isTemporary()) {
          error(
              "Only temporary tables allowed in the init block",
              stmt,
              SqliteModelPackage.Literals.TABLE_DEFINITION__NAME);
        }
      }
    }
  }
 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");
           int _version = version = (version + 1);
           _builder.append(_version, "");
           _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 ");
           int _version_1 = version_1 = (version_1 + 1);
           _builder.append(_version_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");
           int _version_2 = version_2 = (version_2 + 1);
           _builder.append(_version_2, "\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;
 }