Search in sources :

Example 1 with CayenneDataObject

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);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 2 with CayenneDataObject

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]);
}
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 3 with CayenneDataObject

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));
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) HashMap(java.util.HashMap) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) Test(org.junit.Test)

Example 4 with CayenneDataObject

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));
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) Test(org.junit.Test)

Example 5 with CayenneDataObject

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

CayenneDataObject (org.apache.cayenne.CayenneDataObject)7 Test (org.junit.Test)7 DataObject (org.apache.cayenne.DataObject)6 ObjectId (org.apache.cayenne.ObjectId)4 SQLStatement (org.apache.cayenne.access.jdbc.SQLStatement)4 HashMap (java.util.HashMap)3 DbAttribute (org.apache.cayenne.map.DbAttribute)1 DbEntity (org.apache.cayenne.map.DbEntity)1 ObjAttribute (org.apache.cayenne.map.ObjAttribute)1 ObjEntity (org.apache.cayenne.map.ObjEntity)1