use of org.apache.cayenne.CayenneDataObject in project cayenne by apache.
the class MergerFactoryIT method testAddTableToDb.
@Test
public void testAddTableToDb() throws Exception {
dropTableIfPresent("NEW_TABLE");
assertTokensAndExecute(0, 0);
DbEntity dbEntity = new DbEntity("NEW_TABLE");
DbAttribute column1 = new DbAttribute("ID", Types.INTEGER, dbEntity);
column1.setMandatory(true);
column1.setPrimaryKey(true);
dbEntity.addAttribute(column1);
DbAttribute column2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity);
column2.setMaxLength(10);
column2.setMandatory(false);
dbEntity.addAttribute(column2);
map.addDbEntity(dbEntity);
assertTokensAndExecute(1, 0);
assertTokensAndExecute(0, 0);
ObjEntity objEntity = new ObjEntity("NewTable");
objEntity.setDbEntity(dbEntity);
ObjAttribute oatr1 = new ObjAttribute("name");
oatr1.setDbAttributePath(column2.getName());
oatr1.setType("java.lang.String");
objEntity.addAttribute(oatr1);
map.addObjEntity(objEntity);
for (int i = 0; i < 5; i++) {
CayenneDataObject dao = (CayenneDataObject) context.newObject(objEntity.getName());
dao.writeProperty(oatr1.getName(), "test " + i);
}
context.commitChanges();
// clear up
map.removeObjEntity(objEntity.getName(), true);
map.removeDbEntity(dbEntity.getName(), true);
resolver.refreshMappingCache();
assertNull(map.getObjEntity(objEntity.getName()));
assertNull(map.getDbEntity(dbEntity.getName()));
assertFalse(map.getDbEntities().contains(dbEntity));
assertTokensAndExecute(1, 0);
assertTokensAndExecute(0, 0);
}
use of org.apache.cayenne.CayenneDataObject in project cayenne by apache.
the class CayenneSQLTemplateProcessorTest 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", 3);
idMap.put("ID_COLUMN2", "aaa");
ObjectId id = new ObjectId("T", idMap);
DataObject dataObject = new CayenneDataObject();
dataObject.setObjectId(id);
Map<String, Object> map = Collections.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(3, compiled.getBindings()[0]);
assertBindingValue("aaa", compiled.getBindings()[1]);
}
use of org.apache.cayenne.CayenneDataObject in project cayenne by apache.
the class Expression_ParamsTest method testInParameter_AsValues.
@Test
public void testInParameter_AsValues() throws Exception {
Expression inExp = ExpressionFactory.exp("k1 in ($ap, $bp)");
String e1String = "k1 in (\"a\", \"b\")";
Expression e1 = ExpressionFactory.exp(e1String);
Map<String, Object> params = new HashMap<String, Object>();
params.put("ap", "a");
params.put("bp", "b");
Expression transformed = inExp.params(params);
TstTraversalHandler.compareExps(e1, transformed);
assertEquals(e1String, transformed.toString());
// just in case manually check params
DataObject o1 = new CayenneDataObject();
o1.writePropertyDirectly("k1", "a");
assertTrue(transformed.match(o1));
DataObject o2 = new CayenneDataObject();
o2.writePropertyDirectly("k1", "x");
assertFalse(transformed.match(o2));
}
use of org.apache.cayenne.CayenneDataObject in project cayenne by apache.
the class Expression_ParamsTest method testInParameter.
/**
* Tests how parameter substitution algorithm works on an expression with no
* parameters.
*/
@Test
public void testInParameter() throws Exception {
Expression inExp = ExpressionFactory.exp("k1 in $test");
Expression e1 = ExpressionFactory.exp("k1 in ('a', 'b')");
Expression transformed = inExp.params(Collections.singletonMap("test", new Object[] { "a", "b" }));
TstTraversalHandler.compareExps(e1, transformed);
// just in case manually check params
DataObject o1 = new CayenneDataObject();
o1.writePropertyDirectly("k1", "a");
assertTrue(transformed.match(o1));
DataObject o2 = new CayenneDataObject();
o2.writePropertyDirectly("k1", "x");
assertFalse(transformed.match(o2));
}
use of org.apache.cayenne.CayenneDataObject in project cayenne by apache.
the class CayenneSQLTemplateProcessorTest 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.singletonMap("a", dataObject);
SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
assertEquals("SELECT * FROM ME WHERE COLUMN1 = ?", compiled.getSql());
assertEquals(1, compiled.getBindings().length);
assertBindingValue(5, compiled.getBindings()[0]);
}
Aggregations