Search in sources :

Example 1 with Query

use of org.jumpmind.pos.persist.Query in project openpos-framework by JumpMind.

the class DBSessionQueryTest method testWhereInStatement.

@Test
public void testWhereInStatement() {
    DBSession db = sessionFactory.createDbSession();
    Query<CarStats> modelCounts = new Query<CarStats>().named("carCountByModelWithIn").result(CarStats.class);
    List<String> makes = new ArrayList<>();
    makes.add("Hyundai");
    makes.add("Honda");
    List<CarStats> carStats = db.query(modelCounts, makes, 100);
    assertEquals(3, carStats.size());
    assertEquals("Accent", carStats.get(0).getModel());
    assertEquals("Elantra", carStats.get(1).getModel());
    assertEquals("Santa Fe", carStats.get(2).getModel());
}
Also used : CarStats(org.jumpmind.pos.persist.cars.CarStats) Query(org.jumpmind.pos.persist.Query) DBSession(org.jumpmind.pos.persist.DBSession) Test(org.junit.Test)

Example 2 with Query

use of org.jumpmind.pos.persist.Query in project openpos-framework by JumpMind.

the class DBSessionQueryTest method testAdHocQueryOnePartStatement.

@Test
public void testAdHocQueryOnePartStatement() {
    DBSession db = sessionFactory.createDbSession();
    Query<CarStats> modelCounts = new Query<CarStats>().named("carCountByModel").result(CarStats.class);
    List<CarStats> carStats = db.query(modelCounts, "Hyundai", 100);
    assertEquals(3, carStats.size());
    assertEquals("Accent", carStats.get(0).getModel());
    assertEquals("Elantra", carStats.get(1).getModel());
    assertEquals("Santa Fe", carStats.get(2).getModel());
}
Also used : CarStats(org.jumpmind.pos.persist.cars.CarStats) Query(org.jumpmind.pos.persist.Query) DBSession(org.jumpmind.pos.persist.DBSession) Test(org.junit.Test)

Example 3 with Query

use of org.jumpmind.pos.persist.Query in project openpos-framework by JumpMind.

the class DBSessionQueryTest method testLiteralQueryReplacementInWhereClause.

@Test
public void testLiteralQueryReplacementInWhereClause() {
    sessionFactory.createAndUpgrade();
    DBSession db = sessionFactory.createDbSession();
    db.executeScript(new StringReader("update car_car set color = 'grey' where model = 'Accent';" + "update car_car set color = 'blue' where model = 'Elantra';"));
    Query<CarModel> carsByLiteralFieldMatch = new Query<CarModel>().named("carsByLiteralReplacementInWhereClause").result(CarModel.class);
    Map<String, Object> params = new HashMap<>();
    params.put("columnName", "color");
    params.put("columnValue", "blue");
    List<CarModel> cars = db.query(carsByLiteralFieldMatch, params, 100);
    assertEquals(1, cars.size());
    assertEquals("Hyundai", cars.get(0).getMake());
    assertEquals("Elantra", cars.get(0).getModel());
    assertEquals("blue", cars.get(0).getAdditionalField("color"));
}
Also used : CarModel(org.jumpmind.pos.persist.cars.CarModel) RaceCarModel(org.jumpmind.pos.persist.cars.RaceCarModel) Query(org.jumpmind.pos.persist.Query) StringReader(java.io.StringReader) DBSession(org.jumpmind.pos.persist.DBSession) Test(org.junit.Test)

Example 4 with Query

use of org.jumpmind.pos.persist.Query in project openpos-framework by JumpMind.

the class DBSessionQueryTest method testLiteralQueryReplacementInSelectClause.

@Test
public void testLiteralQueryReplacementInSelectClause() {
    sessionFactory.createAndUpgrade();
    DBSession db = sessionFactory.createDbSession();
    db.executeScript(new StringReader("update car_car set color = 'grey' where model = 'Accent';" + "update car_car set color = 'blue' where model = 'Elantra';"));
    Query<CarModel> carsByLiteralReplacementInSelectClause = new Query<CarModel>().named("carsByLiteralReplacementInSelectClause").result(CarModel.class);
    Map<String, Object> params = new HashMap<>();
    params.put("columnName", "color");
    params.put("columnValue", "blue");
    List<CarModel> cars = db.query(carsByLiteralReplacementInSelectClause, params, 100);
    assertEquals(1, cars.size());
    assertEquals("Hyundai", cars.get(0).getMake());
    assertEquals("Elantra", cars.get(0).getModel());
    assertEquals("blue", cars.get(0).getAdditionalField("color"));
}
Also used : CarModel(org.jumpmind.pos.persist.cars.CarModel) RaceCarModel(org.jumpmind.pos.persist.cars.RaceCarModel) Query(org.jumpmind.pos.persist.Query) StringReader(java.io.StringReader) DBSession(org.jumpmind.pos.persist.DBSession) Test(org.junit.Test)

Example 5 with Query

use of org.jumpmind.pos.persist.Query in project openpos-framework by JumpMind.

the class DBSessionQueryTest method testOptionalParamQuery.

@Test
public void testOptionalParamQuery() {
    DBSession db = sessionFactory.createDbSession();
    Query<CarModel> byMakeAndModel = new Query<CarModel>().named("byMakeAndModel").result(CarModel.class);
    {
        Map<String, Object> params = new HashMap<>();
        params.put("make", "Hyundai");
        params.put("model", "Accent");
        List<CarModel> hyundais = db.query(byMakeAndModel, params, 100);
        assertEquals(1, hyundais.size());
        CarModel hyundaiLookupedUp = hyundais.get(0);
        assertNotNull(hyundaiLookupedUp);
        assertEquals("Hyundai", hyundaiLookupedUp.getMake());
        assertEquals("Accent", hyundaiLookupedUp.getModel());
        assertEquals("2005", hyundaiLookupedUp.getModelYear());
    }
    {
        Map<String, Object> params = new HashMap<>();
        params.put("make", "Hyundai");
        List<CarModel> hyundais = db.query(byMakeAndModel, params, 100);
        assertEquals(3, hyundais.size());
        CarModel hyundaiLookupedUp = hyundais.get(0);
        assertNotNull(hyundaiLookupedUp);
        assertEquals("Hyundai", hyundaiLookupedUp.getMake());
        assertEquals("Accent", hyundaiLookupedUp.getModel());
        assertEquals("2005", hyundaiLookupedUp.getModelYear());
    }
}
Also used : CarModel(org.jumpmind.pos.persist.cars.CarModel) RaceCarModel(org.jumpmind.pos.persist.cars.RaceCarModel) Query(org.jumpmind.pos.persist.Query) DBSession(org.jumpmind.pos.persist.DBSession) Test(org.junit.Test)

Aggregations

Query (org.jumpmind.pos.persist.Query)15 Test (org.junit.Test)15 DBSession (org.jumpmind.pos.persist.DBSession)13 CarModel (org.jumpmind.pos.persist.cars.CarModel)12 RaceCarModel (org.jumpmind.pos.persist.cars.RaceCarModel)10 StringReader (java.io.StringReader)3 CarStats (org.jumpmind.pos.persist.cars.CarStats)3 HashMap (java.util.HashMap)2 SqlStatement (org.jumpmind.pos.persist.SqlStatement)2