Search in sources :

Example 11 with ColumnDef

use of com.developmentontheedge.be5.metadata.model.ColumnDef in project be5 by DevelopmentOnTheEdge.

the class TableDef method isSafeTypeUpdate.

private boolean isSafeTypeUpdate(ColumnDef oldColumn, ColumnDef column, DbmsTypeManager typeManager, SqlExecutor sql) throws ExtendedSqlException {
    SqlColumnType oldType = oldColumn.getType();
    SqlColumnType type = column.getType();
    if (typeManager.getTypeClause(oldType).equals(typeManager.getTypeClause(type)))
        return true;
    // Enlarging VARCHAR column
    if (oldType.getTypeName().equals(TYPE_VARCHAR) && type.getTypeName().equals(TYPE_VARCHAR)) {
        if (type.getSize() >= oldType.getSize())
            return true;
        return sql != null && !sql.hasResult("sql.select.longer", typeManager.normalizeIdentifier(getEntityName()), typeManager.normalizeIdentifier(oldColumn.getName()), type.getSize());
    }
    // Enlarging DECIMAL column
    if (oldType.getTypeName().equals(TYPE_DECIMAL) && type.getTypeName().equals(TYPE_DECIMAL)) {
        if (type.getSize() >= oldType.getSize() && type.getPrecision() >= oldType.getPrecision())
            return true;
    }
    // Adding new variants for ENUM column
    if ((oldType.getTypeName().equals(TYPE_ENUM) || oldType.getTypeName().equals(TYPE_BOOL) || oldType.getTypeName().equals(TYPE_VARCHAR)) && (type.getTypeName().equals(TYPE_ENUM) || type.getTypeName().equals(TYPE_BOOL))) {
        List<String> newValues = Arrays.asList(type.getEnumValues());
        if (!oldType.getTypeName().equals(TYPE_VARCHAR) && newValues.containsAll(Arrays.asList(oldType.getEnumValues())))
            return true;
        return sql != null && !sql.hasResult("sql.select.not.in.range", typeManager.normalizeIdentifier(getEntityName()), typeManager.normalizeIdentifier(oldColumn.getName()), MetadataUtils.toInClause(newValues));
    }
    // Changing ENUM to varchar
    if ((oldType.getTypeName().equals(TYPE_ENUM) || oldType.getTypeName().equals(TYPE_BOOL)) && type.getTypeName().equals(TYPE_VARCHAR)) {
        int len = 0;
        for (String value : oldType.getEnumValues()) {
            len = Math.max(len, value.length());
        }
        if (type.getSize() >= len)
            return true;
        return sql != null && !sql.hasResult("sql.select.longer", typeManager.normalizeIdentifier(getEntityName()), typeManager.normalizeIdentifier(oldColumn.getName()), type.getSize());
    }
    // Changing ENUM to char
    if (oldType.getTypeName().equals(TYPE_ENUM) && type.getTypeName().equals(TYPE_CHAR)) {
        return StreamEx.of(oldType.getEnumValues()).map(String::length).distinct().collect(MoreCollectors.onlyOne()).filter(len -> type.getSize() == len).isPresent();
    }
    return false;
}
Also used : DbmsTypeManager(com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager) Arrays(java.util.Arrays) BeCaseInsensitiveCollection(com.developmentontheedge.be5.metadata.model.base.BeCaseInsensitiveCollection) SqlExecutor(com.developmentontheedge.dbms.SqlExecutor) Iterator(java.util.Iterator) BeVectorCollection(com.developmentontheedge.be5.metadata.model.base.BeVectorCollection) MoreCollectors(one.util.streamex.MoreCollectors) ProjectElementException(com.developmentontheedge.be5.metadata.exception.ProjectElementException) Set(java.util.Set) MetadataUtils(com.developmentontheedge.be5.metadata.MetadataUtils) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) Rdbms(com.developmentontheedge.be5.metadata.sql.Rdbms) ArrayList(java.util.ArrayList) BeModelElement(com.developmentontheedge.be5.metadata.model.base.BeModelElement) List(java.util.List) Strings2(com.developmentontheedge.be5.metadata.util.Strings2) PropertyName(com.developmentontheedge.beans.annot.PropertyName) StreamEx(one.util.streamex.StreamEx) SqlColumnType(com.developmentontheedge.be5.metadata.model.SqlColumnType) Map(java.util.Map) ExtendedSqlException(com.developmentontheedge.dbms.ExtendedSqlException) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) SqlColumnType(com.developmentontheedge.be5.metadata.model.SqlColumnType)

Example 12 with ColumnDef

use of com.developmentontheedge.be5.metadata.model.ColumnDef in project be5 by DevelopmentOnTheEdge.

the class PostgresTypeManagerTest method testTypes.

@Test
public void testTypes() throws ExtendedSqlException {
    TableDef def = createTable(Rdbms.POSTGRESQL);
    addColumn(def, "a", SqlColumnType.TYPE_BLOB);
    addColumn(def, "b", SqlColumnType.TYPE_BIGTEXT);
    addColumn(def, "c", SqlColumnType.TYPE_UINT);
    addColumn(def, "d", SqlColumnType.TYPE_UBIGINT);
    addColumn(def, "e", SqlColumnType.TYPE_DATETIME);
    assertEquals("DROP TABLE IF EXISTS \"table\";\n" + "CREATE TABLE \"table\" (\n" + "a BYTEA NOT NULL,\n" + "b TEXT NOT NULL,\n" + "c INT NOT NULL,\n" + "d BIGINT NOT NULL,\n" + "e TIMESTAMP NOT NULL);\n", def.getDdl());
    TableDef def2 = (TableDef) def.clone(def.getOrigin(), def.getName());
    ColumnDef col = addColumn(def2, "f", SqlColumnType.TYPE_KEY);
    col.setAutoIncrement(true);
    col.setPrimaryKey(true);
    assertEquals("DROP SEQUENCE IF EXISTS table_f_seq;\n" + "CREATE SEQUENCE table_f_seq;\n" + "ALTER TABLE \"table\" ADD COLUMN f BIGINT DEFAULT nextval('table_f_seq'::regclass) PRIMARY KEY;\n", def2.getDiffDdl(def, null));
    assertEquals("ALTER TABLE \"table\" DROP COLUMN f;\n" + "DROP SEQUENCE IF EXISTS table_f_seq;\n" + "DROP INDEX IF EXISTS table_pkey;\n", def.getDiffDdl(def2, null));
}
Also used : ColumnDef(com.developmentontheedge.be5.metadata.model.ColumnDef) IndexColumnDef(com.developmentontheedge.be5.metadata.model.IndexColumnDef) TableDef(com.developmentontheedge.be5.metadata.model.TableDef) Test(org.junit.Test)

Example 13 with ColumnDef

use of com.developmentontheedge.be5.metadata.model.ColumnDef in project be5 by DevelopmentOnTheEdge.

the class SerializationTest method testSerializationBasics.

@Test
public void testSerializationBasics() throws IOException, ProjectSaveException, ProjectLoadException {
    Path path = tmp.newFolder().toPath();
    Project project = ProjectTestUtils.getProject("test");
    Entity entity = ProjectTestUtils.createEntity(project, "entity", "ID");
    TableDef scheme = ProjectTestUtils.createScheme(entity);
    // only for test SqlColumnType getType( Collection<ColumnDef> stack )
    ColumnDef column3 = new ColumnDef("column3", scheme.getColumns());
    column3.setTableTo(entity.getName());
    column3.setColumnsTo("ID");
    DataElementUtils.save(column3);
    Query query = ProjectTestUtils.createQuery(entity, "All records", Arrays.asList('@' + SpecialRoleGroup.ALL_ROLES_EXCEPT_GUEST_GROUP, "-User"));
    query.getOperationNames().setValues(Collections.singleton("op"));
    ProjectTestUtils.createOperation(entity);
    Serialization.save(project, path);
    assertEquals(path, project.getLocation());
    LoadContext lc = new LoadContext();
    Project project2 = Serialization.load(path, lc);
    project2.setDatabaseSystem(Rdbms.POSTGRESQL);
    lc.check();
    Entity entity2 = project2.getEntity("entity");
    assertEquals(entity, entity2);
    assertTrue(entity2.isBesql());
    assertEquals("VARCHAR(20)", entity2.findTableDefinition().getColumns().get("name").getTypeString());
    assertEquals(StreamEx.of("Administrator", "Operator").toSet(), entity2.getQueries().get("All records").getRoles().getFinalValues());
    assertEquals("op", entity2.getQueries().get("All records").getOperationNames().getFinalValuesString());
}
Also used : Path(java.nio.file.Path) Project(com.developmentontheedge.be5.metadata.model.Project) Entity(com.developmentontheedge.be5.metadata.model.Entity) Query(com.developmentontheedge.be5.metadata.model.Query) ColumnDef(com.developmentontheedge.be5.metadata.model.ColumnDef) TableDef(com.developmentontheedge.be5.metadata.model.TableDef) Test(org.junit.Test)

Example 14 with ColumnDef

use of com.developmentontheedge.be5.metadata.model.ColumnDef in project be5 by DevelopmentOnTheEdge.

the class OracleTypeManager method getColumnTriggerDefinition.

@Override
public String getColumnTriggerDefinition(ColumnDef column) {
    String defaultValue = column.getDefaultValue();
    if (defaultValue == null)
        return "";
    ColumnFunction function = new ColumnFunction(defaultValue);
    if (ColumnFunction.TRANSFORM_GENERIC.equals(function.getTransform())) {
        return "CREATE OR REPLACE TRIGGER " + getTriggerName(column) + "\nBEFORE INSERT OR UPDATE OF " + normalizeIdentifier(function.getColumnName()) + " ON " + normalizeIdentifier(column.getEntity().getName()) + "\nFOR EACH ROW" + "\nBEGIN" + "\n   :new." + normalizeIdentifier(column.getName()) + " := '" + column.getEntity().getName() + ".' || :new." + normalizeIdentifier(function.getColumnName()) + ";" + "\nEND;\n";
    }
    return "";
}
Also used : ColumnFunction(com.developmentontheedge.be5.metadata.model.ColumnFunction)

Example 15 with ColumnDef

use of com.developmentontheedge.be5.metadata.model.ColumnDef in project be5 by DevelopmentOnTheEdge.

the class PostgresTypeManager method getCreateIndexClause.

@Override
public String getCreateIndexClause(IndexDef indexDef) {
    Optional<IndexColumnDef> col = indexDef.stream().collect(MoreCollectors.onlyOne()).filter(c -> !c.isFunctional());
    if (col.isPresent()) {
        ColumnDef columnDef = indexDef.getTable().getColumns().get(col.get().getName());
        if (columnDef != null && columnDef.getType().getTypeName().equals(SqlColumnType.TYPE_JSONB)) {
            StringBuilder sb = new StringBuilder();
            sb.append("CREATE ");
            if (indexDef.isUnique())
                sb.append("UNIQUE ");
            sb.append("INDEX ");
            sb.append(normalizeIdentifier(indexDef.getName()));
            sb.append(" ON ");
            sb.append(normalizeIdentifier(indexDef.getTable().getEntityName()));
            sb.append(" USING GIN (");
            sb.append(columnDef.getName());
            sb.append(");");
            return sb.toString();
        }
    }
    return super.getCreateIndexClause(indexDef);
}
Also used : IndexColumnDef(com.developmentontheedge.be5.metadata.model.IndexColumnDef) ColumnDef(com.developmentontheedge.be5.metadata.model.ColumnDef) IndexColumnDef(com.developmentontheedge.be5.metadata.model.IndexColumnDef)

Aggregations

ColumnDef (com.developmentontheedge.be5.metadata.model.ColumnDef)18 DynamicProperty (com.developmentontheedge.beans.DynamicProperty)7 IndexColumnDef (com.developmentontheedge.be5.metadata.model.IndexColumnDef)6 ArrayList (java.util.ArrayList)6 TableDef (com.developmentontheedge.be5.metadata.model.TableDef)5 ColumnFunction (com.developmentontheedge.be5.metadata.model.ColumnFunction)4 Entity (com.developmentontheedge.be5.metadata.model.Entity)4 Rdbms (com.developmentontheedge.be5.metadata.sql.Rdbms)4 DbmsTypeManager (com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager)4 Test (org.junit.Test)4 IndexDef (com.developmentontheedge.be5.metadata.model.IndexDef)3 HashMap (java.util.HashMap)3 ProjectElementException (com.developmentontheedge.be5.metadata.exception.ProjectElementException)2 Project (com.developmentontheedge.be5.metadata.model.Project)2 BeCaseInsensitiveCollection (com.developmentontheedge.be5.metadata.model.base.BeCaseInsensitiveCollection)2 BeModelElement (com.developmentontheedge.be5.metadata.model.base.BeModelElement)2 Timestamp (java.sql.Timestamp)2 Date (java.util.Date)2 Map (java.util.Map)2 Meta (com.developmentontheedge.be5.api.services.Meta)1