Search in sources :

Example 1 with QueryExecution

use of org.motechproject.mds.query.QueryExecution in project motech by motech.

the class MdsBundleIT method verifyCustomQuery.

private void verifyCustomQuery() throws Exception {
    List result = (List) service.executeQuery(new QueryExecution<List>() {

        @Override
        public List execute(Query query, InstanceSecurityRestriction restriction) {
            query.setFilter("someString == param0");
            query.declareParameters("java.lang.String param0");
            return (List) QueryExecutor.execute(query, "anotherString", restriction);
        }
    });
    assertEquals(1, result.size());
    Class objClass = result.get(0).getClass();
    assertInstanceOneDotOne(result.get(0), objClass);
    List<String> names = (List<String>) service.executeSQLQuery(new SqlQueryExecution<List<String>>() {

        @Override
        public List<String> execute(Query query) {
            Map<String, Integer> params = new HashMap<>();
            params.put("param", 4);
            return (List<String>) query.executeWithMap(params);
        }

        @Override
        public String getSqlQuery() {
            String driverName = sqlDBManager.getChosenSQLDriver();
            if (driverName.equals(Constants.Config.MYSQL_DRIVER_CLASSNAME)) {
                return "SELECT someString FROM MDS_FOO WHERE someInt = :param";
            } else {
                return "SELECT \"someString\" FROM \"MDS_FOO\" WHERE \"someInt\" = :param";
            }
        }
    });
    assertEquals(asList("notInSet"), names);
}
Also used : Query(javax.jdo.Query) HashMap(java.util.HashMap) InstanceSecurityRestriction(org.motechproject.mds.util.InstanceSecurityRestriction) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) TestClass(org.motechproject.mds.osgi.TestClass) SqlQueryExecution(org.motechproject.mds.query.SqlQueryExecution) SqlQueryExecution(org.motechproject.mds.query.SqlQueryExecution) QueryExecution(org.motechproject.mds.query.QueryExecution)

Aggregations

ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Query (javax.jdo.Query)1 TestClass (org.motechproject.mds.osgi.TestClass)1 QueryExecution (org.motechproject.mds.query.QueryExecution)1 SqlQueryExecution (org.motechproject.mds.query.SqlQueryExecution)1 InstanceSecurityRestriction (org.motechproject.mds.util.InstanceSecurityRestriction)1