Search in sources :

Example 36 with SchemaManager

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

the class AdvancedJPAJunitTest method testSetup.

/**
 * The setup is done as a test, both to record its failure, and to allow execution in the server.
 */
public void testSetup() {
    ServerSession session = JUnitTestCase.getServerSession();
    new AdvancedTableCreator().replaceTables(session);
    // The EquipmentCode class 'should' be set to read only. We want
    // to be able to create a couple in the Employee populator, so
    // force the read only to false. If EquipmentCode is not
    // actually read only, don't worry, we set the original read
    // only value back on the descriptor and the error will be
    // caught in a later test in this suite.
    ClassDescriptor descriptor = session.getDescriptor(EquipmentCode.class);
    boolean shouldBeReadOnly = descriptor.shouldBeReadOnly();
    descriptor.setShouldBeReadOnly(false);
    // Populate the database with our examples.
    EmployeePopulator employeePopulator = new EmployeePopulator();
    employeePopulator.buildExamples();
    employeePopulator.persistExample(session);
    descriptor.setShouldBeReadOnly(shouldBeReadOnly);
    clearCache();
    // create stored function when database supports it
    if (supportsStoredFunctions()) {
        SchemaManager schema = new SchemaManager(session);
        schema.replaceObject(buildStoredFunction());
    }
}
Also used : ServerSession(org.eclipse.persistence.sessions.server.ServerSession) AdvancedTableCreator(org.eclipse.persistence.testing.models.jpa.advanced.AdvancedTableCreator) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) EmployeePopulator(org.eclipse.persistence.testing.models.jpa.advanced.EmployeePopulator) SchemaManager(org.eclipse.persistence.tools.schemaframework.SchemaManager)

Example 37 with SchemaManager

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

the class TestStoredProceduresCursors method createCursorStoredProcedure.

private static boolean createCursorStoredProcedure(EntityManagerFactory emf) {
    // Setup a stored procedure
    EntityManager em = emf.createEntityManager();
    try {
        StoredProcedureDefinition proc = new StoredProcedureDefinition();
        proc.setName("simple_cursor_procedure");
        proc.addArgument("in_param_one", Integer.class, 10);
        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.isOracle()) {
            proc.addOutputArgument("out_cursor_one", "SYS_REFCURSOR");
            proc.addStatement("OPEN out_cursor_one FOR SELECT ITEM_STRING1 FROM STORED_PROCEDURE_ENTITY WHERE ITEM_INTEGER1 = in_param_one");
        } else if (platform.isDB2()) {
            proc.addOutputArgument("out_cursor_one", "CURSOR");
            proc.addStatement("SET out_cursor_one = CURSOR FOR SELECT ITEM_STRING1 FROM STORED_PROCEDURE_ENTITY WHERE ITEM_INTEGER1 = in_param_one; OPEN out_cursor_one");
        } 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 38 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();
    PopulationManager.getDefaultManager().addAllObjectsForClass(Employee.class, allObjects);
    PopulationManager.getDefaultManager().addAllObjectsForClass(SmallProject.class, allObjects);
    PopulationManager.getDefaultManager().addAllObjectsForClass(LargeProject.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(buildStoredProcedureReadFromAddress(platform));
            schema.replaceObject(buildStoredProcedureReadFromAddressMappedNamed(platform));
            schema.replaceObject(buildStoredProcedureReadFromAddressMappedNumbered(platform));
            schema.replaceObject(buildStoredProcedureReadAllAddresses());
            if (platform.isOracle()) {
                schema.replaceObject(buildOraclePackage());
                schema.replaceObject(buildStoredProcedureReadUsingNamedRefCursor());
                schema.replaceObject(buildStoredProcedureReadUsingUnNamedRefCursor());
            }
            if (platform.isMySQL()) {
                schema.replaceObject(buildMySQLResultSetProcedure());
            }
        } 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 39 with SchemaManager

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

the class EntityManagerSetupImpl method writeMetadataDDLToDatabase.

/**
 * INTERNAL:
 * Generate and write DDL from the persistence unit metadata to the database.
 */
protected void writeMetadataDDLToDatabase(TableCreationType tableCreationType, Map props, DatabaseSessionImpl session, ClassLoader classLoader) {
    SchemaManager mgr = new SchemaManager(session);
    // Set the create database schemas flag on the schema manager.
    String createSchemas = getConfigPropertyAsString(SCHEMA_GENERATION_CREATE_DATABASE_SCHEMAS, props);
    mgr.setCreateDatabaseSchemas(createSchemas != null && createSchemas.equalsIgnoreCase("true"));
    writeDDLToDatabase(mgr, tableCreationType);
}
Also used : SchemaManager(org.eclipse.persistence.tools.schemaframework.SchemaManager) EntityManagerFactoryProvider.getConfigPropertyAsString(org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.getConfigPropertyAsString)

Example 40 with SchemaManager

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

the class EntityManagerSetupImpl method writeDDL.

/**
 * INTERNAL:
 * Generate the DDL from the persistence unit metadata. This DDL generation
 * utilizes the EclipseLink DDL properties.
 */
protected void writeDDL(String ddlGeneration, Map props, DatabaseSessionImpl session, ClassLoader classLoader) {
    // By default the table creation type will be 'none'.
    TableCreationType ddlType = TableCreationType.NONE;
    if (ddlGeneration.equals(PersistenceUnitProperties.CREATE_ONLY)) {
        ddlType = TableCreationType.CREATE;
    } else if (ddlGeneration.equals(PersistenceUnitProperties.DROP_ONLY)) {
        ddlType = TableCreationType.DROP;
    } else if (ddlGeneration.equals(PersistenceUnitProperties.DROP_AND_CREATE)) {
        ddlType = TableCreationType.DROP_AND_CREATE;
    } else if (ddlGeneration.equals(PersistenceUnitProperties.CREATE_OR_EXTEND)) {
        ddlType = TableCreationType.EXTEND;
    } else {
        // Log a warning if we have an unknown ddl generation.
        String validOptions = PersistenceUnitProperties.NONE + ", " + PersistenceUnitProperties.CREATE_ONLY + ", " + PersistenceUnitProperties.DROP_ONLY + ", " + PersistenceUnitProperties.DROP_AND_CREATE + ", " + PersistenceUnitProperties.CREATE_OR_EXTEND;
        session.log(SessionLog.WARNING, SessionLog.PROPERTIES, "ddl_generation_unknown_property_value", new Object[] { PersistenceUnitProperties.DDL_GENERATION, ddlGeneration, persistenceUnitInfo.getPersistenceUnitName(), validOptions });
    }
    if (ddlType != TableCreationType.NONE) {
        String ddlGenerationMode = getConfigPropertyAsString(PersistenceUnitProperties.DDL_GENERATION_MODE, props, PersistenceUnitProperties.DEFAULT_DDL_GENERATION_MODE);
        // Optimize for cases where the value is explicitly set to NONE
        if (!ddlGenerationMode.equals(NONE)) {
            if (isCompositeMember()) {
                // debug output added to make it easier to navigate the log because the method is called outside of composite member deploy
                session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "composite_member_begin_call", new Object[] { "generateDDL", persistenceUnitInfo.getPersistenceUnitName(), state });
            }
            SchemaManager mgr = new SchemaManager(session);
            if (ddlGenerationMode.equals(PersistenceUnitProperties.DDL_DATABASE_GENERATION) || ddlGenerationMode.equals(PersistenceUnitProperties.DDL_BOTH_GENERATION)) {
                writeDDLToDatabase(mgr, ddlType);
            }
            if (ddlGenerationMode.equals(PersistenceUnitProperties.DDL_SQL_SCRIPT_GENERATION) || ddlGenerationMode.equals(PersistenceUnitProperties.DDL_BOTH_GENERATION)) {
                String appLocation = getConfigPropertyAsString(PersistenceUnitProperties.APP_LOCATION, props, PersistenceUnitProperties.DEFAULT_APP_LOCATION);
                // These could be a string (file name urls) or actual writers.
                Object createDDLJdbc = getConfigProperty(PersistenceUnitProperties.CREATE_JDBC_DDL_FILE, props, PersistenceUnitProperties.DEFAULT_CREATE_JDBC_FILE_NAME);
                Object dropDDLJdbc = getConfigProperty(PersistenceUnitProperties.DROP_JDBC_DDL_FILE, props, PersistenceUnitProperties.DEFAULT_DROP_JDBC_FILE_NAME);
                writeDDLToFiles(mgr, appLocation, createDDLJdbc, dropDDLJdbc, ddlType, props);
            }
            // Log a warning if we have an unknown ddl generation mode.
            if ((!ddlGenerationMode.equals(PersistenceUnitProperties.DDL_DATABASE_GENERATION)) && (!ddlGenerationMode.equals(PersistenceUnitProperties.DDL_SQL_SCRIPT_GENERATION)) && (!ddlGenerationMode.equals(PersistenceUnitProperties.DDL_BOTH_GENERATION))) {
                String validOptions = PersistenceUnitProperties.DDL_DATABASE_GENERATION + ", " + PersistenceUnitProperties.DDL_SQL_SCRIPT_GENERATION + ", " + PersistenceUnitProperties.DDL_BOTH_GENERATION;
                session.log(SessionLog.WARNING, SessionLog.PROPERTIES, "ddl_generation_unknown_property_value", new Object[] { PersistenceUnitProperties.DDL_GENERATION_MODE, ddlGenerationMode, persistenceUnitInfo.getPersistenceUnitName(), validOptions });
            }
            if (isCompositeMember()) {
                // debug output added to make it easier to navigate the log because the method is called outside of composite member deploy
                session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "composite_member_end_call", new Object[] { "generateDDL", persistenceUnitInfo.getPersistenceUnitName(), state });
            }
        }
    }
}
Also used : EntityManagerFactoryProvider.getConfigPropertyAsString(org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.getConfigPropertyAsString) 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