Search in sources :

Example 1 with Table

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;
}
Also used : Table(org.folio.dbschema.Table) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ForeignKeys(org.folio.dbschema.ForeignKeys)

Example 2 with Table

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();
}
Also used : Table(org.folio.dbschema.Table) DbFkInfo(org.folio.cql2pgjson.model.DbFkInfo)

Example 3 with Table

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"));
}
Also used : Table(org.folio.dbschema.Table) Test(org.junit.jupiter.api.Test)

Example 4 with Table

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));
}
Also used : Table(org.folio.dbschema.Table) Schema(org.folio.dbschema.Schema) Before(org.junit.Before)

Example 5 with 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));
    }
}
Also used : Path(java.nio.file.Path) Table(org.folio.dbschema.Table) Test(org.junit.Test)

Aggregations

Table (org.folio.dbschema.Table)9 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 DbFkInfo (org.folio.cql2pgjson.model.DbFkInfo)2 Schema (org.folio.dbschema.Schema)2 Test (org.junit.Test)2 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Path (java.nio.file.Path)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Pattern (java.util.regex.Pattern)1 ObjectUtils (org.apache.commons.lang3.ObjectUtils)1