Search in sources :

Example 51 with DataRow

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

the class ReturnTypesMappingIT method testLONGNVARCHAR.

@Test
public void testLONGNVARCHAR() throws Exception {
    String columnName = "LONGNVARCHAR_COLUMN";
    ReturnTypesMap1 test = context.newObject(ReturnTypesMap1.class);
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < 500; i++) {
        buffer.append("ی متوازن، نیازی ندارد که ب large string for tests!!!!\n");
    }
    String longnvarcharValue = buffer.toString();
    test.setLongnvarcharColumn(longnvarcharValue);
    context.commitChanges();
    DataRow testRead = (DataRow) context.performQuery(MappedSelect.query("SelectReturnTypesMap1")).get(0);
    Object columnValue = testRead.get(columnName);
    assertNotNull(columnValue);
    assertEquals(String.class, columnValue.getClass());
    assertEquals(longnvarcharValue, columnValue);
}
Also used : ReturnTypesMap1(org.apache.cayenne.testdo.return_types.ReturnTypesMap1) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 52 with DataRow

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

the class ReturnTypesMappingIT method testNCHAR.

@Test
public void testNCHAR() throws Exception {
    String columnName = "NCHAR_COLUMN";
    ReturnTypesMap1 test = context.newObject(ReturnTypesMap1.class);
    String charValue = "درخت‌های جستجوی متوازن، نیازی ندارد که به صورت!";
    test.setNcharColumn(charValue);
    context.commitChanges();
    DataRow testRead = (DataRow) context.performQuery(MappedSelect.query("SelectReturnTypesMap1")).get(0);
    Object columnValue = testRead.get(columnName);
    assertNotNull(columnValue);
    assertEquals(String.class, columnValue.getClass());
    assertEquals(charValue, columnValue);
}
Also used : ReturnTypesMap1(org.apache.cayenne.testdo.return_types.ReturnTypesMap1) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 53 with DataRow

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

the class DeletedDiffProcessor method nodeRemoved.

@Override
public void nodeRemoved(Object nodeId) {
    ObjectId id = (ObjectId) nodeId;
    final MutableObjectChange objectChangeSet = changeSet.getOrCreate(id, ObjectChangeType.DELETE);
    // TODO: rewrite with SelectById query after Cayenne upgrade
    ObjectIdQuery query = new ObjectIdQuery(id, true, ObjectIdQuery.CACHE);
    QueryResponse result = channel.onQuery(null, query);
    @SuppressWarnings("unchecked") List<DataRow> rows = result.firstList();
    if (rows.isEmpty()) {
        LOGGER.warn("No DB snapshot for object to be deleted, no changes will be recorded. ID: " + id);
        return;
    }
    final DataRow row = rows.get(0);
    ClassDescriptor descriptor = channel.getEntityResolver().getClassDescriptor(id.getEntityName());
    final CommitLogEntity entity = entityFactory.getEntity(id);
    descriptor.visitProperties(new PropertyVisitor() {

        @Override
        public boolean visitAttribute(AttributeProperty property) {
            if (!entity.isIncluded(property.getName())) {
                return true;
            }
            Object value;
            if (entity.isConfidential(property.getName())) {
                value = Confidential.getInstance();
            } else {
                String key = property.getAttribute().getDbAttributeName();
                value = row.get(key);
            }
            if (value != null) {
                objectChangeSet.attributeChanged(property.getName(), value, null);
            }
            return true;
        }

        @Override
        public boolean visitToOne(ToOneProperty property) {
            if (!entity.isIncluded(property.getName())) {
                return true;
            }
            // TODO: is there such a thing as "confidential" relationship that we need to hide?
            DbRelationship dbRelationship = property.getRelationship().getDbRelationships().get(0);
            ObjectId value = row.createTargetObjectId(property.getTargetDescriptor().getEntity().getName(), dbRelationship);
            if (value != null) {
                objectChangeSet.toOneRelationshipDisconnected(property.getName(), value);
            }
            return true;
        }

        @Override
        public boolean visitToMany(ToManyProperty property) {
            return true;
        }
    });
}
Also used : MutableObjectChange(org.apache.cayenne.commitlog.model.MutableObjectChange) CommitLogEntity(org.apache.cayenne.commitlog.meta.CommitLogEntity) ObjectId(org.apache.cayenne.ObjectId) DataRow(org.apache.cayenne.DataRow) QueryResponse(org.apache.cayenne.QueryResponse) DbRelationship(org.apache.cayenne.map.DbRelationship) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery)

Example 54 with DataRow

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

the class FrontBasePkGenerator method longPkFromDatabase.

/**
 * @since 3.0
 */
@Override
protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception {
    String template = "SELECT #result('UNIQUE' 'long') FROM " + entity.getName();
    final long[] pkHolder = new long[1];
    SQLTemplate query = new SQLTemplate(entity, template);
    OperationObserver observer = new DoNothingOperationObserver() {

        @Override
        public void nextRows(Query query, List<?> dataRows) {
            if (dataRows.size() != 1) {
                throw new CayenneRuntimeException("Error fetching PK. Expected one row, got %d", dataRows.size());
            }
            DataRow row = (DataRow) dataRows.get(0);
            Number pk = (Number) row.get("UNIQUE");
            pkHolder[0] = pk.longValue();
        }
    };
    node.performQueries(Collections.singleton((Query) query), observer);
    return pkHolder[0];
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Query(org.apache.cayenne.query.Query) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) OperationObserver(org.apache.cayenne.access.OperationObserver) DoNothingOperationObserver(org.apache.cayenne.access.util.DoNothingOperationObserver) ArrayList(java.util.ArrayList) List(java.util.List) DoNothingOperationObserver(org.apache.cayenne.access.util.DoNothingOperationObserver) DataRow(org.apache.cayenne.DataRow)

Example 55 with DataRow

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

the class ObjectResolver method objectsFromDataRows.

/**
 * Converts rows to objects.
 * <p>
 * Synchronization note. This method requires EXTERNAL synchronization on
 * ObjectStore and DataRowStore.
 * </p>
 */
List<Persistent> objectsFromDataRows(List<? extends DataRow> rows) {
    if (rows == null || rows.size() == 0) {
        return new ArrayList<>(1);
    }
    List<Persistent> results = new ArrayList<>(rows.size());
    for (DataRow row : rows) {
        // nulls are possible here since 3.0 for some varieties of EJBQL,
        // simple example of this: "select p.toGallery+ from Painting p" where toGallery is null.
        results.add(objectFromDataRow(row));
    }
    // now deal with snapshots
    cache.snapshotsUpdatedForObjects(results, rows, refreshObjects);
    return results;
}
Also used : ArrayList(java.util.ArrayList) Persistent(org.apache.cayenne.Persistent) DataRow(org.apache.cayenne.DataRow)

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