Search in sources :

Example 16 with SchemaManager

use of org.eclipse.persistence.tools.schemaframework.SchemaManager in project eclipselink by eclipse-ee4j.

the class TestStoredProcedures method createSimpleStoredProcedure.

/**
 * Creates a simple stored procedure for the given EntityManagerFactory
 * @return boolean indicating if storedProcedure was created
 */
private static boolean createSimpleStoredProcedure(EntityManagerFactory emf) {
    // Setup a stored procedure
    EntityManager em = emf.createEntityManager();
    try {
        StoredProcedureDefinition proc = new StoredProcedureDefinition();
        proc.setName("simple_order_procedure");
        proc.addArgument("in_param_one", String.class, 10);
        proc.addArgument("in_param_two", String.class, 10);
        proc.addArgument("in_param_three", String.class, 10);
        proc.addOutputArgument("out_param_one", String.class, 30);
        DatabaseSession dbs = ((EntityManagerImpl) em).getDatabaseSession();
        SchemaManager manager = new SchemaManager(dbs);
        Platform platform = dbs.getDatasourcePlatform();
        // Add more platform specific diction to support more platforms
        if (platform.isMySQL()) {
            proc.addStatement("SET out_param_one = CONCAT('One: ',in_param_one,' Two: ',in_param_two,' Three: ',in_param_three)");
        } else if (platform.isOracle()) {
            proc.addStatement("out_param_one := 'One: ' || in_param_one || ' Two: ' || in_param_two || ' Three: ' || in_param_three");
        } else if (platform.isDB2() || platform.isDB2Z()) {
            proc.addStatement("SET out_param_one = 'One: ' || in_param_one || ' Two: ' || in_param_two || ' Three: ' || in_param_three");
        } else {
            return false;
        }
        try {
            manager.dropObject(proc);
        } catch (Exception e) {
        // Ignore any drop exceptions since the procedure may not exist yet
        }
        manager.createObject(proc);
        return true;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Platform(org.eclipse.persistence.internal.databaseaccess.Platform) DatabasePlatform(org.eclipse.persistence.platform.database.DatabasePlatform) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) StoredProcedureDefinition(org.eclipse.persistence.tools.schemaframework.StoredProcedureDefinition) SchemaManager(org.eclipse.persistence.tools.schemaframework.SchemaManager)

Example 17 with SchemaManager

use of org.eclipse.persistence.tools.schemaframework.SchemaManager in project eclipselink by eclipse-ee4j.

the class EmployeePopulator method persistExample.

public void persistExample(Session session) {
    Vector allObjects = new Vector();
    UnitOfWork unitOfWork = session.acquireUnitOfWork();
    // unitOfWork.removeAllReadOnlyClasses();
    PopulationManager.getDefaultManager().addAllObjectsForClass(Employee.class, allObjects);
    PopulationManager.getDefaultManager().addAllObjectsForClass(SmallProject.class, allObjects);
    PopulationManager.getDefaultManager().addAllObjectsForClass(LargeProject.class, allObjects);
    PopulationManager.getDefaultManager().addAllObjectsForClass(Runner.class, allObjects);
    unitOfWork.registerAllObjects(allObjects);
    unitOfWork.commit();
    DatabasePlatform platform = session.getLogin().getPlatform();
    if (TestCase.supportsStoredProcedures(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;
        }
        try {
            SchemaManager schema = new SchemaManager((DatabaseSession) session);
            schema.replaceObject(buildStoredProcedureParameterTest(platform));
            schema.replaceObject(buildStoredProcedureReadFromAddress(platform));
            schema.replaceObject(buildStoredProcedureReadFromAddressMappedNamed(platform));
            schema.replaceObject(buildStoredProcedureReadFromAddressMappedNumbered(platform));
            schema.replaceObject(buildStoredProcedureUpdateFromAddress(platform));
            schema.replaceObject(buildStoredProcedureResultSetAndUpdateFromAddress(platform));
            schema.replaceObject(buildStoredProcedureReadAllAddresses());
            schema.replaceObject(buildStoredProcedureReadAllEmployees());
            schema.replaceObject(buildStoredProcedureReadAddressCity(platform));
            schema.replaceObject(buildStoredProcedureDeleteAllResponsibilities());
            if (platform.isOracle()) {
                schema.replaceObject(buildOraclePackage());
                schema.replaceObject(buildStoredProcedureReadUsingNamedRefCursor());
                schema.replaceObject(buildStoredProcedureReadUsingPosRefCursor());
                schema.replaceObject(buildStoredProcedureReadUsingSysCursor());
            }
            if (platform.isMySQL()) {
                schema.replaceObject(buildMySQLResultSetProcedure());
                schema.replaceObject(buildStoredProcedureReadNoAddresses());
            }
        } finally {
            if (useFastTableCreatorAfterInitialCreate && !isFirstCreation) {
                SchemaManager.FAST_TABLE_CREATOR = orig_FAST_TABLE_CREATOR;
            }
        }
        // next time it deletes the rows instead.
        isFirstCreation = false;
    }
    // Force uppercase for Postgres.
    if (platform.isPostgreSQL()) {
        session.getLogin().setShouldForceFieldNamesToUpperCase(true);
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) SchemaManager(org.eclipse.persistence.tools.schemaframework.SchemaManager) Vector(java.util.Vector)

Example 18 with SchemaManager

use of org.eclipse.persistence.tools.schemaframework.SchemaManager in project eclipselink by eclipse-ee4j.

the class NativeSequencingTestSuite method clearSimpleTypeInstances.

@Before
public void clearSimpleTypeInstances() {
    final DatabasePlatform platform = JpaHelper.getServerSession(emf).getPlatform();
    if (!platform.isMySQL()) {
        JpaHelper.getServerSession(emf).getSessionLog().log(SessionLog.WARNING, NativeSequencingTestSuite.class.getName() + " requires MySQL.");
    }
    assumeTrue(platform.isMySQL());
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.createQuery("DELETE FROM " + ENTITY_TYPE).executeUpdate();
    em.createNativeQuery("ALTER TABLE " + TABLE_NAME + " DROP COLUMN SID").executeUpdate();
    em.createNativeQuery("ALTER TABLE " + TABLE_NAME + " ADD COLUMN SID INTEGER PRIMARY KEY AUTO_INCREMENT FIRST").executeUpdate();
    em.close();
    Server session = JpaHelper.getServerSession(emf);
    new SchemaManager(session).replaceSequences();
    session.getSequencingControl().initializePreallocated();
}
Also used : EntityManager(jakarta.persistence.EntityManager) Server(org.eclipse.persistence.sessions.server.Server) DatabasePlatform(org.eclipse.persistence.platform.database.DatabasePlatform) SchemaManager(org.eclipse.persistence.tools.schemaframework.SchemaManager) Before(org.junit.Before)

Example 19 with SchemaManager

use of org.eclipse.persistence.tools.schemaframework.SchemaManager in project eclipselink by eclipse-ee4j.

the class CustomFeaturesJUnitTestSuite method buildOracleStoredProcedureReadFromEmployeeInOut.

public void buildOracleStoredProcedureReadFromEmployeeInOut(Session session) {
    if (TestCase.supportsStoredProcedures(session)) {
        StoredProcedureDefinition proc = new StoredProcedureDefinition();
        proc.setName("Read_Employee_InOut");
        proc.addInOutputArgument("employee_id_v", Integer.class);
        proc.addOutputArgument("nchar_v", "NCHAR");
        String statement = "SELECT NCHARTYPE INTO nchar_v FROM CUSTOM_FEATURE_EMPLOYEE WHERE (ID = employee_id_v)";
        proc.addStatement(statement);
        SchemaManager schema = new SchemaManager(((DatabaseSession) session));
        schema.replaceObject(proc);
    } else
        fail("store procedure is not supported!");
}
Also used : DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) StoredProcedureDefinition(org.eclipse.persistence.tools.schemaframework.StoredProcedureDefinition) SchemaManager(org.eclipse.persistence.tools.schemaframework.SchemaManager)

Example 20 with SchemaManager

use of org.eclipse.persistence.tools.schemaframework.SchemaManager in project eclipselink by eclipse-ee4j.

the class CustomFeaturesJUnitTestSuite method buildOraclePackage.

public PackageDefinition buildOraclePackage(Session session) {
    if (TestCase.supportsStoredProcedures(session)) {
        PackageDefinition types = new PackageDefinition();
        types.setName("Cursor_Type");
        types.addStatement("Type Any_Cursor is REF CURSOR");
        SchemaManager schema = new SchemaManager(((DatabaseSession) session));
        schema.replaceObject(types);
        return types;
    } else {
        fail("store procedure is not supported!");
        return null;
    }
}
Also used : PackageDefinition(org.eclipse.persistence.tools.schemaframework.PackageDefinition) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) SchemaManager(org.eclipse.persistence.tools.schemaframework.SchemaManager)

Aggregations

SchemaManager (org.eclipse.persistence.tools.schemaframework.SchemaManager)40 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)17 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)9 Vector (java.util.Vector)4 DatabasePlatform (org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)4 StoredProcedureDefinition (org.eclipse.persistence.tools.schemaframework.StoredProcedureDefinition)4 EntityManager (jakarta.persistence.EntityManager)3 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)3 EntityManagerFactoryProvider.getConfigPropertyAsString (org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.getConfigPropertyAsString)3 DatabaseSessionImpl (org.eclipse.persistence.internal.sessions.DatabaseSessionImpl)3 DatabasePlatform (org.eclipse.persistence.platform.database.DatabasePlatform)3 SessionBroker (org.eclipse.persistence.sessions.broker.SessionBroker)3 Employee (org.eclipse.persistence.testing.models.employee.domain.Employee)3 DynamicClassLoader (org.eclipse.persistence.dynamic.DynamicClassLoader)2 DynamicEntity (org.eclipse.persistence.dynamic.DynamicEntity)2 DynamicHelper (org.eclipse.persistence.dynamic.DynamicHelper)2 DynamicType (org.eclipse.persistence.dynamic.DynamicType)2 Platform (org.eclipse.persistence.internal.databaseaccess.Platform)2 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)2 ManyToManyMapping (org.eclipse.persistence.mappings.ManyToManyMapping)2