Search in sources :

Example 1 with ColumnDef

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

the class DefaultTypeManager method getColumnDefinitionClause.

@Override
public String getColumnDefinitionClause(ColumnDef column) {
    StringBuilder sb = new StringBuilder(normalizeIdentifier(column.getName()));
    sb.append(' ').append(getTypeClause(column));
    if (column.isAutoIncrement()) {
        sb.append(' ').append(getAutoIncrementClause(column));
    }
    if (column.getDefaultValue() != null) {
        String defaultValue = getDefaultValue(column);
        ColumnFunction function = new ColumnFunction(defaultValue);
        if (function.isTransformed()) {
            if (getGeneratedPrefix() != null) {
                sb.append(' ').append(getGeneratedPrefix()).append(' ').append(function.getDefinition(column.getProject().getDatabaseSystem(), column.getEntity().getName()));
            }
        } else {
            sb.append(' ').append("DEFAULT ").append(defaultValue);
        }
    }
    addCanBeNullAndAndConstraintClause(column, sb);
    if (column.isPrimaryKey()) {
        sb.append(' ').append("PRIMARY KEY");
    }
    return sb.toString();
}
Also used : ColumnFunction(com.developmentontheedge.be5.metadata.model.ColumnFunction)

Example 2 with ColumnDef

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

the class MySqlTypeManager method getRenameColumnStatements.

@Override
public String getRenameColumnStatements(ColumnDef column, String newName) {
    StringBuilder sb = new StringBuilder();
    ColumnDef newColumn = (ColumnDef) column.clone(column.getOrigin(), newName);
    sb.append("ALTER TABLE ").append(normalizeIdentifier(column.getTable().getEntityName())).append(" CHANGE COLUMN ").append(normalizeIdentifier(column.getName())).append(' ').append(getColumnDefinitionClause(newColumn)).append(';');
    // TODO: check whether it's necessary to update existing null values
    return sb.toString();
}
Also used : ColumnDef(com.developmentontheedge.be5.metadata.model.ColumnDef)

Example 3 with ColumnDef

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

the class OracleTypeManager method getDropTriggerDefinition.

@Override
public String getDropTriggerDefinition(ColumnDef column) {
    String defaultValue = column.getDefaultValue();
    if (defaultValue == null)
        return "";
    ColumnFunction function = new ColumnFunction(defaultValue);
    if (ColumnFunction.TRANSFORM_GENERIC.equals(function.getTransform())) {
        return "DROP TRIGGER " + getTriggerName(column) + ";\n";
    }
    return "";
}
Also used : ColumnFunction(com.developmentontheedge.be5.metadata.model.ColumnFunction)

Example 4 with ColumnDef

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

the class SqlServerTypeManager method getAlterColumnStatements.

@Override
public String getAlterColumnStatements(ColumnDef column, ColumnDef oldColumn) {
    String tableName = column.getTable().getEntityName();
    String columnName = column.getName();
    String prefix = "ALTER TABLE " + normalizeIdentifier(tableName) + " ALTER COLUMN " + normalizeIdentifier(columnName) + " ";
    StringBuilder sb = new StringBuilder();
    StringBuilder endSb = new StringBuilder();
    for (IndexDef index : oldColumn.getTable().getIndices().getAvailableElements()) {
        for (IndexColumnDef indexColumn : index.getAvailableElements()) {
            if (indexColumn.getName().equals(oldColumn.getName())) {
                sb.append(index.getDropDdl());
                endSb.append(index.getCreateDdl());
                break;
            }
        }
    }
    if (column.isCanBeNull() && (!oldColumn.isCanBeNull() || (!column.getType().toString().equals(oldColumn.getType().toString())))) {
        sb.append(prefix).append(getTypeClause(column.getType())).append(" NULL;");
    }
    if (!column.isCanBeNull() && (oldColumn.isCanBeNull() || (!column.getType().toString().equals(oldColumn.getType().toString())))) {
        if (oldColumn.isCanBeNull() && !Strings2.isNullOrEmpty(column.getDefaultValue())) {
            sb.append("UPDATE ").append(normalizeIdentifier(tableName)).append(" SET ").append(normalizeIdentifier(columnName)).append('=').append(column.getDefaultValue()).append(" WHERE ").append(normalizeIdentifier(columnName)).append(" IS NULL;");
        }
        sb.append(prefix).append(getTypeClause(column.getType())).append(" NOT NULL;");
    }
    if (!Strings2.nullToEmpty(column.getDefaultValue()).equals(Strings2.nullToEmpty(oldColumn.getDefaultValue()))) {
        if (!Strings2.isNullOrEmpty(oldColumn.getDefaultValue())) {
            sb.append(getDropDefaultStatements(tableName, columnName));
        }
        if (!Strings2.isNullOrEmpty(column.getDefaultValue())) {
            sb.append("ALTER TABLE ").append(normalizeIdentifier(tableName)).append(" ADD CONSTRAINT df_").append(tableName).append('_').append(columnName).append(" DEFAULT ").append(column.getDefaultValue()).append(" FOR ").append(normalizeIdentifier(columnName)).append(';');
        }
    }
    sb.append(endSb);
    return sb.toString();
}
Also used : IndexColumnDef(com.developmentontheedge.be5.metadata.model.IndexColumnDef) IndexDef(com.developmentontheedge.be5.metadata.model.IndexDef)

Example 5 with ColumnDef

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

the class SqlServerTypeManager method getTypeClause.

@Override
public String getTypeClause(ColumnDef column) {
    if (column.getDefaultValue() != null) {
        String defaultValue = getDefaultValue(column);
        ColumnFunction function = new ColumnFunction(defaultValue);
        if (function.isTransformed()) {
            return "";
        }
    }
    return super.getTypeClause(column);
}
Also used : ColumnFunction(com.developmentontheedge.be5.metadata.model.ColumnFunction)

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