use of com.robotoworks.mechanoid.db.sqliteModel.MigrationBlock in project mechanoid by robotoworks.
the class SqliteModelGenerator method doGenerate.
public void doGenerate(final Resource resource, final IFileSystemAccess fsa) {
EList<EObject> _contents = resource.getContents();
EObject _head = IterableExtensions.<EObject>head(_contents);
Model model = ((Model) _head);
SqliteDatabaseSnapshot.Builder _get = this.mDbSnapshotBuilderProvider.get();
final SqliteDatabaseSnapshot snapshot = _get.build(model);
String _packageName = model.getPackageName();
DatabaseBlock _database = model.getDatabase();
String _name = _database.getName();
String _pascalize = Strings.pascalize(_name);
String _concat = "Abstract".concat(_pascalize);
String _concat_1 = _concat.concat("OpenHelper");
String _resolveFileName = Strings.resolveFileName(_packageName, _concat_1);
CharSequence _generate = this.mOpenHelperGenerator.generate(model, snapshot);
fsa.generateFile(_resolveFileName, _generate);
String _packageName_1 = model.getPackageName();
DatabaseBlock _database_1 = model.getDatabase();
String _name_1 = _database_1.getName();
String _pascalize_1 = Strings.pascalize(_name_1);
String _concat_2 = _pascalize_1.concat("OpenHelper");
String _resolveFileName_1 = Strings.resolveFileName(_packageName_1, _concat_2);
CharSequence _generateStub = this.mOpenHelperGenerator.generateStub(model, snapshot);
fsa.generateFile(_resolveFileName_1, MechanoidOutputConfigurationProvider.DEFAULT_STUB_OUTPUT, _generateStub);
String _packageName_2 = model.getPackageName();
DatabaseBlock _database_2 = model.getDatabase();
String _name_2 = _database_2.getName();
String _pascalize_2 = Strings.pascalize(_name_2);
String _concat_3 = _pascalize_2.concat("Contract");
String _resolveFileName_2 = Strings.resolveFileName(_packageName_2, _concat_3);
CharSequence _generate_1 = this.mContentProviderContractGenerator.generate(model, snapshot);
fsa.generateFile(_resolveFileName_2, _generate_1);
String _packageName_3 = model.getPackageName();
DatabaseBlock _database_3 = model.getDatabase();
String _name_3 = _database_3.getName();
String _pascalize_3 = Strings.pascalize(_name_3);
String _concat_4 = "Abstract".concat(_pascalize_3);
String _concat_5 = _concat_4.concat("ContentProvider");
String _resolveFileName_3 = Strings.resolveFileName(_packageName_3, _concat_5);
CharSequence _generate_2 = this.mContentProviderGenerator.generate(model, snapshot);
fsa.generateFile(_resolveFileName_3, _generate_2);
String _packageName_4 = model.getPackageName();
DatabaseBlock _database_4 = model.getDatabase();
String _name_4 = _database_4.getName();
String _pascalize_4 = Strings.pascalize(_name_4);
String _concat_6 = _pascalize_4.concat("ContentProvider");
String _resolveFileName_4 = Strings.resolveFileName(_packageName_4, _concat_6);
CharSequence _generateStub_1 = this.mContentProviderGenerator.generateStub(model, snapshot);
fsa.generateFile(_resolveFileName_4, MechanoidOutputConfigurationProvider.DEFAULT_STUB_OUTPUT, _generateStub_1);
Collection<CreateTableStatement> _tables = snapshot.getTables();
final Consumer<CreateTableStatement> _function = new Consumer<CreateTableStatement>() {
public void accept(final CreateTableStatement statement) {
SqliteModelGenerator.this.generateActiveRecordEntity(resource, fsa, ((CreateTableStatement) statement));
}
};
_tables.forEach(_function);
Collection<CreateViewStatement> _views = snapshot.getViews();
final Consumer<CreateViewStatement> _function_1 = new Consumer<CreateViewStatement>() {
public void accept(final CreateViewStatement statement) {
SqliteModelGenerator.this.generateActiveRecordEntity(resource, fsa, ((CreateViewStatement) statement));
}
};
_views.forEach(_function_1);
Collection<CreateTableStatement> _configInitTables = ModelUtil.getConfigInitTables(model);
final Consumer<CreateTableStatement> _function_2 = new Consumer<CreateTableStatement>() {
public void accept(final CreateTableStatement statement) {
SqliteModelGenerator.this.generateActiveRecordEntity(resource, fsa, ((CreateTableStatement) statement));
}
};
_configInitTables.forEach(_function_2);
Collection<CreateViewStatement> _configInitViews = ModelUtil.getConfigInitViews(model);
final Consumer<CreateViewStatement> _function_3 = new Consumer<CreateViewStatement>() {
public void accept(final CreateViewStatement statement) {
SqliteModelGenerator.this.generateActiveRecordEntity(resource, fsa, ((CreateViewStatement) statement));
}
};
_configInitViews.forEach(_function_3);
DatabaseBlock _database_5 = model.getDatabase();
EList<MigrationBlock> _migrations = _database_5.getMigrations();
final Procedure2<MigrationBlock, Integer> _function_4 = new Procedure2<MigrationBlock, Integer>() {
public void apply(final MigrationBlock item, final Integer index) {
SqliteModelGenerator.this.generateMigration(resource, fsa, item, ((index).intValue() + 1));
}
};
IterableExtensions.<MigrationBlock>forEach(_migrations, _function_4);
}
use of com.robotoworks.mechanoid.db.sqliteModel.MigrationBlock 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;
}
use of com.robotoworks.mechanoid.db.sqliteModel.MigrationBlock 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;
}
use of com.robotoworks.mechanoid.db.sqliteModel.MigrationBlock in project mechanoid by robotoworks.
the class ModelUtil method findStatementsOfTypeBetween.
public static <T extends DDLStatement> ArrayList<T> findStatementsOfTypeBetween(final Class<T> statementType, final DDLStatement from, final DDLStatement to) {
ArrayList<T> list = new ArrayList<T>();
boolean between = false;
DatabaseBlock db = ModelUtil.<DatabaseBlock>getAncestorOfType(from, DatabaseBlock.class);
EList<MigrationBlock> _migrations = db.getMigrations();
for (final MigrationBlock migration : _migrations) {
EList<DDLStatement> _statements = migration.getStatements();
for (final DDLStatement ddl : _statements) {
{
boolean _equals = Objects.equal(ddl, to);
if (_equals) {
return list;
}
Class<? extends DDLStatement> _class = ddl.getClass();
boolean _isAssignableFrom = statementType.isAssignableFrom(_class);
if (_isAssignableFrom) {
if (between) {
list.add(((T) ddl));
}
}
boolean _equals_1 = Objects.equal(ddl, from);
if (_equals_1) {
between = true;
}
}
}
}
return list;
}
use of com.robotoworks.mechanoid.db.sqliteModel.MigrationBlock 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;
}
Aggregations