Search in sources :

Example 1 with SQLTemplate

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

the class CayenneContextCayenneIT method testObjectForPK.

@Test
public void testObjectForPK() throws Exception {
    context.performGenericQuery(new SQLTemplate(ClientMtTable1.class, "insert into MT_TABLE1 " + "(TABLE1_ID, GLOBAL_ATTRIBUTE1, SERVER_ATTRIBUTE1) " + "values (1, 'g1', 's1')"));
    ClientMtTable1 o = Cayenne.objectForPK(context, ClientMtTable1.class, 1);
    assertNotNull(o);
    assertEquals("g1", o.getGlobalAttribute1());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) Test(org.junit.Test)

Example 2 with SQLTemplate

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

the class SQLTemplateCustomizer method createSQLTemplate.

public SQLTemplate createSQLTemplate(Class<?> root, String defaultTemplate) {
    SQLTemplate template = new SQLTemplate(root, defaultTemplate);
    updateSQLTemplate(template);
    return template;
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate)

Example 3 with SQLTemplate

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

the class BindDirectiveIT method performInsertForParameters.

/**
 * Inserts row for given parameters
 *
 * @return inserted row
 */
private Map<String, ?> performInsertForParameters(Map<String, Object> parameters, String templateString) throws Exception {
    // TODO: do we really care if an inserting SQLTemplate is executed via
    // ObjectContext?
    SQLTemplate template = new SQLTemplate(Object.class, templateString);
    template.setParams(parameters);
    MockOperationObserver observer = new MockOperationObserver();
    node.performQueries(Collections.singletonList(template), observer);
    return ObjectSelect.dataRowQuery(Artist.class).selectOne(context);
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Artist(org.apache.cayenne.testdo.testmap.Artist) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver)

Example 4 with SQLTemplate

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

the class BindDirectiveIT method testBind_Collection.

@Test
public void testBind_Collection() throws Exception {
    TableHelper tArtist = new TableHelper(dbHelper, "ARTIST").setColumns("ARTIST_ID", "ARTIST_NAME");
    // insert 3 artists
    for (int i = 1; i < 4; i++) {
        tArtist.insert(new Long(i), "Artist" + i);
    }
    // now select only with names: Artist1 and Artist3
    Set<String> artistNames = new HashSet<String>();
    artistNames.add("Artist1");
    artistNames.add("Artist3");
    String sql = "SELECT * FROM ARTIST WHERE ARTIST_NAME in (#bind($ARTISTNAMES))";
    SQLTemplate query = new SQLTemplate(Artist.class, sql);
    // customize for DB's that require trimming CHAR spaces
    query.setTemplate(OracleAdapter.class.getName(), "SELECT * FROM ARTIST WHERE RTRIM(ARTIST_NAME) in (#bind($ARTISTNAMES))");
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    query.setParams(Collections.singletonMap("ARTISTNAMES", artistNames));
    List<?> result = context.performQuery(query);
    assertEquals(2, result.size());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) OracleAdapter(org.apache.cayenne.dba.oracle.OracleAdapter) TableHelper(org.apache.cayenne.test.jdbc.TableHelper) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with SQLTemplate

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

the class ResultDirectiveIT method selectForQuery.

private Map<String, Object> selectForQuery(String sql) {
    SQLTemplate template = new SQLTemplate(Artist.class, sql);
    template.setColumnNamesCapitalization(CapsStrategy.UPPER);
    MockOperationObserver observer = new MockOperationObserver();
    runtime.getDataDomain().performQueries(Collections.singletonList(template), observer);
    @SuppressWarnings("unchecked") List<Map<String, Object>> data = observer.rowsForQuery(template);
    assertEquals(1, data.size());
    Map<String, Object> row = data.get(0);
    return row;
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Map(java.util.Map)

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