Search in sources :

Example 1 with DbSqlBuilder

use of jodd.db.oom.sqlgen.DbSqlBuilder in project jodd by oblac.

the class DbSqlTemplateTest method testMatch.

@Test
public void testMatch() {
    DbSqlBuilder st;
    Boy boy = new Boy();
    boy.id = 1;
    boy.girlId = 3;
    st = sql("$T{boy boy} where $M{boy=boy}").use("boy", boy);
    assertEquals("BOY boy where (boy.GIRL_ID=:boy.girlId and boy.ID=:boy.id)", st.generateQuery());
    Map<String, ParameterValue> map = st.getQueryParameters();
    assertEquals(2, map.size());
    assertEquals(Integer.valueOf(1), map.get("boy.id").getValue());
    assertEquals(Integer.valueOf(3), map.get("boy.girlId").getValue());
    boy.id = 4;
    boy.girlId = 1;
    st = sql("$T{boy b} where $M{b=boy}").use("boy", boy);
    assertEquals("BOY b where (b.GIRL_ID=:boy.girlId and b.ID=:boy.id)", st.generateQuery());
    map = st.getQueryParameters();
    assertEquals(2, map.size());
    assertEquals(Integer.valueOf(4), map.get("boy.id").getValue());
    assertEquals(Integer.valueOf(1), map.get("boy.girlId").getValue());
    BadBoy badBoy = new BadBoy();
    st = sql("$T{boy b} where $M{b=boy}").use("boy", badBoy);
    assertEquals("BOY b where (1=1)", st.generateQuery());
    map = st.getQueryParameters();
    assertNull(map);
    st = sql("$T{boy b} where $M{b=boy}").use("boy", badBoy);
    assertEquals("BOY b where (1=1)", st.generateQuery());
    map = st.getQueryParameters();
    assertNull(map);
    badBoy.ajdi = Integer.valueOf(4);
    badBoy.girlId = Integer.valueOf(1);
    st = sql("$T{boy b} where $M{b=boy}").use("boy", badBoy);
    assertEquals("BOY b where (b.ID=:boy.ajdi and b.GIRL_ID=:boy.girlId)", st.generateQuery());
    map = st.getQueryParameters();
    assertEquals(2, map.size());
    assertEquals(Integer.valueOf(4), map.get("boy.ajdi").getValue());
    assertEquals(Integer.valueOf(1), map.get("boy.girlId").getValue());
    badBoy.ajdi = null;
    st = sql("$T{boy b} where $M{b=boy.*}").use("boy", badBoy);
    assertEquals("BOY b where (b.ID=:boy.ajdi and b.GIRL_ID=:boy.girlId and b.NAME=:boy.nejm)", st.generateQuery());
    map = st.getQueryParameters();
    assertEquals(3, map.size());
    assertEquals(Integer.valueOf(1), map.get("boy.girlId").getValue());
}
Also used : BadBoy(jodd.db.oom.tst.BadBoy) ParameterValue(jodd.db.oom.sqlgen.ParameterValue) DbSqlBuilder(jodd.db.oom.sqlgen.DbSqlBuilder) Boy(jodd.db.oom.tst.Boy) BadBoy(jodd.db.oom.tst.BadBoy) Test(org.junit.Test)

Example 2 with DbSqlBuilder

use of jodd.db.oom.sqlgen.DbSqlBuilder in project jodd by oblac.

the class DbSqlTemplateTest method testColumns1.

@Test
public void testColumns1() {
    DbSqlBuilder st;
    // 1
    st = sql("$T{Boy} | $C{Boy.id} | $C{Boy.*}");
    assertEquals("BOY | BOY.ID | BOY.GIRL_ID, BOY.ID, BOY.NAME", st.generateQuery());
    st = sql("$T{BadBoy} | $C{BadBoy.ajdi} | $C{BadBoy.*} | $C{BadBoy.+}");
    assertEquals("BOY | BOY.ID | BOY.ID, BOY.GIRL_ID, BOY.NAME | BOY.ID", st.generateQuery());
    // 2
    st = sql("$T{b b} | $C{b.id}").use("b", new Boy());
    assertEquals("BOY b | b.ID", st.generateQuery());
    st = sql("$T{b bb} | $C{bb.id} | $C{bb.*}").use("b", new Boy());
    assertEquals("BOY bb | bb.ID | bb.GIRL_ID, bb.ID, bb.NAME", st.generateQuery());
    st = sql("$T{b bb} | $C{bb.ajdi} | $C{bb.*} | $C{bb.+}").use("b", new BadBoy());
    assertEquals("BOY bb | bb.ID | bb.ID, bb.GIRL_ID, bb.NAME | bb.ID", st.generateQuery());
    // 3
    st = sql("$T{b b} | $C{b.id}").use("b", Boy.class);
    assertEquals("BOY b | b.ID", st.generateQuery());
    st = sql("$T{b bb} | $C{bb.id} | $C{bb.*}").use("b", Boy.class);
    assertEquals("BOY bb | bb.ID | bb.GIRL_ID, bb.ID, bb.NAME", st.generateQuery());
    st = sql("$T{b bb} | $C{bb.ajdi} | $C{bb.*}").use("b", BadBoy.class);
    assertEquals("BOY bb | bb.ID | bb.ID, bb.GIRL_ID, bb.NAME", st.generateQuery());
    st = sql("$T{b bb} | $C{bb.ajdi} | $C{bb.*} | $C{bb.+}").use("b", BadBoy.class);
    assertEquals("BOY bb | bb.ID | bb.ID, bb.GIRL_ID, bb.NAME | bb.ID", st.generateQuery());
}
Also used : BadBoy(jodd.db.oom.tst.BadBoy) DbSqlBuilder(jodd.db.oom.sqlgen.DbSqlBuilder) Boy(jodd.db.oom.tst.Boy) BadBoy(jodd.db.oom.tst.BadBoy) Test(org.junit.Test)

Example 3 with DbSqlBuilder

use of jodd.db.oom.sqlgen.DbSqlBuilder in project jodd by oblac.

the class DbSqlTemplateTest method testJoin.

@Test
public void testJoin() {
    DbSqlBuilder st = sql("select $C{bb.*}, $C{bg.+} from $T{BadGirl bg} join $T{Boy bb} on $bg.+=bb.GIRL_ID");
    assertEquals("select bb.GIRL_ID, bb.ID, bb.NAME, bg.ID from GIRL bg join BOY bb on bg.ID=bb.GIRL_ID", st.generateQuery());
}
Also used : DbSqlBuilder(jodd.db.oom.sqlgen.DbSqlBuilder) Test(org.junit.Test)

Example 4 with DbSqlBuilder

use of jodd.db.oom.sqlgen.DbSqlBuilder in project jodd by oblac.

the class DbSqlBuilderTest method testStrings.

@Test
public void testStrings() {
    DbSqlBuilder dbc = sql().$("123").$("xxx");
    assertEquals("123xxx", dbc.generateQuery());
}
Also used : DbSqlBuilder(jodd.db.oom.sqlgen.DbSqlBuilder) Test(org.junit.Test)

Example 5 with DbSqlBuilder

use of jodd.db.oom.sqlgen.DbSqlBuilder in project jodd by oblac.

the class DbSqlBuilderTest method testValue.

@Test
@SuppressWarnings({ "unchecked" })
public void testValue() {
    Boy b = new Boy();
    DbSqlBuilder dbc = new DbSqlBuilder();
    assertEquals(":zzz", dbc.value("zzz", Integer.valueOf(b.girlId)).generateQuery());
    assertEquals(1, dbc.getQueryParameters().size());
    assertEquals(Integer.valueOf(0), dbc.getQueryParameters().get("zzz").getValue());
    assertEquals(":zzz :p0", dbc.$().value(Integer.valueOf(3)).generateQuery());
    assertEquals(2, dbc.getQueryParameters().size());
    assertEquals(Integer.valueOf(3), dbc.getQueryParameters().get("p0").getValue());
    assertEquals(":zzz :p0 :p1", dbc.$().valueRef("val").use("val", Integer.valueOf(7)).generateQuery());
    assertEquals(3, dbc.getQueryParameters().size());
    assertEquals(Integer.valueOf(3), dbc.getQueryParameters().get("p0").getValue());
    assertEquals(Integer.valueOf(7), dbc.getQueryParameters().get("p1").getValue());
    dbc = new DbSqlBuilder();
    List l = new ArrayList();
    l.add("v1");
    l.add(Integer.valueOf(2));
    assertEquals(":zzz0, :zzz1", dbc.value("zzz", l).generateQuery());
    assertEquals("v1", dbc.getQueryParameters().get("zzz0").getValue());
    assertEquals(Integer.valueOf(2), dbc.getQueryParameters().get("zzz1").getValue());
}
Also used : DbSqlBuilder(jodd.db.oom.sqlgen.DbSqlBuilder) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Boy(jodd.db.oom.tst.Boy) BadBoy(jodd.db.oom.tst.BadBoy) Test(org.junit.Test)

Aggregations

DbSqlBuilder (jodd.db.oom.sqlgen.DbSqlBuilder)21 Test (org.junit.Test)20 BadBoy (jodd.db.oom.tst.BadBoy)10 Boy (jodd.db.oom.tst.Boy)9 List (java.util.List)2 DbSession (jodd.db.DbSession)2 DbThreadSession (jodd.db.DbThreadSession)2 ParameterValue (jodd.db.oom.sqlgen.ParameterValue)2 BadGirl (jodd.db.oom.tst.BadGirl)2 Girl (jodd.db.oom.tst.Girl)2 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 DbQuery (jodd.db.DbQuery)1 QueryMapper (jodd.db.QueryMapper)1 DbOomQuery (jodd.db.oom.DbOomQuery)1