Java Code Examples for org.apache.ddlutils.model.TypeMap#isNumericType()

The following examples show how to use org.apache.ddlutils.model.TypeMap#isNumericType() . 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 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 2
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 3
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 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: 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 6
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 7
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 8
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 9
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 10
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 11
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 3 votes vote down vote up
/**
 * Determines whether the given default spec is a non-empty spec that shall be used in a DEFAULT
 * expression. E.g. if the spec is an empty string and the type is a numeric type, then it is
 * no valid default value whereas if it is a string type, then it is valid.
 * 
 * @param defaultSpec The default value spec
 * @param typeCode    The JDBC type code
 * @return <code>true</code> if the default value spec is valid
 */
protected boolean isValidDefaultValue(String defaultSpec, int typeCode)
{
    return (defaultSpec != null) &&
           ((defaultSpec.length() > 0) ||
            (!TypeMap.isNumericType(typeCode) && !TypeMap.isDateTimeType(typeCode)));
}
 
Example 12
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 3 votes vote down vote up
/**
 * Determines whether the given default spec is a non-empty spec that shall be used in a DEFAULT
 * expression. E.g. if the spec is an empty string and the type is a numeric type, then it is
 * no valid default value whereas if it is a string type, then it is valid.
 * 
 * @param defaultSpec The default value spec
 * @param typeCode    The JDBC type code
 * @return <code>true</code> if the default value spec is valid
 */
protected boolean isValidDefaultValue(String defaultSpec, int typeCode)
{
    return (defaultSpec != null) &&
           ((defaultSpec.length() > 0) ||
            (!TypeMap.isNumericType(typeCode) && !TypeMap.isDateTimeType(typeCode)));
}