Search in sources :

Example 11 with SchemaManager

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

the class TablesCheckTestSuite method setup.

/**
 * Tests suite setup: Creates database table for the tests.
 */
@Override
public void setup() {
    final TableCreator creator = new TableCreator();
    creator.addTableDefinition(BEER_TD);
    final DatabaseSession session = getDatabaseSession();
    final SchemaManager manager = new SchemaManager(getDatabaseSession());
    // Table life cycle is related to this test life cycle.
    creator.createTables(session, manager);
}
Also used : TableCreator(org.eclipse.persistence.tools.schemaframework.TableCreator) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) SchemaManager(org.eclipse.persistence.tools.schemaframework.SchemaManager)

Example 12 with SchemaManager

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

the class OracleNativeSeqInitTest method setup.

@Override
public void setup() {
    if (!getSession().getPlatform().supportsSequenceObjects()) {
        throw new TestWarningException("This test requires a platform that supports sequence objects");
    }
    ClassDescriptor descriptor = getSession().getDescriptor(Employee.class);
    if (!descriptor.usesSequenceNumbers()) {
        throw new TestWarningException("Employee doesn't use sequencing");
    }
    originalSequence = getSession().getPlatform().getSequence(descriptor.getSequenceNumberName());
    usesNativeSequencingOriginal = (originalSequence instanceof NativeSequence || (originalSequence instanceof DefaultSequence && getSession().getPlatform().getDefaultSequence() instanceof NativeSequence)) && !originalSequence.shouldAcquireValueAfterInsert();
    if (!usesNativeSequencingOriginal) {
        NativeSequence newSequence = new NativeSequence(originalSequence.getName(), originalSequence.getPreallocationSize());
        newSequence.onConnect(originalSequence.getDatasourcePlatform());
        getAbstractSession().getPlatform().addSequence(newSequence);
        sequence = newSequence;
    } else {
        sequence = originalSequence;
    }
    seqPreallocationSizeOriginal = originalSequence.getPreallocationSize();
    lastSeqNumberOriginal = getSession().getNextSequenceNumberValue(Employee.class).intValue() - 1;
    usesBatchWritingOriginal = getSession().getPlatform().usesBatchWriting();
    shouldCacheAllStatementsOriginal = getSession().getPlatform().shouldCacheAllStatements();
    getDatabaseSession().getSequencingControl().initializePreallocated();
    sequenceDefinition = new SequenceObjectDefinition(sequence);
    sequenceDefinition.setQualifier(getSession().getLogin().getTableQualifier());
    if (shouldUseSchemaManager) {
        schemaManager = new SchemaManager(getDatabaseSession());
        // make sure that upcoming DROP and CREATE haven't been cached
        // and therefore for sure will go through
        getSession().getPlatform().setShouldCacheAllStatements(false);
        // This is the worst case scenario settings - SchemaManager should handle it.
        getSession().getPlatform().setUsesBatchWriting(true);
        getSession().getPlatform().setShouldCacheAllStatements(true);
    } else {
        getSession().getPlatform().setUsesBatchWriting(false);
        getSession().getPlatform().setShouldCacheAllStatements(false);
    }
    // all three modes start with dropping an existing sequence (if any)
    try {
        drop();
    } catch (DatabaseException exception) {
    // Ignore already deleted
    }
    if (mode == DROP_CREATE) {
        // sequence doesn't exist.
        // create sequence with seqPreallocationSize.
        // note that both increment and starting value are set to
        // sequenceDefinition.getIncrement()
        sequence.setInitialValue(1);
        sequence.setPreallocationSize(seqPreallocationSize);
        create();
        // next available sequence number.
        idExpected = 1;
    } else if (mode == CREATE_CREATE) {
        // sequence doesn't exist,
        // create sequence with seqPreallocationSizeOld
        // note that both increment and starting value are set to
        // sequenceDefinition.getIncrement()
        sequence.setInitialValue(1);
        sequence.setPreallocationSize(seqPreallocationSizeOld);
        create();
        // now sequence exists,
        // create sequence with seqPreallocationSize
        // Note that createOnDatabase will call alterOnDatabase
        sequence.setInitialValue(1);
        sequence.setPreallocationSize(seqPreallocationSize);
        create();
        // next available sequence number.
        // note that the second createOnDatabase selects NEXTVAL during existance check,
        // because it is the first call to NEXTVAL, the starting sequence value is returned,
        // and this value was set to seqPreallocationSizeOld by the first createOnDatabase
        idExpected = 1 + seqPreallocationSizeOld;
    } else if (mode == NEXTVAL_ALTER) {
        // sequence doesn't exist,
        // create sequence with seqPreallocationSizeOld
        // note that both increment and starting value are set to
        // sequenceDefinition.getIncrement()
        sequence.setInitialValue(1);
        sequence.setPreallocationSize(seqPreallocationSizeOld);
        create();
        // now sequence exists,
        // select NEXTVAL
        // because it is the first call to NEXTVAL, the starting sequence value is returned,
        // and this value was set to seqPreallocationSizeOld by the first createOnDatabase
        sequenceDefinition.checkIfExist((AbstractSession) getSession());
        // alter increment of sequence with seqPreallocationSize.
        sequence.setInitialValue(1);
        sequence.setPreallocationSize(seqPreallocationSize);
        alter();
        // next available sequence number.
        // because there was just one call to NEXTVAL, the starting sequence value is returned,
        // and this value was set to seqPreallocationSizeOld by createOnDatabase
        idExpected = 1 + seqPreallocationSizeOld;
    } else if (mode == CREATE_ALTER) {
        // sequence doesn't exist,
        // create sequence with seqPreallocationSizeOld
        // note that both increment and starting value are set to
        // sequenceDefinition.getIncrement()
        sequence.setInitialValue(1);
        sequence.setPreallocationSize(seqPreallocationSizeOld);
        create();
        // alter increment of sequence with seqPreallocationSize.
        sequence.setInitialValue(1);
        sequence.setPreallocationSize(seqPreallocationSize);
        alter();
        // next available sequence number.
        idExpected = 1;
    }
    getSession().getPlatform().getSequence(descriptor.getSequenceNumberName()).setPreallocationSize(seqPreallocationSize);
}
Also used : NativeSequence(org.eclipse.persistence.sequencing.NativeSequence) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DefaultSequence(org.eclipse.persistence.sequencing.DefaultSequence) SequenceObjectDefinition(org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition) SchemaManager(org.eclipse.persistence.tools.schemaframework.SchemaManager)

Example 13 with SchemaManager

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

the class TogglingFastTableCreator method replaceTables.

@Override
public void replaceTables(DatabaseSession session) {
    // on Symfoware, to avoid table locking issues only the first invocation
    // of an instance of this class (drops & re-)creates the tables
    // if the system property is set.
    session.getSessionLog().log(SessionLog.FINEST, "TogglingFastTableCreator: useFastTableCreatorAfterInitialCreate: " + useFastTableCreatorAfterInitialCreate);
    boolean isFirstCreate = !isFastTableCreator();
    session.getSessionLog().log(SessionLog.FINEST, "TogglingFastTableCreator: " + getTableCreatorName() + " - isFirstCreate: " + isFirstCreate);
    session.getSessionLog().log(SessionLog.FINEST, "TogglingFastTableCreator: Current fastTableCreators: " + fastTableCreators);
    if (useFastTableCreatorAfterInitialCreate && !isFirstCreate) {
        session.getSessionLog().log(SessionLog.FINEST, "TogglingFastTableCreator: " + getTableCreatorName() + " - toggling true");
        String sequenceTableName = getSequenceTableName(session);
        List<TableDefinition> tables = getTableDefinitions();
        for (TableDefinition table : tables) {
            if (!table.getName().equals(sequenceTableName)) {
                SchemaManager schemaManager = new SchemaManager(session);
                AbstractSession abstarctSession = schemaManager.getSession();
                try {
                    abstarctSession.priviledgedExecuteNonSelectingCall(new org.eclipse.persistence.queries.SQLCall("DELETE FROM " + table.getFullName()));
                } catch (DatabaseException ex) {
                // Ignore database exception. eg. If there is no table to delete, it gives database exception.
                }
            }
        }
    } else {
        super.replaceTables(session);
    }
    // next time just delete the rows instead.
    if (useFastTableCreatorAfterInitialCreate) {
        setFastTableCreator();
        session.getSessionLog().log(SessionLog.FINEST, "TogglingFastTableCreator: " + getTableCreatorName() + " added to fastTableCreators");
    }
}
Also used : TableDefinition(org.eclipse.persistence.tools.schemaframework.TableDefinition) SchemaManager(org.eclipse.persistence.tools.schemaframework.SchemaManager) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 14 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();
    // Disable the read-only classes for model population. Specifically,
    // in this case we want to be able to create EquipmentCode objects.
    unitOfWork.removeAllReadOnlyClasses();
    PopulationManager.getDefaultManager().addAllObjectsForClass(Employee.class, allObjects);
    PopulationManager.getDefaultManager().addAllObjectsForClass(SmallProject.class, allObjects);
    PopulationManager.getDefaultManager().addAllObjectsForClass(LargeProject.class, allObjects);
    /*        PopulationManager.getDefaultManager().addAllObjectsForClass(GoldBuyer.class, allObjects);
        PopulationManager.getDefaultManager().addAllObjectsForClass(PlatinumBuyer.class, allObjects);*/
    PopulationManager.getDefaultManager().addAllObjectsForClass(EquipmentCode.class, allObjects);
    unitOfWork.registerAllObjects(allObjects);
    unitOfWork.commit();
    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(buildOracleStoredProcedureReadFromAddress((DatabaseSession) session));
            schema.replaceObject(buildOracleStoredProcedureReadInOut((DatabaseSession) session));
        } 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 (session.getPlatform().isPostgreSQL()) {
        session.getLogin().setShouldForceFieldNamesToUpperCase(true);
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) SchemaManager(org.eclipse.persistence.tools.schemaframework.SchemaManager)

Example 15 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)

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