Java Code Examples for org.apache.ddlutils.model.Column#isAutoIncrement()

The following examples show how to use org.apache.ddlutils.model.Column#isAutoIncrement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GemFireXDBuilder.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the database-native type for the given column.
 * 
 * @param column
 *          The column
 * @return The native type
 */
@Override
protected String getNativeType(Column column) {
  // if the size of types overflows, then try to go to the next larger
  // type (e.g. see #43238)
  final int columnSize = column.getSizeAsInt();
  final int columnType = column.getTypeCode();
  if (columnSize > 0) {
    final ColumnSizeSpec sizeSpec = this._maxSizes.get(columnType);
    if (sizeSpec != null && columnSize >= sizeSpec._size) {
      return sizeSpec._higherType;
    }
  }
  // boost TINYINT and SMALLINT autoincrement columns to INT (#43227)
  if (column.isAutoIncrement()) {
    switch (column.getTypeCode()) {
      case Types.TINYINT:
      case Types.SMALLINT:
        return "INTEGER";
    }
  }
  final String nativeType = getPlatformInfo().getNativeType(columnType);
  return nativeType != null ? nativeType : column.getType();
}
 
Example 2
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Prints the default value stmt part for the column.
 * 
 * @param table  The table
 * @param column The column
 */ 
protected void writeColumnDefaultValueStmt(Table table, Column column) throws IOException
{
    Object parsedDefault = column.getParsedDefaultValue();

    if (parsedDefault != null)
    {
        if (!getPlatformInfo().isDefaultValuesForLongTypesSupported() && 
            ((column.getTypeCode() == Types.LONGVARBINARY) || (column.getTypeCode() == Types.LONGVARCHAR)))
        {
            throw new ModelException("The platform does not support default values for LONGVARCHAR or LONGVARBINARY columns");
        }
        // we write empty default value strings only if the type is not a numeric or date/time type
        if (isValidDefaultValue(column.getDefaultValue(), column.getTypeCode()))
        {
            print(" DEFAULT ");
            writeColumnDefaultValue(table, column);
        }
    }
    else if (getPlatformInfo().isDefaultValueUsedForIdentitySpec() && column.isAutoIncrement())
    {
        print(" DEFAULT ");
        writeColumnDefaultValue(table, column);
    }
}
 
Example 3
Source File: GemFireXDBuilder.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the database-native type for the given column.
 * 
 * @param column
 *          The column
 * @return The native type
 */
@Override
protected String getNativeType(Column column) {
  // if the size of types overflows, then try to go to the next larger
  // type (e.g. see #43238)
  final int columnSize = column.getSizeAsInt();
  final int columnType = column.getTypeCode();
  if (columnSize > 0) {
    final ColumnSizeSpec sizeSpec = this._maxSizes.get(columnType);
    if (sizeSpec != null && columnSize >= sizeSpec._size) {
      return sizeSpec._higherType;
    }
  }
  // boost TINYINT and SMALLINT autoincrement columns to INT (#43227)
  if (column.isAutoIncrement()) {
    switch (column.getTypeCode()) {
      case Types.TINYINT:
      case Types.SMALLINT:
        return "INTEGER";
    }
  }
  final String nativeType = getPlatformInfo().getNativeType(columnType);
  return nativeType != null ? nativeType : column.getType();
}
 
Example 4
Source File: DataDtdWriter.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the DTD attribute entry for the given column.
 * 
 * @param column The column
 * @param writer The writer to write the attribute entry to
 */
private void writeColumnAttributeEntry(Column column, PrintWriter writer) throws IOException
{
    writer.print("    <!--");
    if (column.isPrimaryKey())
    {
        writer.print(" primary key,");
    }
    if (column.isAutoIncrement())
    {
        writer.print(" auto increment,");
    }
    writer.print(" JDBC type: "+column.getType());
    if ((column.getSize() != null) && (column.getSize().length() > 0))
    {
        writer.print("("+column.getSize()+")");
    }
    writer.println(" -->");
    writer.print("    "+column.getName()+" CDATA ");
    if ((column.getDefaultValue() != null) && (column.getDefaultValue().length() > 0))
    {
        writer.println("\"" + column.getDefaultValue() + "\"");
    }
    else
    {
        writer.println(column.isRequired() ? "#REQUIRED" : "#IMPLIED");
    }
}
 
Example 5
Source File: WriteDataToDatabaseCommand.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
protected void writeAddIdentityUsingAlterTable(Platform platform,
    Database model) throws DataSinkException {
  // if identity column has to be set using alter table then
  // do it after loading the data
  if (_addIdentityUsingAlterTable && platform.getPlatformInfo()
          .isAddingIdentityUsingAlterTableSupported()) {
    StringWriter buffer = new StringWriter();
  
    SqlBuilder builder = platform.getSqlBuilder();
    builder.setWriter(buffer);
    for (int index = 0; index < model.getTableCount(); index++) {
      Table table = model.getTable(index);
      for (int col = 0; col < table.getColumnCount(); col++) {
        Column column = table.getColumn(col);
        if (column.isAutoIncrement()) {
          try {
            builder.writeAddIdentityColumnUsingAlterTable(table, column);
          } catch (IOException ioe) {
            throw new DataSinkException(ioe);
          }
        }
      }
    }
    String sql = buffer.toString();
    platform.evaluateBatch(sql, false);
  }
}
 
Example 6
Source File: InterbaseBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the SQL to drop a column.
 * 
 * @param table  The table
 * @param column The column to drop
 */
public void dropColumn(Table table, Column column) throws IOException
{
    if (column.isAutoIncrement())
    {
        writeAutoIncrementDropStmts(table, column);
    }
    print("ALTER TABLE ");
    printlnIdentifier(getTableName(table));
    printIndent();
    print("DROP ");
    printIdentifier(getColumnName(column));
    printEndOfStatement();
}
 
Example 7
Source File: InterbaseBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the SQL to add/insert a column.
 * 
 * @param model      The database model
 * @param table      The table
 * @param newColumn  The new column
 * @param prevColumn The column after which the new column shall be added; <code>null</code>
 *                   if the new column is to be inserted at the beginning
 */
public void insertColumn(Database model, Table table, Column newColumn, Column prevColumn) throws IOException
{
    print("ALTER TABLE ");
    printlnIdentifier(getTableName(table));
    printIndent();
    print("ADD ");
    writeColumn(table, newColumn);
    printEndOfStatement();

    if (prevColumn != null)
    {
        // Even though Interbase can only add columns, we can move them later on
        print("ALTER TABLE ");
        printlnIdentifier(getTableName(table));
        printIndent();
        print("ALTER ");
        printIdentifier(getColumnName(newColumn));
        print(" POSITION ");
        // column positions start at 1 in Interbase
        print(String.valueOf(table.getColumnIndex(prevColumn) + 2));
        printEndOfStatement();
    }
    if (newColumn.isAutoIncrement())
    {
        writeAutoIncrementCreateStmts(model, table, newColumn);
    }
}
 
Example 8
Source File: HsqlDbBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void writeColumn(Table table, Column column) throws IOException
{
    //see comments in columnsDiffer about null/"" defaults
    printIdentifier(getColumnName(column));
    print(" ");
    print(getSqlType(column));
    if (column.isAutoIncrement())
    {
        if (!column.isPrimaryKey())
        {
            throw new ModelException("Column "+column.getName()+" in table "+table.getName()+" is auto-incrementing but not a primary key column, which is not supported by the platform");
        }
        print(" ");
        writeColumnAutoIncrementStmt(table, column);
    }
    else
    {
        writeColumnDefaultValueStmt(table, column);
    }
    if (column.isRequired())
    {
        print(" ");
        writeColumnNotNullableStmt();
    }
    else if (getPlatformInfo().isNullAsDefaultValueRequired() &&
             getPlatformInfo().hasNullDefault(column.getTypeCode()))
    {
        print(" ");
        writeColumnNullableStmt();
    }
}
 
Example 9
Source File: PostgreSqlBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the SQL to drop a column.
 * 
 * @param table  The table
 * @param column The column to drop
 */
public void dropColumn(Table table, Column column) throws IOException
{
    print("ALTER TABLE ");
    printlnIdentifier(getTableName(table));
    printIndent();
    print("DROP COLUMN ");
    printIdentifier(getColumnName(column));
    printEndOfStatement();
    if (column.isAutoIncrement())
    {
        dropAutoIncrementSequence(table, column);
    }
}
 
Example 10
Source File: PostgreSqlModelReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
    Table table = super.readTable(metaData, values);

    if (table != null)
    {
        // PostgreSQL also returns unique indexes for pk and non-pk auto-increment columns
        // which are of the form "[table]_[column]_key"
        HashMap uniquesByName = new HashMap();

        for (int indexIdx = 0; indexIdx < table.getIndexCount(); indexIdx++)
        {
            Index index = table.getIndex(indexIdx);

            if (index.isUnique() && (index.getName() != null))
            {
                uniquesByName.put(index.getName(), index);
            }
        }
        for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++)
        {
            Column column = table.getColumn(columnIdx);

            if (column.isAutoIncrement())
            {
                String indexName = table.getName() + "_" + column.getName() + "_key";

                if (uniquesByName.containsKey(indexName))
                {
                    table.removeIndex((Index)uniquesByName.get(indexName));
                    uniquesByName.remove(indexName);
                }
            }
        }
    }
    return table;
}
 
Example 11
Source File: Oracle8Builder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the SQL to drop a column.
 * 
 * @param table  The table
 * @param column The column to drop
 */
public void dropColumn(Table table, Column column) throws IOException
{
    if (column.isAutoIncrement())
    {
        dropAutoIncrementTrigger(table, column);
        dropAutoIncrementSequence(table, column);
    }
    print("ALTER TABLE ");
    printlnIdentifier(getTableName(table));
    printIndent();
    print("DROP COLUMN ");
    printIdentifier(getColumnName(column));
    printEndOfStatement();
}
 
Example 12
Source File: PostgreSqlModelReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
    Table table = super.readTable(metaData, values);

    if (table != null)
    {
        // PostgreSQL also returns unique indexes for pk and non-pk auto-increment columns
        // which are of the form "[table]_[column]_key"
        HashMap uniquesByName = new HashMap();

        for (int indexIdx = 0; indexIdx < table.getIndexCount(); indexIdx++)
        {
            Index index = table.getIndex(indexIdx);

            if (index.isUnique() && (index.getName() != null))
            {
                uniquesByName.put(index.getName(), index);
            }
        }
        for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++)
        {
            Column column = table.getColumn(columnIdx);

            if (column.isAutoIncrement())
            {
                String indexName = table.getName() + "_" + column.getName() + "_key";

                if (uniquesByName.containsKey(indexName))
                {
                    table.removeIndex((Index)uniquesByName.get(indexName));
                    uniquesByName.remove(indexName);
                }
            }
        }
    }
    return table;
}
 
Example 13
Source File: PostgreSqlBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void createTable(Database database, Table table, Map parameters) throws IOException
{
    for (int idx = 0; idx < table.getColumnCount(); idx++)
    {
        Column column = table.getColumn(idx);

        if (column.isAutoIncrement())
        {
            createAutoIncrementSequence(table, column);
        }
    }
    super.createTable(database, table, parameters);
}
 
Example 14
Source File: InterbaseBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the SQL to add/insert a column.
 * 
 * @param model      The database model
 * @param table      The table
 * @param newColumn  The new column
 * @param prevColumn The column after which the new column shall be added; <code>null</code>
 *                   if the new column is to be inserted at the beginning
 */
public void insertColumn(Database model, Table table, Column newColumn, Column prevColumn) throws IOException
{
    print("ALTER TABLE ");
    printlnIdentifier(getTableName(table));
    printIndent();
    print("ADD ");
    writeColumn(table, newColumn);
    printEndOfStatement();

    if (prevColumn != null)
    {
        // Even though Interbase can only add columns, we can move them later on
        print("ALTER TABLE ");
        printlnIdentifier(getTableName(table));
        printIndent();
        print("ALTER ");
        printIdentifier(getColumnName(newColumn));
        print(" POSITION ");
        // column positions start at 1 in Interbase
        print(String.valueOf(table.getColumnIndex(prevColumn) + 2));
        printEndOfStatement();
    }
    if (newColumn.isAutoIncrement())
    {
        writeAutoIncrementCreateStmts(model, table, newColumn);
    }
}
 
Example 15
Source File: HsqlDbBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void writeColumn(Table table, Column column) throws IOException
{
    //see comments in columnsDiffer about null/"" defaults
    printIdentifier(getColumnName(column));
    print(" ");
    print(getSqlType(column));
    if (column.isAutoIncrement())
    {
        if (!column.isPrimaryKey())
        {
            throw new ModelException("Column "+column.getName()+" in table "+table.getName()+" is auto-incrementing but not a primary key column, which is not supported by the platform");
        }
        print(" ");
        writeColumnAutoIncrementStmt(table, column);
    }
    else
    {
        writeColumnDefaultValueStmt(table, column);
    }
    if (column.isRequired())
    {
        print(" ");
        writeColumnNotNullableStmt();
    }
    else if (getPlatformInfo().isNullAsDefaultValueRequired() &&
             getPlatformInfo().hasNullDefault(column.getTypeCode()))
    {
        print(" ");
        writeColumnNullableStmt();
    }
}
 
Example 16
Source File: MckoiBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void writeColumnDefaultValue(Table table, Column column) throws IOException
{
    if (column.isAutoIncrement())
    {
        // we start at value 1 to avoid issues with jdbc
        print("NEXTVAL('");
        print(getConstraintName("seq", table, column.getName(), null));
        print("')");
    }
    else
    {
        super.writeColumnDefaultValue(table, column);
    }
}
 
Example 17
Source File: FirebirdBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void addColumn(Database model, Table table, Column newColumn) throws IOException
{
    print("ALTER TABLE ");
    printlnIdentifier(getTableName(table));
    printIndent();
    print("ADD ");
    writeColumn(table, newColumn);
    printEndOfStatement();
    if (newColumn.isAutoIncrement())
    {
        writeAutoIncrementCreateStmts(model, table, newColumn);
    }
}
 
Example 18
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
     * Outputs the DDL for the specified column.
     * 
     * @param table  The table containing the column
     * @param column The column
     */
    protected void writeColumn(Table table, Column column) throws IOException
    {
        //see comments in columnsDiffer about null/"" defaults
        printIdentifier(getColumnName(column));
        print(" ");
        print(getSqlType(column));
        writeColumnDefaultValueStmt(table, column);
        if (column.isRequired())
        {
            print(" ");
            writeColumnNotNullableStmt();
        }
        else if (getPlatformInfo().isNullAsDefaultValueRequired() &&
                 getPlatformInfo().hasNullDefault(column.getTypeCode()))
        {
            print(" ");
            writeColumnNullableStmt();
        }
// GemStone changes BEGIN
        if (column.isAutoIncrement() &&
            !getPlatformInfo().isDefaultValueUsedForIdentitySpec() &&
            !(getPlatform().isAddIdentityUsingAlterTableOn() &&
                getPlatformInfo().isAddingIdentityUsingAlterTableSupported()))
        /* (original code)
        if (column.isAutoIncrement() && !getPlatformInfo().isDefaultValueUsedForIdentitySpec())
        */
// GemStone changes END
        {
            if (!getPlatformInfo().isNonPrimaryKeyIdentityColumnsSupported() && !column.isPrimaryKey())
            {
                throw new ModelException("Column "+column.getName()+" in table "+table.getQualifiedName()+" is auto-incrementing but not a primary key column, which is not supported by the platform");
            }
            print(" ");
            writeColumnAutoIncrementStmt(table, column);
        }
    }
 
Example 19
Source File: PostgreSqlBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void addColumn(Database model, Table table, Column newColumn) throws IOException
{
    if (newColumn.isAutoIncrement())
    {
        createAutoIncrementSequence(table, newColumn);
    }
    super.addColumn(model, table, newColumn);
}
 
Example 20
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a copy of the given model adjusted for type changes because of the native type mappings
 * which when read back from the database will map to different types.
 * 
 * @param sourceModel The source model
 * @return The adjusted model
 */
protected Database adjustModel(Database sourceModel)
{
    Database model = new CloneHelper().clone(sourceModel);

    for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++)
    {
        Table table = model.getTable(tableIdx);

        for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++)
        {
            Column column     = table.getColumn(columnIdx);
            int    origType   = column.getTypeCode();
            int    targetType = getPlatformInfo().getTargetJdbcType(origType);

            // we adjust the column types if the native type would back-map to a
            // different jdbc type
            if (targetType != origType)
            {
                column.setTypeCode(targetType);
                // we should also adapt the default value
                if (column.getDefaultValue() != null)
                {
                    DefaultValueHelper helper = getPlatform().getSqlBuilder().getDefaultValueHelper();

                    column.setDefaultValue(helper.convert(column.getDefaultValue(), origType, targetType));
                }
            }
            // we also promote the default size if the column has no size
            // spec of its own
            if ((column.getSize() == null) && getPlatformInfo().hasSize(targetType))
            {
                Integer defaultSize = getPlatformInfo().getDefaultSize(targetType);

                if (defaultSize != null)
                {
                    column.setSize(defaultSize.toString());
                }
            }
            // finally the platform might return a synthetic default value if the column
            // is a primary key column
            if (getPlatformInfo().isSyntheticDefaultValueForRequiredReturned() &&
                (column.getDefaultValue() == null) && column.isRequired() && !column.isAutoIncrement())
            {
                switch (column.getTypeCode())
                {
                    case Types.TINYINT:
                    case Types.SMALLINT:
                    case Types.INTEGER:
                    case Types.BIGINT:
                        column.setDefaultValue("0");
                        break;
                    case Types.REAL:
                    case Types.FLOAT:
                    case Types.DOUBLE:
                        column.setDefaultValue("0.0");
                        break;
                    case Types.BIT:
                        column.setDefaultValue("false");
                        break;
                    default:
                        column.setDefaultValue("");
                        break;
                }
            }
            if (column.isPrimaryKey() && getPlatformInfo().isPrimaryKeyColumnAutomaticallyRequired())
            {
                column.setRequired(true);
            }
            if (column.isAutoIncrement() && getPlatformInfo().isIdentityColumnAutomaticallyRequired())
            {
                column.setRequired(true);
            }
        }
        // we also add the default names to foreign keys that are initially unnamed
        for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++)
        {
            ForeignKey fk = table.getForeignKey(fkIdx);

            if (fk.getName() == null)
            {
                fk.setName(getPlatform().getSqlBuilder().getForeignKeyName(table, fk));
            }
        }
    }
    return model;
}