Search in sources :

Example 21 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class SQLQueryTest method testNumberedParameters.

/**
 * Test use of an SQL query using numbered parameters ("?1", "?2", etc).
 * @throws Exception
 */
public void testNumberedParameters() 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 = ?1 AND LASTNAME = ?2) OR " + "(FIRSTNAME = ?1 AND LASTNAME = ?3)";
        Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        List list = (List) query.execute("John", "Smith", "Brown");
        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 : BigInteger(java.math.BigInteger) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) BigInteger(java.math.BigInteger) List(java.util.List) Person(org.jpox.samples.models.company.Person) BigDecimal(java.math.BigDecimal) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 22 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class JPQLQueryTest method testFilterUPPER.

/**
 * Test using UPPER(str) function.
 */
public void testFilterUPPER() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query q = pm.newQuery("JPQL", "SELECT p FROM " + Person.class.getName() + " p WHERE UPPER(firstName) = 'ANA'");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        Iterator it = c.iterator();
        assertEquals("Ana", ((Person) it.next()).getFirstName());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) Person(org.jpox.samples.models.company.Person)

Example 23 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class JPQLQueryTest method testFilterLike.

/**
 * result test
 */
public void testFilterLike() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query q = pm.newQuery("JPQL", "SELECT p FROM " + Person.class.getName() + " p WHERE firstName LIKE 'A.*a'");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        Iterator it = c.iterator();
        assertEquals("Ana", ((Person) it.next()).getFirstName());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) Person(org.jpox.samples.models.company.Person)

Example 24 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class JPQLQueryTest method testFilterInLiteral.

/**
 * Test using "IN (literal, literal)" and "NOT IN (literal, literal)".
 */
public void testFilterInLiteral() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query q = pm.newQuery("JPQL", "SELECT p FROM " + Person.class.getName() + " p WHERE firstName IN ('Ana', 'Alan')");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        Iterator it = c.iterator();
        assertEquals("Ana", ((Person) it.next()).getFirstName());
        q = pm.newQuery("JPQL", "SELECT p FROM " + Person.class.getName() + " p WHERE firstName NOT IN ('Ana', 'Bugs')");
        c = (Collection) q.execute();
        assertEquals(1, c.size());
        it = c.iterator();
        assertEquals("Lami", ((Person) it.next()).getFirstName());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) Person(org.jpox.samples.models.company.Person)

Example 25 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class PersistenceTest method testFetch.

/**
 * Fetch objects and assert basic values
 */
public void testFetch() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person p1 = (Person) pm.getObjectById(id1);
        assertEquals("Bugs", p1.getFirstName());
        assertEquals("Bunny", p1.getLastName());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person)

Aggregations

Person (org.jpox.samples.models.company.Person)132 PersistenceManager (javax.jdo.PersistenceManager)130 Transaction (javax.jdo.Transaction)128 Query (javax.jdo.Query)75 List (java.util.List)60 JDOUserException (javax.jdo.JDOUserException)41 Iterator (java.util.Iterator)40 Collection (java.util.Collection)27 Employee (org.jpox.samples.models.company.Employee)22 HashMap (java.util.HashMap)21 JDODataStoreException (javax.jdo.JDODataStoreException)21 JDOException (javax.jdo.JDOException)16 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)16 ArrayList (java.util.ArrayList)15 BigInteger (java.math.BigInteger)14 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)14 BigDecimal (java.math.BigDecimal)10 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)10 JDOQuery (org.datanucleus.api.jdo.JDOQuery)10 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)10