use of javax.jdo.Transaction in project tests by datanucleus.
the class SQLQueryTest method testNamedParameters.
/**
* Test use of an SQL query using named parameters (":myname1", ":myothername2", etc).
* @throws Exception
*/
public void testNamedParameters() throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// insert new elements for table person
tx.begin();
Person p = new Person(1, "John", "Smith", "a@jpox.org");
pm.makePersistent(p);
p = new Person(2, "Paul", "Smith", "b@jpox.org");
pm.makePersistent(p);
p = new Person(3, "Paul", "Green", "c@jpox.org");
pm.makePersistent(p);
p = new Person(4, "John", "Brown", "d@jpox.org");
pm.makePersistent(p);
p = new Person(5, "Jason", "White", "e@jpox.org");
pm.makePersistent(p);
p = new Person(6, "John", "White", "f@jpox.org");
pm.makePersistent(p);
tx.commit();
tx.begin();
// Use numbered params and reuse param 1
String sqlText = "SELECT count(*) FROM PERSON WHERE (FIRSTNAME = :firstName AND LASTNAME = :surname1) OR " + "(FIRSTNAME = :firstName AND LASTNAME = :surname2)";
Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
Map params = new HashMap();
params.put("firstName", "John");
params.put("surname1", "Smith");
params.put("surname2", "Brown");
List list = (List) query.executeWithMap(params);
Object obj = list.iterator().next();
if (obj instanceof Integer) {
assertEquals(2, ((Integer) obj).intValue());
} else if (obj instanceof Long) {
assertEquals(2, ((Long) obj).intValue());
} else if (obj instanceof BigInteger) {
assertEquals(2, ((BigInteger) obj).intValue());
} else if (obj instanceof BigDecimal) {
assertEquals(2, ((BigDecimal) obj).intValue());
} else {
fail("Test doesnt support count(*) being returned as " + obj.getClass().getName());
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown while running SQL query with parameters : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Person.class);
}
}
use of javax.jdo.Transaction in project tests by datanucleus.
the class SQLQueryTest method testWithCandidateClassWithComments.
/**
* Test of a query with a candidate class with comments in the SELECT.
* @throws Exception Thrown if an error occurs
*/
public void testWithCandidateClassWithComments() throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create some basic data to query
tx.begin();
Person p1 = new Person(1, "First", "Person", "first.person@jpox.org");
pm.makePersistent(p1);
Person p2 = new Person(2, "Second", "Person", "second.person@jpox.org");
pm.makePersistent(p2);
tx.commit();
// Query for a basic object, including the PK field(s) and a comment
tx = pm.currentTransaction();
tx.begin();
String queryStr = null;
if (pmf.getMetadata(Person.class.getName()).getIdentityType().equals(IdentityType.APPLICATION)) {
queryStr = "SELECT /*SomethingOrOther*/PERSONNUM, GLOBALNUM FROM PERSON WHERE FIRSTNAME = 'Second'";
} else {
queryStr = "SELECT /*SomethingOrOther*/PERSON_ID FROM PERSON WHERE FIRSTNAME = 'Second'";
}
Query query = pm.newQuery("javax.jdo.query.SQL", queryStr);
query.setClass(Person.class);
List<Person> results = (List<Person>) query.execute();
assertNotNull(results);
assertEquals("Number of results incorrect", 1, results.size());
Person p = results.iterator().next();
assertTrue("Query with candidate class has returned the wrong Person object.", p.getPersonNum() == 2);
assertTrue("Query with candidate class has returned the wrong Person object.", p.getFirstName().equals("Second"));
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown while performing SQL query using candidate class : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Person.class);
}
}
use of javax.jdo.Transaction in project tests by datanucleus.
the class SQLQueryTest method testInvalidQueryAllowedByConfiguration.
/**
* Test of a query not starting with "SELECT"
*/
public void testInvalidQueryAllowedByConfiguration() throws Exception {
addClassesToSchema(new Class[] { Person.class, Manager.class, Employee.class, Developer.class });
// Try a query
PersistenceManager pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_QUERY_SQL_ALLOWALL, "true");
Transaction tx = pm.currentTransaction();
try {
tx.begin();
String sqlText = "EXECUTE SELECT 1 FROM PERSON";
Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
query.execute();
// expected
} catch (JDODataStoreException dse) {
// expected, if query is invalid by database
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.Transaction in project tests by datanucleus.
the class SQLQueryTest method testNullQuery.
/**
* Test of a null query.
*/
public void testNullQuery() throws Exception {
// Try a null query
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Query query = pm.newQuery("javax.jdo.query.SQL", null);
query.execute();
fail("Should have thrown an exception on a null query, but allowed execution");
} catch (JDOUserException ue) {
// Do nothing, since this is expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.Transaction in project tests by datanucleus.
the class SQLQueryTest method testQueryWithTimeout.
/**
* Test of a query with a timeout.
*/
public void testQueryWithTimeout() throws Exception {
// Try a query
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// TODO Change this to a query that will take a LONG time and check for it
String sqlText = "SELECT count(*) FROM PERSON";
Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
query.addExtension("org.jpox.query.timeout", "1");
query.execute();
tx.commit();
} catch (JDODataStoreException dse) {
fail("JDODataStoreException thrown when using query timeout : " + dse.getCause().getMessage());
} catch (JDOUserException ue) {
// Do nothing, since this is expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations