Search in sources :

Example 86 with DataRow

use of org.apache.cayenne.DataRow in project cayenne by apache.

the class SQLTemplateActionIT method testExecuteUpdateBatch.

@Test
public void testExecuteUpdateBatch() throws Exception {
    String templateString = "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME, DATE_OF_BIRTH) " + "VALUES (#bind($id), #bind($name), #bind($dob 'DATE'))";
    SQLTemplate template = new SQLTemplate(Object.class, templateString);
    Map<String, Object> bindings1 = new HashMap<>();
    bindings1.put("id", 1L);
    bindings1.put("name", "a1");
    bindings1.put("dob", new Date(System.currentTimeMillis()));
    Map<String, Object> bindings2 = new HashMap<>();
    bindings2.put("id", 33L);
    bindings2.put("name", "a$$$$$");
    bindings2.put("dob", new Date(System.currentTimeMillis()));
    template.setParameters(new Map[] { bindings1, bindings2 });
    SQLAction genericAction = adapter.getAction(template, node);
    assertTrue(genericAction instanceof SQLTemplateAction);
    SQLTemplateAction action = (SQLTemplateAction) genericAction;
    assertSame(node, action.dataNode);
    assertSame(template, action.getQuery());
    try (Connection c = dataSourceFactory.getSharedDataSource().getConnection()) {
        MockOperationObserver observer = new MockOperationObserver();
        action.performAction(c, observer);
        int[] batches = observer.countsForQuery(template);
        assertNotNull(batches);
        assertEquals(2, batches.length);
        assertEquals(1, batches[0]);
        assertEquals(1, batches[1]);
    }
    MockOperationObserver observer = new MockOperationObserver();
    ObjectSelect<Artist> query = ObjectSelect.query(Artist.class).orderBy("db:ARTIST_ID", SortOrder.ASCENDING);
    node.performQueries(Collections.singletonList(query), observer);
    @SuppressWarnings("unchecked") List<DataRow> data = observer.rowsForQuery(query);
    assertEquals(2, data.size());
    DataRow row1 = data.get(0);
    assertEquals(bindings1.get("id"), row1.get("ARTIST_ID"));
    assertEquals(bindings1.get("name"), row1.get("ARTIST_NAME"));
    // to compare dates we need to create the binding correctly
    // assertEquals(bindings1.get("dob"), row.get("DATE_OF_BIRTH"));
    DataRow row2 = data.get(1);
    assertEquals(bindings2.get("id"), row2.get("ARTIST_ID"));
    assertEquals(bindings2.get("name"), row2.get("ARTIST_NAME"));
// to compare dates we need to create the binding correctly
// assertEquals(bindings2.get("dob"), row2.get("DATE_OF_BIRTH"));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Artist(org.apache.cayenne.testdo.testmap.Artist) HashMap(java.util.HashMap) Connection(java.sql.Connection) DataRow(org.apache.cayenne.DataRow) Date(java.sql.Date) SQLAction(org.apache.cayenne.query.SQLAction) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 87 with DataRow

use of org.apache.cayenne.DataRow in project cayenne by apache.

the class SQLTemplateActionIT method testSelectSQLTimestamp.

@Test
public void testSelectSQLTimestamp() throws Exception {
    createFourArtists();
    String templateString = "SELECT #result('DATE_OF_BIRTH' 'java.sql.Timestamp' 'DOB') " + "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", 201);
    template.setParameters(bindings);
    SQLAction plan = adapter.getAction(template, node);
    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);
    assertNotNull(row.get("DOB"));
    // Sybase returns a Timestamp subclass... so can't test equality
    assertTrue(java.sql.Timestamp.class.isAssignableFrom(row.get("DOB").getClass()));
}
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)

Example 88 with DataRow

use of org.apache.cayenne.DataRow in project cayenne by apache.

the class SQLTemplateActionIT method testSelectSQLDate.

@Test
public void testSelectSQLDate() throws Exception {
    createFourArtists();
    String templateString = "SELECT #result('DATE_OF_BIRTH' 'java.sql.Date' 'DOB') " + "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", 101);
    template.setParameters(bindings);
    SQLAction plan = adapter.getAction(template, node);
    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);
    assertNotNull(row.get("DOB"));
    assertEquals(java.sql.Date.class, row.get("DOB").getClass());
}
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)

Example 89 with DataRow

use of org.apache.cayenne.DataRow in project cayenne by apache.

the class SQLTemplateActionIT method testSelectUtilDate.

@Test
public void testSelectUtilDate() throws Exception {
    createFourArtists();
    String templateString = "SELECT #result('DATE_OF_BIRTH' 'java.util.Date' 'DOB') " + "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", 101);
    template.setParameters(bindings);
    SQLAction plan = adapter.getAction(template, node);
    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);
    assertNotNull(row.get("DOB"));
    assertEquals(java.util.Date.class, row.get("DOB").getClass());
}
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)

Example 90 with DataRow

use of org.apache.cayenne.DataRow in project cayenne by apache.

the class NestedDataContextReadIT method testObjectFromDataRow.

@Test
public void testObjectFromDataRow() {
    DataContext childContext = (DataContext) runtime.newContext(context);
    DataRow row = new DataRow(8);
    row.put("ARTIST_ID", 5L);
    row.put("ARTIST_NAME", "A");
    row.put("DATE_OF_BIRTH", new Date());
    Artist artist = childContext.objectFromDataRow(Artist.class, row);
    assertNotNull(artist);
    assertEquals(PersistenceState.COMMITTED, artist.getPersistenceState());
    assertSame(childContext, artist.getObjectContext());
    Object parentArtist = context.getObjectStore().getNode(artist.getObjectId());
    assertNotNull(parentArtist);
    assertNotSame(artist, parentArtist);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataObject(org.apache.cayenne.DataObject) DataRow(org.apache.cayenne.DataRow) Date(java.util.Date) Test(org.junit.Test)

Aggregations

DataRow (org.apache.cayenne.DataRow)152 Test (org.junit.Test)113 Artist (org.apache.cayenne.testdo.testmap.Artist)31 ObjectId (org.apache.cayenne.ObjectId)25 DataObject (org.apache.cayenne.DataObject)20 ReturnTypesMap1 (org.apache.cayenne.testdo.return_types.ReturnTypesMap1)20 SQLTemplate (org.apache.cayenne.query.SQLTemplate)18 Date (java.util.Date)14 HashMap (java.util.HashMap)14 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)14 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)12 ArrayList (java.util.ArrayList)9 List (java.util.List)8 Persistent (org.apache.cayenne.Persistent)8 Painting (org.apache.cayenne.testdo.testmap.Painting)8 Connection (java.sql.Connection)6 Calendar (java.util.Calendar)5 Map (java.util.Map)5 QueryResponse (org.apache.cayenne.QueryResponse)5 DataMap (org.apache.cayenne.map.DataMap)5