Search in sources :

Example 1 with ParameterValue

use of jodd.db.oom.sqlgen.ParameterValue 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 ParameterValue

use of jodd.db.oom.sqlgen.ParameterValue 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);
}
Also used : ParameterValue(jodd.db.oom.sqlgen.ParameterValue) DbSqlBuilder(jodd.db.oom.sqlgen.DbSqlBuilder) BadGirl(jodd.db.oom.tst.BadGirl) Girl(jodd.db.oom.tst.Girl) Test(org.junit.Test)

Example 3 with ParameterValue

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

the class DbOomQuery method prepareQuery.

/**
	 * Prepares the query after initialization. Besides default work, it checks if sql generator
	 * is used, and if so, generator hints and query parameters will be used for this query.
	 * Note regarding hints: since hints can be added manually, generators hints will be ignored
	 * if there exists some manually set hints.
	 */
@Override
protected void prepareQuery() {
    super.prepareQuery();
    if (sqlgen == null) {
        return;
    }
    if (hints == null) {
        String[] joinHints = sqlgen.getJoinHints();
        if (joinHints != null) {
            withHints(joinHints);
        }
    }
    // insert parameters
    Map<String, ParameterValue> parameters = sqlgen.getQueryParameters();
    if (parameters == null) {
        return;
    }
    for (Map.Entry<String, ParameterValue> entry : parameters.entrySet()) {
        String paramName = entry.getKey();
        ParameterValue param = entry.getValue();
        DbEntityColumnDescriptor dec = param.getColumnDescriptor();
        if (dec == null) {
            setObject(paramName, param.getValue());
        } else {
            resolveColumnDbSqlType(connection, dec);
            setObject(paramName, param.getValue(), dec.getSqlTypeClass(), dec.getDbSqlType());
        }
    }
}
Also used : ParameterValue(jodd.db.oom.sqlgen.ParameterValue) Map(java.util.Map)

Aggregations

ParameterValue (jodd.db.oom.sqlgen.ParameterValue)3 DbSqlBuilder (jodd.db.oom.sqlgen.DbSqlBuilder)2 Test (org.junit.Test)2 Map (java.util.Map)1 BadBoy (jodd.db.oom.tst.BadBoy)1 BadGirl (jodd.db.oom.tst.BadGirl)1 Boy (jodd.db.oom.tst.Boy)1 Girl (jodd.db.oom.tst.Girl)1