Search in sources :

Example 36 with DataObject

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

the class ValueForNullIT method test.

@Test
public void test() throws Exception {
    DbEntity dbEntity = map.getDbEntity("PAINTING");
    assertNotNull(dbEntity);
    ObjEntity objEntity = map.getObjEntity("Painting");
    assertNotNull(objEntity);
    // insert some rows before adding "not null" column
    final int nrows = 10;
    for (int i = 0; i < nrows; i++) {
        DataObject o = (DataObject) context.newObject("Painting");
        o.writeProperty("paintingTitle", "ptitle" + i);
    }
    context.commitChanges();
    // create and add new column to model and db
    DbAttribute column = new DbAttribute("NEWCOL2", Types.VARCHAR, dbEntity);
    column.setMandatory(false);
    column.setMaxLength(10);
    dbEntity.addAttribute(column);
    assertTrue(dbEntity.getAttributes().contains(column));
    assertEquals(column, dbEntity.getAttribute(column.getName()));
    assertTokensAndExecute(1, 0);
    // need obj attr to be able to query
    ObjAttribute objAttr = new ObjAttribute("newcol2");
    objAttr.setDbAttributePath(column.getName());
    objEntity.addAttribute(objAttr);
    // check that is was merged
    assertTokensAndExecute(0, 0);
    // set not null
    column.setMandatory(true);
    // merge to db
    assertTokensAndExecute(2, 0);
    // check that is was merged
    assertTokensAndExecute(0, 0);
    // check values for null
    Expression qual = ExpressionFactory.matchExp(objAttr.getName(), DEFAULT_VALUE_STRING);
    SelectQuery query = new SelectQuery("Painting", qual);
    @SuppressWarnings("unchecked") List<Persistent> rows = context.performQuery(query);
    assertEquals(nrows, rows.size());
    // clean up
    dbEntity.removeAttribute(column.getName());
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ObjEntity(org.apache.cayenne.map.ObjEntity) DataObject(org.apache.cayenne.DataObject) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) Expression(org.apache.cayenne.exp.Expression) DbAttribute(org.apache.cayenne.map.DbAttribute) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Example 37 with DataObject

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

the class VelocitySQLTemplateProcessorTest method testProcessTemplateNotEqualID.

@Test
public void testProcessTemplateNotEqualID() throws Exception {
    String sqlTemplate = "SELECT * FROM ME WHERE " + "COLUMN1 #bindNotEqual($helper.cayenneExp($a, 'db:ID_COLUMN1')) " + "AND COLUMN2 #bindNotEqual($helper.cayenneExp($a, 'db:ID_COLUMN2'))";
    Map<String, Object> idMap = new HashMap<>();
    idMap.put("ID_COLUMN1", new Integer(3));
    idMap.put("ID_COLUMN2", "aaa");
    ObjectId id = new ObjectId("T", idMap);
    DataObject dataObject = new CayenneDataObject();
    dataObject.setObjectId(id);
    Map<String, Object> map = Collections.<String, Object>singletonMap("a", dataObject);
    SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
    assertEquals("SELECT * FROM ME WHERE COLUMN1 <> ? AND COLUMN2 <> ?", compiled.getSql());
    assertEquals(2, compiled.getBindings().length);
    assertBindingValue(new Integer(3), compiled.getBindings()[0]);
    assertBindingValue("aaa", compiled.getBindings()[1]);
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) HashMap(java.util.HashMap) ObjectId(org.apache.cayenne.ObjectId) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 38 with DataObject

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

the class VelocitySQLTemplateProcessorTest method testProcessTemplateID.

@Test
public void testProcessTemplateID() throws Exception {
    String sqlTemplate = "SELECT * FROM ME WHERE COLUMN1 = #bind($helper.cayenneExp($a, 'db:ID_COLUMN'))";
    DataObject dataObject = new CayenneDataObject();
    dataObject.setObjectId(new ObjectId("T", "ID_COLUMN", 5));
    Map<String, Object> map = Collections.<String, Object>singletonMap("a", dataObject);
    SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
    assertEquals("SELECT * FROM ME WHERE COLUMN1 = ?", compiled.getSql());
    assertEquals(1, compiled.getBindings().length);
    assertBindingValue(new Integer(5), compiled.getBindings()[0]);
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) ObjectId(org.apache.cayenne.ObjectId) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Aggregations

DataObject (org.apache.cayenne.DataObject)38 Test (org.junit.Test)27 ObjectId (org.apache.cayenne.ObjectId)18 DataRow (org.apache.cayenne.DataRow)15 Persistent (org.apache.cayenne.Persistent)8 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)7 Artist (org.apache.cayenne.testdo.testmap.Artist)7 CayenneDataObject (org.apache.cayenne.CayenneDataObject)6 SelectQuery (org.apache.cayenne.query.SelectQuery)6 Date (java.util.Date)5 HashMap (java.util.HashMap)5 SQLStatement (org.apache.cayenne.access.jdbc.SQLStatement)4 Expression (org.apache.cayenne.exp.Expression)3 ObjEntity (org.apache.cayenne.map.ObjEntity)3 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)3 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)3 Map (java.util.Map)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 MockDataObject (org.apache.cayenne.MockDataObject)2 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)2