Search in sources :

Example 36 with DataRow

use of org.apache.cayenne.DataRow 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)

Example 37 with DataRow

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

the class SimpleIdIncrementalFaultListDataRowsIT method testLastIndexOf1.

@Test
public void testLastIndexOf1() {
    // resolve first page
    list.get(0);
    List<DataRow> artists = ObjectSelect.dataRowQuery(Artist.class, Artist.ARTIST_NAME.eq("artist3")).select(context);
    assertEquals(1, artists.size());
    DataRow row = artists.get(0);
    assertEquals(2, list.lastIndexOf(row));
    row.remove("ARTIST_NAME");
    assertEquals(-1, list.lastIndexOf(row));
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 38 with DataRow

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

the class SimpleIdIncrementalFaultListDataRowsIT method testIndexOf2.

@Test
public void testIndexOf2() {
    // resolve first page
    list.get(0);
    List<DataRow> artists = ObjectSelect.dataRowQuery(Artist.class, Artist.ARTIST_NAME.eq("artist2")).select(context);
    assertEquals(1, artists.size());
    DataRow row = artists.get(0);
    assertEquals(1, list.indexOf(row));
    row.remove("ARTIST_NAME");
    assertEquals(-1, list.indexOf(row));
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 39 with DataRow

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

the class PrefetchProcessorTreeBuilderIT method testBuildTreeWithPrefetches.

@Test
public void testBuildTreeWithPrefetches() {
    final ClassDescriptor descriptor = resolver.getClassDescriptor("Artist");
    ObjEntity e2 = resolver.getObjEntity("Painting");
    ObjEntity e3 = resolver.getObjEntity("Gallery");
    ObjEntity e4 = resolver.getObjEntity("Exhibit");
    ObjEntity e5 = resolver.getObjEntity("ArtistExhibit");
    List<DataRow> mainRows = new ArrayList<>();
    Map<String, List<?>> extraRows = new HashMap<>();
    PrefetchTreeNode tree = new PrefetchTreeNode();
    tree.addPath(Artist.PAINTING_ARRAY.getName()).setPhantom(false);
    tree.addPath(Artist.PAINTING_ARRAY.dot(Painting.TO_GALLERY).dot(Gallery.EXHIBIT_ARRAY).getName()).setPhantom(false);
    tree.addPath(Artist.ARTIST_EXHIBIT_ARRAY.getName()).setPhantom(false);
    QueryMetadata metadata = new MockQueryMetadata() {

        @Override
        public ClassDescriptor getClassDescriptor() {
            return descriptor;
        }

        @Override
        public ObjEntity getObjEntity() {
            return descriptor.getEntity();
        }

        @Override
        public DbEntity getDbEntity() {
            return getObjEntity().getDbEntity();
        }

        @Override
        public DataMap getDataMap() {
            return getObjEntity().getDataMap();
        }

        @Override
        public boolean isRefreshingObjects() {
            return true;
        }

        @Override
        public boolean isResolvingInherited() {
            return true;
        }
    };
    HierarchicalObjectResolver resolver = new HierarchicalObjectResolver(context, metadata);
    PrefetchProcessorTreeBuilder builder = new PrefetchProcessorTreeBuilder(resolver, mainRows, extraRows);
    PrefetchProcessorNode n1 = builder.buildTree(tree);
    assertSame(mainRows, n1.getDataRows());
    assertSame(descriptor.getEntity(), n1.getResolver().getEntity());
    PrefetchProcessorNode n2 = (PrefetchProcessorNode) n1.getNode("paintingArray");
    assertNotNull(n2);
    assertSame(e2, n2.getResolver().getEntity());
    assertFalse(n2.isPhantom());
    assertTrue(n2.isPartitionedByParent());
    PrefetchProcessorNode n3 = (PrefetchProcessorNode) n1.getNode("paintingArray.toGallery");
    assertNotNull(n3);
    assertSame(e3, n3.getResolver().getEntity());
    assertTrue(n3.isPhantom());
    assertTrue(n3.isPartitionedByParent());
    PrefetchProcessorNode n4 = (PrefetchProcessorNode) n1.getNode("paintingArray.toGallery.exhibitArray");
    assertNotNull(n4);
    assertSame(e4, n4.getResolver().getEntity());
    assertFalse(n4.isPhantom());
    assertTrue(n4.isPartitionedByParent());
    PrefetchProcessorNode n5 = (PrefetchProcessorNode) n1.getNode("artistExhibitArray");
    assertNotNull(n5);
    assertSame(e5, n5.getResolver().getEntity());
    assertFalse(n5.isPhantom());
    assertTrue(n5.isPartitionedByParent());
}
Also used : MockQueryMetadata(org.apache.cayenne.query.MockQueryMetadata) QueryMetadata(org.apache.cayenne.query.QueryMetadata) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataRow(org.apache.cayenne.DataRow) ObjEntity(org.apache.cayenne.map.ObjEntity) MockQueryMetadata(org.apache.cayenne.query.MockQueryMetadata) PrefetchTreeNode(org.apache.cayenne.query.PrefetchTreeNode) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 40 with DataRow

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

the class ReturnTypesMappingIT method testBINARY.

@Test
public void testBINARY() throws Exception {
    if (unitDbAdapter.supportsLobs()) {
        String columnName = "BINARY_COLUMN";
        ReturnTypesMap2 test = context.newObject(ReturnTypesMap2.class);
        byte[] binaryValue = { 3, 4, 5, -6, 7, 0, 2, 9, 45, 64, 3, 127, -128, -60 };
        test.setBinaryColumn(binaryValue);
        context.commitChanges();
        DataRow testRead = (DataRow) context.performQuery(MappedSelect.query("SelectReturnTypesMap2")).get(0);
        Object columnValue = testRead.get(columnName);
        assertNotNull(columnValue);
        assertEquals(byte[].class, columnValue.getClass());
        assertTrue(Arrays.equals(binaryValue, (byte[]) columnValue));
    }
}
Also used : ReturnTypesMap2(org.apache.cayenne.testdo.return_types.ReturnTypesMap2) DataRow(org.apache.cayenne.DataRow) 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