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

The following examples show how to use org.apache.ddlutils.model.Column#setAutoIncrement() . 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: SapDbModelReader.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
    * {@inheritDoc}
    */
   protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
   {
	Column column = super.readColumn(metaData, values);

	if (column.getDefaultValue() != null)
	{
		// SapDb pads the default value with spaces
		column.setDefaultValue(column.getDefaultValue().trim());
		// SapDb uses the default value for the auto-increment specification
		if (column.getDefaultValue().startsWith("DEFAULT SERIAL"))
		{
			column.setAutoIncrement(true);
			column.setDefaultValue(null);
		}
	}
	if (column.getTypeCode() == Types.DECIMAL)
	{
		// We also perform back-mapping to BIGINT
		if ((column.getSizeAsInt() == 38) && (column.getScale() == 0))
		{
			column.setTypeCode(Types.BIGINT);
		}
	}
	return column;
}
 
Example 2
Source File: DerbyModelReader.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
    Column column       = super.readColumn(metaData, values);
    String defaultValue = column.getDefaultValue();

    if (defaultValue != null)
    {
        // we check for these strings
        //   GENERATED_BY_DEFAULT               -> 'GENERATED BY DEFAULT AS IDENTITY'
        //   AUTOINCREMENT: start 1 increment 1 -> 'GENERATED ALWAYS AS IDENTITY'
        if ("GENERATED_BY_DEFAULT".equals(defaultValue) || defaultValue.startsWith("AUTOINCREMENT:"))
        {
            column.setDefaultValue(null);
            column.setAutoIncrement(true);
        }
        else if (TypeMap.isTextType(column.getTypeCode()))
        {
            column.setDefaultValue(unescape(defaultValue, "'", "''"));
        }
    }
    return column;
}
 
Example 3
Source File: ModelComparator.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Compares the two columns and returns the change necessary to create the second
 * column from the first one if they differe.
 *  
 * @param sourceTable  The source table which contains the source column
 * @param sourceColumn The source column
 * @param targetTable  The target table which contains the target column
 * @param targetColumn The target column
 * @return The change or <code>null</code> if the columns are the same
 */
protected ColumnDefinitionChange compareColumns(Table  sourceTable,
                                                Column sourceColumn,
                                                Table  targetTable,
                                                Column targetColumn)
{
    if (ColumnDefinitionChange.isChanged(getPlatformInfo(), sourceColumn, targetColumn))
    {
        Column  newColumnDef   = _cloneHelper.clone(sourceColumn, true);
        int     targetTypeCode = _platformInfo.getTargetJdbcType(targetColumn.getTypeCode());
        boolean sizeMatters    = _platformInfo.hasSize(targetTypeCode);
        boolean scaleMatters   = _platformInfo.hasPrecisionAndScale(targetTypeCode);

        newColumnDef.setTypeCode(targetColumn.getTypeCode());
        newColumnDef.setSize(sizeMatters || scaleMatters ? targetColumn.getSize() : null);
        newColumnDef.setAutoIncrement(targetColumn.isAutoIncrement());
        newColumnDef.setRequired(targetColumn.isRequired());
        newColumnDef.setDescription(targetColumn.getDescription());
        newColumnDef.setDefaultValue(targetColumn.getDefaultValue());
        return new ColumnDefinitionChange(sourceTable.getQualifiedName(), sourceColumn.getName(), newColumnDef);
    }
    else
    {
        return null;
    }
}
 
Example 4
Source File: DerbyModelReader.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
    Column column       = super.readColumn(metaData, values);
    String defaultValue = column.getDefaultValue();

    if (defaultValue != null)
    {
        // we check for these strings
        //   GENERATED_BY_DEFAULT               -> 'GENERATED BY DEFAULT AS IDENTITY'
        //   AUTOINCREMENT: start 1 increment 1 -> 'GENERATED ALWAYS AS IDENTITY'
        if ("GENERATED_BY_DEFAULT".equals(defaultValue) || defaultValue.startsWith("AUTOINCREMENT:"))
        {
            column.setDefaultValue(null);
            column.setAutoIncrement(true);
        }
        else if (TypeMap.isTextType(column.getTypeCode()))
        {
            column.setDefaultValue(unescape(defaultValue, "'", "''"));
        }
    }
    return column;
}
 
Example 5
Source File: ModelComparator.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Compares the two columns and returns the change necessary to create the second
 * column from the first one if they differe.
 *  
 * @param sourceTable  The source table which contains the source column
 * @param sourceColumn The source column
 * @param targetTable  The target table which contains the target column
 * @param targetColumn The target column
 * @return The change or <code>null</code> if the columns are the same
 */
protected ColumnDefinitionChange compareColumns(Table  sourceTable,
                                                Column sourceColumn,
                                                Table  targetTable,
                                                Column targetColumn)
{
    if (ColumnDefinitionChange.isChanged(getPlatformInfo(), sourceColumn, targetColumn))
    {
        Column  newColumnDef   = _cloneHelper.clone(sourceColumn, true);
        int     targetTypeCode = _platformInfo.getTargetJdbcType(targetColumn.getTypeCode());
        boolean sizeMatters    = _platformInfo.hasSize(targetTypeCode);
        boolean scaleMatters   = _platformInfo.hasPrecisionAndScale(targetTypeCode);

        newColumnDef.setTypeCode(targetColumn.getTypeCode());
        newColumnDef.setSize(sizeMatters || scaleMatters ? targetColumn.getSize() : null);
        newColumnDef.setAutoIncrement(targetColumn.isAutoIncrement());
        newColumnDef.setRequired(targetColumn.isRequired());
        newColumnDef.setDescription(targetColumn.getDescription());
        newColumnDef.setDefaultValue(targetColumn.getDefaultValue());
        return new ColumnDefinitionChange(sourceTable.getQualifiedName(), sourceColumn.getName(), newColumnDef);
    }
    else
    {
        return null;
    }
}
 
Example 6
Source File: ColumnDefinitionChange.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void apply(Database model, boolean caseSensitive)
{
    Column column = findChangedColumn(model, caseSensitive);

    column.setTypeCode(_newColumnDef.getTypeCode());
    column.setSize(_newColumnDef.getSize());
    column.setAutoIncrement(_newColumnDef.isAutoIncrement());
    column.setRequired(_newColumnDef.isRequired());
    column.setDescription(_newColumnDef.getDescription());
    column.setDefaultValue(_newColumnDef.getDefaultValue());
}
 
Example 7
Source File: MckoiModelReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
    Column column = super.readColumn(metaData, values);

    if (column.getSize() != null)
    {
        if (column.getSizeAsInt() <= 0)
        {
            column.setSize(null);
        }
    }

    String defaultValue = column.getDefaultValue();

    if (defaultValue != null)
    {
        if (defaultValue.toLowerCase().startsWith("nextval('") ||
            defaultValue.toLowerCase().startsWith("uniquekey('"))
        {
            column.setDefaultValue(null);
            column.setAutoIncrement(true);
        }
        else if (TypeMap.isTextType(column.getTypeCode()))
        {
            column.setDefaultValue(unescape(column.getDefaultValue(), "'", "\\'"));
        }
    }
    return column;
}
 
Example 8
Source File: Db2ModelReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method that determines the auto increment status using Firebird's system tables.
 *
 * @param table The table
 */
protected void determineAutoIncrementColumns(Table table) throws SQLException
{
    final String query = "SELECT COLNAME FROM SYSCAT.COLUMNS WHERE TABNAME = ? AND IDENTITY = 'Y' AND HIDDEN != 'S'";

    PreparedStatement stmt = null;

    try
    {
        stmt = getConnection().prepareStatement(query);
        stmt.setString(1, table.getName());

        ResultSet rs = stmt.executeQuery();

        while (rs.next())
        {
            String colName = rs.getString(1).trim();
            Column column  = table.findColumn(colName, getPlatform().isDelimitedIdentifierModeOn());

            if (column != null)
            {
                column.setAutoIncrement(true);
            }
        }
    }
    finally
    {
        closeStatement(stmt);
    }
}
 
Example 9
Source File: MaxDbModelReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
    Column column = super.readColumn(metaData, values);

    if (column.getDefaultValue() != null)
    {
        // SapDb pads the default value with spaces
        column.setDefaultValue(column.getDefaultValue().trim());
        // SapDb uses the default value for the auto-increment specification
        if (column.getDefaultValue().startsWith("DEFAULT SERIAL"))
        {
            column.setAutoIncrement(true);
            column.setDefaultValue(null);
        }
    }
    if (column.getTypeCode() == Types.DECIMAL)
    {
        // need to use COLUMN_SIZE for precision instead of NUM_PREC_RADIX
        column.setPrecisionRadix(column.getSizeAsInt());

        // We also perform back-mapping to BIGINT
        if ((column.getSizeAsInt() == 38) && (column.getScale() == 0))
        {
            column.setTypeCode(Types.BIGINT);
        }
    }
    return column;
}
 
Example 10
Source File: ColumnDefinitionChange.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void apply(Database model, boolean caseSensitive)
{
    Column column = findChangedColumn(model, caseSensitive);

    column.setTypeCode(_newColumnDef.getTypeCode());
    column.setSize(_newColumnDef.getSize());
    column.setAutoIncrement(_newColumnDef.isAutoIncrement());
    column.setRequired(_newColumnDef.isRequired());
    column.setDescription(_newColumnDef.getDescription());
    column.setDefaultValue(_newColumnDef.getDefaultValue());
}
 
Example 11
Source File: MckoiModelReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
    Column column = super.readColumn(metaData, values);

    if (column.getSize() != null)
    {
        if (column.getSizeAsInt() <= 0)
        {
            column.setSize(null);
        }
    }

    String defaultValue = column.getDefaultValue();

    if (defaultValue != null)
    {
        if (defaultValue.toLowerCase().startsWith("nextval('") ||
            defaultValue.toLowerCase().startsWith("uniquekey('"))
        {
            column.setDefaultValue(null);
            column.setAutoIncrement(true);
        }
        else if (TypeMap.isTextType(column.getTypeCode()))
        {
            column.setDefaultValue(unescape(column.getDefaultValue(), "'", "\\'"));
        }
    }
    return column;
}
 
Example 12
Source File: Db2ModelReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method that determines the auto increment status using Firebird's system tables.
 *
 * @param table The table
 */
protected void determineAutoIncrementColumns(Table table) throws SQLException
{
    final String query = "SELECT COLNAME FROM SYSCAT.COLUMNS WHERE TABNAME = ? AND IDENTITY = 'Y' AND HIDDEN != 'S'";

    PreparedStatement stmt = null;

    try
    {
        stmt = getConnection().prepareStatement(query);
        stmt.setString(1, table.getName());

        ResultSet rs = stmt.executeQuery();

        while (rs.next())
        {
            String colName = rs.getString(1).trim();
            Column column  = table.findColumn(colName, getPlatform().isDelimitedIdentifierModeOn());

            if (column != null)
            {
                column.setAutoIncrement(true);
            }
        }
    }
    finally
    {
        closeStatement(stmt);
    }
}
 
Example 13
Source File: MaxDbModelReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
    Column column = super.readColumn(metaData, values);

    if (column.getDefaultValue() != null)
    {
        // SapDb pads the default value with spaces
        column.setDefaultValue(column.getDefaultValue().trim());
        // SapDb uses the default value for the auto-increment specification
        if (column.getDefaultValue().startsWith("DEFAULT SERIAL"))
        {
            column.setAutoIncrement(true);
            column.setDefaultValue(null);
        }
    }
    if (column.getTypeCode() == Types.DECIMAL)
    {
        // need to use COLUMN_SIZE for precision instead of NUM_PREC_RADIX
        column.setPrecisionRadix(column.getSizeAsInt());

        // We also perform back-mapping to BIGINT
        if ((column.getSizeAsInt() == 38) && (column.getScale() == 0))
        {
            column.setTypeCode(Types.BIGINT);
        }
    }
    return column;
}
 
Example 14
Source File: DatabaseIO.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Reads a column element from the XML stream reader.
 * 
 * @param xmlReader The reader
 * @return The column object
 */
private Column readColumnElement(XMLStreamReader xmlReader) throws XMLStreamException, IOException
{
    Column column = new Column();

    for (int idx = 0; idx < xmlReader.getAttributeCount(); idx++)
    {
        QName attrQName = xmlReader.getAttributeName(idx);

        if (isSameAs(attrQName, QNAME_ATTRIBUTE_NAME))
        {
            column.setName(xmlReader.getAttributeValue(idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_PRIMARY_KEY))
        {
            column.setPrimaryKey(getAttributeValueAsBoolean(xmlReader, idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_REQUIRED))
        {
            column.setRequired(getAttributeValueAsBoolean(xmlReader, idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_TYPE))
        {
            column.setType(xmlReader.getAttributeValue(idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_SIZE))
        {
            column.setSize(getAttributeValueBeingNullAware(xmlReader, idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_DEFAULT))
        {
            column.setDefaultValue(xmlReader.getAttributeValue(idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_AUTO_INCREMENT))
        {
            column.setAutoIncrement(getAttributeValueAsBoolean(xmlReader, idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_DESCRIPTION))
        {
            column.setDescription(xmlReader.getAttributeValue(idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_JAVA_NAME))
        {
            column.setJavaName(xmlReader.getAttributeValue(idx));
        }
    }
    consumeRestOfElement(xmlReader);
    return column;
}
 
Example 15
Source File: InterbaseModelReader.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method that determines the auto increment status using Interbase's system tables.
 *
 * @param table The table
 */
protected void determineAutoIncrementColumns(Table table) throws SQLException
{
    // Since for long table and column names, the generator name will be shortened
    // we have to determine for each column whether there is a generator for it
    final String query = "SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS";

    InterbaseBuilder builder = (InterbaseBuilder)getPlatform().getSqlBuilder();
    Column[]         columns = table.getColumns();
    HashMap          names   = new HashMap();
    String           name;

    for (int idx = 0; idx < columns.length; idx++)
    {
        name = builder.getGeneratorName(table, columns[idx]);
        if (!getPlatform().isDelimitedIdentifierModeOn())
        {
            name = name.toUpperCase();
        }
        names.put(name, columns[idx]);
    }

    Statement stmt = null;

    try
    {
        stmt = getConnection().createStatement();

        ResultSet rs = stmt.executeQuery(query);

        while (rs.next())
        {
            String generatorName = rs.getString(1).trim();
            Column column        = (Column)names.get(generatorName);

            if (column != null)
            {
                column.setAutoIncrement(true);
            }
        }
    }
    finally
    {
        closeStatement(stmt);
    }
}
 
Example 16
Source File: PostgreSqlModelReader.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
    Column column = super.readColumn(metaData, values);

    if (column.getSize() != null)
    {
        if (column.getSizeAsInt() <= 0)
        {
            column.setSize(null);
            // PostgreSQL reports BYTEA and TEXT as BINARY(-1) and VARCHAR(-1) respectively
            // Since we cannot currently use the Blob/Clob interface with BYTEA, we instead
            // map them to LONGVARBINARY/LONGVARCHAR
            if (column.getTypeCode() == Types.BINARY)
            {
                column.setTypeCode(Types.LONGVARBINARY);
            }
            else if (column.getTypeCode() == Types.VARCHAR)
            {
                column.setTypeCode(Types.LONGVARCHAR);
            }
        }
        // fix issue DDLUTILS-165 as postgresql-8.2-504-jdbc3.jar seems to return Integer.MAX_VALUE
        // on columns defined as TEXT.
        else if (column.getSizeAsInt() == Integer.MAX_VALUE)
        {
            column.setSize(null);
            if (column.getTypeCode() == Types.VARCHAR)
            {
                column.setTypeCode(Types.LONGVARCHAR);
            }
            else if (column.getTypeCode() == Types.BINARY)
            {
                column.setTypeCode(Types.LONGVARBINARY);
            }
        }
    }

    String defaultValue = column.getDefaultValue();

    if ((defaultValue != null) && (defaultValue.length() > 0))
    {
        // If the default value looks like "nextval('ROUNDTRIP_VALUE_seq'::text)"
        // then it is an auto-increment column
        if (defaultValue.startsWith("nextval("))
        {
            column.setAutoIncrement(true);
            defaultValue = null;
        }
        else
        {
            // PostgreSQL returns default values in the forms "-9000000000000000000::bigint" or
            // "'some value'::character varying" or "'2000-01-01'::date"
            switch (column.getTypeCode())
            {
                case Types.INTEGER:
                case Types.BIGINT:
                case Types.DECIMAL:
                case Types.NUMERIC:
                    defaultValue = extractUndelimitedDefaultValue(defaultValue);
                    break;
                case Types.CHAR:
                case Types.VARCHAR:
                case Types.LONGVARCHAR:
                case Types.DATE:
                case Types.TIME:
                case Types.TIMESTAMP:
                    defaultValue = extractDelimitedDefaultValue(defaultValue);
                    break;
            }
            if (TypeMap.isTextType(column.getTypeCode()))
            {
                // We assume escaping via double quote (see also the backslash_quote setting:
                // http://www.postgresql.org/docs/7.4/interactive/runtime-config.html#RUNTIME-CONFIG-COMPATIBLE)
                defaultValue = unescape(defaultValue, "'", "''");
            }
        }
        column.setDefaultValue(defaultValue);
    }
    return column;
}
 
Example 17
Source File: FirebirdModelReader.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method that determines the auto increment status using Firebird's system tables.
 *
 * @param table The table
 */
protected void determineAutoIncrementColumns(Table table) throws SQLException
{
	// Since for long table and column names, the generator name will be shortened
	// we have to determine for each column whether there is a generator for it
    final String query = "SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS WHERE RDB$GENERATOR_NAME NOT LIKE '%$%'";

    FirebirdBuilder builder = (FirebirdBuilder)getPlatform().getSqlBuilder();
	Column[]        columns = table.getColumns();
	HashMap         names   = new HashMap();
    String          name;

	for (int idx = 0; idx < columns.length; idx++)
	{
	    name = builder.getGeneratorName(table, columns[idx]);
        if (!getPlatform().isDelimitedIdentifierModeOn())
        {
            name = name.toUpperCase();
        }
		names.put(name, columns[idx]);
	}

	Statement stmt = null;

	try
	{
	    stmt = getConnection().createStatement();

	    ResultSet rs = stmt.executeQuery(query);

        while (rs.next())
        {
            String generatorName = rs.getString(1).trim();
            Column column        = (Column)names.get(generatorName);

            if (column != null)
            {
                column.setAutoIncrement(true);
            }
        }
	}
    finally
    {
        closeStatement(stmt);
    }
}
 
Example 18
Source File: FirebirdModelReader.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method that determines the auto increment status using Firebird's system tables.
 *
 * @param table The table
 */
protected void determineAutoIncrementColumns(Table table) throws SQLException
{
	// Since for long table and column names, the generator name will be shortened
	// we have to determine for each column whether there is a generator for it
    final String query = "SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS WHERE RDB$GENERATOR_NAME NOT LIKE '%$%'";

    FirebirdBuilder builder = (FirebirdBuilder)getPlatform().getSqlBuilder();
	Column[]        columns = table.getColumns();
	HashMap         names   = new HashMap();
    String          name;

	for (int idx = 0; idx < columns.length; idx++)
	{
	    name = builder.getGeneratorName(table, columns[idx]);
        if (!getPlatform().isDelimitedIdentifierModeOn())
        {
            name = name.toUpperCase();
        }
		names.put(name, columns[idx]);
	}

	Statement stmt = null;

	try
	{
	    stmt = getConnection().createStatement();

	    ResultSet rs = stmt.executeQuery(query);

        while (rs.next())
        {
            String generatorName = rs.getString(1).trim();
            Column column        = (Column)names.get(generatorName);

            if (column != null)
            {
                column.setAutoIncrement(true);
            }
        }
	}
    finally
    {
        closeStatement(stmt);
    }
}
 
Example 19
Source File: PostgreSqlModelReader.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
    Column column = super.readColumn(metaData, values);

    if (column.getSize() != null)
    {
        if (column.getSizeAsInt() <= 0)
        {
            column.setSize(null);
            // PostgreSQL reports BYTEA and TEXT as BINARY(-1) and VARCHAR(-1) respectively
            // Since we cannot currently use the Blob/Clob interface with BYTEA, we instead
            // map them to LONGVARBINARY/LONGVARCHAR
            if (column.getTypeCode() == Types.BINARY)
            {
                column.setTypeCode(Types.LONGVARBINARY);
            }
            else if (column.getTypeCode() == Types.VARCHAR)
            {
                column.setTypeCode(Types.LONGVARCHAR);
            }
        }
        // fix issue DDLUTILS-165 as postgresql-8.2-504-jdbc3.jar seems to return Integer.MAX_VALUE
        // on columns defined as TEXT.
        else if (column.getSizeAsInt() == Integer.MAX_VALUE)
        {
            column.setSize(null);
            if (column.getTypeCode() == Types.VARCHAR)
            {
                column.setTypeCode(Types.LONGVARCHAR);
            }
            else if (column.getTypeCode() == Types.BINARY)
            {
                column.setTypeCode(Types.LONGVARBINARY);
            }
        }
    }

    String defaultValue = column.getDefaultValue();

    if ((defaultValue != null) && (defaultValue.length() > 0))
    {
        // If the default value looks like "nextval('ROUNDTRIP_VALUE_seq'::text)"
        // then it is an auto-increment column
        if (defaultValue.startsWith("nextval("))
        {
            column.setAutoIncrement(true);
            defaultValue = null;
        }
        else
        {
            // PostgreSQL returns default values in the forms "-9000000000000000000::bigint" or
            // "'some value'::character varying" or "'2000-01-01'::date"
            switch (column.getTypeCode())
            {
                case Types.INTEGER:
                case Types.BIGINT:
                case Types.DECIMAL:
                case Types.NUMERIC:
                    defaultValue = extractUndelimitedDefaultValue(defaultValue);
                    break;
                case Types.CHAR:
                case Types.VARCHAR:
                case Types.LONGVARCHAR:
                case Types.DATE:
                case Types.TIME:
                case Types.TIMESTAMP:
                    defaultValue = extractDelimitedDefaultValue(defaultValue);
                    break;
            }
            if (TypeMap.isTextType(column.getTypeCode()))
            {
                // We assume escaping via double quote (see also the backslash_quote setting:
                // http://www.postgresql.org/docs/7.4/interactive/runtime-config.html#RUNTIME-CONFIG-COMPATIBLE)
                defaultValue = unescape(defaultValue, "'", "''");
            }
        }
        column.setDefaultValue(defaultValue);
    }
    return column;
}
 
Example 20
Source File: DatabaseIO.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Reads a column element from the XML stream reader.
 * 
 * @param xmlReader The reader
 * @return The column object
 */
private Column readColumnElement(XMLStreamReader xmlReader) throws XMLStreamException, IOException
{
    Column column = new Column();

    for (int idx = 0; idx < xmlReader.getAttributeCount(); idx++)
    {
        QName attrQName = xmlReader.getAttributeName(idx);

        if (isSameAs(attrQName, QNAME_ATTRIBUTE_NAME))
        {
            column.setName(xmlReader.getAttributeValue(idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_PRIMARY_KEY))
        {
            column.setPrimaryKey(getAttributeValueAsBoolean(xmlReader, idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_REQUIRED))
        {
            column.setRequired(getAttributeValueAsBoolean(xmlReader, idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_TYPE))
        {
            column.setType(xmlReader.getAttributeValue(idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_SIZE))
        {
            column.setSize(getAttributeValueBeingNullAware(xmlReader, idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_DEFAULT))
        {
            column.setDefaultValue(xmlReader.getAttributeValue(idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_AUTO_INCREMENT))
        {
            column.setAutoIncrement(getAttributeValueAsBoolean(xmlReader, idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_DESCRIPTION))
        {
            column.setDescription(xmlReader.getAttributeValue(idx));
        }
        else if (isSameAs(attrQName, QNAME_ATTRIBUTE_JAVA_NAME))
        {
            column.setJavaName(xmlReader.getAttributeValue(idx));
        }
    }
    consumeRestOfElement(xmlReader);
    return column;
}