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;
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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);
}
}
Aggregations