use of io.requery.sql.SchemaModifier in project requery by requery.
the class SchemaUpdater method update.
public void update() {
SchemaModifier schema = new SchemaModifier(configuration);
schema.createTables(mode);
if (mode == TableCreationMode.DROP_CREATE) {
// don't need to check missing columns
return;
}
Function<String, String> columnTransformer = configuration.getColumnTransformer();
Function<String, String> tableTransformer = configuration.getTableTransformer();
// check for missing columns
List<Attribute> missingAttributes = new ArrayList<>();
for (Type<?> type : configuration.getModel().getTypes()) {
if (type.isView()) {
continue;
}
String tableName = type.getName();
if (tableTransformer != null) {
tableName = tableTransformer.apply(tableName);
}
Cursor cursor = queryFunction.apply("PRAGMA table_info(" + tableName + ")");
Map<String, Attribute> map = new LinkedHashMap<>();
for (Attribute attribute : type.getAttributes()) {
if (attribute.isAssociation() && !attribute.isForeignKey()) {
continue;
}
if (columnTransformer == null) {
map.put(attribute.getName(), attribute);
} else {
map.put(columnTransformer.apply(attribute.getName()), attribute);
}
}
if (cursor.getCount() > 0) {
int nameIndex = cursor.getColumnIndex("name");
while (cursor.moveToNext()) {
String name = cursor.getString(nameIndex);
map.remove(name);
}
}
cursor.close();
// whats left in the map are are the missing columns for this type
missingAttributes.addAll(map.values());
}
// foreign keys are created last
Collections.sort(missingAttributes, new Comparator<Attribute>() {
@Override
public int compare(Attribute lhs, Attribute rhs) {
if (lhs.isForeignKey() && rhs.isForeignKey()) {
return 0;
}
if (lhs.isForeignKey()) {
return 1;
}
return -1;
}
});
for (Attribute<?, ?> attribute : missingAttributes) {
schema.addColumn(attribute);
}
}
use of io.requery.sql.SchemaModifier in project requery by requery.
the class SqlitexDatabaseSource method onCreate.
@Override
public void onCreate(SQLiteDatabase db) {
this.db = db;
new SchemaModifier(getConfiguration()).createTables(TableCreationMode.CREATE);
}
use of io.requery.sql.SchemaModifier in project requery by requery.
the class RxTest method setup.
@Before
public void setup() throws SQLException {
Platform platform = new HSQL();
CommonDataSource dataSource = DatabaseType.getDataSource(platform);
EntityModel model = io.requery.test.model.Models.DEFAULT;
CachingProvider provider = Caching.getCachingProvider();
CacheManager cacheManager = provider.getCacheManager();
Configuration configuration = new ConfigurationBuilder(dataSource, model).useDefaultLogging().setWriteExecutor(Executors.newSingleThreadExecutor()).setEntityCache(new EntityCacheBuilder(model).useReferenceCache(true).useSerializableCache(true).useCacheManager(cacheManager).build()).build();
SchemaModifier tables = new SchemaModifier(configuration);
tables.createTables(TableCreationMode.DROP_CREATE);
data = RxSupport.toReactiveStore(new EntityDataStore<Persistable>(configuration));
}
use of io.requery.sql.SchemaModifier in project requery by requery.
the class JacksonTest method setup.
@Before
public void setup() throws SQLException {
CommonDataSource dataSource = DatabaseType.getDataSource(new SQLite());
EntityModel model = Models.MODEL3;
Configuration configuration = new ConfigurationBuilder(dataSource, model).useDefaultLogging().setEntityCache(new WeakEntityCache()).setWriteExecutor(Executors.newSingleThreadExecutor()).build();
SchemaModifier tables = new SchemaModifier(configuration);
tables.createTables(TableCreationMode.DROP_CREATE);
data = new EntityDataStore<>(configuration);
}
use of io.requery.sql.SchemaModifier in project requery by requery.
the class ParameterizedFunctionalTest method setup.
@Before
public void setup() throws SQLException {
CommonDataSource dataSource = DatabaseType.getDataSource(platform);
EntityModel model = Models.DEFAULT;
CachingProvider provider = Caching.getCachingProvider();
CacheManager cacheManager = provider.getCacheManager();
Configuration configuration = new ConfigurationBuilder(dataSource, model).useDefaultLogging().setStatementCacheSize(platform instanceof SQLite ? 0 : 10).setBatchUpdateSize(50).setEntityCache(new EntityCacheBuilder(model).useReferenceCache(true).useSerializableCache(true).useCacheManager(cacheManager).build()).build();
data = new EntityDataStore<>(configuration);
SchemaModifier tables = new SchemaModifier(configuration);
try {
tables.dropTables();
} catch (Exception e) {
// expected if 'drop if exists' not supported (so ignore in that case)
if (!platform.supportsIfExists()) {
throw e;
}
}
TableCreationMode mode = TableCreationMode.CREATE;
System.out.println(tables.createTablesString(mode));
tables.createTables(mode);
}
Aggregations