Search in sources :

Example 16 with TypeDescriptor

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

the class RawDBReader method createViews.

/**
 * Create table functions and views on corrupt user tables. These objects
 * are created in the healthy database. Write the recovery
 * script.
 */
private void createViews(Connection conn, String recoveryScriptName, String controlSchema, String schemaPrefix, String corruptDBLocation, String encryptionAttributes, String dbo, String dboPassword) throws SQLException {
    File recoveryScript = new File(recoveryScriptName);
    PrintWriter scriptWriter = null;
    try {
        scriptWriter = new PrintWriter(recoveryScript);
    } catch (Exception e) {
        throw wrap(e);
    }
    String localDBName = ((EmbedConnection) conn).getDBName();
    scriptWriter.println("connect 'jdbc:derby:" + localDBName + "';\n");
    PreparedStatement ps = prepareStatement(conn, "select s.schemaName, t.tableName, g.conglomerateNumber, c.columnName, c.columnNumber, c.columnDatatype\n" + "from " + controlSchema + ".sysschemas s,\n" + controlSchema + ".systables t,\n" + controlSchema + ".sysconglomerates g,\n" + controlSchema + ".syscolumns c\n" + "where s.schemaName not like 'SYS%' and schemaName != 'NULLID' and schemaName != 'SQLJ'\n" + "and s.schemaID = t.schemaID\n" + "and t.tableID = g.tableID and not g.isindex\n" + "and t.tableID = c.referenceID\n" + "order by s.schemaName, t.tableName, c.columnNumber");
    ResultSet rs = ps.executeQuery();
    ArrayList<String> columnNames = new ArrayList<String>();
    ArrayList<TypeDescriptor> columnTypes = new ArrayList<TypeDescriptor>();
    String corruptSchemaName = null;
    String corruptTableName = null;
    String schemaName = null;
    String tableName = null;
    long conglomerateNumber = -1L;
    while (rs.next()) {
        int col = 1;
        String currentCorruptSchemaName = rs.getString(col++);
        String currentCorruptTableName = rs.getString(col++);
        if (!currentCorruptSchemaName.equals(corruptSchemaName)) {
            scriptWriter.println("create schema " + IdUtil.normalToDelimited(currentCorruptSchemaName) + ";\n");
        }
        String newSchemaName = makeSchemaName(schemaPrefix, currentCorruptSchemaName);
        String newTableName = IdUtil.normalToDelimited(currentCorruptTableName);
        if (schemaName != null) {
            if (!schemaName.equals(newSchemaName) || !tableName.equals(newTableName)) {
                createView(conn, scriptWriter, controlSchema, corruptSchemaName, corruptTableName, schemaName, tableName, conglomerateNumber, columnNames, columnTypes, corruptDBLocation, encryptionAttributes, dbo, dboPassword);
                columnNames.clear();
                columnTypes.clear();
            }
        }
        corruptSchemaName = currentCorruptSchemaName;
        corruptTableName = currentCorruptTableName;
        schemaName = newSchemaName;
        tableName = newTableName;
        conglomerateNumber = rs.getLong(col++);
        columnNames.add(normalizeColumnName(rs.getString(col++)));
        // only need the column number to order the results
        col++;
        columnTypes.add((TypeDescriptor) rs.getObject(col++));
    }
    // create last view
    if (schemaName != null) {
        createView(conn, scriptWriter, controlSchema, corruptSchemaName, corruptTableName, schemaName, tableName, conglomerateNumber, columnNames, columnTypes, corruptDBLocation, encryptionAttributes, dbo, dboPassword);
    }
    rs.close();
    ps.close();
    scriptWriter.flush();
    scriptWriter.close();
}
Also used : ArrayList(java.util.ArrayList) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) SQLException(java.sql.SQLException) TypeDescriptor(org.apache.derby.catalog.TypeDescriptor) ResultSet(java.sql.ResultSet) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 17 with TypeDescriptor

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

the class StaticMethodCallNode method resolveRoutine.

/**
 * Resolve a routine. Obtain a list of routines from the data dictionary
 * of the correct type (functions or procedures) and name.
 * Pick the best routine from the list. Currently only a single routine
 * with a given type and name is allowed, thus if changes are made to
 * support overloaded routines, careful code inspection and testing will
 * be required.
 */
private void resolveRoutine(FromList fromList, SubqueryList subqueryList, List<AggregateNode> aggregates, SchemaDescriptor sd, boolean noSchema) throws StandardException {
    if (sd.getUUID() != null) {
        List<AliasDescriptor> list = getDataDictionary().getRoutineList(sd.getUUID().toString(), methodName, forCallStatement ? AliasInfo.ALIAS_NAME_SPACE_PROCEDURE_AS_CHAR : AliasInfo.ALIAS_NAME_SPACE_FUNCTION_AS_CHAR);
        for (int i = list.size() - 1; i >= 0; i--) {
            AliasDescriptor proc = list.get(i);
            RoutineAliasInfo rai = (RoutineAliasInfo) proc.getAliasInfo();
            int parameterCount = rai.getParameterCount();
            boolean hasVarargs = rai.hasVarargs();
            if (hasVarargs) {
                // for the trailing varargs argument
                if (methodParms.length < (parameterCount - 1)) {
                    continue;
                }
            } else if (parameterCount != methodParms.length) {
                continue;
            }
            // pre-form the method signature. If it is a dynamic result set procedure
            // then we need to add in the ResultSet array
            TypeDescriptor[] parameterTypes = rai.getParameterTypes();
            int sigParameterCount = parameterCount;
            if (rai.getMaxDynamicResultSets() > 0) {
                sigParameterCount++;
            }
            signature = new JSQLType[sigParameterCount];
            for (int p = 0; p < parameterCount; p++) {
                // find the declared type.
                TypeDescriptor td = parameterTypes[p];
                TypeId typeId = TypeId.getTypeId(td);
                TypeId parameterTypeId = typeId;
                // if it's an OUT or INOUT parameter we need an array.
                int parameterMode = rai.getParameterModes()[getRoutineArgIdx(rai, p)];
                if (parameterMode != (ParameterMetaData.parameterModeIn)) {
                    String arrayType;
                    switch(typeId.getJDBCTypeId()) {
                        case java.sql.Types.BOOLEAN:
                        case java.sql.Types.SMALLINT:
                        case java.sql.Types.INTEGER:
                        case java.sql.Types.BIGINT:
                        case java.sql.Types.REAL:
                        case java.sql.Types.DOUBLE:
                            arrayType = getTypeCompiler(typeId).getCorrespondingPrimitiveTypeName().concat("[]");
                            break;
                        default:
                            arrayType = typeId.getCorrespondingJavaTypeName().concat("[]");
                            break;
                    }
                    typeId = TypeId.getUserDefinedTypeId(arrayType);
                }
                // this is the type descriptor of the require method parameter
                DataTypeDescriptor methoddtd = new DataTypeDescriptor(typeId, td.getPrecision(), td.getScale(), td.isNullable(), td.getMaximumWidth());
                signature[p] = new JSQLType(methoddtd);
                // this is the SQL type of the procedure parameter.
                DataTypeDescriptor paramdtd = new DataTypeDescriptor(parameterTypeId, td.getPrecision(), td.getScale(), td.isNullable(), td.getMaximumWidth());
                // if this is the last argument of a varargs routine...
                if (hasVarargs && (p == parameterCount - 1)) {
                    // 
                    for (int idx = p; idx < methodParms.length; idx++) {
                        coerceMethodParameter(fromList, subqueryList, aggregates, rai, methodParms.length, paramdtd, parameterTypeId, parameterMode, idx);
                    }
                } else // NOT the last argument of a varargs routine
                {
                    coerceMethodParameter(fromList, subqueryList, aggregates, rai, methodParms.length, paramdtd, parameterTypeId, parameterMode, p);
                }
            }
            if (sigParameterCount != parameterCount) {
                DataTypeDescriptor dtd = new DataTypeDescriptor(TypeId.getUserDefinedTypeId("java.sql.ResultSet[]"), 0, 0, false, -1);
                signature[parameterCount] = new JSQLType(dtd);
            }
            this.routineInfo = rai;
            ad = proc;
            // SQL, note that we are in system code.
            if (sd.isSystemSchema() && (routineInfo.getReturnType() == null) && routineInfo.getSQLAllowed() != RoutineAliasInfo.NO_SQL) {
                isSystemCode = true;
            }
            routineDefiner = sd.getAuthorizationId();
            break;
        }
    }
    if ((ad == null) && (methodParms.length == 1)) {
        ad = AggregateNode.resolveAggregate(getDataDictionary(), sd, methodName, noSchema);
    }
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) RoutineAliasInfo(org.apache.derby.catalog.types.RoutineAliasInfo) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) TypeDescriptor(org.apache.derby.catalog.TypeDescriptor) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) JSQLType(org.apache.derby.iapi.types.JSQLType) AliasDescriptor(org.apache.derby.iapi.sql.dictionary.AliasDescriptor)

Example 18 with TypeDescriptor

use of org.apache.derby.catalog.TypeDescriptor 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 19 with TypeDescriptor

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

the class DataTypeDescriptor method getRowMultiSetCollation.

/**
 * For a row multi set type return an identical type
 * with the collation type changed. Note that since
 * row types are only ever catalog types the
 * derivation is not used (since derivation is a property
 * of runtime types).
 * <BR>
 *
 * @param collationType
 * @return this  will be returned if no changes are required (e.g.
 * no string types or collation is already correct), otherwise a
 * new instance is returned (leaving this unchanged).
 */
private static TypeDescriptor getRowMultiSetCollation(TypeDescriptor catalogType, int collationType) {
    TypeDescriptor[] rowTypes = catalogType.getRowTypes();
    TypeDescriptor[] newTypes = null;
    for (int t = 0; t < rowTypes.length; t++) {
        TypeDescriptor newType = DataTypeDescriptor.getCatalogType(rowTypes[t], collationType);
        // Is it the exact same as the old type.
        if (newType == rowTypes[t])
            continue;
        if (newTypes == null) {
            // First different type, simply create a new
            // array and copy all the old types across.
            // Any new type will overwrite the old type.
            newTypes = new TypeDescriptor[rowTypes.length];
            System.arraycopy(rowTypes, 0, newTypes, 0, rowTypes.length);
        }
        newTypes[t] = newType;
    }
    // If no change then we continue to use this instance.
    if (newTypes == null)
        return catalogType;
    return DataTypeDescriptor.getRowMultiSet(catalogType.getRowColumnNames(), newTypes);
}
Also used : TypeDescriptor(org.apache.derby.catalog.TypeDescriptor)

Example 20 with TypeDescriptor

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

the class FromVTI method createResultColumnsForTableFunction.

/**
 * Add result columns for a Derby-style Table Function
 */
private void createResultColumnsForTableFunction(TypeDescriptor td) throws StandardException {
    String[] columnNames = td.getRowColumnNames();
    TypeDescriptor[] types = td.getRowTypes();
    for (int i = 0; i < columnNames.length; i++) {
        String columnName = columnNames[i];
        DataTypeDescriptor dtd = DataTypeDescriptor.getType(types[i]);
        ResultColumn rc = getResultColumns().addColumn(exposedName, columnName, dtd);
        // 
        // Stuff a column descriptor into the ResultColumn. We do this so that
        // getColumnPosition() will return the column position within the
        // table function's shape. Later on, projection may remove columns
        // from the ResultColumnList. We don't want getColumnPosition() to say
        // that the column position is the index into the abbreviated ResultColumnList.
        // See DERBY-6040.
        // 
        ColumnDescriptor coldesc = new ColumnDescriptor(columnName, i + 1, dtd, (DataValueDescriptor) null, (DefaultInfo) null, (UUID) null, (UUID) null, 0L, 0L, 0L, false);
        rc.setColumnDescriptor(null, coldesc);
    }
}
Also used : TypeDescriptor(org.apache.derby.catalog.TypeDescriptor) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)

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