Search in sources :

Example 21 with DatabasePlatform

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

the class BaseNcharTest method reset.

@Override
public void reset() {
    if (object != null) {
        UnitOfWork uow = getSession().acquireUnitOfWork();
        uow.deleteObject(object);
        uow.commit();
        object = null;
    }
    controlObject = null;
    DatabasePlatform platform = getSession().getPlatform();
    if (!platform.shouldBindAllParameters()) {
        // restore original value
        platform.setUsesStringBinding(usesStringBindingOriginal);
    }
    if (platformOriginal != null) {
        getSession().getLogin().usePlatform(platformOriginal);
        getDatabaseSession().logout();
        getDatabaseSession().login();
        platformOriginal = null;
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)

Example 22 with DatabasePlatform

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

the class TestParameterBinding method testNUMERICALEXPRESSION_ForceBindJPQLParameters.

@Test
public void testNUMERICALEXPRESSION_ForceBindJPQLParameters() {
    EntityManager em = forceBindEMF.createEntityManager();
    try {
        // Test numerical expression with untyped parameters + typed parameters
        TypedQuery<GenericEntity> query = em.createQuery("SELECT (s.itemInteger1 + ?4) FROM GenericEntity s " + "WHERE (s.itemInteger1 + ?4) > 1", GenericEntity.class);
        query.setParameter(4, 2);
        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", 3, 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 numerical expression with parameters
        TypedQuery<GenericEntity> query = em.createQuery("SELECT (?3 + ?4) FROM GenericEntity s " + "WHERE (?3 + ?4) > 1", GenericEntity.class);
        query.setParameter(3, 2);
        query.setParameter(4, 2);
        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.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();
        // When all the operands of a numeric expression are untyped parameters, error 42X35 on Derby
        if (pl.isDerby()) {
            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 numerical expression with literals
        TypedQuery<GenericEntity> query = em.createQuery("SELECT (s.itemInteger1 + 4) FROM GenericEntity s " + "WHERE ABS(s.itemInteger1 + 4) > 1", GenericEntity.class);
        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", 3, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 0, 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 23 with DatabasePlatform

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

the class TestParameterBinding method testCONCAT_ForceBindJPQLParameters.

@Test
public void testCONCAT_ForceBindJPQLParameters() {
    EntityManager em = forceBindEMF.createEntityManager();
    try {
        // 1: Test string CONCAT with untyped parameter and literal
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 2 FROM GenericEntity s " + "WHERE s.itemString1 = TRIM(CONCAT(?1 , '-'))" + "AND s.itemString1 = TRIM(CONCAT(?2 , '-'))", GenericEntity.class);
        query.setParameter(1, "1");
        query.setParameter(2, "99");
        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 {
        // 1: Test string CONCAT with untyped parameter and untyped parameter
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 2 FROM GenericEntity s " + "WHERE s.itemString1 = TRIM(CONCAT(?1 , ?1))" + "AND s.itemString1 = TRIM(CONCAT(?2 , ?2))", GenericEntity.class);
        query.setParameter(1, "1");
        query.setParameter(2, "99");
        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();
        // When both operands of a CONCAT operator are untyped parameters, error on DB2/z
        if (pl.isDB2Z()) {
            Assert.assertEquals(DatabaseException.class, e.getCause().getClass());
            Assert.assertEquals("com.ibm.db2.jcc.am.SqlSyntaxErrorException", e.getCause().getCause().getClass().getName());
        } else if (pl.isDerby()) {
            // When all the operands of '||' expression 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();
        }
    }
}
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 24 with DatabasePlatform

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

the class TestParameterBinding method testSUBSTR_ForceBindJPQLParameters.

@Test
public void testSUBSTR_ForceBindJPQLParameters() {
    EntityManager em = forceBindEMF.createEntityManager();
    try {
        // Test untyped parameter is first argument of SUBSTRING
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 1 FROM GenericEntity s " + "WHERE TRIM(s.itemString1) = TRIM(SUBSTRING(?1, 1, 5))", GenericEntity.class);
        query.setParameter(1, "HELLO WORLD");
        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", 1, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test untyped parameter is first & second argument of SUBSTRING
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 1 FROM GenericEntity s " + "WHERE TRIM(s.itemString1) = TRIM(SUBSTRING(?1, ?2, 5))", GenericEntity.class);
        query.setParameter(1, "HELLO WORLD");
        query.setParameter(2, 1);
        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 untyped parameter is all arguments of SUBSTRING
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 1 FROM GenericEntity s " + "WHERE s.itemString1 = SUBSTRING(?1, ?2, ?3)", GenericEntity.class);
        query.setParameter(1, "HELLO WORLD");
        query.setParameter(2, 1);
        query.setParameter(3, 5);
        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", 3, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test SUBSTRING function with IN expression
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 1 FROM GenericEntity s " + "WHERE SUBSTRING(s.itemString1, 1, ?1) NOT IN (?2, ?3, ?4, ?5)", GenericEntity.class);
        query.setParameter(1, 5);
        query.setParameter(2, "TEST1");
        query.setParameter(3, "TEST2");
        query.setParameter(4, "HELLO");
        query.setParameter(5, "TEST3");
        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", 7, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 5, 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)

Example 25 with DatabasePlatform

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

the class AdvancedJunitTest method testSwitchBatchDuringSessionEvent.

public void testSwitchBatchDuringSessionEvent() {
    // Test for bug#419326
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        Vegetable vegetable = em.find(Vegetable.class, new VegetablePK("Potato", "Yellow"));
        if (null != vegetable) {
            // cleanup old data
            em.remove(vegetable);
            em.flush();
            commitTransaction(em);
        }
    } catch (RuntimeException e) {
        throw e;
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        clearCache();
        closeEntityManager(em);
    }
    em = createEntityManager();
    DatabasePlatform platform = getServerSession().getPlatform();
    boolean usesBatchWriting = platform.usesBatchWriting();
    boolean usesJDBCBatchWriting = platform.usesJDBCBatchWriting();
    boolean usesExternalConnectionPooling = getServerSession().getLogin().shouldUseExternalConnectionPooling();
    boolean usesExternalTransactionController = getServerSession().getLogin().shouldUseExternalTransactionController();
    final SessionEventAdapter listener = new PostAcquireExclusiveConnectionSqlExecutorListener();
    Vegetable vegetable;
    try {
        // Simulate JDBC batching with external connection pooling
        getServerSession().getLogin().useExternalConnectionPooling();
        getServerSession().getLogin().useExternalTransactionController();
        platform.setUsesBatchWriting(true);
        platform.setUsesJDBCBatchWriting(true);
        em.setProperty(EntityManagerProperties.EXCLUSIVE_CONNECTION_MODE, ExclusiveConnectionMode.Always);
        getServerSession().getEventManager().addListener(listener);
        beginTransaction(em);
        em.setFlushMode(FlushModeType.COMMIT);
        vegetable = em.find(Vegetable.class, new VegetablePK("Potato", "Yellow"));
        if (vegetable == null) {
            vegetable = new Vegetable();
        }
        vegetable.setId(new VegetablePK("Potato", "Yellow"));
        vegetable.setCost(1.10);
        em.persist(vegetable);
        // Here the transaction will be lazily opened, old connection handles released and new
        // connection obtained, triggering postAcquireExclusiveConnection event.
        // The postAcquireExclusiveConnection() invokes a non-batchable statement, due to which
        // current batch is cleared.  This should not cause a duplicate execution of insert.
        commitTransaction(em);
    } catch (jakarta.persistence.RollbackException r) {
        fail("RollbackException exception occurred : " + r.getMessage());
    } finally {
        // Cleanup and revert back to original conditions
        getServerSession().getEventManager().removeListener(listener);
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        platform.setUsesBatchWriting(usesBatchWriting);
        platform.setUsesJDBCBatchWriting(usesJDBCBatchWriting);
        if (!usesExternalConnectionPooling) {
            getServerSession().getLogin().dontUseExternalConnectionPooling();
        }
        if (!usesExternalTransactionController) {
            getServerSession().getLogin().dontUseExternalTransactionController();
        }
        clearCache();
        closeEntityManager(em);
    }
}
Also used : VegetablePK(org.eclipse.persistence.testing.models.jpa.advanced.VegetablePK) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) SessionEventAdapter(org.eclipse.persistence.sessions.SessionEventAdapter) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) Vegetable(org.eclipse.persistence.testing.models.jpa.advanced.Vegetable)

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