Search in sources :

Example 26 with DatabasePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatabasePlatform in project eclipselink by eclipse-ee4j.

the class JDBCHelper method loadProcedures.

protected List<ProcedureType> loadProcedures(String originalCatalogPattern, String originalSchemaPattern, String originalProcedurePattern) {
    List<ProcedureType> dbStoredProcedures = null;
    boolean catalogMatchDontCare = false;
    DatabasePlatform platform = dbwsBuilder.getDatabasePlatform();
    if (platform instanceof MySQLPlatform || platform instanceof DerbyPlatform || platform instanceof PostgreSQLPlatform) {
        // note that get info on other platforms that also require catalogMatchDontCare = true
        catalogMatchDontCare = true;
    }
    String catalogPattern = escapePunctuation(originalCatalogPattern);
    String schemaPattern = escapePunctuation(originalSchemaPattern);
    String procedurePattern = escapePunctuation(originalProcedurePattern);
    // Make sure procedure(s) is/are available
    ResultSet procsInfo = null;
    try {
        DatabaseMetaData databaseMetaData = getDatabaseMetaData(dbwsBuilder.getConnection());
        procsInfo = databaseMetaData.getProcedures(catalogPattern, schemaPattern, procedurePattern);
        // did we get a hit?
        if (procsInfo != null) {
            List<ProcedureType> tmpProcs = new ArrayList<ProcedureType>();
            while (procsInfo.next()) {
                String actualCatalogName = procsInfo.getString(PROCS_INFO_CATALOG);
                String actualSchemaName = procsInfo.getString(PROCS_INFO_SCHEMA);
                String actualProcedureName = procsInfo.getString(PROCS_INFO_NAME);
                short procedureType = procsInfo.getShort(PROCS_INFO_TYPE);
                ProcedureType dbStoredProcedure;
                if (procedureType == procedureReturnsResult) {
                    dbStoredProcedure = new FunctionType(actualProcedureName);
                } else {
                    dbStoredProcedure = new ProcedureType(actualProcedureName);
                }
                if (actualCatalogName != null && actualCatalogName.length() > 0) {
                    dbStoredProcedure.setCatalogName(actualCatalogName);
                }
                if (actualSchemaName != null && actualSchemaName.length() > 0) {
                    dbStoredProcedure.setSchema(actualSchemaName);
                }
                tmpProcs.add(dbStoredProcedure);
            }
            procsInfo.close();
            /* new a temp bucket to hold DbStoredArgs until they can be sorted out with respect
                 * to which DbStoredProcedure owns which args; this has to be done because Oracle can
                 * return multiple hits across multiple packages for the same procedureName.
                 */
            int numProcs = tmpProcs.size();
            if (numProcs > 0) {
                dbStoredProcedures = new ArrayList<ProcedureType>(numProcs);
                ResultSet procedureColumnsInfo = null;
                procedureColumnsInfo = databaseMetaData.getProcedureColumns(catalogPattern, schemaPattern, procedurePattern, "%");
                while (procedureColumnsInfo.next()) {
                    String actualCatalogName = procedureColumnsInfo.getString(PROC_COLS_INFO_CATALOG);
                    String actualSchemaName = procedureColumnsInfo.getString(PROC_COLS_INFO_SCHEMA);
                    String actualProcedureName = procedureColumnsInfo.getString(PROC_COLS_INFO_NAME);
                    String argName = procedureColumnsInfo.getString(PROC_COLS_INFO_COLNAME);
                    // some MySql drivers return empty string, some return null: set to emptyString regardless
                    if (argName == null) {
                        argName = "";
                    }
                    ArgumentType dbStoredArgument = new ArgumentType(argName);
                    short inOut = procedureColumnsInfo.getShort(PROC_COLS_INFO_TYPE);
                    if (inOut == procedureColumnInOut) {
                        dbStoredArgument.setDirection(INOUT);
                    } else if (inOut == procedureColumnOut) {
                        dbStoredArgument.setDirection(OUT);
                    } else if (inOut == procedureColumnReturn) {
                        dbStoredArgument.setDirection(RETURN);
                    } else {
                        // default to ArgumentTypeDirection.IN
                        dbStoredArgument.setDirection(IN);
                    }
                    int jdbcType = procedureColumnsInfo.getInt(PROC_COLS_INFO_DATA_TYPE);
                    int precision = procedureColumnsInfo.getInt(PROC_COLS_INFO_PRECISION);
                    int scale = procedureColumnsInfo.getInt(PROC_COLS_INFO_SCALE);
                    dbStoredArgument.setEnclosedType(buildTypeForJDBCType(jdbcType, precision, scale));
                    // find matching DbStoredProcedure
                    // this dbStoredArgument belongs to a 'regular' procedure
                    ProcedureType matchingProc = null;
                    for (int i = 0; i < tmpProcs.size(); ) {
                        ProcedureType tmpProc = tmpProcs.get(i);
                        if (matches(tmpProc, actualCatalogName, actualSchemaName, actualProcedureName, false, catalogMatchDontCare)) {
                            matchingProc = tmpProc;
                            dbStoredProcedures.add(matchingProc);
                            break;
                        }
                        i++;
                    }
                    if (matchingProc == null) {
                        // look in dbStoredProcedures - matching proc already moved over ?
                        for (ProcedureType dbStoredProcedure : dbStoredProcedures) {
                            if (matches(dbStoredProcedure, actualCatalogName, actualSchemaName, actualProcedureName, false, catalogMatchDontCare)) {
                                matchingProc = dbStoredProcedure;
                                break;
                            }
                        }
                    }
                    if (matchingProc != null) {
                        if (matchingProc.isFunctionType() && dbStoredArgument.getArgumentName().equalsIgnoreCase("")) {
                            ((FunctionType) matchingProc).setReturnArgument(dbStoredArgument);
                        } else {
                            matchingProc.getArguments().add(dbStoredArgument);
                        }
                        tmpProcs.remove(matchingProc);
                    }
                // else some argument that doesn't have a matching proc? ignore for now
                }
                procedureColumnsInfo.close();
                if (!tmpProcs.isEmpty()) {
                    // leftovers are the no-arg procedures
                    dbStoredProcedures.addAll(tmpProcs);
                }
            }
        }
    } catch (SQLException sqlException) {
        throw new IllegalStateException("failure retrieving Stored Procedure metadata", sqlException);
    }
    if (dbStoredProcedures != null && !dbStoredProcedures.isEmpty()) {
        Collections.sort(dbStoredProcedures, new Comparator<ProcedureType>() {

            @Override
            public int compare(ProcedureType o1, ProcedureType o2) {
                String name1 = o1.getProcedureName();
                String name2 = o2.getProcedureName();
                if (!name1.equals(name2)) {
                    return name1.compareTo(name2);
                } else {
                    return o1.getOverload() - o2.getOverload();
                }
            }
        });
    }
    return dbStoredProcedures;
}
Also used : PostgreSQLPlatform(org.eclipse.persistence.platform.database.PostgreSQLPlatform) ProcedureType(org.eclipse.persistence.tools.oracleddl.metadata.ProcedureType) SQLException(java.sql.SQLException) FunctionType(org.eclipse.persistence.tools.oracleddl.metadata.FunctionType) ArrayList(java.util.ArrayList) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) Util.qNameFromString(org.eclipse.persistence.tools.dbws.Util.qNameFromString) DatabaseMetaData(java.sql.DatabaseMetaData) ArgumentType(org.eclipse.persistence.tools.oracleddl.metadata.ArgumentType) DerbyPlatform(org.eclipse.persistence.platform.database.DerbyPlatform) ResultSet(java.sql.ResultSet) MySQLPlatform(org.eclipse.persistence.platform.database.MySQLPlatform)

Example 27 with DatabasePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatabasePlatform in project eclipselink by eclipse-ee4j.

the class MappingSystem method addDescriptors.

@Override
public void addDescriptors(DatabaseSession session) {
    DatabasePlatform platform = session.getLogin().getPlatform();
    ClassDescriptor empDescriptor = (project.getDescriptors().get(Employee.class));
    Employee.addToDescriptor(empDescriptor);
    ClassDescriptor hardwareDescriptor = (project.getDescriptors().get(Hardware.class));
    Hardware.addToDescriptor(hardwareDescriptor);
    ClassDescriptor monitorDescriptor = (project.getDescriptors().get(Monitor.class));
    Monitor.addToDescriptor(monitorDescriptor);
    // If on Access exclude the jobDescription mapping
    if (platform.isAccess()) {
        empDescriptor.getMappings().removeElement(empDescriptor.getMappingForAttributeName("jobDescription"));
    }
    if (platform.getDefaultSequence().shouldAcquireValueAfterInsert()) {
        RelationalDescriptor cubicleDescriptor = ((RelationalDescriptor) project.getDescriptors().get(Cubicle.class));
        cubicleDescriptor.setSequenceNumberField(null);
        cubicleDescriptor.setSequenceNumberName(null);
    }
    (session).addDescriptors(project);
    // Add the Legacy Test Project.
    (session).addDescriptors(legacyProject);
    // Add the MultipleTableTest Test Project.
    (session).addDescriptors(multipleTableProject);
    // Add the keyboard project - tests constraints.
    (session).addDescriptors(keyboardProject);
    ClassDescriptor joystickDescriptor = (keyboardProject.getDescriptors().get(Joystick.class));
    joystickDescriptor.addConstraintDependencies(Keyboard.class);
    // Add the insert order project.
    (session).addDescriptors(bidirectionalProject);
}
Also used : RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)

Example 28 with DatabasePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatabasePlatform in project eclipselink by eclipse-ee4j.

the class MappingSystem method createTables.

@Override
public void createTables(DatabaseSession session) {
    SchemaManager schemaManager = new SchemaManager(session);
    boolean orig_FAST_TABLE_CREATOR = SchemaManager.FAST_TABLE_CREATOR;
    // of an instance of this class (drops & re-)creates the tables.
    if (useFastTableCreatorAfterInitialCreate && !isFirstCreation) {
        SchemaManager.FAST_TABLE_CREATOR = true;
    }
    (new LegacyTableMaker()).replaceTables(session);
    (new MultipleTableTestTableMaker()).replaceTables(session);
    (new KeyboardTables()).replaceTables(session);
    // insert order test table creation
    new BiDirectionInsertOrderTableMaker().replaceTables(session);
    // TableDefinition emp1Definition = Employee1.tableDefinition();
    TableDefinition empDefinition = Employee.tableDefinition();
    DatabasePlatform platform = session.getLogin().getPlatform();
    // Only add this field if NOT on Access or DB2
    if (!platform.isAccess()) {
        empDefinition.addField("JDESC", Byte[].class);
    }
    try {
        schemaManager.replaceObject(empDefinition);
        schemaManager.replaceObject(Employee.joinTableDefinition());
        schemaManager.replaceObject(Employee.employeePhoneJoinTableDefinition());
        schemaManager.replaceObject(Phone.tableDefinition());
        schemaManager.replaceObject(CompanyCard.tableDefinition());
        schemaManager.replaceObject(Computer.tableDefinition());
        schemaManager.replaceObject(Cubicle.tableDefinition());
        schemaManager.replaceObject(EmergencyExit.tableDefinition());
        schemaManager.replaceObject(EmergencyExit.relationTableDefinition());
        schemaManager.replaceObject(Shipment.tableDefinition());
        schemaManager.replaceObject(getPolicyTableDefinition());
        schemaManager.replaceObject(Address.tableDefinition());
        schemaManager.replaceObject(Monitor.tableDefinition());
        schemaManager.replaceObject(Hardware.tableDefinition());
        schemaManager.replaceObject(Peripheral.tableDefinition());
        schemaManager.replaceObject(SecureSystem.tableDefinition());
        schemaManager.replaceObject(Identification.tableDefinition());
        schemaManager.createSequences();
    } finally {
        if (useFastTableCreatorAfterInitialCreate && !isFirstCreation) {
            SchemaManager.FAST_TABLE_CREATOR = orig_FAST_TABLE_CREATOR;
        }
    }
    // next time it deletes the rows instead.
    isFirstCreation = false;
}
Also used : DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)

Example 29 with DatabasePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatabasePlatform in project eclipselink by eclipse-ee4j.

the class RownumFilteringQueryTest method setup.

@Override
public void setup() {
    DatabaseSession session = (DatabaseSession) getSession();
    DatabasePlatform platform = getSession().getPlatform();
    if (!platform.isOracle()) {
        throw new TestWarningException("Oracle Pagination not supported on platform " + platform);
    }
    tracker = new QuerySQLTracker(session);
}
Also used : DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) TestWarningException(org.eclipse.persistence.testing.framework.TestWarningException)

Example 30 with DatabasePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatabasePlatform in project eclipselink by eclipse-ee4j.

the class LOBTestWrapper method setup.

@Override
protected void setup() throws Throwable {
    DatabasePlatform platform = getSession().getPlatform();
    if (!platform.isOracle()) {
        throw new TestWarningException("This test case works on Oracle only");
    }
    if (platform instanceof Oracle8Platform) {
        Oracle8Platform platform8 = (Oracle8Platform) platform;
        shouldUseLocatorForLOBWriteOriginal = platform8.shouldUseLocatorForLOBWrite();
        if (shouldSetUseLocatorForLOBWriteIntoPlatform) {
            platform8.setShouldUseLocatorForLOBWrite(shouldUseLocatorForLOBWrite);
        } else {
            // otherwise don't change the flag.
            this.shouldUseLocatorForLOBWrite = shouldUseLocatorForLOBWriteOriginal;
        }
    } else {
        // can't use locators if it's not Oracle8Platform or higher.
        if (shouldSetUseLocatorForLOBWriteIntoPlatform && shouldUseLocatorForLOBWrite) {
            throw new TestProblemException("Can't call platform.setShouldUseLocatorForLobWrite(true) - it's not Oracle8Platform");
        }
        this.shouldUseLocatorForLOBWrite = false;
    }
    if (!shouldUseLocatorForLOBWrite) {
        usesStringBindingOriginal = platform.usesStringBinding();
        platform.setUsesStringBinding(true);
    }
    super.setup();
}
Also used : Oracle8Platform(org.eclipse.persistence.platform.database.oracle.Oracle8Platform) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)

Aggregations

DatabasePlatform (org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)52 EntityManager (jakarta.persistence.EntityManager)12 DatabaseCall (org.eclipse.persistence.internal.databaseaccess.DatabaseCall)11 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)9 EntityManagerFactoryImpl (org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl)9 Test (org.junit.Test)9 GenericEntity (org.eclipse.persistence.jpa.test.property.model.GenericEntity)8 PersistenceException (jakarta.persistence.PersistenceException)6 Platform (org.eclipse.persistence.internal.databaseaccess.Platform)6 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)6 ResultSetMetaData (java.sql.ResultSetMetaData)5 DatabaseAccessor (org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor)5 FieldTypeDefinition (org.eclipse.persistence.internal.databaseaccess.FieldTypeDefinition)5 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)5 IOException (java.io.IOException)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Vector (java.util.Vector)4