01: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
02:
03: package jodd.db.orm.sqlgen;
04:
05: import java.util.Map;
06:
07: /**
08: * Generates simple update queries.
09: */
10: public class DbSqlUpdate implements DbSqlGenerator {
11:
12: protected DbDynamicSqlTemplate template;
13:
14: /**
15: * Defines update query for specified item.
16: * @param object
17: * @param onlyExisting if <code>true</code> null properties will be ignored.
18: * @param conditions object that will produce where part
19: */
20: public DbSqlUpdate(Object object, boolean onlyExisting,
21: Object conditions) {
22: //template = new DbDynamicSqlTemplate("update $T{t -} set $U{t} $W{where .c}"); // without table alias
23: if (onlyExisting == true) {
24: template = new DbDynamicSqlTemplate(
25: "update $T{t} set $U{t} $W{where !t.c}");
26: } else {
27: template = new DbDynamicSqlTemplate(
28: "update $T{t} set $U{+t} $W{where !t.c}");
29: }
30: template.use("t", object).use("c", conditions);
31: }
32:
33: /**
34: * Defines update query for specified item.
35: * @param object
36: * @param onlyExisting if <code>true</code> null properties will be ignored.
37: * @param conditions string that will be added to the query for where part
38: */
39: public DbSqlUpdate(Object object, boolean onlyExisting,
40: String conditions) {
41: if (conditions == null) {
42: conditions = "";
43: }
44: if (onlyExisting == true) {
45: template = new DbDynamicSqlTemplate(
46: "update $T{t -} set $U{t} " + conditions);
47: } else {
48: template = new DbDynamicSqlTemplate(
49: "update $T{t -} set $U{+t} " + conditions);
50: }
51: template.use("t", object);
52: }
53:
54: // ---------------------------------------------------------------- interface
55:
56: public String generateQuery() {
57: return template.generateQuery();
58: }
59:
60: public Map<String, Object> getQueryParameters() {
61: return template.getQueryParameters();
62: }
63:
64: public Map<String, String[]> getColumnData() {
65: return template.getColumnData();
66: }
67:
68: public Map<String, Object> getQueryReferences() {
69: return template.getQueryReferences();
70: }
71: }
|