Search in sources :

Example 6 with TableDef

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

the class BaseTypeManagerTest method addColumn.

protected ColumnDef addColumn(TableDef def, String name, String type) {
    ColumnDef col = new ColumnDef(name, def.getColumns());
    col.setTypeString(type);
    DataElementUtils.save(col);
    return col;
}
Also used : ColumnDef(com.developmentontheedge.be5.metadata.model.ColumnDef)

Example 7 with TableDef

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

the class Db2TypeManagerTest method testTypes.

@Test
public void testTypes() throws ExtendedSqlException {
    TableDef def = createTable(Rdbms.DB2);
    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("BEGIN DECLARE CONTINUE HANDLER FOR SQLSTATE '42704' BEGIN END; EXECUTE IMMEDIATE 'DROP TABLE \"table\"'; END;\n" + "CREATE TABLE \"table\" (\n" + "A BLOB(16M) NOT NULL,\n" + "B CLOB(128K) 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("ALTER TABLE \"table\" ADD COLUMN F BIGINT GENERATED BY DEFAULT AS IDENTITY (NO CACHE) NOT NULL PRIMARY KEY;\n" + "CALL admin_cmd('REORG TABLE \"table\"');", def2.getDiffDdl(def, null));
    assertEquals("ALTER TABLE \"table\" DROP COLUMN F;\n" + "CALL admin_cmd('REORG TABLE \"table\"');", def.getDiffDdl(def2, null));
}
Also used : ColumnDef(com.developmentontheedge.be5.metadata.model.ColumnDef) TableDef(com.developmentontheedge.be5.metadata.model.TableDef) Test(org.junit.Test)

Example 8 with TableDef

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

the class TableDef method getDangerousDiffStatements.

@Override
public String getDangerousDiffStatements(DdlElement other, SqlExecutor sql) throws ExtendedSqlException {
    if (other == null || (sql != null && sql.isEmpty(getEntityName()))) {
        return "";
    }
    if (!(other instanceof TableDef))
        return other.getDropDdl();
    TableDef def = (TableDef) ((TableDef) other).clone(other.getOrigin(), other.getName());
    DbmsTypeManager typeManager = getProject().getDatabaseSystem().getTypeManager();
    String diff = getColumnsDiff(def, typeManager, true, sql);
    if (diff == null)
        return getDropDdl();
    return diff;
}
Also used : DbmsTypeManager(com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager)

Example 9 with TableDef

use of com.developmentontheedge.be5.metadata.model.TableDef 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 10 with TableDef

use of com.developmentontheedge.be5.metadata.model.TableDef 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)

Aggregations

TableDef (com.developmentontheedge.be5.metadata.model.TableDef)9 ColumnDef (com.developmentontheedge.be5.metadata.model.ColumnDef)8 Entity (com.developmentontheedge.be5.metadata.model.Entity)6 IndexColumnDef (com.developmentontheedge.be5.metadata.model.IndexColumnDef)4 Test (org.junit.Test)4 Project (com.developmentontheedge.be5.metadata.model.Project)3 DbmsTypeManager (com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager)3 ProjectElementException (com.developmentontheedge.be5.metadata.exception.ProjectElementException)2 DdlElement (com.developmentontheedge.be5.metadata.model.DdlElement)2 IndexDef (com.developmentontheedge.be5.metadata.model.IndexDef)2 Module (com.developmentontheedge.be5.metadata.model.Module)2 ViewDef (com.developmentontheedge.be5.metadata.model.ViewDef)2 Rdbms (com.developmentontheedge.be5.metadata.sql.Rdbms)2 FreemarkerSqlException (com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException)1 Query (com.developmentontheedge.be5.metadata.model.Query)1 BeCaseInsensitiveCollection (com.developmentontheedge.be5.metadata.model.base.BeCaseInsensitiveCollection)1 BeModelElement (com.developmentontheedge.be5.metadata.model.base.BeModelElement)1 IndexInfo (com.developmentontheedge.be5.metadata.sql.pojo.IndexInfo)1 SqlColumnInfo (com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo)1 DefaultTypeManager (com.developmentontheedge.be5.metadata.sql.type.DefaultTypeManager)1