01: package net.sourceforge.jaxor;
02:
03: import java.io.Serializable;
04: import java.util.Iterator;
05: import java.util.List;
06:
07: public class MetaParser implements Serializable {
08:
09: private final String _insert;
10: private final String _update;
11: private final String _delete;
12:
13: public MetaParser(MetaRow row) {
14: _insert = insertSQL(row);
15: _update = updateSQL(row);
16: _delete = deleteSQL(row);
17: }
18:
19: public String getInsert() {
20: return _insert;
21: }
22:
23: public String getUpdate() {
24: return _update;
25: }
26:
27: public String getDelete() {
28: return _delete;
29: }
30:
31: private static String insertSQL(MetaRow _meta) {
32: StringBuffer sql = new StringBuffer("INSERT INTO "
33: + _meta.getTableName() + "(");
34: List insert = _meta.getInsertColumns();
35: Iterator itr = insert.iterator();
36: while (itr.hasNext()) {
37: MetaField attr = (MetaField) itr.next();
38: sql.append(attr.getColumn());
39: if (itr.hasNext())
40: sql.append(", ");
41: }
42:
43: sql.append(") VALUES(");
44: itr = insert.iterator();
45: while (itr.hasNext()) {
46: itr.next();
47: sql.append("?");
48: if (itr.hasNext())
49: sql.append(", ");
50: }
51: sql.append(")");
52: return sql.toString();
53: }
54:
55: private static String updateSQL(MetaRow _meta) {
56: StringBuffer sql = new StringBuffer("UPDATE "
57: + _meta.getTableName() + " SET ");
58: List nonKeys = _meta.getUpdateColumns();
59: Iterator itr = nonKeys.iterator();
60: String attrValues = "";
61: while (itr.hasNext()) {
62: MetaField attr = (MetaField) itr.next();
63: if (attrValues.length() > 0)
64: attrValues += ",";
65: attrValues += (attr.getColumn() + " = ?");
66: }
67:
68: sql.append(attrValues);
69: sql.append(" WHERE ");
70: List matchList = _meta.getUpdateMatchColumns();
71: for (Iterator iterator = matchList.iterator(); iterator
72: .hasNext();) {
73: MetaField attribute = (MetaField) iterator.next();
74: String attrName = attribute.getColumn();
75: sql.append(attrName);
76: sql.append(" = ?");
77: if (iterator.hasNext())
78: sql.append(" AND ");
79: }
80: return sql.toString();
81: }
82:
83: private static String deleteSQL(MetaRow _meta) {
84: StringBuffer sql = new StringBuffer("DELETE FROM "
85: + _meta.getTableName() + " WHERE ");
86: List pk = _meta.getPrimaryKey();
87: for (Iterator iterator = pk.iterator(); iterator.hasNext();) {
88: MetaField attribute = (MetaField) iterator.next();
89: sql.append(attribute.getColumn());
90: sql.append(" = ?");
91: if (iterator.hasNext())
92: sql.append(" AND ");
93: }
94: return sql.toString();
95: }
96: }
|