Search in sources :

Example 11 with TypeDescriptor

use of org.apache.derby.catalog.TypeDescriptor in project derby by apache.

the class StaticMethodCallNode method bindExpressionMinion.

private JavaValueNode bindExpressionMinion(FromList fromList, SubqueryList subqueryList, List<AggregateNode> aggregates) throws StandardException {
    bindParameters(fromList, subqueryList, aggregates);
    /* If javaClassName is null then we assume that the current methodName
		 * is an alias and we must go to sysmethods to
		 * get the real method and java class names for this alias.
		 */
    if (javaClassName == null) {
        CompilerContext cc = getCompilerContext();
        // look for a routine
        String schemaName = procedureName.getSchemaName();
        boolean noSchema = schemaName == null;
        SchemaDescriptor sd = getSchemaDescriptor(schemaName, schemaName != null);
        // The field methodName is used by resolveRoutine and
        // is set to the name of the routine (procedureName.getTableName()).
        resolveRoutine(fromList, subqueryList, aggregates, sd, noSchema);
        if ((ad != null) && (ad.getAliasType() == AliasInfo.ALIAS_TYPE_AGGREGATE_AS_CHAR)) {
            resolvedAggregate = new AggregateNode(((SQLToJavaValueNode) methodParms[0]).getSQLValueNode(), new UserAggregateDefinition(ad), procedureName, false, ad.getJavaClassName(), getContextManager());
            // Propagate tags used to flag nodes which need privilege checks. See DERBY-6429.
            resolvedAggregate.copyTagsFrom(this);
            // GROUP BY clause. That is not allowed.
            if (appearsInGroupBy) {
                throw StandardException.newException(SQLState.LANG_AGGREGATE_IN_GROUPBY_LIST);
            }
            return this;
        }
        SchemaDescriptor savedSd = sd;
        if (ad == null && noSchema && !forCallStatement) {
            // Resolve to a built-in SYSFUN function but only
            // if this is a function call and the call
            // was not qualified. E.g. COS(angle). The
            // SYSFUN functions are not in SYSALIASES but
            // an in-memory table, set up in DataDictioanryImpl.
            sd = getSchemaDescriptor("SYSFUN", true);
            resolveRoutine(fromList, subqueryList, aggregates, sd, noSchema);
        }
        if (ad == null) {
            // DERBY-2927. Check if a procedure is being used as a
            // function, or vice versa.
            sd = savedSd;
            if (!forCallStatement) {
                // Procedure as function. We have JDBC escape syntax which
                // may entice users to try that:
                // "{? = CALL <proc>}"
                // 
                // but we don't currently support it (it's not std SQL
                // either). By resolving it as a procedure we can give a
                // better error message.
                // 
                // Note that with the above escape syntax one *can* CALL a
                // function, though:
                // "{? = CALL <func>}"
                // 
                // but such cases have already been resolved above.
                // temporarily: resolve
                forCallStatement = true;
                // as procedure
                resolveRoutine(fromList, subqueryList, aggregates, sd, noSchema);
                // restore it
                forCallStatement = false;
                if (ad != null) {
                    throw StandardException.newException(SQLState.LANG_PROC_USED_AS_FUNCTION, procedureName);
                }
            } else {
                // Maybe a function is being CALLed ?
                // temporarily: resolve
                forCallStatement = false;
                // as function
                resolveRoutine(fromList, subqueryList, aggregates, sd, noSchema);
                // restore it
                forCallStatement = true;
                if (ad != null) {
                    throw StandardException.newException(SQLState.LANG_FUNCTION_USED_AS_PROC, procedureName);
                }
            }
        }
        /* Throw exception if no routine found */
        if (ad == null) {
            throw StandardException.newException(SQLState.LANG_NO_SUCH_METHOD_ALIAS, procedureName);
        }
        if (noSchema) {
            // If no schema was specified, register where we found the
            // routine.
            procedureName.setSchemaName(sd.getSchemaName());
        }
        if (!routineInfo.isDeterministic()) {
            checkReliability(getMethodName(), CompilerContext.NON_DETERMINISTIC_ILLEGAL);
        }
        if (permitsSQL(routineInfo)) {
            checkReliability(getMethodName(), CompilerContext.SQL_IN_ROUTINES_ILLEGAL);
        }
        /* Query is dependent on the AliasDescriptor */
        cc.createDependency(ad);
        methodName = ad.getAliasInfo().getMethodName();
        javaClassName = ad.getJavaClassName();
        // A special exception is made for the optional tools methods.
        if (javaClassName.startsWith("org.apache.derby.") && !javaClassName.startsWith("org.apache.derby.impl.tools.optional.") && !javaClassName.startsWith("org.apache.derby.optional.lucene.") && !javaClassName.startsWith("org.apache.derby.optional.json.") && !javaClassName.startsWith("org.apache.derby.optional.api.") && !javaClassName.startsWith("org.apache.derby.optional.dump.") && !javaClassName.startsWith("org.apache.derby.vti.")) {
            if (!sd.isSystemSchema())
                throw StandardException.newException(SQLState.LANG_TYPE_DOESNT_EXIST2, (Throwable) null, javaClassName);
        }
    }
    verifyClassExist(javaClassName);
    /* Resolve the method call */
    resolveMethodCall(javaClassName, true);
    if (isPrivilegeCollectionRequired())
        getCompilerContext().addRequiredRoutinePriv(ad);
    // return type, then we need to push a CAST node.
    if (routineInfo != null) {
        if (methodParms != null)
            optimizeDomainValueConversion();
        TypeDescriptor returnType = routineInfo.getReturnType();
        // create type dependency if return type is an ANSI UDT
        if (returnType != null) {
            createTypeDependency(DataTypeDescriptor.getType(returnType));
        }
        if (returnType != null && !returnType.isRowMultiSet() && !returnType.isUserDefinedType()) {
            TypeId returnTypeId = TypeId.getBuiltInTypeId(returnType.getJDBCTypeId());
            if (returnTypeId.variableLength()) {
                // Cast the return using a cast node, but have to go
                // into the SQL domain, and back to the Java domain.
                DataTypeDescriptor returnValueDtd = new DataTypeDescriptor(returnTypeId, returnType.getPrecision(), returnType.getScale(), returnType.isNullable(), returnType.getMaximumWidth());
                ValueNode returnValueToSQL = new JavaToSQLValueNode(this, getContextManager());
                ValueNode returnValueCastNode = new CastNode(returnValueToSQL, returnValueDtd, getContextManager());
                // DERBY-2972  Match the collation of the RoutineAliasInfo
                returnValueCastNode.setCollationInfo(returnType.getCollationType(), StringDataValue.COLLATION_DERIVATION_IMPLICIT);
                JavaValueNode returnValueToJava = new SQLToJavaValueNode(returnValueCastNode, getContextManager());
                returnValueToJava.setCollationType(returnType.getCollationType());
                return returnValueToJava.bindExpression(fromList, subqueryList, aggregates);
            }
        }
    }
    return this;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) SchemaDescriptor(org.apache.derby.iapi.sql.dictionary.SchemaDescriptor) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) TypeDescriptor(org.apache.derby.catalog.TypeDescriptor)

Example 12 with TypeDescriptor

use of org.apache.derby.catalog.TypeDescriptor in project derby by apache.

the class DDLConstantAction method adjustUDTDependencies.

/**
 * Add and drop dependencies of a routine on UDTs.
 *
 * @param lcc Interpreter's state variable for this session.
 * @param dd Metadata
 * @param ad Alias descriptor for the routine
 * @param adding True if we are adding dependencies, false if we're dropping them
 */
protected void adjustUDTDependencies(LanguageConnectionContext lcc, DataDictionary dd, AliasDescriptor ad, boolean adding) throws StandardException {
    RoutineAliasInfo routineInfo = null;
    AggregateAliasInfo aggInfo = null;
    // nothing to do if this is not a routine
    switch(ad.getAliasType()) {
        case AliasInfo.ALIAS_TYPE_AGGREGATE_AS_CHAR:
            aggInfo = (AggregateAliasInfo) ad.getAliasInfo();
            break;
        case AliasInfo.ALIAS_TYPE_PROCEDURE_AS_CHAR:
        case AliasInfo.ALIAS_TYPE_FUNCTION_AS_CHAR:
            routineInfo = (RoutineAliasInfo) ad.getAliasInfo();
            break;
        default:
            return;
    }
    TransactionController tc = lcc.getTransactionExecute();
    HashMap<String, AliasDescriptor> addUdtMap = new HashMap<String, AliasDescriptor>();
    HashMap<String, AliasDescriptor> dropUdtMap = new HashMap<String, AliasDescriptor>();
    HashMap<String, AliasDescriptor> udtMap = adding ? addUdtMap : dropUdtMap;
    TypeDescriptor rawReturnType = aggInfo != null ? aggInfo.getReturnType() : routineInfo.getReturnType();
    if (rawReturnType != null) {
        AliasDescriptor returnTypeAD = dd.getAliasDescriptorForUDT(tc, DataTypeDescriptor.getType(rawReturnType));
        if (returnTypeAD != null) {
            udtMap.put(returnTypeAD.getObjectID().toString(), returnTypeAD);
        }
    }
    // table functions can have udt columns. track those dependencies.
    if ((rawReturnType != null) && rawReturnType.isRowMultiSet()) {
        TypeDescriptor[] columnTypes = rawReturnType.getRowTypes();
        int columnCount = columnTypes.length;
        for (int i = 0; i < columnCount; i++) {
            AliasDescriptor columnTypeAD = dd.getAliasDescriptorForUDT(tc, DataTypeDescriptor.getType(columnTypes[i]));
            if (columnTypeAD != null) {
                udtMap.put(columnTypeAD.getObjectID().toString(), columnTypeAD);
            }
        }
    }
    TypeDescriptor[] paramTypes = aggInfo != null ? new TypeDescriptor[] { aggInfo.getForType() } : routineInfo.getParameterTypes();
    if (paramTypes != null) {
        int paramCount = paramTypes.length;
        for (int i = 0; i < paramCount; i++) {
            AliasDescriptor paramType = dd.getAliasDescriptorForUDT(tc, DataTypeDescriptor.getType(paramTypes[i]));
            if (paramType != null) {
                udtMap.put(paramType.getObjectID().toString(), paramType);
            }
        }
    }
    adjustUDTDependencies(lcc, dd, ad, addUdtMap, dropUdtMap);
}
Also used : RoutineAliasInfo(org.apache.derby.catalog.types.RoutineAliasInfo) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) TypeDescriptor(org.apache.derby.catalog.TypeDescriptor) HashMap(java.util.HashMap) AliasDescriptor(org.apache.derby.iapi.sql.dictionary.AliasDescriptor) AggregateAliasInfo(org.apache.derby.catalog.types.AggregateAliasInfo) TransactionController(org.apache.derby.iapi.store.access.TransactionController)

Example 13 with TypeDescriptor

use of org.apache.derby.catalog.TypeDescriptor in project derby by apache.

the class RawDBReader method makeTableSignature.

/**
 * Make the signature of a table from its column names and types
 */
private String makeTableSignature(// for serializable types
String controlSchema, ArrayList<String> columnNames, ArrayList<TypeDescriptor> columnTypes) {
    StringBuilder buffer = new StringBuilder();
    buffer.append("( ");
    for (int i = 0; i < columnNames.size(); i++) {
        if (i > 0) {
            buffer.append(", ");
        }
        buffer.append(columnNames.get(i) + " ");
        TypeDescriptor type = columnTypes.get(i);
        if (type.isUserDefinedType()) {
            buffer.append(controlSchema + ".serializable");
        } else {
            buffer.append(type.getSQLstring());
        }
    }
    buffer.append(" )");
    return buffer.toString();
}
Also used : TypeDescriptor(org.apache.derby.catalog.TypeDescriptor)

Example 14 with TypeDescriptor

use of org.apache.derby.catalog.TypeDescriptor in project derby by apache.

the class Changes10_6 method testCLOBGETSUBSTRING.

/**
 * Make sure that SYSIBM.CLOBGETSUBSTRING has the correct return value.
 * See https://issues.apache.org/jira/browse/DERBY-4214
 */
public void testCLOBGETSUBSTRING() throws Exception {
    Version initialVersion = new Version(getOldMajor(), getOldMinor(), 0, 0);
    Version firstVersionHavingThisFunction = new Version(10, 3, 0, 0);
    Version firstVersionHavingCorrectReturnType = new Version(10, 5, 0, 0);
    int wrongLength = 32672;
    int correctLength = 10890;
    int actualJdbcType;
    int actualLength;
    Object returnType;
    boolean hasFunction = initialVersion.compareTo(firstVersionHavingThisFunction) >= 0;
    boolean hasCorrectReturnType = initialVersion.compareTo(firstVersionHavingCorrectReturnType) >= 0;
    Statement s = createStatement();
    ResultSet rs = s.executeQuery("select a.aliasinfo\n" + "from sys.sysschemas s, sys.sysaliases a\n" + "where s.schemaid = a.schemaid\n" + "and s.schemaname = 'SYSIBM'\n" + "and alias = 'CLOBGETSUBSTRING'\n");
    rs.next();
    switch(getPhase()) {
        case PH_CREATE:
        case PH_SOFT_UPGRADE:
        case PH_POST_SOFT_UPGRADE:
            if (!hasFunction) {
                break;
            }
            returnType = getTypeDescriptor(rs.getObject(1));
            actualJdbcType = getJDBCTypeId(returnType);
            actualLength = getMaximumWidth(returnType);
            int expectedLength = hasCorrectReturnType ? correctLength : wrongLength;
            assertEquals(java.sql.Types.VARCHAR, actualJdbcType);
            assertEquals(expectedLength, actualLength);
            break;
        case PH_HARD_UPGRADE:
            RoutineAliasInfo rai = (RoutineAliasInfo) rs.getObject(1);
            TypeDescriptor td = (TypeDescriptor) rai.getReturnType();
            assertEquals(java.sql.Types.VARCHAR, td.getJDBCTypeId());
            assertEquals(correctLength, td.getMaximumWidth());
            break;
    }
    rs.close();
    s.close();
}
Also used : RoutineAliasInfo(org.apache.derby.catalog.types.RoutineAliasInfo) TypeDescriptor(org.apache.derby.catalog.TypeDescriptor) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet)

Example 15 with TypeDescriptor

use of org.apache.derby.catalog.TypeDescriptor in project derby by apache.

the class DataFileVTI method getTypeSignature.

private DataTypeDescriptor[] getTypeSignature(Connection conn, String tableSignature) throws Exception {
    String createTable = "create table " + DUMMY_TABLE_NAME + tableSignature;
    String dropTable = "drop table " + DUMMY_TABLE_NAME;
    try {
        conn.prepareStatement(createTable).execute();
    } catch (SQLException se) {
        throw new Exception("Illegal table signature: " + tableSignature, se);
    }
    String select = "select c.columndatatype, c.columnnumber\n" + "from sys.syscolumns c, sys.systables t\n" + "where c.referenceid = t.tableid\n" + "and t.tablename = ?\n" + "order by c.columnnumber";
    PreparedStatement ps = conn.prepareStatement(select);
    ps.setString(1, DUMMY_TABLE_NAME.toUpperCase());
    ResultSet rs = ps.executeQuery();
    ArrayList<DataTypeDescriptor> list = new ArrayList<DataTypeDescriptor>();
    while (rs.next()) {
        list.add(DataTypeDescriptor.getType((TypeDescriptor) rs.getObject(1)));
    }
    rs.close();
    ps.close();
    DataTypeDescriptor[] result = new DataTypeDescriptor[list.size()];
    list.toArray(result);
    conn.prepareStatement(dropTable).execute();
    return result;
}
Also used : TypeDescriptor(org.apache.derby.catalog.TypeDescriptor) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) SQLException(java.sql.SQLException) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) EOFException(java.io.EOFException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Aggregations

TypeDescriptor (org.apache.derby.catalog.TypeDescriptor)22 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)14 UUID (org.apache.derby.catalog.UUID)6 RoutineAliasInfo (org.apache.derby.catalog.types.RoutineAliasInfo)6 AliasDescriptor (org.apache.derby.iapi.sql.dictionary.AliasDescriptor)5 ResultSet (java.sql.ResultSet)3 ArrayList (java.util.ArrayList)3 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)3 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)3 IOException (java.io.IOException)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 DataDescriptorGenerator (org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator)2 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)2 SequenceDescriptor (org.apache.derby.iapi.sql.dictionary.SequenceDescriptor)2 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)2 SQLChar (org.apache.derby.iapi.types.SQLChar)2 SQLLongint (org.apache.derby.iapi.types.SQLLongint)2 SQLVarchar (org.apache.derby.iapi.types.SQLVarchar)2 TypeId (org.apache.derby.iapi.types.TypeId)2