Search in sources :

Example 31 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class DataContextSQLTemplateIT method testSQLResultSetMappingScalarArray.

@Test
public void testSQLResultSetMappingScalarArray() throws Exception {
    createFourArtists();
    String sql = "SELECT count(1) AS X, 77 AS Y FROM ARTIST";
    DataMap map = context.getEntityResolver().getDataMap("testmap");
    SQLTemplate query = new SQLTemplate(map, sql, false);
    query.setTemplate(FrontBaseAdapter.class.getName(), "SELECT COUNT(ARTIST_ID) X, 77 Y FROM ARTIST GROUP BY Y");
    query.setTemplate(OpenBaseAdapter.class.getName(), "SELECT COUNT(ARTIST_ID) X, 77 Y FROM ARTIST GROUP BY 77");
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    SQLResult rsMap = new SQLResult();
    rsMap.addColumnResult("X");
    rsMap.addColumnResult("Y");
    query.setResult(rsMap);
    List<?> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    Object o = objects.get(0);
    assertTrue(o instanceof Object[]);
    Object[] row = (Object[]) o;
    assertEquals(2, row.length);
    assertEquals(4, ((Number) row[0]).intValue());
    assertEquals(77, ((Number) row[1]).intValue());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SQLResult(org.apache.cayenne.map.SQLResult) FrontBaseAdapter(org.apache.cayenne.dba.frontbase.FrontBaseAdapter) OpenBaseAdapter(org.apache.cayenne.dba.openbase.OpenBaseAdapter) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 32 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class DataContextSQLTemplateIT method testQueryWithLineBreakAfterMacro.

@Test
public void testQueryWithLineBreakAfterMacro() throws Exception {
    createFourArtists();
    // see CAY-726 for details
    String template = "SELECT #result('count(*)' 'int' 'X')" + System.getProperty("line.separator") + "FROM ARTIST";
    SQLTemplate query = sqlTemplateCustomizer.createSQLTemplate(Artist.class, template);
    query.setFetchingDataRows(true);
    List<?> result = context.performQuery(query);
    assertEquals(4, ((DataRow) result.get(0)).get("X"));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Test(org.junit.Test)

Example 33 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class DataContextSQLTemplateIT method testBindObjectEqualNull.

@Test
public void testBindObjectEqualNull() throws Exception {
    createFourArtistsAndThreePaintingsDataSet();
    String template = "SELECT * FROM PAINTING t0" + " WHERE #bindObjectEqual($a [ 't0.ARTIST_ID' ] [ 'ARTIST_ID' ] ) ORDER BY PAINTING_ID";
    SQLTemplate query = new SQLTemplate(Painting.class, template);
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    query.setParams(Collections.singletonMap("a", null));
    List<?> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    Painting p = (Painting) objects.get(0);
    assertEquals(8, Cayenne.intPKForObject(p));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 34 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class DataContextSQLTemplateIT method testColumnNamesCapitalization.

@Test
public void testColumnNamesCapitalization() throws Exception {
    createFourArtistsAndThreePaintingsDataSet();
    String template = "SELECT * FROM ARTIST ORDER BY ARTIST_ID";
    SQLTemplate query = new SQLTemplate(Artist.class, template);
    query.setColumnNamesCapitalization(CapsStrategy.LOWER);
    query.setFetchingDataRows(true);
    List<DataRow> rows = context.performQuery(query);
    DataRow row1 = rows.get(0);
    assertFalse(row1.containsKey("ARTIST_ID"));
    assertTrue(row1.containsKey("artist_id"));
    DataRow row2 = rows.get(1);
    assertFalse(row2.containsKey("ARTIST_ID"));
    assertTrue(row2.containsKey("artist_id"));
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    List<DataRow> rowsUpper = context.performQuery(query);
    DataRow row3 = rowsUpper.get(0);
    assertFalse(row3.containsKey("artist_id"));
    assertTrue(row3.containsKey("ARTIST_ID"));
    DataRow row4 = rowsUpper.get(1);
    assertFalse(row4.containsKey("artist_id"));
    assertTrue(row4.containsKey("ARTIST_ID"));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 35 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class DataContextSQLTemplateIT method testBindObjectNotEqualNull.

@Test
public void testBindObjectNotEqualNull() throws Exception {
    createFourArtistsAndThreePaintingsDataSet();
    String template = "SELECT * FROM PAINTING t0" + " WHERE #bindObjectNotEqual($a [ 't0.ARTIST_ID' ] [ 'ARTIST_ID' ] ) ORDER BY PAINTING_ID";
    SQLTemplate query = new SQLTemplate(Painting.class, template);
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    query.setParams(Collections.singletonMap("a", null));
    List<Painting> objects = context.performQuery(query);
    assertEquals(2, objects.size());
    Painting p1 = objects.get(0);
    assertEquals(6, Cayenne.intPKForObject(p1));
    Painting p2 = objects.get(1);
    assertEquals(7, Cayenne.intPKForObject(p2));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

SQLTemplate (org.apache.cayenne.query.SQLTemplate)118 Test (org.junit.Test)108 Artist (org.apache.cayenne.testdo.testmap.Artist)27 DataRow (org.apache.cayenne.DataRow)18 HashMap (java.util.HashMap)16 MockOperationObserver (org.apache.cayenne.access.MockOperationObserver)14 Query (org.apache.cayenne.query.Query)14 SelectQuery (org.apache.cayenne.query.SelectQuery)11 Painting (org.apache.cayenne.testdo.testmap.Painting)11 List (java.util.List)8 Connection (java.sql.Connection)7 SQLAction (org.apache.cayenne.query.SQLAction)7 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)6 Map (java.util.Map)5 OperationObserver (org.apache.cayenne.access.OperationObserver)5 DataMap (org.apache.cayenne.map.DataMap)5 AbstractPerson (org.apache.cayenne.testdo.inheritance_people.AbstractPerson)5 PersonNotes (org.apache.cayenne.testdo.inheritance_people.PersonNotes)5 Date (java.sql.Date)4 FrontBaseAdapter (org.apache.cayenne.dba.frontbase.FrontBaseAdapter)4