Search in sources :

Example 6 with PLSQLPackageType

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

the class JPAMetadataGenerator method generateXmlEntityMappings.

/**
 * Generate an XMLEntityMappings instance based on a given list of meta-model database types.
 *
 * @param databaseTypes the list of meta-model database types to be used to generate an XMLEntityMappings
 * @see org.eclipse.persistence.tools.oracleddl.metadata.CompositeDatabaseType
 */
public XMLEntityMappings generateXmlEntityMappings(List<CompositeDatabaseType> databaseTypes) {
    List<ProcedureType> procedures = new ArrayList<ProcedureType>();
    List<TableType> tables = new ArrayList<TableType>();
    // populate lists of TableTypes and ProcedureTypes
    for (CompositeDatabaseType dbType : databaseTypes) {
        if (dbType.isTableType()) {
            tables.add((TableType) dbType);
        } else if (dbType.isProcedureType()) {
            procedures.add((ProcedureType) dbType);
        }
    }
    // handle stored procedure overloading
    handleOverloading(procedures);
    // process TableTypes
    for (TableType table : tables) {
        EntityAccessor entity = processTableType(table);
        xmlEntityMappings.getEntities().add(entity);
    }
    // process ProcedureTypes
    for (ProcedureType procedure : procedures) {
        // PL/SQL stored procedures and functions will have a PLSQLPackageType as its parent
        PLSQLPackageType pkgType = procedure.getParentType();
        if (pkgType != null) {
            // handle PL/SQL
            if (procedure.isFunctionType()) {
                xmlEntityMappings.getNamedPLSQLStoredFunctionQueries().add(processPLSQLFunctionType((FunctionType) procedure, pkgType));
            } else {
                xmlEntityMappings.getNamedPLSQLStoredProcedureQueries().add(processPLSQLProcedureType(procedure, pkgType));
            }
        } else {
            // handle top-level (non-PL/SQL) functions and procedures
            if (procedure.isFunctionType()) {
                xmlEntityMappings.getNamedStoredFunctionQueries().add(processFunctionType((FunctionType) procedure));
            } else {
                xmlEntityMappings.getNamedStoredProcedureQueries().add(processProcedureType(procedure));
            }
        }
    }
    return xmlEntityMappings;
}
Also used : EntityAccessor(org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor) ProcedureType(org.eclipse.persistence.tools.oracleddl.metadata.ProcedureType) TableType(org.eclipse.persistence.tools.oracleddl.metadata.TableType) ObjectTableType(org.eclipse.persistence.tools.oracleddl.metadata.ObjectTableType) PLSQLPackageType(org.eclipse.persistence.tools.oracleddl.metadata.PLSQLPackageType) FunctionType(org.eclipse.persistence.tools.oracleddl.metadata.FunctionType) ArrayList(java.util.ArrayList) CompositeDatabaseType(org.eclipse.persistence.tools.oracleddl.metadata.CompositeDatabaseType)

Example 7 with PLSQLPackageType

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

the class OracleHelper method loadProcedures.

/**
 * Generates a {@code List<ProcedureType>} based on a given set of patterns.
 */
@Override
protected List<ProcedureType> loadProcedures(List<String> catalogPatterns, List<String> schemaPatterns, List<String> procedureNamePatterns) {
    List<ProcedureType> allProcsAndFuncs = new ArrayList<ProcedureType>();
    List<String> topLevelSchemaPatterns = new ArrayList<String>();
    List<String> topLevelProcedureNamePatterns = new ArrayList<String>();
    Map<String, Set<String>> packagePatterns = new HashMap<String, Set<String>>();
    for (int i = 0, len = catalogPatterns.size(); i < len; i++) {
        String catalogPattern = catalogPatterns.get(i);
        String schemaPattern = schemaPatterns.get(i);
        if (schemaPattern == null) {
            schemaPattern = dbwsBuilder.getUsername().toUpperCase();
        }
        if (catalogPattern == null || catalogPattern.length() == 0 || TOPLEVEL.equals(catalogPattern)) {
            topLevelSchemaPatterns.add(schemaPattern);
            topLevelProcedureNamePatterns.add(procedureNamePatterns.get(i));
        } else {
            Set<String> packageNames = packagePatterns.get(schemaPattern);
            if (packageNames == null) {
                packageNames = new HashSet<String>();
                packagePatterns.put(schemaPattern, packageNames);
            }
            packageNames.add(catalogPattern);
        }
    }
    if (topLevelProcedureNamePatterns.size() > 0) {
        try {
            List<ProcedureType> topLevelProcedures = dtBuilder.buildProcedures(dbwsBuilder.getConnection(), topLevelSchemaPatterns, topLevelProcedureNamePatterns);
            if (topLevelProcedures != null && topLevelProcedures.size() > 0) {
                allProcsAndFuncs.addAll(topLevelProcedures);
            }
        } catch (ParseException e) {
            dbwsBuilder.logMessage(Level.WARNING, e.getMessage());
        }
        try {
            List<FunctionType> topLevelFunctions = dtBuilder.buildFunctions(dbwsBuilder.getConnection(), topLevelSchemaPatterns, topLevelProcedureNamePatterns);
            if (topLevelFunctions != null && topLevelFunctions.size() > 0) {
                allProcsAndFuncs.addAll(topLevelFunctions);
            }
        } catch (ParseException e) {
            dbwsBuilder.logMessage(Level.WARNING, e.getMessage());
        }
    }
    if (packagePatterns.size() > 0) {
        try {
            // unravel map
            List<String> schemaPats = new ArrayList<String>();
            List<String> packagePats = new ArrayList<String>();
            for (Map.Entry<String, Set<String>> entry : packagePatterns.entrySet()) {
                String schema = entry.getKey();
                for (String packageName : entry.getValue()) {
                    schemaPats.add(schema);
                    packagePats.add(packageName);
                }
            }
            List<PLSQLPackageType> packages = dtBuilder.buildPackages(dbwsBuilder.getConnection(), schemaPats, packagePats);
            if (packages == null || packages.isEmpty()) {
                logPackageNotFoundWarnings(NO_PKG_MSG, schemaPats, packagePats);
            } else {
                for (PLSQLPackageType pakage : packages) {
                    // check DDL generation
                    ShadowDDLGenerator ddlGenerator = new ShadowDDLGenerator(pakage);
                    dbwsBuilder.getTypeDDL().addAll(ddlGenerator.getAllCreateDDLs());
                    dbwsBuilder.getTypeDropDDL().addAll(ddlGenerator.getAllDropDDLs());
                    // check for overloading
                    Map<String, List<ProcedureType>> overloadMap = new HashMap<String, List<ProcedureType>>();
                    List<ProcedureType> procedures = pakage.getProcedures();
                    for (ProcedureType procedure : procedures) {
                        String procedureName = procedure.getProcedureName();
                        List<ProcedureType> multipleProcedures = overloadMap.get(procedureName);
                        if (multipleProcedures == null) {
                            multipleProcedures = new ArrayList<ProcedureType>();
                            overloadMap.put(procedureName, multipleProcedures);
                        }
                        multipleProcedures.add(procedure);
                    }
                    for (List<ProcedureType> procs : overloadMap.values()) {
                        if (procs.size() > 1) {
                            for (int i = 0, len = procs.size(); i < len; i++) {
                                procs.get(i).setOverload(i);
                            }
                        }
                    }
                    // Check against procedureNamePatterns
                    String tmp = buildNamePatternsList(procedureNamePatterns);
                    for (ProcedureType procedure : procedures) {
                        if (sqlMatch(tmp, procedure.getProcedureName())) {
                            allProcsAndFuncs.add(procedure);
                        }
                    }
                }
            }
        } catch (ParseException e) {
            dbwsBuilder.logMessage(Level.WARNING, e.getMessage());
        }
    }
    return allProcsAndFuncs.isEmpty() ? null : allProcsAndFuncs;
}
Also used : ProcedureType(org.eclipse.persistence.tools.oracleddl.metadata.ProcedureType) Set(java.util.Set) HashSet(java.util.HashSet) PLSQLPackageType(org.eclipse.persistence.tools.oracleddl.metadata.PLSQLPackageType) HashMap(java.util.HashMap) FunctionType(org.eclipse.persistence.tools.oracleddl.metadata.FunctionType) ArrayList(java.util.ArrayList) Util.qNameFromString(org.eclipse.persistence.tools.dbws.Util.qNameFromString) ArrayList(java.util.ArrayList) List(java.util.List) ParseException(org.eclipse.persistence.tools.oracleddl.parser.ParseException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with PLSQLPackageType

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

the class OracleHelper method buildDbArtifacts.

@Override
public void buildDbArtifacts() {
    super.buildDbArtifacts();
    // list of all directly-referenced packages
    Set<PLSQLPackageType> directPackages = new HashSet<PLSQLPackageType>();
    for (ProcedureType procedureType : dbStoredProcedures) {
        for (ArgumentType argumentType : procedureType.getArguments()) {
            DatabaseType argumentDataType = argumentType.getEnclosedType();
            if (argumentDataType.isPLSQLType()) {
                PLSQLType plsqlType = (PLSQLType) argumentDataType;
                directPackages.add(plsqlType.getParentType());
            }
        }
    }
    // any indirectly-referenced packages?
    final Set<PLSQLPackageType> indirectPackages = new HashSet<PLSQLPackageType>();
    DatabaseTypeVisitor indirectVisitor = new BaseDatabaseTypeVisitor() {

        @Override
        public void beginVisit(PLSQLPackageType databaseType) {
            indirectPackages.add(databaseType);
        }
    };
    for (PLSQLPackageType pckage : directPackages) {
        pckage.accept(indirectVisitor);
    }
    Set<PLSQLPackageType> packages = new HashSet<PLSQLPackageType>();
    packages.addAll(directPackages);
    packages.addAll(indirectPackages);
    for (PLSQLPackageType pckage : packages) {
        ShadowDDLGenerator ddlGenerator = new ShadowDDLGenerator(pckage);
        dbwsBuilder.getTypeDDL().addAll(ddlGenerator.getAllCreateDDLs());
        dbwsBuilder.getTypeDropDDL().addAll(ddlGenerator.getAllDropDDLs());
    }
}
Also used : BaseDatabaseTypeVisitor(org.eclipse.persistence.tools.oracleddl.metadata.visit.BaseDatabaseTypeVisitor) ProcedureType(org.eclipse.persistence.tools.oracleddl.metadata.ProcedureType) PLSQLPackageType(org.eclipse.persistence.tools.oracleddl.metadata.PLSQLPackageType) CompositeDatabaseType(org.eclipse.persistence.tools.oracleddl.metadata.CompositeDatabaseType) ComplexDatabaseType(org.eclipse.persistence.internal.helper.ComplexDatabaseType) DatabaseType(org.eclipse.persistence.tools.oracleddl.metadata.DatabaseType) Util.getAttributeClassForDatabaseType(org.eclipse.persistence.tools.dbws.Util.getAttributeClassForDatabaseType) BaseDatabaseTypeVisitor(org.eclipse.persistence.tools.oracleddl.metadata.visit.BaseDatabaseTypeVisitor) DatabaseTypeVisitor(org.eclipse.persistence.tools.oracleddl.metadata.visit.DatabaseTypeVisitor) ArgumentType(org.eclipse.persistence.tools.oracleddl.metadata.ArgumentType) PLSQLType(org.eclipse.persistence.tools.oracleddl.metadata.PLSQLType) HashSet(java.util.HashSet)

Example 9 with PLSQLPackageType

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

the class JPAMetadataGenerator method processPLSQLArgument.

/**
 * Generate a PL/SQL parameter based on the given ArgumentType. For
 * non-PL/SQL arguments the processArgument method should be used.
 */
protected PLSQLParameterMetadata processPLSQLArgument(ArgumentType arg) {
    // for %ROWTYPE, we need to create a PL/SQL record that mirrors the Table
    if (arg.getEnclosedType().isROWTYPEType()) {
        ROWTYPEType rType = (ROWTYPEType) arg.getEnclosedType();
        TableType tableType = (TableType) rType.getEnclosedType();
        PLSQLRecordType plsqlRec = new PLSQLRecordType(rType.getTypeName());
        plsqlRec.setParentType(new PLSQLPackageType());
        for (FieldType col : tableType.getColumns()) {
            FieldType ft = new FieldType(col.getFieldName());
            ft.setEnclosedType(col.getEnclosedType());
            plsqlRec.addField(ft);
        }
        arg.setEnclosedType(plsqlRec);
    }
    PLSQLParameterMetadata param = new PLSQLParameterMetadata();
    // handle cursor
    if (arg.isPLSQLCursorType()) {
        param.setDirection(OUT_CURSOR_STR);
    }
    // handle stored function return type
    if (arg.getDirection() == ArgumentTypeDirection.RETURN) {
        param.setName(arg.isPLSQLCursorType() ? CURSOR_STR : RESULT_STR);
    } else {
        // direction is already set for cursor type
        if (!arg.isPLSQLCursorType()) {
            param.setDirection(arg.getDirection().name());
        }
        param.setName(arg.getArgumentName());
    }
    String dbType = arg.getTypeName();
    // handle composites
    if (arg.isComposite()) {
        DatabaseType enclosedType = arg.getEnclosedType();
        // need to prepend the package name for most PL/SQL and Cursor types
        if (enclosedType.isPLSQLType() || enclosedType.isPLSQLCursorType()) {
            dbType = getQualifiedTypeName(enclosedType);
        }
        // process the composite enclosed type
        processCompositeType(enclosedType, dbType);
    }
    param.setDatabaseType(processTypeName(dbType));
    return param;
}
Also used : TableType(org.eclipse.persistence.tools.oracleddl.metadata.TableType) ObjectTableType(org.eclipse.persistence.tools.oracleddl.metadata.ObjectTableType) PLSQLRecordType(org.eclipse.persistence.tools.oracleddl.metadata.PLSQLRecordType) PLSQLPackageType(org.eclipse.persistence.tools.oracleddl.metadata.PLSQLPackageType) CompositeDatabaseType(org.eclipse.persistence.tools.oracleddl.metadata.CompositeDatabaseType) DatabaseType(org.eclipse.persistence.tools.oracleddl.metadata.DatabaseType) ROWTYPEType(org.eclipse.persistence.tools.oracleddl.metadata.ROWTYPEType) PLSQLParameterMetadata(org.eclipse.persistence.internal.jpa.metadata.queries.PLSQLParameterMetadata) FieldType(org.eclipse.persistence.tools.oracleddl.metadata.FieldType) Util.getAttributeTypeNameForFieldType(org.eclipse.persistence.tools.metadata.generation.Util.getAttributeTypeNameForFieldType)

Example 10 with PLSQLPackageType

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

the class PLSQLRecordTypeTestSuite method setUp.

@SuppressWarnings({ "rawtypes", "unchecked" })
@BeforeClass
public static void setUp() throws ClassNotFoundException, SQLException {
    AllTests.setUp();
    String ddlCreateProp = System.getProperty(DATABASE_DDL_CREATE_KEY, DEFAULT_DATABASE_DDL_CREATE);
    if ("true".equalsIgnoreCase(ddlCreateProp)) {
        ddlCreate = true;
    }
    String ddlDropProp = System.getProperty(DATABASE_DDL_DROP_KEY, DEFAULT_DATABASE_DDL_DROP);
    if ("true".equalsIgnoreCase(ddlDropProp)) {
        ddlDrop = true;
    }
    String ddlDebugProp = System.getProperty(DATABASE_DDL_DEBUG_KEY, DEFAULT_DATABASE_DDL_DEBUG);
    if ("true".equalsIgnoreCase(ddlDebugProp)) {
        ddlDebug = true;
    }
    if (ddlCreate) {
        runDdl(conn, CREATE_A_PHONE_TYPE, ddlDebug);
        runDdl(conn, CREATE_A_PHONE_TYPE_TABLE, ddlDebug);
        runDdl(conn, CREATE_A_PHONE_TYPE_VARRAY, ddlDebug);
        runDdl(conn, CREATE_A_CONTACT_TYPE, ddlDebug);
        runDdl(conn, CREATE_PACKAGE2_PACKAGE, ddlDebug);
        runDdl(conn, CREATE_PACKAGE2_EMPREC_TYPE, ddlDebug);
    }
    String schema = System.getProperty(DATABASE_USERNAME_KEY, DEFAULT_DATABASE_USERNAME);
    List<String> procedurePatterns = new ArrayList<String>();
    procedurePatterns.add("COPYRECORD");
    procedurePatterns.add("COPYRECORD_FUNC");
    procedurePatterns.add("COPYCOMPLEXRECORD");
    procedurePatterns.add("COPYCOMPLEXRECORD_FUNC");
    procedurePatterns.add("COPYMORECOMPLEXRECORD_FUNC");
    procedurePatterns.add("COPYSUPERCOMPLEXRECORD");
    // use DatabaseTypeBuilder to generate a list of PackageTypes
    dbTypeBuilder = new DatabaseTypeBuilder();
    dbProcedures = new ArrayList();
    try {
        // process the package
        List<PLSQLPackageType> packages = dbTypeBuilder.buildPackages(conn, schema, "PACKAGE2");
        for (PLSQLPackageType pkgType : packages) {
            // now get the desired procedures/functions from the processed package
            for (ProcedureType procType : pkgType.getProcedures()) {
                if (procedurePatterns.contains(procType.getProcedureName())) {
                    dbProcedures.add(procType);
                }
            }
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
}
Also used : ProcedureType(org.eclipse.persistence.tools.oracleddl.metadata.ProcedureType) DatabaseTypeBuilder(org.eclipse.persistence.tools.oracleddl.util.DatabaseTypeBuilder) PLSQLPackageType(org.eclipse.persistence.tools.oracleddl.metadata.PLSQLPackageType) ArrayList(java.util.ArrayList) AllTests.documentToString(org.eclipse.persistence.tools.metadata.generation.test.AllTests.documentToString) ParseException(org.eclipse.persistence.tools.oracleddl.parser.ParseException) BeforeClass(org.junit.BeforeClass)

Aggregations

PLSQLPackageType (org.eclipse.persistence.tools.oracleddl.metadata.PLSQLPackageType)11 ProcedureType (org.eclipse.persistence.tools.oracleddl.metadata.ProcedureType)10 ArrayList (java.util.ArrayList)9 ParseException (org.eclipse.persistence.tools.oracleddl.parser.ParseException)7 AllTests.documentToString (org.eclipse.persistence.tools.metadata.generation.test.AllTests.documentToString)6 DatabaseTypeBuilder (org.eclipse.persistence.tools.oracleddl.util.DatabaseTypeBuilder)6 BeforeClass (org.junit.BeforeClass)6 CompositeDatabaseType (org.eclipse.persistence.tools.oracleddl.metadata.CompositeDatabaseType)4 TableType (org.eclipse.persistence.tools.oracleddl.metadata.TableType)4 ObjectTableType (org.eclipse.persistence.tools.oracleddl.metadata.ObjectTableType)3 HashSet (java.util.HashSet)2 ArgumentType (org.eclipse.persistence.tools.oracleddl.metadata.ArgumentType)2 DatabaseType (org.eclipse.persistence.tools.oracleddl.metadata.DatabaseType)2 FieldType (org.eclipse.persistence.tools.oracleddl.metadata.FieldType)2 FunctionType (org.eclipse.persistence.tools.oracleddl.metadata.FunctionType)2 PLSQLRecordType (org.eclipse.persistence.tools.oracleddl.metadata.PLSQLRecordType)2 ROWTYPEType (org.eclipse.persistence.tools.oracleddl.metadata.ROWTYPEType)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1