use of org.eclipse.persistence.internal.databaseaccess.DatabaseCall in project eclipselink by eclipse-ee4j.
the class ReadObjectQuery method executeObjectLevelReadQueryFromResultSet.
/**
* INTERNAL:
* Execute the query building the objects directly from the database result-set.
* @exception DatabaseException - an error has occurred on the database
* @return object - the first object found or null if none.
*/
@Override
protected Object executeObjectLevelReadQueryFromResultSet() throws DatabaseException {
AbstractSession session = this.session;
DatabasePlatform platform = session.getPlatform();
DatabaseCall call = ((DatasourceCallQueryMechanism) this.queryMechanism).selectResultSet();
Statement statement = call.getStatement();
ResultSet resultSet = call.getResult();
DatabaseAccessor accessor = (DatabaseAccessor) ((List<Accessor>) this.accessors).get(0);
boolean exceptionOccured = false;
try {
if (!resultSet.next()) {
return null;
}
ResultSetMetaData metaData = resultSet.getMetaData();
return this.descriptor.getObjectBuilder().buildObjectFromResultSet(this, null, resultSet, session, accessor, metaData, platform, call.getFields(), call.getFieldsArray());
} catch (SQLException exception) {
exceptionOccured = true;
DatabaseException commException = accessor.processExceptionForCommError(session, exception, call);
if (commException != null) {
throw commException;
}
throw DatabaseException.sqlException(exception, call, accessor, session, false);
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
accessor.releaseStatement(statement, call.getSQLString(), call, session);
}
if (accessor != null) {
session.releaseReadConnection(accessor);
}
} catch (SQLException exception) {
if (!exceptionOccured) {
// in the case of an external connection pool the connection may be null after the statement release
// if it is null we will be unable to check the connection for a comm error and
// therefore must return as if it was not a comm error.
DatabaseException commException = accessor.processExceptionForCommError(session, exception, call);
if (commException != null) {
throw commException;
}
throw DatabaseException.sqlException(exception, call, accessor, session, false);
}
}
}
}
use of org.eclipse.persistence.internal.databaseaccess.DatabaseCall in project eclipselink by eclipse-ee4j.
the class PLSQLTestSuite method testOracleTypeProcessing.
/**
* Test processing of OracleObject and OracleArray annotations.
*
* @see OracleArray
* @see org.eclipse.persistence.platform.database.oracle.annotations.OracleObject
*/
public void testOracleTypeProcessing() {
if (!getServerSession().getPlatform().isOracle()) {
return;
}
EntityManager em = createEntityManager();
beginTransaction(em);
try {
Query query = em.createNamedQuery("TEST_ORACLE_TYPES");
assertNotNull("EntityManager could not create query [TEST_ORACLE_TYPES]", query);
assertTrue("Expected EJBQueryImpl but was [" + query.getClass().getName() + "]", query instanceof EJBQueryImpl);
DatabaseCall call = ((EJBQueryImpl) query).getDatabaseQuery().getCall();
assertNotNull("The DatabaseCall was not set on the query", call);
assertTrue("Expected PLSQLStoredProcedureCall but was [" + call.getClass().getName() + "]", call instanceof PLSQLStoredProcedureCall);
PLSQLStoredProcedureCall plsqlCall = (PLSQLStoredProcedureCall) call;
List<PLSQLargument> args = plsqlCall.getArguments();
assertTrue("Expected 2 arguments, but was [" + args.size() + "]", args.size() == 2);
boolean foundINArg = false;
boolean foundOUTArg = false;
for (PLSQLargument arg : args) {
if (arg.name.equals("P_IN")) {
foundINArg = true;
assertNotNull("databaseType for arg P_IN is null", arg.databaseType);
assertTrue("Expected arg P_IN to be an OracleArrayType, but was [" + arg.databaseType.getClass().getName() + "]", arg.databaseType instanceof OracleArrayType);
OracleArrayType arrayType = (OracleArrayType) arg.databaseType;
assertTrue("Expected arg P_IN to have databaseType set with type name VARRAY_NUMERO_UNO, but was [" + arrayType.getTypeName() + "]", arrayType.getTypeName().equals("VARRAY_NUMERO_UNO"));
assertNotNull("Expected VARRAY_NUMERO_UNO to have nested type VARCHAR, but was null", arrayType.getNestedType());
assertTrue("Expected VARRAY_NUMERO_UNO to have nested type VARCHAR, but was [" + arrayType.getNestedType().getTypeName() + "]", arrayType.getNestedType().getTypeName().equals("VARCHAR"));
} else if (arg.name.equals("P_OUT")) {
foundOUTArg = true;
assertNotNull("databaseType for arg P_OUT is null", arg.databaseType);
assertTrue("Expected arg P_OUT to be an OracleObjectType, but was [" + arg.databaseType.getClass().getName() + "]", arg.databaseType instanceof OracleObjectType);
OracleObjectType objectType = (OracleObjectType) arg.databaseType;
assertTrue("Expected arg P_OUT to have databaseType set with type name OBJECT_NUMERO_DOS, but was [" + objectType.getTypeName() + "]", objectType.getTypeName().equals("OBJECT_NUMERO_DOS"));
assertTrue("Expected OBJECT_NUMERO_DOS to have 2 fields, but was [" + objectType.getFields().size() + "]", objectType.getFields().size() == 2);
for (String key : objectType.getFields().keySet()) {
DatabaseType dbType = objectType.getFields().get(key);
if (key.equals("OO_FLD1")) {
assertTrue("Expected field OO_FLD1 to have databaseType NUMERIC, but was [" + dbType.getTypeName() + "]", dbType.getTypeName().equals("NUMERIC"));
} else if (key.equals("OO_FLD2")) {
assertTrue("Expected field OO_FLD2 to have databaseType NUMERIC, but was [" + dbType.getTypeName() + "]", dbType.getTypeName().equals("NUMERIC"));
} else {
fail("Expected OBJECT_NUMERO_DOS to have fields OO_FLD1 and OO_FLD2 but encountered field [" + key + "]");
}
}
} else {
fail("Expected arg name to be one of P_IN or P_OUT, but was [" + arg.name + "]");
}
}
assertTrue("IN arg P_IN was not processed", foundINArg);
assertTrue("OUT arg P_OUT was not processed", foundOUTArg);
} finally {
closeEntityManagerAndTransaction(em);
}
}
use of org.eclipse.persistence.internal.databaseaccess.DatabaseCall 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.DatabaseCall 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.DatabaseCall 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();
}
}
}
Aggregations