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());
}
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);
}
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());
}
}
}
Aggregations