Search in sources :

Example 11 with SQLTemplate

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

the class ThrowOnPartialSchemaStrategyIT method testThrowOnPartialStrategyWithOneTable.

@Test
public void testThrowOnPartialStrategyWithOneTable() throws Exception {
    createOneTable("SUS1");
    setStrategy(ThrowOnPartialSchemaStrategy.class);
    String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
    SQLTemplate query = new SQLTemplate(Object.class, template);
    MockOperationObserver observer = new MockOperationObserver();
    try {
        node.performQueries(Collections.singletonList(query), observer);
        assertEquals(1, existingTables().size());
        fail("Must have thrown on partial schema");
    } catch (CayenneRuntimeException e) {
    // expected
    }
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 12 with SQLTemplate

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

the class ThrowOnPartialSchemaStrategyIT method testThrowOnPartialStrategyTableExist.

@Test
public void testThrowOnPartialStrategyTableExist() throws Exception {
    String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
    SQLTemplate query = new SQLTemplate(Object.class, template);
    MockOperationObserver observer = new MockOperationObserver();
    createOneTable("SUS1");
    createOneTable("SUS2");
    setStrategy(ThrowOnPartialSchemaStrategy.class);
    node.performQueries(Collections.singletonList(query), observer);
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 13 with SQLTemplate

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

the class ThrowOnPartialSchemaStrategyIT method testThrowOnPartialStrategyTableNoExist.

@Test
public void testThrowOnPartialStrategyTableNoExist() throws Exception {
    String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
    SQLTemplate query = new SQLTemplate(Object.class, template);
    MockOperationObserver observer = new MockOperationObserver();
    setStrategy(ThrowOnPartialSchemaStrategy.class);
    try {
        node.performQueries(Collections.singletonList((Query) query), observer);
    } catch (CayenneRuntimeException e) {
        assertNotNull(e);
    }
    try {
        node.performQueries(Collections.singletonList((Query) query), observer);
    } catch (CayenneRuntimeException e) {
        assertNotNull(e);
    }
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Query(org.apache.cayenne.query.Query) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 14 with SQLTemplate

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

the class SQLTemplateActionIT method testExtractTemplateString.

@Test
public void testExtractTemplateString() throws Exception {
    SQLTemplate template = new SQLTemplate(Artist.class, "A\nBC");
    SQLTemplateAction action = new SQLTemplateAction(template, node);
    assertEquals("A BC", action.extractTemplateString());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Test(org.junit.Test)

Example 15 with SQLTemplate

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

the class SQLTemplateActionIT method testExecuteSelect.

@Test
public void testExecuteSelect() throws Exception {
    createFourArtists();
    String templateString = "SELECT * FROM ARTIST WHERE ARTIST_ID = #bind($id)";
    SQLTemplate template = new SQLTemplate(Object.class, templateString);
    sqlTemplateCustomizer.updateSQLTemplate(template);
    Map<String, Object> bindings = new HashMap<>();
    bindings.put("id", 201l);
    template.setParameters(bindings);
    // must ensure the right SQLTemplateAction is created
    SQLAction plan = adapter.getAction(template, node);
    assertTrue(plan instanceof SQLTemplateAction);
    MockOperationObserver observer = new MockOperationObserver();
    try (Connection c = dataSourceFactory.getSharedDataSource().getConnection()) {
        plan.performAction(c, observer);
    }
    List<DataRow> rows = observer.rowsForQuery(template);
    assertNotNull(rows);
    assertEquals(1, rows.size());
    DataRow row = rows.get(0);
    // In the absence of ObjEntity most DB's return a Long here, except for
    // Oracle
    // that has no BIGINT type and
    // returns BigDecimal, so do a Number comparison
    Number id = (Number) row.get("ARTIST_ID");
    assertNotNull(id);
    assertEquals(((Number) bindings.get("id")).longValue(), id.longValue());
    assertEquals("artist4", row.get("ARTIST_NAME"));
    assertTrue(row.containsKey("DATE_OF_BIRTH"));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) HashMap(java.util.HashMap) Connection(java.sql.Connection) SQLAction(org.apache.cayenne.query.SQLAction) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) DataRow(org.apache.cayenne.DataRow) 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