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

The following examples show how to use org.apache.ddlutils.model.Column#getTypeCode() . 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: 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 2
Source File: MSSqlModelComparator.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Returns all columns that are changed in a way that makes it necessary to recreate foreign keys and
 * indexes using them.
 *  
 * @param sourceTable The source table
 * @param targetTable The target table
 * @return The columns (from the target table)
 */
private List getRelevantChangedColumns(Table sourceTable, Table targetTable)
{
    List result = new ArrayList();

    for (int columnIdx = 0; columnIdx < targetTable.getColumnCount(); columnIdx++)
    {
        Column targetColumn = targetTable.getColumn(columnIdx);
        Column sourceColumn = sourceTable.findColumn(targetColumn.getName(), isCaseSensitive());

        if (sourceColumn != null)
        {
            int targetTypeCode = getPlatformInfo().getTargetJdbcType(targetColumn.getTypeCode());

            if ((targetTypeCode != sourceColumn.getTypeCode()) ||
                ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn))
            {
                result.add(targetColumn);
            }
        }
    }
    return result;
}
 
Example 3
Source File: PlatformImplBase.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method for retrieving the value for a column from the given result set
 * using the type code of the column.
 * 
 * @param resultSet The result set
 * @param column    The column
 * @param idx       The value's index in the result set (starting from 1) 
 * @return The value
 */
protected Object getObjectFromResultSet(ResultSet resultSet, Column column, int idx) throws SQLException
{
    int    originalJdbcType = column.getTypeCode();
    int    targetJdbcType   = getPlatformInfo().getTargetJdbcType(originalJdbcType);
    int    jdbcType         = originalJdbcType;
    Object value            = null;

    // in general we're trying to retrieve the value using the original type
    // but sometimes we also need the target type:
    if ((originalJdbcType == Types.BLOB) && (targetJdbcType != Types.BLOB))
    {
        // we should not use the Blob interface if the database doesn't map to this type 
        jdbcType = targetJdbcType;
    }
    if ((originalJdbcType == Types.CLOB) && (targetJdbcType != Types.CLOB))
    {
        // we should not use the Clob interface if the database doesn't map to this type 
        jdbcType = targetJdbcType;
    }
    value = extractColumnValue(resultSet, null, idx, jdbcType);
    return resultSet.wasNull() ? null : value;
}
 
Example 4
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 5
Source File: FirebirdModelReader.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.getTypeCode() == Types.FLOAT)
	{
		column.setTypeCode(Types.REAL);
	}
       else if (TypeMap.isTextType(column.getTypeCode()))
       {
           column.setDefaultValue(unescape(column.getDefaultValue(), "'", "''"));
       }
	return column;
}
 
Example 6
Source File: Oracle9Builder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected String getNativeType(Column column) {
  // convert the type to DATE or TIMESTAMP depending on precision
  // the JDBC type was converted to TIMESTAMP for both by Oracle8ModelReader
  // getColumn method, but the precision will still help us determine the
  // actual type to use (see comments in #45096)
  final int typeCode = column.getTypeCode();
  if (typeCode == Types.TIMESTAMP) {
    if (column.getSizeAsInt() <= 7) {
      return "DATE";
    }
  }
  return super.getNativeType(column);
}
 
Example 7
Source File: FirebirdBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected String getNativeDefaultValue(Column column)
{
    if ((column.getTypeCode() == Types.BIT) || (column.getTypeCode() == Types.BOOLEAN))
    {
        return getDefaultValueHelper().convert(column.getDefaultValue(), column.getTypeCode(), Types.SMALLINT);
    }
    else
    {
        return super.getNativeDefaultValue(column);
    }
}
 
Example 8
Source File: MSSqlBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected String getNativeDefaultValue(Column column)
{
	// Sql Server wants BIT default values as 0 or 1
    if ((column.getTypeCode() == Types.BIT) || (column.getTypeCode() == Types.BOOLEAN))
    {
        return getDefaultValueHelper().convert(column.getDefaultValue(), column.getTypeCode(), Types.SMALLINT);
    }
    else
    {
        return super.getNativeDefaultValue(column);
    }
}
 
Example 9
Source File: Oracle9Builder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected String getNativeType(Column column) {
  // convert the type to DATE or TIMESTAMP depending on precision
  // the JDBC type was converted to TIMESTAMP for both by Oracle8ModelReader
  // getColumn method, but the precision will still help us determine the
  // actual type to use (see comments in #45096)
  final int typeCode = column.getTypeCode();
  if (typeCode == Types.TIMESTAMP) {
    if (column.getSizeAsInt() <= 7) {
      return "DATE";
    }
  }
  return super.getNativeType(column);
}
 
Example 10
Source File: MSSqlBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected String getNativeDefaultValue(Column column)
{
	// Sql Server wants BIT default values as 0 or 1
    if ((column.getTypeCode() == Types.BIT) || (column.getTypeCode() == Types.BOOLEAN))
    {
        return getDefaultValueHelper().convert(column.getDefaultValue(), column.getTypeCode(), Types.SMALLINT);
    }
    else
    {
        return super.getNativeDefaultValue(column);
    }
}
 
Example 11
Source File: FirebirdBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected String getNativeDefaultValue(Column column)
{
    if ((column.getTypeCode() == Types.BIT) || (column.getTypeCode() == Types.BOOLEAN))
    {
        return getDefaultValueHelper().convert(column.getDefaultValue(), column.getTypeCode(), Types.SMALLINT);
    }
    else
    {
        return super.getNativeDefaultValue(column);
    }
}
 
Example 12
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Asserts that the two given columns are equal.
 * 
 * @param expected      The expected column
 * @param actual        The actual column
 * @param caseSensitive Whether case matters when comparing
 */
protected void assertEquals(Column expected, Column actual, boolean caseSensitive)
{
    if (caseSensitive)
    {
        assertEquals("Column names do not match.",
                     getPlatform().getSqlBuilder().shortenName(expected.getName(), getSqlBuilder().getMaxColumnNameLength()),
                     getPlatform().getSqlBuilder().shortenName(actual.getName(), getSqlBuilder().getMaxColumnNameLength()));
    }
    else
    {
        assertEquals("Column names do not match (ignoring case).",
                     getPlatform().getSqlBuilder().shortenName(expected.getName().toUpperCase(), getSqlBuilder().getMaxColumnNameLength()),
                     getPlatform().getSqlBuilder().shortenName(actual.getName().toUpperCase(), getSqlBuilder().getMaxColumnNameLength()));
    }
    assertEquals("Primary key status not the same for column "+actual.getName()+".",
                 expected.isPrimaryKey(),
                 actual.isPrimaryKey());
    assertEquals("Required status not the same for column "+actual.getName()+".",
                 expected.isRequired(),
                 actual.isRequired());
    if (getPlatformInfo().getIdentityStatusReadingSupported())
    {
        // we're only comparing this if the platform can actually read the
        // auto-increment status back from an existing database
        assertEquals("Auto-increment status not the same for column "+actual.getName()+".",
                     expected.isAutoIncrement(),
                     actual.isAutoIncrement());
    }
    assertEquals("Type not the same for column "+actual.getName()+".",
                 expected.getType(),
                 actual.getType());
    assertEquals("Type code not the same for column "+actual.getName()+".",
                 expected.getTypeCode(),
                 actual.getTypeCode());
    assertEquals("Parsed default values do not match for column "+actual.getName()+".",
                 expected.getParsedDefaultValue(),
                 actual.getParsedDefaultValue());

    // comparing the size makes only sense for types where it is relevant
    if ((expected.getTypeCode() == Types.NUMERIC) ||
        (expected.getTypeCode() == Types.DECIMAL))
    {
        assertEquals("Precision not the same for column "+actual.getName()+".",
                     expected.getSizeAsInt(),
                     actual.getSizeAsInt());
        assertEquals("Scale not the same for column "+actual.getName()+".",
                     expected.getScale(),
                     actual.getScale());
    }
    else if ((expected.getTypeCode() == Types.CHAR) ||
             (expected.getTypeCode() == Types.VARCHAR) ||
             (expected.getTypeCode() == Types.BINARY) ||
             (expected.getTypeCode() == Types.VARBINARY))
    {
        assertEquals("Size not the same for column "+actual.getName()+".",
                     expected.getSize(),
                     actual.getSize());
    }
}
 
Example 13
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Generates the string representation of the given value.
 * 
 * @param column The column
 * @param value  The value
 * @return The string representation
 */
protected String getValueAsString(Column column, Object value)
{
    if (value == null)
    {
        return "NULL";
    }

    StringBuilder result = new StringBuilder();

    // TODO: Handle binary types (BINARY, VARBINARY, LONGVARBINARY, BLOB)
    switch (column.getTypeCode())
    {
        case Types.DATE:
            result.append(getPlatformInfo().getValueQuoteToken());
            if (!(value instanceof String) && (getValueDateFormat() != null))
            {
                // TODO: Can the format method handle java.sql.Date properly ?
                result.append(getValueDateFormat().format(value));
            }
            else
            {
                result.append(value.toString());
            }
            result.append(getPlatformInfo().getValueQuoteToken());
            break;
        case Types.TIME:
            result.append(getPlatformInfo().getValueQuoteToken());
            if (!(value instanceof String) && (getValueTimeFormat() != null))
            {
                // TODO: Can the format method handle java.sql.Date properly ?
                result.append(getValueTimeFormat().format(value));
            }
            else
            {
                result.append(value.toString());
            }
            result.append(getPlatformInfo().getValueQuoteToken());
            break;
        case Types.TIMESTAMP:
            result.append(getPlatformInfo().getValueQuoteToken());
            // TODO: SimpleDateFormat does not support nano seconds so we would
            //       need a custom date formatter for timestamps
            result.append(value.toString());
            result.append(getPlatformInfo().getValueQuoteToken());
            break;
        case Types.REAL:
        case Types.NUMERIC:
        case Types.FLOAT:
        case Types.DOUBLE:
        case Types.DECIMAL:
            result.append(getPlatformInfo().getValueQuoteToken());
            if (!(value instanceof String) && (getValueNumberFormat() != null))
            {
                result.append(getValueNumberFormat().format(value));
            }
            else
            {
                result.append(value.toString());
            }
            result.append(getPlatformInfo().getValueQuoteToken());
            break;
        default:
            result.append(getPlatformInfo().getValueQuoteToken());
            result.append(escapeStringValue(value.toString()));
            result.append(getPlatformInfo().getValueQuoteToken());
            break;
    }
    return result.toString();
}
 
Example 14
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Generates the string representation of the given value.
 * 
 * @param column The column
 * @param value  The value
 * @return The string representation
 */
protected String getValueAsString(Column column, Object value)
{
    if (value == null)
    {
        return "NULL";
    }

    StringBuilder result = new StringBuilder();

    // TODO: Handle binary types (BINARY, VARBINARY, LONGVARBINARY, BLOB)
    switch (column.getTypeCode())
    {
        case Types.DATE:
            result.append(getPlatformInfo().getValueQuoteToken());
            if (!(value instanceof String) && (getValueDateFormat() != null))
            {
                // TODO: Can the format method handle java.sql.Date properly ?
                result.append(getValueDateFormat().format(value));
            }
            else
            {
                result.append(value.toString());
            }
            result.append(getPlatformInfo().getValueQuoteToken());
            break;
        case Types.TIME:
            result.append(getPlatformInfo().getValueQuoteToken());
            if (!(value instanceof String) && (getValueTimeFormat() != null))
            {
                // TODO: Can the format method handle java.sql.Date properly ?
                result.append(getValueTimeFormat().format(value));
            }
            else
            {
                result.append(value.toString());
            }
            result.append(getPlatformInfo().getValueQuoteToken());
            break;
        case Types.TIMESTAMP:
            result.append(getPlatformInfo().getValueQuoteToken());
            // TODO: SimpleDateFormat does not support nano seconds so we would
            //       need a custom date formatter for timestamps
            result.append(value.toString());
            result.append(getPlatformInfo().getValueQuoteToken());
            break;
        case Types.REAL:
        case Types.NUMERIC:
        case Types.FLOAT:
        case Types.DOUBLE:
        case Types.DECIMAL:
            result.append(getPlatformInfo().getValueQuoteToken());
            if (!(value instanceof String) && (getValueNumberFormat() != null))
            {
                result.append(getValueNumberFormat().format(value));
            }
            else
            {
                result.append(value.toString());
            }
            result.append(getPlatformInfo().getValueQuoteToken());
            break;
        default:
            result.append(getPlatformInfo().getValueQuoteToken());
            result.append(escapeStringValue(value.toString()));
            result.append(getPlatformInfo().getValueQuoteToken());
            break;
    }
    return result.toString();
}
 
Example 15
Source File: SapDbPlatform.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected TableDefinitionChangesPredicate getTableDefinitionChangesPredicate()
{
    return new DefaultTableDefinitionChangesPredicate()
    {
        protected boolean isSupported(Table intermediateTable, TableChange change)
        {
            if ((change instanceof RemoveColumnChange) ||
                (change instanceof AddPrimaryKeyChange) ||
                (change instanceof PrimaryKeyChange) ||
                (change instanceof RemovePrimaryKeyChange))
            {
                return true;
            }
            else if (change instanceof AddColumnChange) 
            {
                AddColumnChange addColumnChange = (AddColumnChange)change;

                // SapDB can only add not insert columns, and required columns have to have
                // a default value or be IDENTITY
                return (addColumnChange.getNextColumn() == null) &&
                       (!addColumnChange.getNewColumn().isRequired() ||
                        !StringUtilsExt.isEmpty(addColumnChange.getNewColumn().getDefaultValue()));
            }
            else if (change instanceof ColumnDefinitionChange)
            {
                ColumnDefinitionChange colChange = (ColumnDefinitionChange)change;

                // SapDB has a ALTER TABLE MODIFY COLUMN but it is limited regarding the type conversions
                // it can perform, so we don't use it here but rather rebuild the table
                Column curColumn = intermediateTable.findColumn(colChange.getChangedColumn(), isDelimitedIdentifierModeOn());
                Column newColumn = colChange.getNewColumn();

                // we can however handle the change if only the default value or the required status was changed
                return ((curColumn.getTypeCode() == newColumn.getTypeCode()) &&
                       !ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), curColumn, newColumn) &&
                       (curColumn.isAutoIncrement() == newColumn.isAutoIncrement()));
            }
            else
            {
                return false;
            }
        }
    };
}
 
Example 16
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Asserts that the two given columns are equal.
 * 
 * @param expected      The expected column
 * @param actual        The actual column
 * @param caseSensitive Whether case matters when comparing
 */
protected void assertEquals(Column expected, Column actual, boolean caseSensitive)
{
    if (caseSensitive)
    {
        assertEquals("Column names do not match.",
                     getPlatform().getSqlBuilder().shortenName(expected.getName(), getSqlBuilder().getMaxColumnNameLength()),
                     getPlatform().getSqlBuilder().shortenName(actual.getName(), getSqlBuilder().getMaxColumnNameLength()));
    }
    else
    {
        assertEquals("Column names do not match (ignoring case).",
                     getPlatform().getSqlBuilder().shortenName(expected.getName().toUpperCase(), getSqlBuilder().getMaxColumnNameLength()),
                     getPlatform().getSqlBuilder().shortenName(actual.getName().toUpperCase(), getSqlBuilder().getMaxColumnNameLength()));
    }
    assertEquals("Primary key status not the same for column "+actual.getName()+".",
                 expected.isPrimaryKey(),
                 actual.isPrimaryKey());
    assertEquals("Required status not the same for column "+actual.getName()+".",
                 expected.isRequired(),
                 actual.isRequired());
    if (getPlatformInfo().getIdentityStatusReadingSupported())
    {
        // we're only comparing this if the platform can actually read the
        // auto-increment status back from an existing database
        assertEquals("Auto-increment status not the same for column "+actual.getName()+".",
                     expected.isAutoIncrement(),
                     actual.isAutoIncrement());
    }
    assertEquals("Type not the same for column "+actual.getName()+".",
                 expected.getType(),
                 actual.getType());
    assertEquals("Type code not the same for column "+actual.getName()+".",
                 expected.getTypeCode(),
                 actual.getTypeCode());
    assertEquals("Parsed default values do not match for column "+actual.getName()+".",
                 expected.getParsedDefaultValue(),
                 actual.getParsedDefaultValue());

    // comparing the size makes only sense for types where it is relevant
    if ((expected.getTypeCode() == Types.NUMERIC) ||
        (expected.getTypeCode() == Types.DECIMAL))
    {
        assertEquals("Precision not the same for column "+actual.getName()+".",
                     expected.getSizeAsInt(),
                     actual.getSizeAsInt());
        assertEquals("Scale not the same for column "+actual.getName()+".",
                     expected.getScale(),
                     actual.getScale());
    }
    else if ((expected.getTypeCode() == Types.CHAR) ||
             (expected.getTypeCode() == Types.VARCHAR) ||
             (expected.getTypeCode() == Types.BINARY) ||
             (expected.getTypeCode() == Types.VARBINARY))
    {
        assertEquals("Size not the same for column "+actual.getName()+".",
                     expected.getSize(),
                     actual.getSize());
    }
}
 
Example 17
Source File: MSSqlBuilder.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected String getValueAsString(Column column, Object value)
{
    if (value == null)
    {
        return "NULL";
    }

    StringBuilder result = new StringBuilder();

    switch (column.getTypeCode())
    {
        case Types.REAL:
        case Types.NUMERIC:
        case Types.FLOAT:
        case Types.DOUBLE:
        case Types.DECIMAL:
            // SQL Server does not want quotes around the value
            if (!(value instanceof String) && (getValueNumberFormat() != null))
            {
                result.append(getValueNumberFormat().format(value));
            }
            else
            {
                result.append(value.toString());
            }
            break;
        case Types.DATE:
            result.append("CAST(");
            result.append(getPlatformInfo().getValueQuoteToken());
            result.append(value instanceof String ? (String)value : getValueDateFormat().format(value));
            result.append(getPlatformInfo().getValueQuoteToken());
            result.append(" AS datetime)");
            break;
        case Types.TIME:
            result.append("CAST(");
            result.append(getPlatformInfo().getValueQuoteToken());
            result.append(value instanceof String ? (String)value : getValueTimeFormat().format(value));
            result.append(getPlatformInfo().getValueQuoteToken());
            result.append(" AS datetime)");
            break;
        case Types.TIMESTAMP:
            result.append("CAST(");
            result.append(getPlatformInfo().getValueQuoteToken());
            result.append(value.toString());
            result.append(getPlatformInfo().getValueQuoteToken());
            result.append(" AS datetime)");
            break;
    }
    return super.getValueAsString(column, value);
}
 
Example 18
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 19
Source File: MSSqlBuilder.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected String getValueAsString(Column column, Object value)
{
    if (value == null)
    {
        return "NULL";
    }

    StringBuilder result = new StringBuilder();

    switch (column.getTypeCode())
    {
        case Types.REAL:
        case Types.NUMERIC:
        case Types.FLOAT:
        case Types.DOUBLE:
        case Types.DECIMAL:
            // SQL Server does not want quotes around the value
            if (!(value instanceof String) && (getValueNumberFormat() != null))
            {
                result.append(getValueNumberFormat().format(value));
            }
            else
            {
                result.append(value.toString());
            }
            break;
        case Types.DATE:
            result.append("CAST(");
            result.append(getPlatformInfo().getValueQuoteToken());
            result.append(value instanceof String ? (String)value : getValueDateFormat().format(value));
            result.append(getPlatformInfo().getValueQuoteToken());
            result.append(" AS datetime)");
            break;
        case Types.TIME:
            result.append("CAST(");
            result.append(getPlatformInfo().getValueQuoteToken());
            result.append(value instanceof String ? (String)value : getValueTimeFormat().format(value));
            result.append(getPlatformInfo().getValueQuoteToken());
            result.append(" AS datetime)");
            break;
        case Types.TIMESTAMP:
            result.append("CAST(");
            result.append(getPlatformInfo().getValueQuoteToken());
            result.append(value.toString());
            result.append(getPlatformInfo().getValueQuoteToken());
            result.append(" AS datetime)");
            break;
    }
    return super.getValueAsString(column, value);
}
 
Example 20
Source File: ColumnDefinitionChange.java    From gemfirexd-oss with Apache License 2.0 3 votes vote down vote up
/**
 * Determines whether the jdbc type of the given target column is different from the one of the given source column.
 * This method uses the platform info object to determine the actual jdbc type that the target column would have
 * in the database, and compares that to the type of he source column.
 * 
 * @param platformInfo The info object for the current platform
 * @param sourceColumn The source column
 * @param targetColumn The target column
 * @return <code>true</code> if the jdbc types differ
 */
public static boolean isTypeChanged(PlatformInfo platformInfo, Column sourceColumn, Column targetColumn)
{
    int targetTypeCode = platformInfo.getTargetJdbcType(targetColumn.getTypeCode());

    return targetTypeCode != sourceColumn.getTypeCode();
}