Search in sources :

Example 36 with PersistenceManager

use of javax.jdo.PersistenceManager in project tests by datanucleus.

the class SQLQueryTest method testSelectStarQuery.

/**
 * Test of the use of SELECT * in a query.
 */
public void testSelectStarQuery() throws Exception {
    try {
        // Persist something to select
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager m1 = new Manager(1, "Barney", "Rubble", "barney.rubble@flintstones.com", 100, "123456");
            pm.makePersistent(m1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Do an SQL query to find the Manager
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            String sqlText = "SELECT * FROM MANAGER";
            Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
            query.setClass(Manager.class);
            List results = (List) query.execute();
            assertTrue("\"SELECT *\" query returned null, yet should have returned some results", results != null);
            assertEquals("Number of Manager objects retrieved from \"SELECT *\" query was incorrect", 1, results.size());
            Manager mgr = (Manager) results.iterator().next();
            // These will cause further SQL statements to retrieve the fields in the Person/Employee part of the object
            assertEquals("\"SELECT *\" query returned Manager with incorrect first name", "Barney", mgr.getFirstName());
            assertEquals("\"SELECT *\" query returned Manager with incorrect last name", "Rubble", mgr.getLastName());
            assertEquals("\"SELECT *\" query returned Manager with incorrect email", "barney.rubble@flintstones.com", mgr.getEmailAddress());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown from \"SELECT *\" query : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 37 with PersistenceManager

use of javax.jdo.PersistenceManager 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 38 with PersistenceManager

use of javax.jdo.PersistenceManager 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 39 with PersistenceManager

use of javax.jdo.PersistenceManager 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 40 with PersistenceManager

use of javax.jdo.PersistenceManager 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)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)1664 Transaction (javax.jdo.Transaction)1542 Query (javax.jdo.Query)671 List (java.util.List)397 JDOUserException (javax.jdo.JDOUserException)352 Collection (java.util.Collection)309 Iterator (java.util.Iterator)239 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)185 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)170 Person (org.jpox.samples.models.company.Person)146 Employee (org.jpox.samples.models.company.Employee)128 Manager (org.jpox.samples.models.company.Manager)107 HashSet (java.util.HashSet)101 ArrayList (java.util.ArrayList)85 SQLException (java.sql.SQLException)82 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)77 JDOException (javax.jdo.JDOException)74 Extent (javax.jdo.Extent)72 JDOFatalUserException (javax.jdo.JDOFatalUserException)68 JDODataStoreException (javax.jdo.JDODataStoreException)65