Search in sources :

Example 36 with TypeId

use of org.apache.derby.iapi.types.TypeId in project derby by apache.

the class ResultColumnList method createListFromResultSetMetaData.

/**
 * Generate an RCL to match the contents of a ResultSetMetaData.
 * This is useful when dealing with VTIs.
 *
 * @param rsmd			The ResultSetMetaData.
 * @param tableName		The TableName for the BCNs.
 * @param javaClassName	The name of the VTI
 *
 * @exception StandardException			Thrown on error
 */
void createListFromResultSetMetaData(ResultSetMetaData rsmd, TableName tableName, String javaClassName) throws StandardException {
    try {
        // JDBC columns #s are 1-based
        // Check to make sure # of columns >= 1
        int numColumns = rsmd.getColumnCount();
        if (numColumns <= 0) {
            throw StandardException.newException(SQLState.LANG_INVALID_V_T_I_COLUMN_COUNT, javaClassName, String.valueOf(numColumns));
        }
        for (int index = 1; index <= numColumns; index++) {
            boolean nullableResult = (rsmd.isNullable(index) != ResultSetMetaData.columnNoNulls);
            TypeId cti;
            int jdbcColumnType = rsmd.getColumnType(index);
            switch(jdbcColumnType) {
                case Types.JAVA_OBJECT:
                case Types.OTHER:
                    {
                        cti = TypeId.getUserDefinedTypeId(rsmd.getColumnTypeName(index));
                        break;
                    }
                default:
                    {
                        cti = TypeId.getBuiltInTypeId(jdbcColumnType);
                        break;
                    }
            }
            // Handle the case where a VTI returns a bad column type
            if (cti == null) {
                throw StandardException.newException(SQLState.LANG_BAD_J_D_B_C_TYPE_INFO, Integer.toString(index));
            }
            // Get the maximum byte storage for this column
            int maxWidth;
            /* Get maximum byte storage from rsmd for variable
				 * width types, set it to MAXINT for the long types,
				 * otherwise get it from the TypeId
				 */
            if (cti.variableLength()) {
                maxWidth = rsmd.getColumnDisplaySize(index);
            } else if (jdbcColumnType == Types.LONGVARCHAR || jdbcColumnType == Types.LONGVARBINARY) {
                maxWidth = Integer.MAX_VALUE;
            } else {
                maxWidth = 0;
            }
            int precision = cti.isDecimalTypeId() ? rsmd.getPrecision(index) : 0;
            int scale = cti.isDecimalTypeId() ? rsmd.getScale(index) : 0;
            DataTypeDescriptor dts = new DataTypeDescriptor(cti, precision, scale, nullableResult, maxWidth);
            addColumn(tableName, rsmd.getColumnName(index), dts);
        }
    } catch (Throwable t) {
        if (t instanceof StandardException) {
            throw (StandardException) t;
        } else {
            throw StandardException.unexpectedUserException(t);
        }
    }
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) StandardException(org.apache.derby.shared.common.error.StandardException) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Example 37 with TypeId

use of org.apache.derby.iapi.types.TypeId in project derby by apache.

the class ResultColumnList method streamableType.

private static boolean streamableType(ResultColumn rc) {
    DataTypeDescriptor dtd = rc.getType();
    TypeId s = TypeId.getBuiltInTypeId(dtd.getTypeName());
    if (s != null) {
        return s.streamStorable();
    } else {
        return false;
    }
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Example 38 with TypeId

use of org.apache.derby.iapi.types.TypeId in project derby by apache.

the class TernaryOperatorNode method trimBind.

/**
 * Bind trim expression.
 * The variable receiver is the string that needs to be trimmed.
 * The variable leftOperand is the character that needs to be trimmed from
 *     receiver.
 *
 * @return	The new top of the expression tree.
 *
 * @exception StandardException		Thrown on error
 */
private ValueNode trimBind() throws StandardException {
    TypeId receiverType;
    TypeId resultType = TypeId.getBuiltInTypeId(Types.VARCHAR);
    /* Is there a ? parameter for the receiver? */
    if (receiver.requiresTypeFromContext()) {
        /*
			** According to the SQL standard, if trim has a ? receiver,
			** its type is varchar with the implementation-defined maximum length
			** for a varchar.
			*/
        receiver.setType(getVarcharDescriptor());
        // it's collation from the compilation schema.
        if (!leftOperand.requiresTypeFromContext()) {
            receiver.setCollationInfo(leftOperand.getTypeServices());
        } else {
            receiver.setCollationUsingCompilationSchema();
        }
    }
    /* Is there a ? parameter on the left? */
    if (leftOperand.requiresTypeFromContext()) {
        /* Set the left operand type to varchar. */
        leftOperand.setType(getVarcharDescriptor());
        // collation of ? operand should be picked up from the context.
        // By the time we come here, receiver will have correct collation
        // set on it and hence we can rely on it to get correct collation
        // for the ? for the character that needs to be used for trimming.
        leftOperand.setCollationInfo(receiver.getTypeServices());
    }
    bindToBuiltIn();
    /*
		** Check the type of the receiver - this function is allowed only on
		** string value types.  
		*/
    receiverType = receiver.getTypeId();
    if (receiverType.userType())
        throwBadType("trim", receiverType.getSQLTypeName());
    receiver = castArgToString(receiver);
    if (receiverType.getTypeFormatId() == StoredFormatIds.CLOB_TYPE_ID) {
        // special case for CLOBs: if we start with a CLOB, we have to get
        // a CLOB as a result (as opposed to a VARCHAR), because we can have a
        // CLOB that is beyond the max length of VARCHAR (ex. "clob(100k)").
        // This is okay because CLOBs, like VARCHARs, allow variable-length
        // values (which is a must for the trim to actually work).
        resultType = receiverType;
    }
    /*
		** Check the type of the leftOperand (trimSet).
		** The leftOperand should be a string value type.  
		*/
    TypeId leftCTI;
    leftCTI = leftOperand.getTypeId();
    if (leftCTI.userType())
        throwBadType("trim", leftCTI.getSQLTypeName());
    leftOperand = castArgToString(leftOperand);
    /*
		** The result type of trim is varchar.
		*/
    setResultType(resultType);
    // Result of TRIM should pick up the collation of the character string
    // that is getting trimmed (which is variable receiver) because it has
    // correct collation set on it.
    setCollationInfo(receiver.getTypeServices());
    return this;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId)

Example 39 with TypeId

use of org.apache.derby.iapi.types.TypeId in project derby by apache.

the class TernaryOperatorNode method substrBind.

/**
 * Bind substr expression.
 *
 * @return	The new top of the expression tree.
 *
 * @exception StandardException		Thrown on error
 */
ValueNode substrBind() throws StandardException {
    TypeId receiverType;
    TypeId resultType = TypeId.getBuiltInTypeId(Types.VARCHAR);
    /* Is there a ? parameter for the receiver? */
    if (receiver.requiresTypeFromContext()) {
        /*
			** According to the SQL standard, if substr has a ? receiver,
			** its type is varchar with the implementation-defined maximum length
			** for a varchar.
			*/
        receiver.setType(getVarcharDescriptor());
        // collation of ? operand should be same as the compilation schema
        // because that is the only context available for us to pick up the
        // collation. There are no other character operands to SUBSTR method
        // to pick up the collation from.
        receiver.setCollationUsingCompilationSchema();
    }
    /* Is there a ? parameter on the left? */
    if (leftOperand.requiresTypeFromContext()) {
        /* Set the left operand type to int. */
        leftOperand.setType(new DataTypeDescriptor(TypeId.INTEGER_ID, true));
    }
    /* Is there a ? parameter on the right? */
    if ((rightOperand != null) && rightOperand.requiresTypeFromContext()) {
        /* Set the right operand type to int. */
        rightOperand.setType(new DataTypeDescriptor(TypeId.INTEGER_ID, true));
    }
    bindToBuiltIn();
    if (!leftOperand.getTypeId().isNumericTypeId() || (rightOperand != null && !rightOperand.getTypeId().isNumericTypeId()))
        throw StandardException.newException(SQLState.LANG_DB2_FUNCTION_INCOMPATIBLE, "SUBSTR", "FUNCTION");
    /*
		** Check the type of the receiver - this function is allowed only on
		** string value types.  
		*/
    receiverType = receiver.getTypeId();
    switch(receiverType.getJDBCTypeId()) {
        case Types.CHAR:
        case Types.VARCHAR:
        case Types.LONGVARCHAR:
        case Types.CLOB:
            break;
        default:
            {
                throwBadType("SUBSTR", receiverType.getSQLTypeName());
            }
    }
    if (receiverType.getTypeFormatId() == StoredFormatIds.CLOB_TYPE_ID) {
        // special case for CLOBs: if we start with a CLOB, we have to get
        // a CLOB as a result (as opposed to a VARCHAR), because we can have a
        // CLOB that is beyond the max length of VARCHAR (ex. "clob(100k)").
        // This is okay because CLOBs, like VARCHARs, allow variable-length
        // values (which is a must for the substr to actually work).
        resultType = receiverType;
    }
    // Determine the maximum length of the result
    int resultLen = receiver.getTypeServices().getMaximumWidth();
    if (rightOperand != null && rightOperand instanceof ConstantNode) {
        if (((ConstantNode) rightOperand).getValue().getInt() < resultLen)
            resultLen = ((ConstantNode) rightOperand).getValue().getInt();
    }
    /*
		** The result type of substr is a string type
		*/
    setType(new DataTypeDescriptor(resultType, true, resultLen));
    // Result of SUSBSTR should pick up the collation of the 1st argument
    // to SUBSTR. The 1st argument to SUBSTR is represented by the variable
    // receiver in this class.
    setCollationInfo(receiver.getTypeServices());
    return this;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Example 40 with TypeId

use of org.apache.derby.iapi.types.TypeId in project derby by apache.

the class TimestampOperatorNode method bindExpression.

/**
 * Bind this expression.  This means binding the sub-expressions,
 * as well as figuring out what the return type is for this expression.
 *
 * @param fromList		The FROM list for the query this
 *				expression is in, for binding columns.
 * @param subqueryList		The subquery list being built as we find SubqueryNodes
 * @param aggregates        The aggregate list being built as we find AggregateNodes
 *
 * @return	The new top of the expression tree.
 *
 * @exception StandardException		Thrown on error
 */
@Override
ValueNode bindExpression(FromList fromList, SubqueryList subqueryList, List<AggregateNode> aggregates) throws StandardException {
    leftOperand = leftOperand.bindExpression(fromList, subqueryList, aggregates);
    rightOperand = rightOperand.bindExpression(fromList, subqueryList, aggregates);
    // Set the type if there is a parameter involved here
    if (leftOperand.requiresTypeFromContext()) {
        leftOperand.setType(DataTypeDescriptor.getBuiltInDataTypeDescriptor(Types.DATE));
    }
    // Set the type if there is a parameter involved here
    if (rightOperand.requiresTypeFromContext()) {
        rightOperand.setType(DataTypeDescriptor.getBuiltInDataTypeDescriptor(Types.TIME));
    }
    TypeId leftTypeId = leftOperand.getTypeId();
    TypeId rightTypeId = rightOperand.getTypeId();
    if (!(leftOperand.requiresTypeFromContext() || leftTypeId.isStringTypeId() || leftTypeId.getJDBCTypeId() == Types.DATE))
        throw StandardException.newException(SQLState.LANG_BINARY_OPERATOR_NOT_SUPPORTED, operator, leftTypeId.getSQLTypeName(), rightTypeId.getSQLTypeName());
    if (!(rightOperand.requiresTypeFromContext() || rightTypeId.isStringTypeId() || rightTypeId.getJDBCTypeId() == Types.TIME))
        throw StandardException.newException(SQLState.LANG_BINARY_OPERATOR_NOT_SUPPORTED, operator, leftTypeId.getSQLTypeName(), rightTypeId.getSQLTypeName());
    setType(DataTypeDescriptor.getBuiltInDataTypeDescriptor(Types.TIMESTAMP));
    return genSQLJavaSQLTree();
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId)

Aggregations

TypeId (org.apache.derby.iapi.types.TypeId)53 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)32 TypeCompiler (org.apache.derby.iapi.sql.compile.TypeCompiler)8 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)6 ClassFactory (org.apache.derby.iapi.services.loader.ClassFactory)4 UserDefinedTypeIdImpl (org.apache.derby.catalog.types.UserDefinedTypeIdImpl)3 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)3 StandardException (org.apache.derby.shared.common.error.StandardException)3 TypeDescriptor (org.apache.derby.catalog.TypeDescriptor)2 DefaultInfoImpl (org.apache.derby.catalog.types.DefaultInfoImpl)2 RoutineAliasInfo (org.apache.derby.catalog.types.RoutineAliasInfo)2 ClassInspector (org.apache.derby.iapi.services.loader.ClassInspector)2 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)2 ProviderList (org.apache.derby.iapi.sql.depend.ProviderList)2 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)2 JSQLType (org.apache.derby.iapi.types.JSQLType)2 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Properties (java.util.Properties)1