use of org.folio.dbschema.Table in project raml-module-builder by folio-org.
the class SchemaMaker method tables.
/**
* @return the tables of schema plus tables to delete (tables that exist in previousSchema but not in schema);
* add foreign keys to delete (those that exist in previousSchema but not in schema).
* Nothing to do for indexes because rmb_internal_index handles them.
*/
List<Table> tables() {
if (previousSchema == null || previousSchema.getTables() == null) {
return schema.getTables();
}
Map<String, Table> tableForName = new HashMap<>();
schema.getTables().forEach(table -> tableForName.put(table.getTableName(), table));
List<Table> list = new ArrayList<>(schema.getTables());
previousSchema.getTables().forEach(oldTable -> {
Table newTable = tableForName.get(oldTable.getTableName());
if (newTable == null) {
oldTable.setMode("delete");
list.add(oldTable);
return;
}
List<ForeignKeys> oldForeignKeys = oldTable.getForeignKeys();
if (oldForeignKeys == null || oldForeignKeys.isEmpty()) {
return;
}
List<ForeignKeys> newForeignKeys = newTable.getForeignKeys() == null ? Collections.emptyList() : newTable.getForeignKeys();
List<ForeignKeys> allForeignKeys = new ArrayList<>(newForeignKeys);
oldForeignKeys.forEach(oldForeignKey -> {
if (newForeignKeys.stream().anyMatch(newForeignKey -> sameForeignKey(oldForeignKey, newForeignKey))) {
// an entry for oldForeignKey exists in newForeignKeys, nothing to do
return;
}
oldForeignKey.settOps(TableOperation.DELETE);
allForeignKeys.add(oldForeignKey);
});
newTable.setForeignKeys(allForeignKeys);
});
return list;
}
use of org.folio.dbschema.Table in project raml-module-builder by folio-org.
the class CQL2PgJSON method pgSubQuery.
private String pgSubQuery(CQLTermNode node, List<DbFkInfo> fks, boolean childToParent) throws QueryValidationException {
String currentTableName = dbTable.getTableName();
Table targetTable = null;
StringBuilder sb = new StringBuilder();
if (childToParent) {
// child to parent
targetTable = DbSchemaUtils.getTable(dbSchema, fks.get(fks.size() - 1).getTargetTable());
for (DbFkInfo fk : fks) {
sb.append(currentTableName).append('.').append(fk.getField().replace(".", "_")).append(" IN ( SELECT id FROM ").append(fk.getTargetTable()).append(" WHERE ");
currentTableName = fk.getTargetTable();
}
} else {
// parent to child
targetTable = DbSchemaUtils.getTable(dbSchema, fks.get(0).getTable());
for (int i = fks.size() - 1; i >= 0; i--) {
DbFkInfo fk = fks.get(i);
sb.append(currentTableName).append(".id IN ( SELECT ").append(fks.get(i).getField()).append(" FROM ").append(fk.getTable()).append(" WHERE ");
currentTableName = fk.getTable();
}
}
String[] foreignTarget = node.getIndex().split("\\.", 2);
sb.append(indexNodeForForeignTable(node, targetTable, foreignTarget));
for (int i = 0; i < fks.size(); i++) {
sb.append(')');
}
return sb.toString();
}
use of org.folio.dbschema.Table in project raml-module-builder by folio-org.
the class TableTest method invalidTableName.
@Test
void invalidTableName() {
Table table = new Table();
assertThrows(IllegalArgumentException.class, () -> table.setTableName("foo&bar"));
}
use of org.folio.dbschema.Table in project raml-module-builder by folio-org.
the class DbSchemaUtilsTest method setupSchema.
@Before
public void setupSchema() {
Schema schema = new Schema();
table = new Table();
table.setTableName("users");
table.setFullTextIndex(Arrays.asList(newIndex("name")));
table.setGinIndex(Arrays.asList(newIndex("name")));
table.setIndex(Arrays.asList(newIndex("name")));
table.setUniqueIndex(Arrays.asList(newIndex("email")));
table.setLikeIndex(Arrays.asList(newIndex("address")));
schema.setTables(Arrays.asList(table));
}
use of org.folio.dbschema.Table in project raml-module-builder by folio-org.
the class DBSchemaTest method makeInstanceWithSpecifiedDBSchemaPath.
@Test
public void makeInstanceWithSpecifiedDBSchemaPath() throws Exception {
Path dbSchemaPath = Paths.get("./test_db_schema.json");
if (dbSchemaPath == null) {
throw new Exception("Can't find path");
}
CQL2PgJSON cql2pgjson = new CQL2PgJSON("instance.jsonb");
cql2pgjson.setDbSchemaPath(dbSchemaPath.toString());
List<Table> tables = cql2pgjson.getDbSchema().getTables();
String[] tableArray = new String[] { "loan_type", "material_type", "service_point_user" };
for (String tableName : tableArray) {
boolean found = false;
for (Table table : tables) {
if (tableName.equalsIgnoreCase(table.getTableName())) {
found = true;
break;
}
}
assertThat(tableName, found, is(true));
}
}
Aggregations