Search in sources :

Example 26 with Transaction

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);
    }
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) BigDecimal(java.math.BigDecimal) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException) BigInteger(java.math.BigInteger) Transaction(javax.jdo.Transaction) BigInteger(java.math.BigInteger) List(java.util.List) Person(org.jpox.samples.models.company.Person) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with Transaction

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);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Person(org.jpox.samples.models.company.Person) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 28 with Transaction

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();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 29 with Transaction

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();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 30 with Transaction

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();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Aggregations

Transaction (javax.jdo.Transaction)1552 PersistenceManager (javax.jdo.PersistenceManager)1542 Query (javax.jdo.Query)635 List (java.util.List)395 JDOUserException (javax.jdo.JDOUserException)347 Collection (java.util.Collection)287 Iterator (java.util.Iterator)216 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)183 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)156 Person (org.jpox.samples.models.company.Person)144 Employee (org.jpox.samples.models.company.Employee)122 Manager (org.jpox.samples.models.company.Manager)98 HashSet (java.util.HashSet)97 SQLException (java.sql.SQLException)80 ArrayList (java.util.ArrayList)77 JDOException (javax.jdo.JDOException)73 Extent (javax.jdo.Extent)70 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)68 JDODataStoreException (javax.jdo.JDODataStoreException)65 JDOFatalUserException (javax.jdo.JDOFatalUserException)65