Search in sources :

Example 56 with Column

use of org.molgenis.emx2.Column in project molgenis-emx2 by molgenis.

the class JsonLdExample method create.

public static void create(Schema schema) {
    Table personTable = schema.create(table("Person").setSemantics("https://schema.org/docs/jsonldcontext.jsonld#Person").add(column("name").setPkey().setSemantics("https://schema.org/docs/jsonldcontext.jsonld#name"), column("jobTitle").setSemantics("https://schema.org/docs/jsonldcontext.jsonld#jobTitle").setRequired(true), column("telephone").setSemantics("https://schema.org/docs/jsonldcontext.jsonld#telephone"), column("url").setSemantics("https://schema.org/docs/jsonldcontext.jsonld#url")));
    personTable.insert(row("name", "Jane Doe", "jobTitle", "Professor", "telephone", "(425) 123-4567", "url", "http://www.janedoe.com"), row("id", "2", "name", "Mary Stone", "jobTitle", "Cook"));
    Table recipeTable = schema.create(table("Recipe").setSemantics("https://schema.org/docs/jsonldcontext.jsonld#Recipe").add(column("name").setPkey().setSemantics("https://schema.org/docs/jsonldcontext.jsonld#name"), column("author").setType(REF).setRefTable("Person").setSemantics("https://schema.org/docs/jsonldcontext.jsonld#author").setRequired(true), column("datePublished").setType(DATE).setSemantics("https://schema.org/docs/jsonldcontext.jsonld#datePublished").setRequired(true), column("prepTime").setSemantics("https://schema.org/docs/jsonldcontext.jsonld#prepTime").setRequired(true)));
    recipeTable.insert(row("name", "Mary's Cookies", "author", "Mary Stone", "datePublished", "2018-03-10", "prepTime", "PT20M"));
}
Also used : Table(org.molgenis.emx2.Table)

Example 57 with Column

use of org.molgenis.emx2.Column in project molgenis-emx2 by molgenis.

the class SettingsDataModel method create.

public static void create(Schema schema) {
    TableMetadata tm = table("Settings");
    tm.add(column("schema").setKey(1));
    tm.add(column("key").setKey(1));
    tm.add(column("value"));
    schema.create(tm);
}
Also used : TableMetadata(org.molgenis.emx2.TableMetadata)

Example 58 with Column

use of org.molgenis.emx2.Column in project molgenis-emx2 by molgenis.

the class TableSort method sortTableByDependency.

public static void sortTableByDependency(List<TableMetadata> tableList) {
    ArrayList<TableMetadata> result = new ArrayList<>();
    ArrayList<TableMetadata> todo = new ArrayList<>(tableList);
    // ensure deterministic order
    todo.sort(new Comparator<TableMetadata>() {

        @Override
        public int compare(TableMetadata o1, TableMetadata o2) {
            return o1.getTableName().compareTo(o2.getTableName());
        }
    });
    while (!todo.isEmpty()) {
        int size = todo.size();
        for (int i = 0; i < todo.size(); i++) {
            TableMetadata current = todo.get(i);
            boolean depends = false;
            for (int j = 0; j < todo.size(); j++) {
                if (current.getInherit() != null && current.getImportSchema() == null && todo.get(j).equals(current.getInheritedTable())) {
                    depends = true;
                    break;
                }
            }
            if (!depends)
                for (Column c : current.getColumns()) {
                    if (c.getRefTableName() != null && !c.isRefback()) {
                        for (int j = 0; j < todo.size(); j++) {
                            if (i != j && (todo.get(j).getTableName().equals(c.getRefTableName()))) {
                                depends = true;
                                break;
                            }
                        }
                    }
                }
            if (!depends) {
                result.add(todo.get(i));
                todo.remove(i);
            }
        }
        // check for circular relationship
        if (size == todo.size()) {
            throw new MolgenisException("circular dependency error: following tables have circular dependency: " + todo.stream().map(TableMetadata::getTableName).collect(Collectors.joining(",")));
        }
    }
    tableList.clear();
    tableList.addAll(result);
}
Also used : TableMetadata(org.molgenis.emx2.TableMetadata) Column(org.molgenis.emx2.Column) ArrayList(java.util.ArrayList) MolgenisException(org.molgenis.emx2.MolgenisException)

Example 59 with Column

use of org.molgenis.emx2.Column in project molgenis-emx2 by molgenis.

the class TestGetSetAllTypesOnRows method addContents.

private void addContents(SchemaMetadata m, List<ColumnType> columnTypes) {
    TableMetadata t = m.create(table("TypeTest"));
    for (ColumnType columnType : columnTypes) {
        t.add(column("test" + columnType).setType(columnType).setRequired(true));
        t.add(column("test" + columnType + "_nullable").setType(columnType));
        t.add(column("test" + columnType + "_readonly").setType(columnType).setReadonly(true));
    }
}
Also used : ColumnType(org.molgenis.emx2.ColumnType)

Example 60 with Column

use of org.molgenis.emx2.Column in project molgenis-emx2 by molgenis.

the class SqlColumnRefExecutor method createRefConstraints.

public static void createRefConstraints(DSLContext jooq, Column refColumn) {
    validateColumn(refColumn);
    Name fkeyConstraintName = name(getRefConstraintName(refColumn));
    Name thisTable = getJooqTable(refColumn.getTable()).getQualifiedName();
    List<Name> thisColumns = refColumn.getReferences().stream().map(c -> name(c.getName())).collect(Collectors.toList());
    List<Name> otherColumns = refColumn.getReferences().stream().map(c -> name(c.getRefTo())).collect(Collectors.toList());
    Name fkeyTable = name(refColumn.getRefTable().getSchemaName(), refColumn.getRefTableName());
    ConstraintForeignKeyOnStep constraint = constraint(fkeyConstraintName).foreignKey(thisColumns.toArray(new Name[thisColumns.size()])).references(fkeyTable, otherColumns.toArray(new Name[otherColumns.size()])).onUpdateCascade();
    if (refColumn.isCascadeDelete()) {
        constraint = constraint.onDeleteCascade();
    }
    jooq.alterTable(getJooqTable(refColumn.getTable())).add(constraint).execute();
    jooq.execute("ALTER TABLE {0} ALTER CONSTRAINT {1} DEFERRABLE INITIALLY IMMEDIATE", thisTable, fkeyConstraintName);
    jooq.createIndex(getIndexName(refColumn)).on(thisTable, thisColumns.toArray(new Name[thisColumns.size()])).execute();
}
Also used : Column(org.molgenis.emx2.Column) SqlColumnExecutor.validateColumn(org.molgenis.emx2.sql.SqlColumnExecutor.validateColumn) DSL.constraint(org.jooq.impl.DSL.constraint) List(java.util.List) DSL.name(org.jooq.impl.DSL.name) SqlTableMetadataExecutor.getJooqTable(org.molgenis.emx2.sql.SqlTableMetadataExecutor.getJooqTable) Name(org.jooq.Name) DSLContext(org.jooq.DSLContext) Collectors(java.util.stream.Collectors) ConstraintForeignKeyOnStep(org.jooq.ConstraintForeignKeyOnStep) ConstraintForeignKeyOnStep(org.jooq.ConstraintForeignKeyOnStep) Name(org.jooq.Name)

Aggregations

org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column)20 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)19 Row (org.molgenis.emx2.Row)15 Table (org.molgenis.emx2.Table)12 HashMap (java.util.HashMap)11 Column (org.molgenis.emx2.Column)11 List (java.util.List)9 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint)9 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint)9 Column (com.google.bigtable.v2.Column)8 Schema (org.molgenis.emx2.Schema)8 Family (com.google.bigtable.v2.Family)7 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint)7 ColumnType (org.molgenis.emx2.ColumnType)7 MolgenisException (org.molgenis.emx2.MolgenisException)7 TableMetadata (org.molgenis.emx2.TableMetadata)7 Collectors (java.util.stream.Collectors)6 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table)5 Map (java.util.Map)4