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);
}
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]);
}
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]);
}
Aggregations