use of jodd.db.oom.sqlgen.DbSqlBuilder in project jodd by oblac.
the class DbSqlBuilderTest method testCriteria.
@Test
public void testCriteria() {
BadBoy bb = new BadBoy();
BadGirl bg = new BadGirl();
DbSqlBuilder dbc = sql().$("select").columnsAll("bb").columnsIds("bg").$(" from").table(bb, "bb").table(bg, "bg").$().match("bb", bb).$().match("bg", bg);
assertEquals("select bb.ID, bb.GIRL_ID, bb.NAME, bg.ID from BOY bb, GIRL bg (1=1) (1=1)", dbc.generateQuery());
dbc = sql().$("select").columnsAll("bb").columnsIds("bg").$(" from").table(bb, "bb").table(bg, "bg").$(" where ").match("bb", bb).$().match("bg", bg).$(" or ").refId("bb").$("=").value(Long.valueOf(5L));
assertEquals("select bb.ID, bb.GIRL_ID, bb.NAME, bg.ID from BOY bb, GIRL bg where (1=1) (1=1) or bb.ID=:p0", dbc.generateQuery());
dbc.reset();
bb.ajdi = bg.fooid = Integer.valueOf(1);
assertEquals("select bb.ID, bb.GIRL_ID, bb.NAME, bg.ID from BOY bb, GIRL bg where (bb.ID=:badBoy.ajdi) (bg.ID=:badGirl.fooid) or bb.ID=:p0", dbc.generateQuery());
// test double call
assertEquals("select bb.ID, bb.GIRL_ID, bb.NAME, bg.ID from BOY bb, GIRL bg where (bb.ID=:badBoy.ajdi) (bg.ID=:badGirl.fooid) or bb.ID=:p0", dbc.generateQuery());
}
use of jodd.db.oom.sqlgen.DbSqlBuilder in project jodd by oblac.
the class DbSqlBuilderTest method testUpdateSet.
@Test
public void testUpdateSet() {
Boy b = new Boy();
b.id = 1;
b.girlId = 2;
DbSqlBuilder dbc = sql().set("b", b).table("Boy", "b");
assertEquals("set GIRL_ID=:boy.girlId, ID=:boy.id BOY b", dbc.generateQuery());
assertEquals(2, dbc.getQueryParameters().size());
dbc = sql().set("Boy", b).table("Boy", null);
assertEquals("set GIRL_ID=:boy.girlId, ID=:boy.id BOY", dbc.generateQuery());
assertEquals(2, dbc.getQueryParameters().size());
dbc = sql().setAll("b", b).table("Boy", "b");
assertEquals("set GIRL_ID=:boy.girlId, ID=:boy.id, NAME=:boy.name BOY b", dbc.generateQuery());
assertEquals(3, dbc.getQueryParameters().size());
BadBoy bb = new BadBoy();
dbc = sql().set("b", bb).table("BadBoy", "b");
assertEquals("set BOY b", dbc.generateQuery());
dbc = sql().set("BadBoy", bb).table("BadBoy", null);
assertEquals("set BOY", dbc.generateQuery());
dbc = sql().setAll("b", bb).table("BadBoy", "b");
assertEquals("set ID=:badBoy.ajdi, GIRL_ID=:badBoy.girlId, NAME=:badBoy.nejm BOY b", dbc.generateQuery());
}
use of jodd.db.oom.sqlgen.DbSqlBuilder in project jodd by oblac.
the class DbSqlBuilderTest method testTable.
@Test
public void testTable() {
DbSqlBuilder s;
// [1]
s = sql().table("Boy");
assertEquals("BOY", s.generateQuery());
assertNotNull(s.getTableDescriptor("Boy"));
s = sql().table("Boy", null);
assertEquals("BOY", s.generateQuery());
assertNotNull(s.getTableDescriptor("Boy"));
s = sql().table("Boy", "bbb");
assertEquals("BOY bbb", s.generateQuery());
assertNotNull(s.getTableDescriptor("bbb"));
// [2]
s = sql().table("BadBoy");
assertEquals("BOY", s.generateQuery());
assertNotNull(s.getTableDescriptor("BadBoy"));
s = sql().table("BadBoy", null);
assertEquals("BOY", s.generateQuery());
assertNotNull(s.getTableDescriptor("BadBoy"));
s = sql().table("BadBoy", "bbb");
assertEquals("BOY bbb", s.generateQuery());
assertNotNull(s.getTableDescriptor("bbb"));
// [3]
s = sql().table(Boy.class);
assertEquals("BOY Boy", s.generateQuery());
assertNotNull(s.getTableDescriptor("Boy"));
s = sql().table(Boy.class, null);
assertEquals("BOY", s.generateQuery());
assertNotNull(s.getTableDescriptor("Boy"));
s = sql().table(Boy.class, "bbb");
assertEquals("BOY bbb", s.generateQuery());
assertNotNull(s.getTableDescriptor("bbb"));
// [4]
s = sql().table(BadBoy.class);
assertEquals("BOY BadBoy", s.generateQuery());
assertNotNull(s.getTableDescriptor("BadBoy"));
s = sql().table(BadBoy.class, null);
assertEquals("BOY", s.generateQuery());
assertNotNull(s.getTableDescriptor("BadBoy"));
s = sql().table(BadBoy.class, "bbb");
assertEquals("BOY bbb", s.generateQuery());
assertNotNull(s.getTableDescriptor("bbb"));
// [5]
s = sql().table("bbb").use("bbb", Boy.class);
assertEquals("BOY", s.generateQuery());
assertNotNull(s.getTableDescriptor("bbb"));
s = sql().table("bbb", null).use("bbb", Boy.class);
assertEquals("BOY", s.generateQuery());
assertNotNull(s.getTableDescriptor("bbb"));
s = sql().table("bbb", "x").use("bbb", Boy.class);
assertEquals("BOY x", s.generateQuery());
assertNotNull(s.getTableDescriptor("x"));
// [6]
s = sql().table("Boy bbb");
assertEquals("BOY bbb", s.generateQuery());
assertNotNull(s.getTableDescriptor("bbb"));
s = sql().table("BadBoy bbb");
assertEquals("BOY bbb", s.generateQuery());
assertNotNull(s.getTableDescriptor("bbb"));
}
use of jodd.db.oom.sqlgen.DbSqlBuilder in project jodd by oblac.
the class DbSqlBuilderTest method testCriteria2.
@Test
public void testCriteria2() {
Girl girl = new Girl();
girl.speciality = "piano";
Girl girl_condition = new Girl();
girl_condition.speciality = "swim";
String tableRef = "ggg";
DbSqlBuilder dsb = sql().$("update ").table(girl, tableRef).set(tableRef, girl).$("where ").match(tableRef, "conditionRef").use("conditionRef", girl_condition);
String sql = dsb.generateQuery();
Map<String, ParameterValue> params = dsb.getQueryParameters();
assertEquals(2, params.size());
assertEquals("piano", params.get("girl.speciality").getValue());
assertEquals("swim", params.get("conditionRef.speciality").getValue());
assertEquals("update GIRL ggg set SPECIALITY=:girl.speciality " + "where (ggg.SPECIALITY=:conditionRef.speciality)", sql);
}
use of jodd.db.oom.sqlgen.DbSqlBuilder in project jodd by oblac.
the class DbSqlBuilderTest method testInsert.
@Test
public void testInsert() {
Boy b = new Boy();
DbSqlBuilder dbc = sql().insert("Boy", b);
assertEquals("insert into BOY (GIRL_ID, ID) values (:boy.girlId, :boy.id)", dbc.generateQuery());
assertEquals(2, dbc.getQueryParameters().size());
assertEquals(Integer.valueOf(0), dbc.getQueryParameters().get("boy.girlId").getValue());
assertEquals(Integer.valueOf(0), dbc.getQueryParameters().get("boy.id").getValue());
dbc = sql().insert(Boy.class, b);
assertEquals("insert into BOY (GIRL_ID, ID) values (:boy.girlId, :boy.id)", dbc.generateQuery());
assertEquals(2, dbc.getQueryParameters().size());
assertEquals(Integer.valueOf(0), dbc.getQueryParameters().get("boy.girlId").getValue());
assertEquals(Integer.valueOf(0), dbc.getQueryParameters().get("boy.id").getValue());
dbc = sql().insert(b);
assertEquals("insert into BOY (GIRL_ID, ID) values (:boy.girlId, :boy.id)", dbc.generateQuery());
assertEquals(2, dbc.getQueryParameters().size());
assertEquals(Integer.valueOf(0), dbc.getQueryParameters().get("boy.girlId").getValue());
assertEquals(Integer.valueOf(0), dbc.getQueryParameters().get("boy.id").getValue());
}
Aggregations