Search in sources :

Example 31 with DatabasePlatform

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

the class LOBTestWrapper method reset.

@Override
public void reset() throws Throwable {
    super.reset();
    DatabasePlatform platform = getSession().getPlatform();
    if (!shouldUseLocatorForLOBWrite) {
        platform.setUsesStringBinding(usesStringBindingOriginal);
    }
    if (platform instanceof Oracle8Platform) {
        Oracle8Platform platform8 = (Oracle8Platform) platform;
        if (shouldSetUseLocatorForLOBWriteIntoPlatform) {
            platform8.setShouldUseLocatorForLOBWrite(shouldUseLocatorForLOBWriteOriginal);
        }
    }
}
Also used : Oracle8Platform(org.eclipse.persistence.platform.database.oracle.Oracle8Platform) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)

Example 32 with DatabasePlatform

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

the class BaseNcharTest method setup.

@Override
protected void setup() {
    if (!getSession().getPlatform().isOracle()) {
        throw new TestWarningException("This test case works on Oracle only");
    }
    try {
        getOracle9Platform();
    } catch (ClassCastException ex) {
        DatabasePlatform platform = getSession().getPlatform();
        try {
            getSession().getLogin().usePlatform(new org.eclipse.persistence.platform.database.oracle.Oracle9Platform());
            getDatabaseSession().logout();
            getDatabaseSession().login();
            platformOriginal = platform;
            getOracle9Platform();
        } catch (Exception ex2) {
            throw new TestWarningException("This test case works with Oracle9 platform or higher");
        }
    }
    DatabasePlatform platform = getSession().getPlatform();
    if (!platform.shouldBindAllParameters()) {
        // without binding string binding must be enabled to so that a big String for NCLOB is still bound.
        usesStringBindingOriginal = platform.usesStringBinding();
        platform.setUsesStringBinding(true);
    }
}
Also used : Oracle9Platform(org.eclipse.persistence.platform.database.oracle.Oracle9Platform) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)

Example 33 with DatabasePlatform

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

the class EntityManagerJUnitTestSuite method testNativeSequences.

public void testNativeSequences() {
    SessionBroker broker = getSessionBroker();
    DatabasePlatform platform = (DatabasePlatform) broker.getPlatform(Address.class);
    boolean doesPlatformSupportIdentity = platform.supportsIdentity();
    boolean doesPlatformSupportSequenceObjects = platform.supportsSequenceObjects();
    String errorMsg = "";
    if (doesPlatformSupportIdentity != doesPlatformSupportSequenceObjects) {
        // SEQ_GEN_IDENTITY sequence defined by
        // @GeneratedValue(strategy=IDENTITY)
        Sequence identitySequence = platform.getSequence("SEQ_GEN_IDENTITY");
        if (identitySequence != null) {
            boolean isIdentity = identitySequence.shouldAcquireValueAfterInsert();
            if (doesPlatformSupportIdentity != isIdentity) {
                errorMsg = "SEQ_GEN_IDENTITY: doesPlatformSupportIdentity = " + doesPlatformSupportIdentity + ", but isIdentity = " + isIdentity + "; ";
            }
        }
    }
    // ADDRESS_BR1_SEQ sequence defined by
    // @GeneratedValue(generator="ADDRESS_BR1_SEQ")
    // @SequenceGenerator(name="ADDRESS_BR1_SEQ", allocationSize=25)
    boolean isSequenceObject = !platform.getSequence("ADDRESS_BR1_SEQ").shouldAcquireValueAfterInsert();
    if (doesPlatformSupportSequenceObjects != isSequenceObject) {
        errorMsg = errorMsg + "ADDRESS_BR1_SEQ: doesPlatformSupportSequenceObjects = " + doesPlatformSupportSequenceObjects + ", but isSequenceObject = " + isSequenceObject;
    }
    if (errorMsg.length() > 0) {
        fail(errorMsg);
    }
}
Also used : Address(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.Address) SessionBroker(org.eclipse.persistence.sessions.broker.SessionBroker) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) Sequence(org.eclipse.persistence.sequencing.Sequence) NativeSequence(org.eclipse.persistence.sequencing.NativeSequence)

Example 34 with DatabasePlatform

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

the class TestParameterBinding method testIN_ForceBindJPQLParameters.

@Test
public void testIN_ForceBindJPQLParameters() {
    EntityManager em = forceBindEMF.createEntityManager();
    try {
        // Test all the operands of an IN predicate
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 2 FROM GenericEntity s " + "WHERE ?1 IN (?2, ?3, ?4)", GenericEntity.class);
        query.setParameter(1, 4);
        query.setParameter(2, 4);
        query.setParameter(3, 5);
        query.setParameter(4, 6);
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.isDB2Z() || pl.isDerby()) {
            Assert.fail("Expected a failure from " + pl);
        }
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 5, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 4, call.getParameters().size());
        }
    } catch (PersistenceException e) {
        Platform pl = forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.isDB2Z()) {
            // When all the operands of an IN predicate are untyped parameters, error on DB2/z
            Assert.assertEquals(DatabaseException.class, e.getCause().getClass());
            Assert.assertEquals("com.ibm.db2.jcc.am.SqlSyntaxErrorException", e.getCause().getCause().getClass().getName());
        } else if (pl.isDerby()) {
            // Use as the left operand of an IN list is not allowed when all operands are untyped parameters, error 42X35 on Derby
            Assert.assertEquals(DatabaseException.class, e.getCause().getClass());
            Assert.assertEquals("java.sql.SQLSyntaxErrorException", e.getCause().getCause().getClass().getName());
        } else {
            Assert.fail("Unexpected failure: " + e);
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test the first and second operands of an IN predicate
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 2 FROM GenericEntity s " + "WHERE ?1 IN (?2, 'b', 'c')", GenericEntity.class);
        query.setParameter(1, "a");
        query.setParameter(2, "a");
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 5, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 2, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test the first operand of an IN predicate and zero or more operands of the IN list except for the first operand of the IN list
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 2 FROM GenericEntity s " + "WHERE ?1 IN (5, ?2, 6)", GenericEntity.class);
        query.setParameter(1, 4);
        query.setParameter(2, 4);
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 5, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 2, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test any or all operands of the IN list of the IN predicate and the first operand of the IN predicate is not an untyped parameter marker
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 2 FROM GenericEntity s " + "WHERE s.itemString1 IN (?1, 'b', ?2)", GenericEntity.class);
        query.setParameter(1, "a");
        query.setParameter(2, "c");
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 4, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 2, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
}
Also used : DatabaseCall(org.eclipse.persistence.internal.databaseaccess.DatabaseCall) EntityManager(jakarta.persistence.EntityManager) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) Platform(org.eclipse.persistence.internal.databaseaccess.Platform) GenericEntity(org.eclipse.persistence.jpa.test.property.model.GenericEntity) PersistenceException(jakarta.persistence.PersistenceException) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) Test(org.junit.Test)

Example 35 with DatabasePlatform

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

the class TestParameterBinding method testLIKE_ForceBindJPQLParameters.

@Test
public void testLIKE_ForceBindJPQLParameters() {
    EntityManager em = forceBindEMF.createEntityManager();
    try {
        // Test the first operand of a LIKE predicate (the match-expression) is untyped parameter
        // when at least one other operand (the pattern-expression or escape-expression)
        // is not an untyped parameter marker
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 1 FROM GenericEntity s " + "WHERE ?1 LIKE ?2 ESCAPE '_'", GenericEntity.class);
        query.setParameter(1, "HELLO_WORLD");
        query.setParameter(2, "HELLO%");
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 4, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 2, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test the first operand of a LIKE predicate (the match-expression) is untyped parameter
        // when at least one other operand (the pattern-expression or escape-expression)
        // is not an untyped parameter marker
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 1 FROM GenericEntity s " + "WHERE s.itemString1 LIKE ?2", GenericEntity.class);
        query.setParameter(2, "HELLO%");
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 2, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 1, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
}
Also used : DatabaseCall(org.eclipse.persistence.internal.databaseaccess.DatabaseCall) EntityManager(jakarta.persistence.EntityManager) GenericEntity(org.eclipse.persistence.jpa.test.property.model.GenericEntity) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) Test(org.junit.Test)

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