Search in sources :

Example 76 with DataTypeDescriptor

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

the class TernaryOperatorNode method castArgToString.

/* cast arg to a varchar */
protected ValueNode castArgToString(ValueNode vn) throws StandardException {
    TypeCompiler vnTC = vn.getTypeCompiler();
    if (!vn.getTypeId().isStringTypeId()) {
        DataTypeDescriptor dtd = DataTypeDescriptor.getBuiltInDataTypeDescriptor(Types.VARCHAR, true, vnTC.getCastToCharWidth(vn.getTypeServices()));
        ValueNode newNode = new CastNode(vn, dtd, getContextManager());
        // DERBY-2910 - Match current schema collation for implicit cast as we do for
        // explicit casts per SQL Spec 6.12 (10)
        newNode.setCollationUsingCompilationSchema();
        ((CastNode) newNode).bindCastNodeOnly();
        return newNode;
    }
    return vn;
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) TypeCompiler(org.apache.derby.iapi.sql.compile.TypeCompiler)

Example 77 with DataTypeDescriptor

use of org.apache.derby.iapi.types.DataTypeDescriptor 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 78 with DataTypeDescriptor

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

the class SYSSEQUENCESRowFactory method buildDescriptor.

/**
 * Make an  Tuple Descriptor out of a SYSSEQUENCES row
 *
 * @param row                   a SYSSEQUENCES row
 * @param parentTupleDescriptor unused
 * @param dd                    dataDictionary
 * @return a  descriptor equivalent to a SYSSEQUENCES row
 * @throws org.apache.derby.shared.common.error.StandardException
 *          thrown on failure
 */
public TupleDescriptor buildDescriptor(ExecRow row, TupleDescriptor parentTupleDescriptor, DataDictionary dd) throws StandardException {
    DataValueDescriptor col;
    SequenceDescriptor descriptor;
    UUID ouuid;
    String sequenceName;
    UUID suuid;
    Long currentValue;
    long startValue;
    long minimumValue;
    long maximumValue;
    long increment;
    String cycleOption;
    DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(row.nColumns() == SYSSEQUENCES_COLUMN_COUNT, "Wrong number of columns for a SYSSEQUENCES row");
    }
    // first column is uuid of this sequence descriptor (char(36))
    col = row.getColumn(SYSSEQUENCES_SEQUENCEID);
    String oidString = col.getString();
    ouuid = getUUIDFactory().recreateUUID(oidString);
    // second column is sequenceName (varchar(128))
    col = row.getColumn(SYSSEQUENCES_SEQUENCENAME);
    sequenceName = col.getString();
    // third column is uuid of this sequence descriptors schema (char(36))
    col = row.getColumn(SYSSEQUENCES_SCHEMAID);
    String schemaIdString = col.getString();
    suuid = getUUIDFactory().recreateUUID(schemaIdString);
    // fourth column is the data type of this sequene generator
    /*
          ** What is stored in the column is a TypeDescriptorImpl, which
          ** points to a BaseTypeIdImpl.  These are simple types that are
          ** intended to be movable to the client, so they don't have
          ** the entire implementation.  We need to wrap them in DataTypeServices
          ** and TypeId objects that contain the full implementations for
          ** language processing.
          */
    TypeDescriptor catalogType = (TypeDescriptor) row.getColumn(SYSSEQUENCES_SEQUENCEDATATYPE).getObject();
    DataTypeDescriptor dataTypeServices = DataTypeDescriptor.getType(catalogType);
    col = row.getColumn(SYSSEQUENCES_CURRENT_VALUE);
    if (col.isNull()) {
        currentValue = null;
    } else {
        currentValue = col.getLong();
    }
    col = row.getColumn(SYSSEQUENCES_START_VALUE);
    startValue = col.getLong();
    col = row.getColumn(SYSSEQUENCES_MINIMUM_VALUE);
    minimumValue = col.getLong();
    col = row.getColumn(SYSSEQUENCES_MAXIMUM_VALUE);
    maximumValue = col.getLong();
    col = row.getColumn(SYSSEQUENCES_INCREMENT);
    increment = col.getLong();
    col = row.getColumn(SYSSEQUENCES_CYCLE_OPTION);
    cycleOption = col.getString();
    descriptor = ddg.newSequenceDescriptor(dd.getSchemaDescriptor(suuid, null), ouuid, sequenceName, dataTypeServices, currentValue, startValue, minimumValue, maximumValue, increment, cycleOption.equals("Y") ? true : false);
    return descriptor;
}
Also used : DataDescriptorGenerator(org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator) TypeDescriptor(org.apache.derby.catalog.TypeDescriptor) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) SequenceDescriptor(org.apache.derby.iapi.sql.dictionary.SequenceDescriptor) UUID(org.apache.derby.catalog.UUID)

Example 79 with DataTypeDescriptor

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

the class DB2LengthOperatorNode method bindExpression.

/**
 * Bind this operator
 *
 * @param fromList			The query's FROM list
 * @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 {
    bindOperand(fromList, subqueryList, aggregates);
    // This operator is not allowed on XML types.
    TypeId operandType = operand.getTypeId();
    if (operandType.isXMLTypeId()) {
        throw StandardException.newException(SQLState.LANG_UNARY_FUNCTION_BAD_TYPE, getOperatorString(), operandType.getSQLTypeName());
    }
    setType(new DataTypeDescriptor(TypeId.getBuiltInTypeId(Types.INTEGER), operand.getTypeServices().isNullable()));
    return this;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Example 80 with DataTypeDescriptor

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

the class SYSSTATEMENTSRowFactory method makeSYSSTATEMENTSrow.

// ///////////////////////////////////////////////////////////////////////////
// 
// METHODS
// 
// ///////////////////////////////////////////////////////////////////////////
/**
 * Make a SYSSTATEMENTS row.
 * <p>
 * <B>WARNING</B>: When empty row is true, this method takes
 * a snapshot of the SPSD and creates a row.  It is imperative
 * that that row remain consistent with the descriptor (the
 * valid and StorablePreparedStatement fields must be in sync).
 * If this row is to be written out and valid is true, then
 * this call and the insert should be synchronized on the
 * SPSD. This method has <B>NO</B> synchronization.
 *
 * @param compileMe			passed into SPSDescriptorImpl.getPreparedStatement().
 *							if true, we (re)compile the stmt
 * @param spsDescriptor		In-memory tuple to be converted to a disk row.
 *
 * @return	Row suitable for inserting into SYSSTATEMENTS.
 *
 * @exception   StandardException thrown on failure
 */
public ExecRow makeSYSSTATEMENTSrow(boolean compileMe, SPSDescriptor spsDescriptor) throws StandardException {
    DataTypeDescriptor dtd;
    ExecRow row;
    DataValueDescriptor col;
    String name = null;
    UUID uuid = null;
    String uuidStr = null;
    // schema
    String suuidStr = null;
    // compilation schema
    String compUuidStr = null;
    String text = null;
    String usingText = null;
    ExecPreparedStatement preparedStatement = null;
    String typeStr = null;
    boolean valid = true;
    Timestamp time = null;
    boolean initiallyCompilable = true;
    if (spsDescriptor != null) {
        name = spsDescriptor.getName();
        uuid = spsDescriptor.getUUID();
        suuidStr = spsDescriptor.getSchemaDescriptor().getUUID().toString();
        uuidStr = uuid.toString();
        text = spsDescriptor.getText();
        valid = spsDescriptor.isValid();
        time = spsDescriptor.getCompileTime();
        typeStr = spsDescriptor.getTypeAsString();
        initiallyCompilable = spsDescriptor.initiallyCompilable();
        preparedStatement = spsDescriptor.getPreparedStatement(compileMe);
        compUuidStr = (spsDescriptor.getCompSchemaId() != null) ? spsDescriptor.getCompSchemaId().toString() : null;
        usingText = spsDescriptor.getUsingText();
    }
    /* Build the row to insert */
    row = getExecutionFactory().getValueRow(SYSSTATEMENTS_COLUMN_COUNT);
    /* 1st column is STMTID */
    row.setColumn(1, new SQLChar(uuidStr));
    /* 2nd column is STMTNAME */
    row.setColumn(2, new SQLVarchar(name));
    /* 3rd column is SCHEMAID */
    row.setColumn(3, new SQLChar(suuidStr));
    /* 4th column is TYPE */
    row.setColumn(4, new SQLChar(typeStr));
    /* 5th column is VALID */
    row.setColumn(5, new SQLBoolean(valid));
    /* 6th column is TEXT */
    row.setColumn(6, dvf.getLongvarcharDataValue(text));
    /* 7th column is LASTCOMPILED */
    row.setColumn(7, new SQLTimestamp(time));
    /* 8th column is COMPILATIONSCHEMAID */
    row.setColumn(8, new SQLChar(compUuidStr));
    /* 9th column is USINGTEXT */
    row.setColumn(9, dvf.getLongvarcharDataValue(usingText));
    /* 
		** 10th column is CONSTANTSTATE
		**
		** CONSTANTSTATE is really a formatable StorablePreparedStatement.
		*/
    row.setColumn(10, new UserType(preparedStatement));
    /* 11th column is INITIALLY_COMPILABLE */
    row.setColumn(11, new SQLBoolean(initiallyCompilable));
    return row;
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) SQLVarchar(org.apache.derby.iapi.types.SQLVarchar) Timestamp(java.sql.Timestamp) ExecPreparedStatement(org.apache.derby.iapi.sql.execute.ExecPreparedStatement) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) UUID(org.apache.derby.catalog.UUID) UserType(org.apache.derby.iapi.types.UserType) SQLBoolean(org.apache.derby.iapi.types.SQLBoolean)

Aggregations

DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)99 TypeId (org.apache.derby.iapi.types.TypeId)32 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)14 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)14 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)9 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)8 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)8 TypeDescriptor (org.apache.derby.catalog.TypeDescriptor)7 UUID (org.apache.derby.catalog.UUID)5 ClassFactory (org.apache.derby.iapi.services.loader.ClassFactory)5 ResultColumnDescriptor (org.apache.derby.iapi.sql.ResultColumnDescriptor)5 TypeCompiler (org.apache.derby.iapi.sql.compile.TypeCompiler)5 StandardException (org.apache.derby.shared.common.error.StandardException)5 Properties (java.util.Properties)4 UserDefinedTypeIdImpl (org.apache.derby.catalog.types.UserDefinedTypeIdImpl)4 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)4 AliasDescriptor (org.apache.derby.iapi.sql.dictionary.AliasDescriptor)3 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)3 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)3 ExecIndexRow (org.apache.derby.iapi.sql.execute.ExecIndexRow)3