org.apache.ddlutils.model.TypeMap Java Examples

The following examples show how to use org.apache.ddlutils.model.TypeMap. 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: 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 #2
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Compares the specified attribute value of the given bean with the expected object.
 * 
 * @param expected The expected object
 * @param bean     The bean
 * @param attrName The attribute name
 */
protected void assertEquals(Object expected, Object bean, String attrName)
{
    DynaBean dynaBean = (DynaBean)bean;
    Object   value    = dynaBean.get(attrName);

    if ((value instanceof byte[]) && !(expected instanceof byte[]) && (dynaBean instanceof SqlDynaBean))
    {
        SqlDynaClass dynaClass = (SqlDynaClass)((SqlDynaBean)dynaBean).getDynaClass();
        Column       column    = ((SqlDynaProperty)dynaClass.getDynaProperty(attrName)).getColumn();

        if (TypeMap.isBinaryType(column.getTypeCode()))
        {
            value = new BinaryObjectsHelper().deserialize((byte[])value);
        }
    }
    if (expected == null)
    {
        assertNull(value);
    }
    else
    {
        assertEquals(expected, value);
    }
}
 
Example #3
Source File: InterbaseModelReader.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Adjusts the columns in the table by fixing types and default values.
 * 
 * @param table The table
 */
protected void adjustColumns(Table table)
{
    Column[] columns = table.getColumns();

    for (int idx = 0; idx < columns.length; idx++)
    {
        if (columns[idx].getTypeCode() == Types.FLOAT)
        {
            columns[idx].setTypeCode(Types.REAL);
        }
        else if ((columns[idx].getTypeCode() == Types.NUMERIC) || (columns[idx].getTypeCode() == Types.DECIMAL))
        {
            if ((columns[idx].getTypeCode() == Types.NUMERIC) && (columns[idx].getSizeAsInt() == 18) && (columns[idx].getScale() == 0))
            {
                columns[idx].setTypeCode(Types.BIGINT);
            }
        }
        else if (TypeMap.isTextType(columns[idx].getTypeCode()))
        {
            columns[idx].setDefaultValue(unescape(columns[idx].getDefaultValue(), "'", "''"));
        }
    }
}
 
Example #4
Source File: Oracle8Builder.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void printDefaultValue(Object defaultValue, int typeCode) throws IOException
{
    if (defaultValue != null)
    {
        String  defaultValueStr = defaultValue.toString();
        boolean shouldUseQuotes = !TypeMap.isNumericType(typeCode) && !defaultValueStr.startsWith("TO_DATE(");

        if (shouldUseQuotes)
        {
            // characters are only escaped when within a string literal 
            print(getPlatformInfo().getValueQuoteToken());
            print(escapeStringValue(defaultValueStr));
            print(getPlatformInfo().getValueQuoteToken());
        }
        else
        {
            print(defaultValueStr);
        }
    }
}
 
Example #5
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Prints the default value of the column.
 * 
 * @param defaultValue The default value
 * @param typeCode     The type code to write the default value for
 */ 
protected void printDefaultValue(Object defaultValue, int typeCode) throws IOException
{
    if (defaultValue != null)
    {
        boolean shouldUseQuotes = !TypeMap.isNumericType(typeCode);

        if (shouldUseQuotes)
        {
            // characters are only escaped when within a string literal 
            print(getPlatformInfo().getValueQuoteToken());
            print(escapeStringValue(defaultValue.toString()));
            print(getPlatformInfo().getValueQuoteToken());
        }
        else
        {
            print(defaultValue.toString());
        }
    }
}
 
Example #6
Source File: SapDbBuilder.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException
{
    boolean charSizeChanged = TypeMap.isTextType(targetColumn.getTypeCode()) &&
                              TypeMap.isTextType(targetColumn.getTypeCode()) &&
                              ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn) &&
                              !StringUtilsExt.isEmpty(targetColumn.getSize());

    if (charSizeChanged)
    {
        print("SUBSTR(");
    }
    printIdentifier(getColumnName(sourceColumn));
    if (charSizeChanged)
    {
        print(",1,");
        print(targetColumn.getSize());
        print(")");
    }
}
 
Example #7
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 #8
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Compares the specified attribute value of the given bean with the expected object.
 * 
 * @param expected The expected object
 * @param bean     The bean
 * @param attrName The attribute name
 */
protected void assertEquals(Object expected, Object bean, String attrName)
{
    DynaBean dynaBean = (DynaBean)bean;
    Object   value    = dynaBean.get(attrName);

    if ((value instanceof byte[]) && !(expected instanceof byte[]) && (dynaBean instanceof SqlDynaBean))
    {
        SqlDynaClass dynaClass = (SqlDynaClass)((SqlDynaBean)dynaBean).getDynaClass();
        Column       column    = ((SqlDynaProperty)dynaClass.getDynaProperty(attrName)).getColumn();

        if (TypeMap.isBinaryType(column.getTypeCode()))
        {
            value = new BinaryObjectsHelper().deserialize((byte[])value);
        }
    }
    if (expected == null)
    {
        assertNull(value);
    }
    else
    {
        assertEquals(expected, value);
    }
}
 
Example #9
Source File: InterbaseModelReader.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Adjusts the columns in the table by fixing types and default values.
 * 
 * @param table The table
 */
protected void adjustColumns(Table table)
{
    Column[] columns = table.getColumns();

    for (int idx = 0; idx < columns.length; idx++)
    {
        if (columns[idx].getTypeCode() == Types.FLOAT)
        {
            columns[idx].setTypeCode(Types.REAL);
        }
        else if ((columns[idx].getTypeCode() == Types.NUMERIC) || (columns[idx].getTypeCode() == Types.DECIMAL))
        {
            if ((columns[idx].getTypeCode() == Types.NUMERIC) && (columns[idx].getSizeAsInt() == 18) && (columns[idx].getScale() == 0))
            {
                columns[idx].setTypeCode(Types.BIGINT);
            }
        }
        else if (TypeMap.isTextType(columns[idx].getTypeCode()))
        {
            columns[idx].setDefaultValue(unescape(columns[idx].getDefaultValue(), "'", "''"));
        }
    }
}
 
Example #10
Source File: Oracle8Builder.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void printDefaultValue(Object defaultValue, int typeCode) throws IOException
{
    if (defaultValue != null)
    {
        String  defaultValueStr = defaultValue.toString();
        boolean shouldUseQuotes = !TypeMap.isNumericType(typeCode) && !defaultValueStr.startsWith("TO_DATE(");

        if (shouldUseQuotes)
        {
            // characters are only escaped when within a string literal 
            print(getPlatformInfo().getValueQuoteToken());
            print(escapeStringValue(defaultValueStr));
            print(getPlatformInfo().getValueQuoteToken());
        }
        else
        {
            print(defaultValueStr);
        }
    }
}
 
Example #11
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Prints the default value of the column.
 * 
 * @param defaultValue The default value
 * @param typeCode     The type code to write the default value for
 */ 
protected void printDefaultValue(Object defaultValue, int typeCode) throws IOException
{
    if (defaultValue != null)
    {
        boolean shouldUseQuotes = !TypeMap.isNumericType(typeCode);

        if (shouldUseQuotes)
        {
            // characters are only escaped when within a string literal 
            print(getPlatformInfo().getValueQuoteToken());
            print(escapeStringValue(defaultValue.toString()));
            print(getPlatformInfo().getValueQuoteToken());
        }
        else
        {
            print(defaultValue.toString());
        }
    }
}
 
Example #12
Source File: SapDbBuilder.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException
{
    boolean charSizeChanged = TypeMap.isTextType(targetColumn.getTypeCode()) &&
                              TypeMap.isTextType(targetColumn.getTypeCode()) &&
                              ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn) &&
                              !StringUtilsExt.isEmpty(targetColumn.getSize());

    if (charSizeChanged)
    {
        print("SUBSTR(");
    }
    printIdentifier(getColumnName(sourceColumn));
    if (charSizeChanged)
    {
        print(",1,");
        print(targetColumn.getSize());
        print(")");
    }
}
 
Example #13
Source File: MSSqlBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException
{
    boolean sizeChanged = ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn);
    boolean typeChanged = ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), sourceColumn, targetColumn);

    if (sizeChanged || typeChanged)
    {
        if (TypeMap.isTextType(targetColumn.getTypeCode()) && sizeChanged &&
            (targetColumn.getSize() != null) && (sourceColumn.getSizeAsInt() > targetColumn.getSizeAsInt()))
        {
            print("SUBSTRING(CAST(");
            printIdentifier(getColumnName(sourceColumn));
            print(" AS ");
            print(getNativeType(targetColumn));
            print("),1,");
            print(getSizeSpec(targetColumn));
            print(")");
        }
        else
        {
            print("CAST(");
            printIdentifier(getColumnName(sourceColumn));
            print(" AS ");
            print(getSqlType(targetColumn));
            print(")");
        }
    }
    else
    {
        printIdentifier(getColumnName(sourceColumn));
    }
}
 
Example #14
Source File: DefaultValueHelper.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a boolean default value to the given target type.
 * 
 * @param defaultValue   The default value
 * @param targetTypeCode The target type code
 * @return The converted value
 */
private Object convertBoolean(String defaultValue, int targetTypeCode)
{
    Boolean value  = null;
    Object  result = null;

    try
    {
        value = (Boolean)ConvertUtils.convert(defaultValue, Boolean.class);
    }
    catch (ConversionException ex)
    {
        return defaultValue;
    }
    
    if ((targetTypeCode == Types.BIT) || (targetTypeCode == Types.BOOLEAN))
    {
        result = value;
    }
    else if (TypeMap.isNumericType(targetTypeCode))
    {
        result = (value.booleanValue() ? Integer.valueOf(1) : Integer.valueOf(0));
    }
    else
    {
        result = value.toString();
    }
    return result;
}
 
Example #15
Source File: FirebirdBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException
{
    boolean sizeChanged = ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn);
    boolean typeChanged = ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), sourceColumn, targetColumn);

    if (sizeChanged || typeChanged)
    {
        boolean needSubstr = TypeMap.isTextType(targetColumn.getTypeCode()) && sizeChanged &&
                             (targetColumn.getSize() != null) && (sourceColumn.getSizeAsInt() > targetColumn.getSizeAsInt());

        if (needSubstr)
        {
            print("SUBSTRING(");
        }
        // we're not using CAST but instead string construction which does not require us to know the size
        print("(");
        printIdentifier(getColumnName(sourceColumn));
        print(" || '' ");
        if (needSubstr)
        {
            print(") FROM 1 FOR ");
            print(targetColumn.getSize());
            print(")");
        }
        else
        {
            print(")");
        }
    }
    else
    {
        super.writeCastExpression(sourceColumn, targetColumn);
    }
}
 
Example #16
Source File: Db2Builder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException
{
    String sourceNativeType = getBareNativeType(sourceColumn);
    String targetNativeType = getBareNativeType(targetColumn);

    if (sourceNativeType.equals(targetNativeType) &&
        !ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn))
    {
        printIdentifier(getColumnName(sourceColumn));
    }
    else
    {
        String type = getSqlType(targetColumn);

        // DB2 has the limitation that it cannot convert numeric values
        // to VARCHAR, though it can convert them to CHAR
        if (TypeMap.isNumericType(sourceColumn.getTypeCode()) &&
            "VARCHAR".equalsIgnoreCase(targetNativeType))
        {
            Object sizeSpec = targetColumn.getSize();
            
            if (sizeSpec == null)
            {
                sizeSpec = getPlatformInfo().getDefaultSize(targetColumn.getTypeCode());
            }
            type = "CHAR(" + sizeSpec.toString() + ")";
        }

        print("CAST(");
        printIdentifier(getColumnName(sourceColumn));
        print(" AS ");
        print(type);
        print(")");
    }
}
 
Example #17
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 #18
Source File: DefaultValueHelper.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a boolean default value to the given target type.
 * 
 * @param defaultValue   The default value
 * @param targetTypeCode The target type code
 * @return The converted value
 */
private Object convertBoolean(String defaultValue, int targetTypeCode)
{
    Boolean value  = null;
    Object  result = null;

    try
    {
        value = (Boolean)ConvertUtils.convert(defaultValue, Boolean.class);
    }
    catch (ConversionException ex)
    {
        return defaultValue;
    }
    
    if ((targetTypeCode == Types.BIT) || (targetTypeCode == Types.BOOLEAN))
    {
        result = value;
    }
    else if (TypeMap.isNumericType(targetTypeCode))
    {
        result = (value.booleanValue() ? Integer.valueOf(1) : Integer.valueOf(0));
    }
    else
    {
        result = value.toString();
    }
    return result;
}
 
Example #19
Source File: PlatformImplBase.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Allows the platform to postprocess the model just read from the database.
 * 
 * @param model The model
 */
protected void postprocessModelFromDatabase(Database model)
{
    // Default values for CHAR/VARCHAR/LONGVARCHAR columns have quotation marks
    // around them which we'll remove now
    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);

            if (TypeMap.isTextType(column.getTypeCode()) ||
                TypeMap.isDateTimeType(column.getTypeCode()))
            {
                String defaultValue = column.getDefaultValue();

                if ((defaultValue != null) && (defaultValue.length() >= 2) &&
                    defaultValue.startsWith("'") && defaultValue.endsWith("'"))
                {
                    defaultValue = defaultValue.substring(1, defaultValue.length() - 1);
                    column.setDefaultValue(defaultValue);
                }
            }
        }
    }
}
 
Example #20
Source File: Oracle8Builder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException
{
    boolean sizeChanged = TypeMap.isTextType(targetColumn.getTypeCode()) &&
                          ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn) &&
                          !StringUtilsExt.isEmpty(targetColumn.getSize());

    if (sizeChanged)
    {
        print("SUBSTR(");
    }
    if (ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), sourceColumn, targetColumn))
    {
        print("CAST (");
        printIdentifier(getColumnName(sourceColumn));
        print(" AS ");
        print(getSqlType(targetColumn));
        print(")");
    }
    else
    {
        printIdentifier(getColumnName(sourceColumn));
    }
    if (sizeChanged)
    {
        print(",0,");
        print(targetColumn.getSize());
        print(")");
    }
}
 
Example #21
Source File: Oracle10Platform.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private void registerOracleSpecificJDBCTypes() {
  getLog().debug( "Trying to register " + DATABASENAME+ " specific JDBCTypes." );
    try {
      Class oracleTypes = Class.forName( ORACLE_JDBC_TYPES_CLASSNAME );
      Field timestamptz = oracleTypes.getField( "TIMESTAMPTZ" );
      Field timestampltz = oracleTypes.getField( "TIMESTAMPLTZ" );
      TypeMap.registerJdbcType( timestamptz.getInt( timestamptz ), "TIMESTAMP(6) WITH TIME ZONE", JdbcTypeCategoryEnum.DATETIME );
      TypeMap.registerJdbcType( timestampltz.getInt( timestampltz ), "TIMESTAMP(6) WITH LOCAL TIME ZONE", JdbcTypeCategoryEnum.DATETIME );
    }
    catch ( Exception e ) {
      getLog().warn( "Could not register additional JDBC Types: " + e.getLocalizedMessage(), e );
    }
}
 
Example #22
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 #23
Source File: HsqlDbBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException
{
    boolean sizeChanged = ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn);
    boolean typeChanged = ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), sourceColumn, targetColumn);

    if (sizeChanged || typeChanged)
    {
        boolean needSubstr = TypeMap.isTextType(targetColumn.getTypeCode()) && sizeChanged && (sourceColumn.getSizeAsInt() > targetColumn.getSizeAsInt());

        if (needSubstr)
        {
            print("SUBSTR(");
        }
        print("CAST(");
        printIdentifier(getColumnName(sourceColumn));
        print(" AS ");
        if (needSubstr)
        {
            print(getNativeType(targetColumn));
        }
        else
        {
            print(getSqlType(targetColumn));
        }
        print(")");
        if (needSubstr)
        {
            print(",1,");
            print(targetColumn.getSize());
            print(")");
        }
    }
    else
    {
        super.writeCastExpression(sourceColumn, targetColumn);
    }
}
 
Example #24
Source File: HsqlDbModelReader.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 (TypeMap.isTextType(column.getTypeCode()) &&
        (column.getDefaultValue() != null))
    {
        column.setDefaultValue(unescape(column.getDefaultValue(), "'", "''"));
    }
    return column;
}
 
Example #25
Source File: DataConverterRegistration.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the jdbc type.
 *
 * @param jdbcTypeName The jdbc type name
 */
public void setJdbcType(String jdbcTypeName) throws BuildException
{
    Integer typeCode = TypeMap.getJdbcTypeCode(jdbcTypeName);

    if (typeCode == null)
    {
        throw new BuildException("Unknown jdbc type "+jdbcTypeName);
    }
    else
    {
        _typeCode = typeCode.intValue();
    }
}
 
Example #26
Source File: DerbyBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException
{
    if (ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn) ||
        ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), sourceColumn, targetColumn))
    {
        String targetNativeType = getNativeType(targetColumn);

        // Derby currently has the limitation that it cannot convert numeric values
        // to VARCHAR, though it can convert them to CHAR
        if (TypeMap.isNumericType(sourceColumn.getTypeCode()) &&
            "VARCHAR".equalsIgnoreCase(targetNativeType))
        {
            targetNativeType = "CHAR";
        }

        print("CAST (");
        printIdentifier(getColumnName(sourceColumn));
        print(" AS ");
        print(getSqlType(targetColumn, targetNativeType));
        print(")");
    }
    else
    {
        printIdentifier(getColumnName(sourceColumn));
    }
}
 
Example #27
Source File: TestDatabaseIO.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts the data in a column object.
 * 
 * @param name            The expected name
 * @param typeCode        The exected JDBC type code
 * @param size            The expected size value
 * @param scale           The expected scale value
 * @param defaultValue    The expected default value
 * @param description     The expected description
 * @param javaName        The expected java name
 * @param isPrimaryKey    The expected primary key status
 * @param isRequired      The expected required satus
 * @param isAutoIncrement The expected auto increment status
 * @param column          The column
 */
private void assertEquals(String  name,
                          int     typeCode,
                          int     size,
                          int     scale,
                          String  defaultValue,
                          String  description,
                          String  javaName,
                          boolean isPrimaryKey,
                          boolean isRequired,
                          boolean isAutoIncrement,
                          Column  column)
{
    assertEquals(name, column.getName());
    assertEquals(TypeMap.getJdbcTypeName(typeCode), column.getType());
    assertEquals(typeCode, column.getTypeCode());
    assertEquals(size, column.getSizeAsInt());
    assertEquals(size, column.getPrecisionRadix());
    assertEquals(scale, column.getScale());
    if ((size <= 0) && (scale <= 0)) {
        assertNull(column.getSize());
    }
    else if (scale == 0) {
        assertEquals("" + size, column.getSize());
    }
    else {
        assertEquals("" + size + "," + scale, column.getSize());
    }
    assertEquals(defaultValue, column.getDefaultValue());
    assertEquals(description, column.getDescription());
    assertEquals(javaName, column.getJavaName());
    assertEquals(isPrimaryKey, column.isPrimaryKey());
    assertEquals(isRequired, column.isRequired());
    assertEquals(isAutoIncrement, column.isAutoIncrement());
}
 
Example #28
Source File: TestDllUtils.java    From Eagle with Apache License 2.0 5 votes vote down vote up
@Test
public void testTable(){
    Table table = new Table();
    Column column = new Column();
    column.setName("id");
    column.setDefaultValue("-1");
    column.setDescription("rowkey");
    column.setPrimaryKey(true);
    column.setType(TypeMap.VARCHAR);
    table.addColumn(column);
    table.setName("eagle_table");

    System.out.println(table.toString());

}
 
Example #29
Source File: TestDllUtils.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Test
public void testTable(){
    Table table = new Table();
    Column column = new Column();
    column.setName("id");
    column.setDefaultValue("-1");
    column.setDescription("rowkey");
    column.setPrimaryKey(true);
    column.setType(TypeMap.VARCHAR);
    table.addColumn(column);
    table.setName("eagle_table");

    System.out.println(table.toString());

}
 
Example #30
Source File: DataConverterRegistration.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the jdbc type.
 *
 * @param jdbcTypeName The jdbc type name
 */
public void setJdbcType(String jdbcTypeName) throws BuildException
{
    Integer typeCode = TypeMap.getJdbcTypeCode(jdbcTypeName);

    if (typeCode == null)
    {
        throw new BuildException("Unknown jdbc type "+jdbcTypeName);
    }
    else
    {
        _typeCode = typeCode.intValue();
    }
}