Search in sources :

Example 6 with IndexDef

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

the class TableDef method getCreateDdl.

@Override
public String getCreateDdl() {
    StringBuilder sb = new StringBuilder();
    Project project = getProject();
    Rdbms dbms = project.getDatabaseSystem();
    DbmsTypeManager typeManager = dbms.getTypeManager();
    sb.append(typeManager.getCreateTableClause(getEntityName()) + " (\n");
    StringBuilder triggers = new StringBuilder();
    for (ColumnDef column : getColumns().getAvailableElements()) {
        sb.append(typeManager.getColumnDefinitionClause(column));
        sb.append(",\n");
        triggers.append(typeManager.getColumnTriggerDefinition(column));
    }
    if (getColumnsCount() > 0) {
        sb.delete(sb.length() - 2, sb.length());
    }
    sb.append(");\n");
    ColumnDef autoincrementColumn = getAutoincrementColumn();
    if (autoincrementColumn != null) {
        String value = getStartId();
        if (value != null)
            sb.append(typeManager.getStartingIncrementDefinition(getEntityName(), autoincrementColumn.getName(), Long.parseLong(value)));
    }
    sb.append(triggers);
    List<String> indexDdls = new ArrayList<>();
    for (IndexDef index : getIndices().getAvailableElements()) {
        if (!typeManager.isFunctionalIndexSupported() && index.isFunctional())
            continue;
        if (dbms == Rdbms.DB2 && index.getName().length() > Rdbms.DB2_INDEX_LENGTH)
            continue;
        indexDdls.add(index.getCreateDdl() + "\n");
    }
    Collections.sort(indexDdls);
    sb.append(String.join("", indexDdls));
    return sb.toString();
}
Also used : Rdbms(com.developmentontheedge.be5.metadata.sql.Rdbms) ArrayList(java.util.ArrayList) DbmsTypeManager(com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager)

Aggregations

IndexColumnDef (com.developmentontheedge.be5.metadata.model.IndexColumnDef)4 ColumnDef (com.developmentontheedge.be5.metadata.model.ColumnDef)3 IndexDef (com.developmentontheedge.be5.metadata.model.IndexDef)3 Rdbms (com.developmentontheedge.be5.metadata.sql.Rdbms)3 DbmsTypeManager (com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager)3 TableDef (com.developmentontheedge.be5.metadata.model.TableDef)2 ArrayList (java.util.ArrayList)2 ProjectElementException (com.developmentontheedge.be5.metadata.exception.ProjectElementException)1 Entity (com.developmentontheedge.be5.metadata.model.Entity)1 Module (com.developmentontheedge.be5.metadata.model.Module)1 Project (com.developmentontheedge.be5.metadata.model.Project)1 ViewDef (com.developmentontheedge.be5.metadata.model.ViewDef)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 ResultSet (java.sql.ResultSet)1 Test (org.junit.Test)1