Search in sources :

Example 1 with DatabasePlatform

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

the class JUnitTestCase method supportsStoredFunctions.

public static boolean supportsStoredFunctions(String puName) {
    DatabasePlatform platform = getDatabaseSession(puName).getPlatform();
    // TODO: DB2 should be in this list.
    if (platform.isOracle() || platform.isMySQL()) {
        return true;
    }
    warning("This database does not support stored function creation.");
    return false;
}
Also used : DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)

Example 2 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 3 with DatabasePlatform

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

the class BatchCommitTransactionExceptionTest method test.

@Override
public void test() {
    Session session = getSession();
    DatabasePlatform plat = session.getPlatform();
    // test to fail on MYSQL. Tracking bug 335724
    if (plat.isOracle() || plat.isSQLServer() || plat.isSybase() || plat.isSQLAnywhere() || plat.isMySQL() || plat.isSymfoware()) {
        DatabaseLogin login = session.getLogin();
        login.useBatchWriting();
        try {
            ((DatabaseSession) session).beginTransaction();
            session.executeNonSelectingCall(new org.eclipse.persistence.queries.SQLCall("Insert into BOB Value"));
            ((DatabaseSession) session).commitTransaction();
        } catch (org.eclipse.persistence.exceptions.EclipseLinkException e) {
            this.caughtException = e;
            ((DatabaseSession) session).rollbackTransaction();
        }
    } else {
        throw new TestWarningException("Test not run on this database platform");
    }
}
Also used : DatabaseLogin(org.eclipse.persistence.sessions.DatabaseLogin) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) Session(org.eclipse.persistence.sessions.Session) TestWarningException(org.eclipse.persistence.testing.framework.TestWarningException)

Example 4 with DatabasePlatform

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

the class SessionsFactory method processStructConverterConfig.

/**
 * INTERNAL:
 */
@SuppressWarnings({ "unchecked" })
protected void processStructConverterConfig(StructConverterConfig converterClassConfig, DatabaseLogin login) {
    if (converterClassConfig != null) {
        Platform platform = login.getDatasourcePlatform();
        if (platform instanceof DatabasePlatform) {
            Iterator<String> i = converterClassConfig.getStructConverterClasses().iterator();
            while (i.hasNext()) {
                String converterClassName = i.next();
                try {
                    Class<StructConverter> converterClass = (Class<StructConverter>) m_classLoader.loadClass(converterClassName);
                    StructConverter converter = null;
                    if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                        try {
                            converter = AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(converterClass));
                        } catch (PrivilegedActionException ex) {
                            throw (Exception) ex.getCause();
                        }
                    } else {
                        converter = PrivilegedAccessHelper.newInstanceFromClass(converterClass);
                    }
                    ((DatabasePlatform) platform).addStructConverter(converter);
                } catch (Exception exception) {
                    throw SessionLoaderException.failedToLoadTag("struct-converter", converterClassName, exception);
                }
            }
        }
    }
}
Also used : Platform(org.eclipse.persistence.internal.databaseaccess.Platform) DatasourcePlatform(org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform) ServerPlatform(org.eclipse.persistence.platform.server.ServerPlatform) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) NoServerPlatform(org.eclipse.persistence.platform.server.NoServerPlatform) StructConverter(org.eclipse.persistence.platform.database.converters.StructConverter) PrivilegedNewInstanceFromClass(org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass) PrivilegedActionException(java.security.PrivilegedActionException) PrivilegedNewInstanceFromClass(org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) ValidationException(org.eclipse.persistence.exceptions.ValidationException) PrivilegedActionException(java.security.PrivilegedActionException) SessionLoaderException(org.eclipse.persistence.exceptions.SessionLoaderException)

Example 5 with DatabasePlatform

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

the class OwnershipSystem method createTables.

@Override
public void createTables(DatabaseSession session) {
    // Drop old constraints.
    DatabasePlatform platform = (DatabasePlatform) session.getDatasourcePlatform();
    boolean supportsAlterTableConstraints = platform.supportsForeignKeyConstraints() || (platform.supportsUniqueColumns() && !platform.requiresUniqueConstraintCreationOnTableCreate());
    if (supportsAlterTableConstraints)
        try {
            if (platform.isMySQL()) {
                session.executeNonSelectingSQL("Alter TABLE OBJECT_A DROP FOREIGN KEY OWNER_A_ONE_TO_ONE_");
                session.executeNonSelectingSQL("Alter TABLE OBJECT_C DROP FOREIGN KEY OWNER_C_ONE_TO_ONE_");
            } else {
                session.executeNonSelectingSQL("Alter TABLE OBJECT_A DROP CONSTRAINT OWNER_A_ONE_TO_ONE_");
                session.executeNonSelectingSQL("Alter TABLE OBJECT_C DROP CONSTRAINT OWNER_C_ONE_TO_ONE_");
            }
        } catch (Exception ignore) {
        }
    SchemaManager schemaManager = new SchemaManager(session);
    if (supportsAlterTableConstraints) {
        schemaManager.dropConstraints(ObjectA.tableDefinition());
        schemaManager.dropConstraints(ObjectB.tableDefinition());
        schemaManager.dropConstraints(ObjectC.tableDefinition());
        schemaManager.dropConstraints(ObjectD.tableDefinition());
        schemaManager.dropConstraints(ObjectE.tableDefinition());
    }
    schemaManager.replaceObject(ObjectA.tableDefinition());
    schemaManager.replaceObject(ObjectB.tableDefinition());
    schemaManager.replaceObject(ObjectC.tableDefinition());
    schemaManager.replaceObject(ObjectD.tableDefinition());
    schemaManager.replaceObject(ObjectE.tableDefinition());
    if (supportsAlterTableConstraints) {
        schemaManager.createConstraints(ObjectA.tableDefinition());
        schemaManager.createConstraints(ObjectB.tableDefinition());
        schemaManager.createConstraints(ObjectC.tableDefinition());
        schemaManager.createConstraints(ObjectD.tableDefinition());
        schemaManager.createConstraints(ObjectE.tableDefinition());
    }
    schemaManager.createSequences();
}
Also used : 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