Search in sources :

Example 16 with DbSqlBuilder

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

the class DbSqlTemplateTest method testTables.

@Test
public void testTables() {
    DbSqlBuilder st;
    // 1
    st = sql("$T{Boy}");
    assertEquals("BOY", st.generateQuery());
    st = sql("$T{BadBoy}");
    assertEquals("BOY", st.generateQuery());
    st = sql("$T{Boy b}");
    assertEquals("BOY b", st.generateQuery());
    st = sql("$T{BadBoy b}");
    assertEquals("BOY b", st.generateQuery());
    // 2
    st = sql("$T{b}").use("b", new Boy());
    assertEquals("BOY", st.generateQuery());
    st = sql("$T{b b}").use("b", new Boy());
    assertEquals("BOY b", st.generateQuery());
    st = sql("$T{b}").use("b", new BadBoy());
    assertEquals("BOY", st.generateQuery());
    // 3
    st = sql("$T{b}").use("b", Boy.class);
    assertEquals("BOY", st.generateQuery());
    st = sql("$T{b}").use("b", BadBoy.class);
    assertEquals("BOY", 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 17 with DbSqlBuilder

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

the class DbSqlTemplateTest method testManyTables.

@Test
public void testManyTables() {
    DbSqlBuilder st = sql("$T{Boy, Girl girl}");
    assertEquals("BOY, GIRL girl", st.generateQuery());
}
Also used : DbSqlBuilder(jodd.db.oom.sqlgen.DbSqlBuilder) Test(org.junit.Test)

Example 18 with DbSqlBuilder

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

the class DbSqlTemplateTest method testColumns3.

@Test
public void testColumns3() {
    DbSqlBuilder st;
    st = sql("$C{b.id,b.name} from $T{b b}").use("b", Boy.class);
    assertEquals("b.ID, b.NAME from BOY b", st.generateQuery());
    st = sql("$C{    a.ajdi ,  a.nejm     } from $T{b a}").use("b", BadBoy.class);
    assertEquals("a.ID, a.NAME from BOY a", st.generateQuery());
    st = sql("$C{b.id,b.name} from $T{b b}").use("b", Boy.class);
    assertEquals("b.ID as BOY$ID, b.NAME as BOY$NAME from BOY b", st.aliasColumnsAs(TABLE_NAME).generateQuery());
    st = sql("$C{    a.ajdi ,  a.nejm     } from $T{b a}").use("b", BadBoy.class);
    assertEquals("a.ID as BOY$ID, a.NAME as BOY$NAME from BOY a", st.aliasColumnsAs(TABLE_NAME).generateQuery());
    st = sql("$b.id as d, $C{b.name} from $T{b b}").use("b", Boy.class).aliasColumnsAs(TABLE_NAME);
    assertEquals("b.ID as d, b.NAME as BOY$NAME from BOY b", st.generateQuery());
    st = sql("$a.ajdi as d, $C{    a.nejm     } from $T{b a}").use("b", BadBoy.class).aliasColumnsAs(TABLE_NAME);
    assertEquals("a.ID as d, a.NAME as BOY$NAME from BOY a", st.generateQuery());
    st = sql("$C{b.*} from $T{b b}").use("b", Boy.class).aliasColumnsAs(TABLE_NAME);
    assertEquals("b.GIRL_ID as BOY$GIRL_ID, b.ID as BOY$ID, b.NAME as BOY$NAME from BOY b", st.generateQuery());
    st = sql("$C{a.*} from $T{b a}").use("b", BadBoy.class);
    assertEquals("a.ID, a.GIRL_ID, a.NAME from BOY a", st.generateQuery());
    st = sql("$C{a.*} from $T{BadBoy a}");
    assertEquals("a.ID, a.GIRL_ID, a.NAME from BOY a", st.generateQuery());
    st = sql("$C{BadBoy.ajdi} from $T{BadBoy BadBoy}");
    assertEquals("BadBoy.ID from BOY BadBoy", st.generateQuery());
    st = sql("$C{BadBoy.ajdi} from $T{BadBoy}");
    assertEquals("BOY.ID from BOY", 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 19 with DbSqlBuilder

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

the class DbSqlTemplateTest method testColumns2.

@Test
public void testColumns2() {
    DbSqlBuilder st;
    // 1
    st = sql("$T{Boy} | $C{Boy.id} | $C{Boy.*}");
    assertEquals("BOY | BOY.ID as BOY$ID | BOY.GIRL_ID as BOY$GIRL_ID, BOY.ID as BOY$ID, BOY.NAME as BOY$NAME", st.aliasColumnsAs(TABLE_NAME).generateQuery());
    assertEquals("BOY | BOY.ID as Boy$ID | BOY.GIRL_ID as Boy$GIRL_ID, BOY.ID as Boy$ID, BOY.NAME as Boy$NAME", st.reset().aliasColumnsAs(TABLE_REFERENCE).generateQuery());
    assertEquals("BOY | BOY.ID as col_0_ | BOY.GIRL_ID as col_1_, BOY.ID as col_2_, BOY.NAME as col_3_", st.reset().aliasColumnsAs(COLUMN_CODE).generateQuery());
    st = sql("$T{BadBoy BadBoy} | $C{BadBoy.ajdi} | $C{BadBoy.*}");
    assertEquals("BOY BadBoy | BadBoy.ID as BOY$ID | BadBoy.ID as BOY$ID, BadBoy.GIRL_ID as BOY$GIRL_ID, BadBoy.NAME as BOY$NAME", st.aliasColumnsAs(TABLE_NAME).generateQuery());
    assertEquals("BOY BadBoy | BadBoy.ID as BadBoy$ID | BadBoy.ID as BadBoy$ID, BadBoy.GIRL_ID as BadBoy$GIRL_ID, BadBoy.NAME as BadBoy$NAME", st.reset().aliasColumnsAs(TABLE_REFERENCE).generateQuery());
    assertEquals("BOY BadBoy | BadBoy.ID as col_0_ | BadBoy.ID as col_1_, BadBoy.GIRL_ID as col_2_, BadBoy.NAME as col_3_", st.reset().aliasColumnsAs(COLUMN_CODE).generateQuery());
}
Also used : DbSqlBuilder(jodd.db.oom.sqlgen.DbSqlBuilder) Test(org.junit.Test)

Example 20 with DbSqlBuilder

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

the class DbSqlTemplateTest method testReferencesAndEscapes.

@Test
public void testReferencesAndEscapes() {
    DbSqlBuilder st;
    assertEquals("...$foo...", new DbSqlBuilder("...\\$foo...").generateQuery());
    assertEquals("$foo", new DbSqlBuilder("\\$foo").generateQuery());
    assertEquals("...\\$foo...", new DbSqlBuilder("...\\\\\\$foo...").generateQuery());
    assertEquals("\\$foo", new DbSqlBuilder("\\\\\\$foo").generateQuery());
    assertEquals("$f", new DbSqlBuilder("\\$f").generateQuery());
    st = sql("$T{BadBoy b} x$b.ajdi=2").aliasColumnsAs(TABLE_NAME);
    assertEquals("BOY b xb.ID=2", st.generateQuery());
    st = sql("$T{BadBoy b} $b.ajdi+2").aliasColumnsAs(TABLE_NAME);
    assertEquals("BOY b b.ID+2", st.generateQuery());
    st = sql("$T{Boy, BadBoy b} - \\$$Boy.id $b.ajdi");
    assertEquals("BOY, BOY b - $BOY.ID b.ID", st.generateQuery());
    st = sql("$T{Boy}, $T{BadBoy b} - \\$$Boy.id $b.ajdi");
    assertEquals("BOY, BOY b - $BOY.ID b.ID", st.generateQuery());
    st = sql("$C{b.ajdi} $T{Boy Boy, BadBoy b} - \\$$Boy.id $b.ajdi").aliasColumnsAs(TABLE_NAME);
    assertEquals("b.ID as BOY$ID BOY Boy, BOY b - $Boy.ID b.ID", st.generateQuery());
    st = sql("\\$C{b.ajdi} $T{Boy, BadBoy b} - \\$$Boy.id $b.ajdi");
    assertEquals("$C{b.ajdi} BOY, BOY b - $BOY.ID b.ID", st.generateQuery());
    st = sql("\\$C{b.*} $T{Boy, BadBoy b} - $Boy.id");
    assertEquals("$C{b.*} BOY, BOY b - BOY.ID", st.generateQuery());
    st = sql("$C{b.*} $T{Boy Boy, BadBoy  b} - $b.ajdi 'foo\\$'").aliasColumnsAs(TABLE_NAME);
    assertEquals("b.ID as BOY$ID, b.GIRL_ID as BOY$GIRL_ID, b.NAME as BOY$NAME BOY Boy, BOY b - b.ID 'foo$'", st.generateQuery());
    st = sql("$T{BadBoy  b} - $b.ajdi=2,$b.ajdi<$b.ajdi").aliasColumnsAs(TABLE_NAME);
    assertEquals("BOY b - b.ID=2,b.ID<b.ID", st.generateQuery());
}
Also used : DbSqlBuilder(jodd.db.oom.sqlgen.DbSqlBuilder) 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