Search in sources :

Example 1 with PLSQLCursorType

use of org.eclipse.persistence.tools.oracleddl.metadata.PLSQLCursorType in project eclipselink by eclipse-ee4j.

the class OracleHelper method buildProcedureOperation.

/**
 * Builds query operations for a given ProcedureOperationModel.
 */
@Override
public void buildProcedureOperation(ProcedureOperationModel procedureOperationModel) {
    for (ProcedureType storedProcedure : procedureOperationModel.getDbStoredProcedures()) {
        boolean hasComplexArgs = hasComplexArgs(storedProcedure);
        QueryOperation qo = new QueryOperation();
        qo.setName(getNameForQueryOperation(procedureOperationModel, storedProcedure));
        String qualifiedProcName = getQualifiedProcedureName(procedureOperationModel, storedProcedure);
        dbwsBuilder.logMessage(FINEST, BUILDING_QUERYOP_FOR + qualifiedProcName);
        QueryHandler qh = null;
        // before assigning queryHandler, check for named query in OR project
        List<DatabaseQuery> queries = dbwsBuilder.getOrProject().getQueries();
        if (queries.size() > 0) {
            for (DatabaseQuery q : queries) {
                if (q.getName().equals(qo.getName())) {
                    qh = new NamedQueryHandler();
                    ((NamedQueryHandler) qh).setName(qo.getName());
                }
            }
        }
        if (qh == null) {
            if (storedProcedure.isFunctionType()) {
                qh = new StoredFunctionQueryHandler();
            } else {
                qh = new StoredProcedureQueryHandler();
            }
            ((StoredProcedureQueryHandler) qh).setName(qualifiedProcName);
        }
        qo.setQueryHandler(qh);
        String returnType = procedureOperationModel.getReturnType();
        boolean isCollection = procedureOperationModel.isCollection();
        boolean isSimpleXMLFormat = procedureOperationModel.isSimpleXMLFormat();
        Result result = null;
        /**
         * For multiple OUT args as well as a stored function with OUT args, we want
         * the result to be a collection and the type to be "xsd:any".  We will
         * force SimpleXMLFormat for now as well.
         */
        int outArgCount = 0;
        for (ArgumentType argument : storedProcedure.getArguments()) {
            ArgumentTypeDirection argDirection = argument.getDirection();
            if (argDirection == OUT) {
                outArgCount++;
            }
        }
        if (outArgCount > 1 || (outArgCount > 0 && storedProcedure.isFunctionType())) {
            isCollection = true;
            isSimpleXMLFormat = true;
            result = new CollectionResult();
            result.setType(ANY_QNAME);
        } else {
            if (storedProcedure.isFunctionType()) {
                ArgumentType returnArg = ((FunctionType) storedProcedure).getReturnArgument();
                result = buildResultForStoredFunction(returnArg, returnType);
                // tags to better represent the PL/SQL record/table/column type
                if (returnArg.getEnclosedType().isPLSQLCursorType()) {
                    customizeSimpleXMLTagNames((PLSQLCursorType) returnArg.getEnclosedType(), procedureOperationModel);
                }
            } else if (hasComplexArgs) {
                if (Util.noOutArguments(storedProcedure)) {
                    result = new Result();
                    // rowcount
                    result.setType(new QName(SCHEMA_URL, INT, SCHEMA_PREFIX));
                }
            } else {
                // if user overrides returnType, assume they're right
                if (returnType != null) {
                    result = new Result();
                    result.setType(buildCustomQName(returnType, dbwsBuilder));
                } else {
                    if (isCollection) {
                        result = new CollectionResult();
                        if (isSimpleXMLFormat) {
                            result.setType(SXF_QNAME_CURSOR);
                        }
                    } else {
                        result = new Result();
                        result.setType(SXF_QNAME);
                    }
                }
            }
        }
        for (ArgumentType arg : storedProcedure.getArguments()) {
            String argName = arg.getArgumentName();
            if (argName != null) {
                QName xmlType = null;
                ProcedureArgument pa = null;
                // for INOUT's
                ProcedureArgument paShadow = null;
                Parameter parm = null;
                ArgumentTypeDirection direction = arg.getDirection();
                if (!hasComplexArgs) {
                    if (arg.getEnclosedType().isPLSQLCursorType()) {
                        PLSQLCursorType cursorType = (PLSQLCursorType) arg.getEnclosedType();
                        if (cursorType.isWeaklyTyped()) {
                            xmlType = buildCustomQName("SYS_REFCURSOR", dbwsBuilder);
                        }
                    } else {
                        xmlType = getXMLTypeFromJDBCType(Util.getJDBCTypeFromTypeName(arg.getTypeName()));
                    }
                } else {
                    // handle PL/SQL records and collections
                    if (arg.getEnclosedType().isPLSQLType()) {
                        String packageName = ((PLSQLType) arg.getEnclosedType()).getParentType().getPackageName();
                        // may need to prepend package name
                        String typeString = (packageName != null && packageName.length() > 0) ? packageName + UNDERSCORE + arg.getTypeName() : arg.getTypeName();
                        // may need to strip off %
                        typeString = typeString.contains(PERCENT) ? typeString.replace(PERCENT, UNDERSCORE) : typeString;
                        xmlType = buildCustomQName(nct.generateSchemaAlias(typeString), dbwsBuilder);
                    } else if (arg.getEnclosedType().isVArrayType() || arg.getEnclosedType().isObjectType() || arg.getEnclosedType().isObjectTableType()) {
                        // handle advanced JDBC types
                        xmlType = buildCustomQName(nct.generateSchemaAlias(arg.getTypeName()), dbwsBuilder);
                    } else {
                        switch(Util.getJDBCTypeFromTypeName(arg.getTypeName())) {
                            case STRUCT:
                            case ARRAY:
                                String typeString = nct.generateSchemaAlias(arg.getTypeName());
                                xmlType = buildCustomQName(typeString, dbwsBuilder);
                                break;
                            default:
                                xmlType = getXMLTypeFromJDBCType(Util.getJDBCTypeFromTypeName(arg.getTypeName()));
                                break;
                        }
                    }
                }
                if (direction == null || direction == IN) {
                    parm = new Parameter();
                    parm.setName(argName);
                    parm.setType(xmlType);
                    // handle optional arg
                    parm.setOptional(arg.optional());
                    pa = new ProcedureArgument();
                    pa.setName(argName);
                    pa.setParameterName(argName);
                    if (qh instanceof StoredProcedureQueryHandler) {
                        ((StoredProcedureQueryHandler) qh).getInArguments().add(pa);
                    }
                } else {
                    // the first OUT/INOUT arg determines singleResult vs. collectionResult
                    pa = new ProcedureOutputArgument();
                    ProcedureOutputArgument pao = (ProcedureOutputArgument) pa;
                    pao.setName(argName);
                    pao.setParameterName(argName);
                    boolean isCursor = arg.isPLSQLCursorType() || arg.getTypeName().contains(CURSOR_STR);
                    // tags to better represent the PL/SQL record/table/column type
                    if (arg.isPLSQLCursorType()) {
                        customizeSimpleXMLTagNames((PLSQLCursorType) arg.getEnclosedType(), procedureOperationModel);
                    }
                    if (isCursor && returnType == null) {
                        // if user overrides returnType, assume they're right
                        pao.setResultType(SXF_QNAME_CURSOR);
                        if (result == null) {
                            result = new CollectionResult();
                            result.setType(SXF_QNAME_CURSOR);
                        }
                    } else {
                        // Hmm, multiple OUT's gonna be a problem - later!
                        if (returnType != null && !isSimpleXMLFormat) {
                            xmlType = qNameFromString(OPEN_PAREN + dbwsBuilder.getTargetNamespace() + CLOSE_PAREN + returnType, dbwsBuilder.getSchema());
                        }
                        if (isCursor) {
                            pao.setResultType(new QName(EMPTY_STRING, CURSOR_OF_STR + returnType));
                            Result newResult = new CollectionResult();
                            newResult.setType(result.getType());
                            result = newResult;
                        } else {
                            pao.setResultType(xmlType);
                        }
                        if (result == null) {
                            if (isCollection) {
                                result = new CollectionResult();
                            } else {
                                result = new Result();
                            }
                            result.setType(xmlType);
                        }
                    }
                    if (direction == INOUT) {
                        parm = new Parameter();
                        parm.setName(argName);
                        parm.setType(xmlType);
                        result.setType(xmlType);
                        if (qh instanceof StoredProcedureQueryHandler) {
                            ((StoredProcedureQueryHandler) qh).getInOutArguments().add(pao);
                        }
                        paShadow = new ProcedureArgument();
                        paShadow.setName(argName);
                        paShadow.setParameterName(argName);
                    } else {
                        // OUT arg
                        if (qh instanceof StoredProcedureQueryHandler) {
                            ((StoredProcedureQueryHandler) qh).getOutArguments().add(pao);
                        }
                    }
                }
                // for XMLType, we want the type code to be 'OPAQUE' (2007)
                if (arg.getEnclosedType() == ScalarDatabaseTypeEnum.XMLTYPE_TYPE) {
                    pa.setJdbcType(getJDBCTypeForTypeName(XMLTYPE_STR));
                }
                if (hasComplexArgs && arg.getEnclosedType().isPLSQLType()) {
                    pa.setComplexTypeName(storedProcedure.getCatalogName() + UNDERSCORE + arg.getTypeName());
                    if (paShadow != null) {
                        paShadow.setComplexTypeName(pa.getComplexTypeName());
                    }
                }
                if (parm != null) {
                    qo.getParameters().add(parm);
                }
            }
        }
        if (procedureOperationModel.getBinaryAttachment()) {
            Attachment attachment = new Attachment();
            attachment.setMimeType(APP_OCTET_STREAM);
            result.setAttachment(attachment);
        }
        // the user may want simpleXMLFormat
        handleSimpleXMLFormat(isSimpleXMLFormat, result, procedureOperationModel);
        qo.setResult(result);
        dbwsBuilder.getXrServiceModel().getOperations().put(qo.getName(), qo);
    }
    finishProcedureOperation();
}
Also used : ProcedureType(org.eclipse.persistence.tools.oracleddl.metadata.ProcedureType) StoredProcedureQueryHandler(org.eclipse.persistence.internal.xr.StoredProcedureQueryHandler) StoredFunctionQueryHandler(org.eclipse.persistence.internal.xr.StoredFunctionQueryHandler) QueryHandler(org.eclipse.persistence.internal.xr.QueryHandler) NamedQueryHandler(org.eclipse.persistence.internal.xr.NamedQueryHandler) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Util.buildCustomQName(org.eclipse.persistence.tools.dbws.Util.buildCustomQName) QName(javax.xml.namespace.QName) FunctionType(org.eclipse.persistence.tools.oracleddl.metadata.FunctionType) StoredProcedureQueryHandler(org.eclipse.persistence.internal.xr.StoredProcedureQueryHandler) PLSQLCursorType(org.eclipse.persistence.tools.oracleddl.metadata.PLSQLCursorType) Attachment(org.eclipse.persistence.internal.xr.Attachment) Util.qNameFromString(org.eclipse.persistence.tools.dbws.Util.qNameFromString) ArgumentType(org.eclipse.persistence.tools.oracleddl.metadata.ArgumentType) ProcedureArgument(org.eclipse.persistence.internal.xr.ProcedureArgument) ProcedureOutputArgument(org.eclipse.persistence.internal.xr.ProcedureOutputArgument) CollectionResult(org.eclipse.persistence.internal.xr.CollectionResult) Result(org.eclipse.persistence.internal.xr.Result) StoredFunctionQueryHandler(org.eclipse.persistence.internal.xr.StoredFunctionQueryHandler) CollectionResult(org.eclipse.persistence.internal.xr.CollectionResult) NamedQueryHandler(org.eclipse.persistence.internal.xr.NamedQueryHandler) Parameter(org.eclipse.persistence.internal.xr.Parameter) QueryOperation(org.eclipse.persistence.internal.xr.QueryOperation) ArgumentTypeDirection(org.eclipse.persistence.tools.oracleddl.metadata.ArgumentTypeDirection)

Example 2 with PLSQLCursorType

use of org.eclipse.persistence.tools.oracleddl.metadata.PLSQLCursorType in project eclipselink by eclipse-ee4j.

the class BaseDBWSBuilderHelper method buildDatabaseTypeFromMetadataType.

/**
 * Build a org.eclipse.persistence.internal.helper.DatabaseType  instance  from an
 * org.eclipse.persistence.tools.oracleddl.metadata.DatabaseType instance.  In the
 * the case of PLSQL Packages, the catalog (package) name can be passed in as well.
 */
@SuppressWarnings("rawtypes")
protected org.eclipse.persistence.internal.helper.DatabaseType buildDatabaseTypeFromMetadataType(DatabaseType dType, String catalog) {
    // argument could be from a different package
    if (dType.isPLSQLType()) {
        PLSQLType pType = (PLSQLType) dType;
        catalog = pType.getParentType().getPackageName();
    }
    // handle cursors
    if (dType.isPLSQLCursorType()) {
        if (dType.isArgumentType()) {
            dType = ((ArgumentType) dType).getEnclosedType();
        }
        PLSQLCursorType pType = (PLSQLCursorType) dType;
        return new PLSQLCursor(pType.getParentType().getPackageName() + DOT + pType.getCursorName());
    }
    if (dType.isArgumentType()) {
        dType = ((ArgumentType) dType).getEnclosedType();
    } else if (dType.isTYPEType()) {
        dType = ((TYPEType) dType).getEnclosedType();
    }
    // composite types
    if (dType.isComposite()) {
        String typeName = dType.getTypeName();
        // for %ROWTYPE, the compatible JDBC type name cannot contain '%'
        String compatibleType = typeName.contains(PERCENT) ? typeName.replace(PERCENT, UNDERSCORE) : typeName;
        String javaTypeName = compatibleType.toLowerCase();
        // handle PL/SQL types
        if (dType.isPLSQLType()) {
            // for %ROWTYPE we don't want the catalog name prepended even if non-null
            if (catalog != null && !typeName.contains(ROWTYPE_STR)) {
                typeName = (catalog + DOT).concat(typeName);
                compatibleType = (catalog + UNDERSCORE).concat(compatibleType);
                javaTypeName = (catalog.toLowerCase() + DOT).concat(javaTypeName);
            }
            // handle PL/SQL record
            if (dType.isPLSQLRecordType()) {
                PLSQLrecord plsqlRec = new PLSQLrecord();
                plsqlRec.setTypeName(typeName);
                plsqlRec.setCompatibleType(compatibleType);
                plsqlRec.setJavaTypeName(javaTypeName);
                // process fields
                for (FieldType fld : ((PLSQLRecordType) dType).getFields()) {
                    if (fld.getEnclosedType().isPrecisionType()) {
                        PrecisionType precisionType = (PrecisionType) fld.getEnclosedType();
                        plsqlRec.addField(fld.getFieldName(), buildDatabaseTypeFromMetadataType(precisionType), (int) precisionType.getPrecision(), (int) precisionType.getScale());
                    } else if (fld.getEnclosedType().isSizedType()) {
                        SizedType sizedType = (SizedType) fld.getEnclosedType();
                        plsqlRec.addField(fld.getFieldName(), buildDatabaseTypeFromMetadataType(sizedType), (int) sizedType.getSize());
                    } else {
                        plsqlRec.addField(fld.getFieldName(), buildDatabaseTypeFromMetadataType(fld.getEnclosedType(), catalog));
                    }
                }
                return plsqlRec;
            }
            // assumes PL/SQL collection
            PLSQLCollection plsqlCollection = new PLSQLCollection();
            plsqlCollection.setTypeName(typeName);
            plsqlCollection.setCompatibleType(compatibleType);
            plsqlCollection.setJavaTypeName(javaTypeName + COLLECTION_WRAPPER_SUFFIX);
            plsqlCollection.setNestedType(buildDatabaseTypeFromMetadataType(((PLSQLCollectionType) dType).getEnclosedType(), catalog));
            return plsqlCollection;
        }
        // handle advanced Oracle types
        if (dType.isVArrayType()) {
            OracleArrayType varray = new OracleArrayType();
            varray.setTypeName(typeName);
            varray.setCompatibleType(compatibleType);
            varray.setJavaTypeName(getGeneratedWrapperClassName(javaTypeName, dbwsBuilder.getProjectName()));
            varray.setNestedType(buildDatabaseTypeFromMetadataType(((VArrayType) dType).getEnclosedType(), null));
            return varray;
        }
        if (dType.isObjectType()) {
            OracleObjectType objType = new OracleObjectType();
            objType.setTypeName(typeName);
            objType.setCompatibleType(compatibleType);
            objType.setJavaTypeName(getGeneratedJavaClassName(javaTypeName, dbwsBuilder.getProjectName()));
            objType.setJavaType(getWrapperClass(objType.getJavaTypeName()));
            Map<String, org.eclipse.persistence.internal.helper.DatabaseType> fields = objType.getFields();
            ObjectType oType = (ObjectType) dType;
            for (FieldType field : oType.getFields()) {
                fields.put(field.getFieldName(), buildDatabaseTypeFromMetadataType(field.getEnclosedType()));
            }
            return objType;
        }
        if (dType.isObjectTableType()) {
            OracleArrayType tableType = new OracleArrayType();
            tableType.setTypeName(typeName);
            tableType.setCompatibleType(compatibleType);
            tableType.setJavaTypeName(getGeneratedWrapperClassName(javaTypeName, dbwsBuilder.getProjectName()));
            org.eclipse.persistence.internal.helper.DatabaseType nestedType = buildDatabaseTypeFromMetadataType(((ObjectTableType) dType).getEnclosedType(), null);
            // need to set the Java Type on the nested type
            Class wrapper = getWrapperClass(nestedType);
            if (wrapper != null) {
                ((ComplexDatabaseType) nestedType).setJavaType(wrapper);
            }
            tableType.setNestedType(nestedType);
            return tableType;
        }
        return null;
    } else if (dType.isScalar()) {
        org.eclipse.persistence.internal.helper.DatabaseType theType = OraclePLSQLTypes.getDatabaseTypeForCode(dType.getTypeName());
        if (theType != null) {
            return theType;
        }
    }
    // scalar types
    return JDBCTypes.getDatabaseTypeForCode(org.eclipse.persistence.tools.dbws.Util.getJDBCTypeFromTypeName(dType.getTypeName()));
}
Also used : ScalarDatabaseType(org.eclipse.persistence.tools.oracleddl.metadata.ScalarDatabaseType) CompositeDatabaseType(org.eclipse.persistence.tools.oracleddl.metadata.CompositeDatabaseType) ComplexDatabaseType(org.eclipse.persistence.internal.helper.ComplexDatabaseType) DatabaseType(org.eclipse.persistence.tools.oracleddl.metadata.DatabaseType) PLSQLCursorType(org.eclipse.persistence.tools.oracleddl.metadata.PLSQLCursorType) PrecisionType(org.eclipse.persistence.tools.oracleddl.metadata.PrecisionType) ObjectType(org.eclipse.persistence.tools.oracleddl.metadata.ObjectType) OracleObjectType(org.eclipse.persistence.platform.database.oracle.jdbc.OracleObjectType) VArrayType(org.eclipse.persistence.tools.oracleddl.metadata.VArrayType) OracleObjectType(org.eclipse.persistence.platform.database.oracle.jdbc.OracleObjectType) PLSQLrecord(org.eclipse.persistence.platform.database.oracle.plsql.PLSQLrecord) PLSQLCursor(org.eclipse.persistence.platform.database.oracle.plsql.PLSQLCursor) PLSQLRecordType(org.eclipse.persistence.tools.oracleddl.metadata.PLSQLRecordType) FieldType(org.eclipse.persistence.tools.oracleddl.metadata.FieldType) ComplexDatabaseType(org.eclipse.persistence.internal.helper.ComplexDatabaseType) ROWTYPEType(org.eclipse.persistence.tools.oracleddl.metadata.ROWTYPEType) TYPEType(org.eclipse.persistence.tools.oracleddl.metadata.TYPEType) PLSQLCollectionType(org.eclipse.persistence.tools.oracleddl.metadata.PLSQLCollectionType) PLSQLCollection(org.eclipse.persistence.platform.database.oracle.plsql.PLSQLCollection) SizedType(org.eclipse.persistence.tools.oracleddl.metadata.SizedType) PLSQLType(org.eclipse.persistence.tools.oracleddl.metadata.PLSQLType) OracleArrayType(org.eclipse.persistence.platform.database.oracle.jdbc.OracleArrayType)

Aggregations

PLSQLCursorType (org.eclipse.persistence.tools.oracleddl.metadata.PLSQLCursorType)2 QName (javax.xml.namespace.QName)1 ComplexDatabaseType (org.eclipse.persistence.internal.helper.ComplexDatabaseType)1 Attachment (org.eclipse.persistence.internal.xr.Attachment)1 CollectionResult (org.eclipse.persistence.internal.xr.CollectionResult)1 NamedQueryHandler (org.eclipse.persistence.internal.xr.NamedQueryHandler)1 Parameter (org.eclipse.persistence.internal.xr.Parameter)1 ProcedureArgument (org.eclipse.persistence.internal.xr.ProcedureArgument)1 ProcedureOutputArgument (org.eclipse.persistence.internal.xr.ProcedureOutputArgument)1 QueryHandler (org.eclipse.persistence.internal.xr.QueryHandler)1 QueryOperation (org.eclipse.persistence.internal.xr.QueryOperation)1 Result (org.eclipse.persistence.internal.xr.Result)1 StoredFunctionQueryHandler (org.eclipse.persistence.internal.xr.StoredFunctionQueryHandler)1 StoredProcedureQueryHandler (org.eclipse.persistence.internal.xr.StoredProcedureQueryHandler)1 OracleArrayType (org.eclipse.persistence.platform.database.oracle.jdbc.OracleArrayType)1 OracleObjectType (org.eclipse.persistence.platform.database.oracle.jdbc.OracleObjectType)1 PLSQLCollection (org.eclipse.persistence.platform.database.oracle.plsql.PLSQLCollection)1 PLSQLCursor (org.eclipse.persistence.platform.database.oracle.plsql.PLSQLCursor)1 PLSQLrecord (org.eclipse.persistence.platform.database.oracle.plsql.PLSQLrecord)1 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)1