Search in sources :

Example 1 with JPAEntityManager

use of org.datanucleus.api.jpa.JPAEntityManager in project tests by datanucleus.

the class StoredProcedureTest method testExecuteOutputParam.

public void testExecuteOutputParam() {
    if (vendorID == null) {
        return;
    }
    if (storeMgr instanceof RDBMSStoreManager) {
        DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
        if (!dba.supportsOption(DatastoreAdapter.STORED_PROCEDURES)) {
            LOG.warn("Database doesnt support stored procedures so ignoring the test");
            return;
        }
    }
    String procName = "DN_PROC_OUTPUTPARAM";
    RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
    ManagedConnection mc = rdbmsMgr.getConnectionManager().getConnection(-1);
    try {
        Connection conn = (Connection) mc.getConnection();
        Statement stmt = conn.createStatement();
        // Drop it first
        String dropStmt = "DROP PROCEDURE IF EXISTS " + procName;
        stmt.execute(dropStmt);
        // Create it
        String createStmt = "CREATE PROCEDURE " + procName + "(OUT PARAM1 INT) BEGIN " + "SELECT COUNT(*) INTO PARAM1 FROM JPA_AN_PERSON; END";
        stmt.execute(createStmt);
    } catch (SQLException sqle) {
        fail("Exception in drop-create of stored procedure : " + sqle.getMessage());
    } finally {
        mc.close();
    }
    try {
        JPAEntityManager em = (JPAEntityManager) getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
            em.persist(p);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        em = (JPAEntityManager) getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            // Get value to compare against
            Query q = em.createQuery("SELECT COUNT(p) FROM " + Person.class.getName() + " p");
            Long count = (Long) q.getSingleResult();
            // Execute stored proc and compare
            StoredProcedureQuery spq = em.createStoredProcedureQuery(procName);
            spq.registerStoredProcedureParameter("PARAM1", Integer.class, ParameterMode.OUT);
            boolean val = spq.execute();
            assertFalse("Flag for result set returned true but should have been false", val);
            Object paramVal = spq.getOutputParameterValue("PARAM1");
            assertEquals("Output parameter is incorrect", new Integer(count.intValue()), paramVal);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        // Cleanup data
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Query(javax.persistence.Query) SQLException(java.sql.SQLException) Statement(java.sql.Statement) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) JPAEntityManager(org.datanucleus.api.jpa.JPAEntityManager) SQLException(java.sql.SQLException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 2 with JPAEntityManager

use of org.datanucleus.api.jpa.JPAEntityManager in project tests by datanucleus.

the class StoredProcedureTest method testExecuteInputParamAndOutputParam.

public void testExecuteInputParamAndOutputParam() {
    if (vendorID == null) {
        return;
    }
    if (storeMgr instanceof RDBMSStoreManager) {
        DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
        if (!dba.supportsOption(DatastoreAdapter.STORED_PROCEDURES)) {
            LOG.warn("Database doesnt support stored procedures so ignoring the test");
            return;
        }
    }
    String procName = "DN_PROC_INPUTPARAM_OUTPUTPARAM";
    RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
    ManagedConnection mc = rdbmsMgr.getConnectionManager().getConnection(-1);
    try {
        Connection conn = (Connection) mc.getConnection();
        Statement stmt = conn.createStatement();
        // Drop it first
        String dropStmt = "DROP PROCEDURE IF EXISTS " + procName;
        stmt.execute(dropStmt);
        // Create it
        String createStmt = "CREATE PROCEDURE " + procName + "(IN PARAM1 VARCHAR(255), OUT PARAM2 INT) BEGIN " + "SELECT COUNT(*) INTO PARAM2 FROM JPA_AN_PERSON WHERE FIRSTNAME = PARAM1; END";
        stmt.execute(createStmt);
    } catch (SQLException sqle) {
        fail("Exception in drop-create of stored procedure : " + sqle.getMessage());
    } finally {
        mc.close();
    }
    try {
        JPAEntityManager em = (JPAEntityManager) getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
            Person p2 = new Person(102, "Fred", "Gravel", "fred.gravel@warnerbros.com");
            em.persist(p1);
            em.persist(p2);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        em = (JPAEntityManager) getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            // Get value to compare against
            Query q = em.createQuery("SELECT COUNT(p) FROM " + Person.class.getName() + " p WHERE p.firstName = 'Fred'");
            Long count = (Long) q.getSingleResult();
            // Execute stored proc and compare
            StoredProcedureQuery spq = em.createStoredProcedureQuery(procName);
            spq.registerStoredProcedureParameter("PARAM1", String.class, ParameterMode.IN);
            spq.registerStoredProcedureParameter("PARAM2", Integer.class, ParameterMode.OUT);
            spq.setParameter("PARAM1", "Fred");
            boolean val = spq.execute();
            assertFalse("Flag for result set returned true but should have been false", val);
            Object paramVal = spq.getOutputParameterValue("PARAM2");
            assertEquals("Output parameter is incorrect", new Integer(count.intValue()), paramVal);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        // Cleanup data
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Query(javax.persistence.Query) SQLException(java.sql.SQLException) Statement(java.sql.Statement) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) JPAEntityManager(org.datanucleus.api.jpa.JPAEntityManager) SQLException(java.sql.SQLException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 3 with JPAEntityManager

use of org.datanucleus.api.jpa.JPAEntityManager in project tests by datanucleus.

the class StoredProcedureTest method testExecuteWithoutParamsReturningResultSet.

public void testExecuteWithoutParamsReturningResultSet() {
    if (vendorID == null) {
        return;
    }
    if (storeMgr instanceof RDBMSStoreManager) {
        DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
        if (!dba.supportsOption(DatastoreAdapter.STORED_PROCEDURES)) {
            LOG.warn("Database doesnt support stored procedures so ignoring the test");
            return;
        }
    }
    String procName = "DN_PROC_NOPARAMS_RS";
    RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
    ManagedConnection mc = rdbmsMgr.getConnectionManager().getConnection(-1);
    try {
        Connection conn = (Connection) mc.getConnection();
        Statement stmt = conn.createStatement();
        // Drop it first
        String dropStmt = "DROP PROCEDURE IF EXISTS " + procName;
        stmt.execute(dropStmt);
        // Create it
        String createStmt = "CREATE PROCEDURE " + procName + "() BEGIN " + "SELECT COUNT(*) FROM JPA_AN_PERSON; END";
        stmt.execute(createStmt);
    } catch (SQLException sqle) {
        fail("Exception in drop-create of stored procedure : " + sqle.getMessage());
    } finally {
        mc.close();
    }
    try {
        JPAEntityManager em = (JPAEntityManager) getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
            em.persist(p);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        em = (JPAEntityManager) getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            // Get value to compare against
            Query q = em.createQuery("SELECT COUNT(p) FROM " + Person.class.getName() + " p");
            Long count = (Long) q.getSingleResult();
            // Execute stored proc and compare
            StoredProcedureQuery spq = em.createStoredProcedureQuery(procName);
            boolean val = spq.execute();
            assertTrue("Return from execute should have been true", val);
            List results = spq.getResultList();
            assertNotNull("ResultSet was null!", results);
            assertEquals("Number of results was wrong", 1, results.size());
            assertEquals("Result set result was wrong", count, results.get(0));
            assertFalse("More results present but should be the end", spq.hasMoreResults());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        // Cleanup data
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Query(javax.persistence.Query) SQLException(java.sql.SQLException) Statement(java.sql.Statement) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) JPAEntityManager(org.datanucleus.api.jpa.JPAEntityManager) SQLException(java.sql.SQLException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) List(java.util.List) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 4 with JPAEntityManager

use of org.datanucleus.api.jpa.JPAEntityManager in project tests by datanucleus.

the class StoredProcedureTest method testNamedProcWithMultipleResultSet.

public void testNamedProcWithMultipleResultSet() {
    if (vendorID == null) {
        return;
    }
    if (storeMgr instanceof RDBMSStoreManager) {
        DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
        if (!dba.supportsOption(DatastoreAdapter.STORED_PROCEDURES)) {
            LOG.warn("Database doesnt support stored procedures so ignoring the test");
            return;
        }
    }
    String procName = "DN_PROC_NAMED_RS2";
    RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
    ManagedConnection mc = rdbmsMgr.getConnectionManager().getConnection(-1);
    try {
        Connection conn = (Connection) mc.getConnection();
        Statement stmt = conn.createStatement();
        // Drop it first
        String dropStmt = "DROP PROCEDURE IF EXISTS " + procName;
        stmt.execute(dropStmt);
        // Create it
        String createStmt = "CREATE PROCEDURE " + procName + "(IN PARAM1 VARCHAR(255), IN PARAM2 VARCHAR(255)) BEGIN " + "SELECT * FROM JPA_AN_PERSON WHERE FIRSTNAME = PARAM1;" + "SELECT * FROM JPA_AN_ACCOUNT WHERE USERNAME = PARAM2;" + "END";
        stmt.execute(createStmt);
    } catch (SQLException sqle) {
        fail("Exception in drop-create of stored procedure : " + sqle.getMessage());
    } finally {
        mc.close();
    }
    try {
        JPAEntityManager em = (JPAEntityManager) getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
            em.persist(p);
            Account a = new Account();
            a.setUsername("Fred");
            a.setEnabled(true);
            em.persist(a);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        em = (JPAEntityManager) getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            LOG.info(">> Executing stored proc");
            // Execute stored proc and compare
            StoredProcedureQuery spq = em.createNamedStoredProcedureQuery("myNamedSP2");
            spq.setParameter("PARAM1", "Fred");
            spq.setParameter("PARAM2", "Fred");
            boolean val = spq.execute();
            assertTrue("Return from execute should have been true", val);
            List results = spq.getResultList();
            assertNotNull("ResultSet was null!", results);
            assertEquals("Number of results was wrong", 1, results.size());
            for (Object result : results) {
                LOG.info(">> result=" + result);
            }
            assertTrue("More results should be present but werent", spq.hasMoreResults());
            List results2 = spq.getResultList();
            assertNotNull("ResultSet2 was null!", results2);
            assertEquals("Number of results2 was wrong", 1, results2.size());
            for (Object result : results2) {
                LOG.info(">> result=" + result);
            }
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        // Cleanup data
        clean(Account.class);
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Account(org.datanucleus.samples.annotations.models.company.Account) SQLException(java.sql.SQLException) Statement(java.sql.Statement) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) JPAEntityManager(org.datanucleus.api.jpa.JPAEntityManager) SQLException(java.sql.SQLException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) List(java.util.List) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 5 with JPAEntityManager

use of org.datanucleus.api.jpa.JPAEntityManager in project tests by datanucleus.

the class StoredProcedureTest method testGetResultListWithoutParamsReturningResultSet.

public void testGetResultListWithoutParamsReturningResultSet() {
    if (vendorID == null) {
        return;
    }
    if (storeMgr instanceof RDBMSStoreManager) {
        DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
        if (!dba.supportsOption(DatastoreAdapter.STORED_PROCEDURES)) {
            LOG.warn("Database doesnt support stored procedures so ignoring the test");
            return;
        }
    }
    String procName = "DN_PROC_NOPARAMS_RS";
    RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
    ManagedConnection mc = rdbmsMgr.getConnectionManager().getConnection(-1);
    try {
        Connection conn = (Connection) mc.getConnection();
        Statement stmt = conn.createStatement();
        // Drop it first
        String dropStmt = "DROP PROCEDURE IF EXISTS " + procName;
        stmt.execute(dropStmt);
        // Create it
        String createStmt = "CREATE PROCEDURE " + procName + "() BEGIN " + "SELECT COUNT(*) FROM JPA_AN_PERSON; END";
        stmt.execute(createStmt);
    } catch (SQLException sqle) {
        fail("Exception in drop-create of stored procedure : " + sqle.getMessage());
    } finally {
        mc.close();
    }
    try {
        JPAEntityManager em = (JPAEntityManager) getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
            em.persist(p);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        em = (JPAEntityManager) getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            // Get value to compare against
            Query q = em.createQuery("SELECT COUNT(p) FROM " + Person.class.getName() + " p");
            Long count = (Long) q.getSingleResult();
            // Execute stored proc and compare
            StoredProcedureQuery spq = em.createStoredProcedureQuery(procName);
            List results = spq.getResultList();
            assertNotNull("ResultSet was null!", results);
            assertEquals("Number of results was wrong", 1, results.size());
            assertEquals("Result set result was wrong", count, results.get(0));
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        // Cleanup data
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Query(javax.persistence.Query) SQLException(java.sql.SQLException) Statement(java.sql.Statement) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) JPAEntityManager(org.datanucleus.api.jpa.JPAEntityManager) SQLException(java.sql.SQLException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) List(java.util.List) Person(org.datanucleus.samples.annotations.models.company.Person)

Aggregations

Connection (java.sql.Connection)6 SQLException (java.sql.SQLException)6 Statement (java.sql.Statement)6 EntityTransaction (javax.persistence.EntityTransaction)6 StoredProcedureQuery (javax.persistence.StoredProcedureQuery)6 JPAEntityManager (org.datanucleus.api.jpa.JPAEntityManager)6 Person (org.datanucleus.samples.annotations.models.company.Person)6 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)6 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)6 DatastoreAdapter (org.datanucleus.store.rdbms.adapter.DatastoreAdapter)6 Query (javax.persistence.Query)5 List (java.util.List)4 Account (org.datanucleus.samples.annotations.models.company.Account)1