1 |
|
package ca.spaz.sql; |
2 |
|
|
3 |
|
import java.sql.*; |
4 |
|
import java.util.*; |
5 |
|
|
6 |
|
import org.apache.log4j.Logger; |
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
public class SQLUpdate extends SQLSelectableStatement implements Columns { |
12 |
|
|
13 |
|
|
14 |
|
|
15 |
0 |
private static final Logger logger = Logger.getLogger(SQLUpdate.class); |
16 |
|
|
17 |
|
private SQLColumnSet cols; |
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
public SQLUpdate(String tableName) { |
24 |
0 |
super(tableName, true, false, class="keyword">true); |
25 |
0 |
cols = new SQLColumnSet(); |
26 |
0 |
} |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
protected void doExecute(Connection con) throws SQLException { |
32 |
0 |
Statement stmt = con.createStatement(); |
33 |
0 |
String query = this.getQueryString(); |
34 |
0 |
if (logger.isDebugEnabled()) { |
35 |
0 |
logger.debug("executeQuery() - Statement to be executed: " + query); |
36 |
|
} |
37 |
|
|
38 |
0 |
stmt.executeUpdate(query); |
39 |
0 |
} |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
protected String getQueryString() { |
45 |
0 |
StringBuffer sb = new StringBuffer(); |
46 |
0 |
sb.append("UPDATE "); |
47 |
0 |
sb.append(getTableName()); |
48 |
0 |
sb.append(" SET "); |
49 |
0 |
List names = cols.getNames(); |
50 |
0 |
List terms = cols.getValues(); |
51 |
0 |
for (int i=0; i<names.size(); i++) { |
52 |
0 |
Object name = names.get(i); |
53 |
0 |
Object term = terms.get(i); |
54 |
0 |
if (term == null) { |
55 |
0 |
term = "NULL"; |
56 |
|
} |
57 |
0 |
sb.append(name.toString()); |
58 |
0 |
sb.append(" = '"); |
59 |
0 |
sb.append(escape(term.toString())); |
60 |
0 |
sb.append("' "); |
61 |
0 |
if (i < names.size() - 1) { |
62 |
0 |
sb.append(", "); |
63 |
|
} |
64 |
|
} |
65 |
0 |
sb.append(getWhere()); |
66 |
0 |
return sb.toString(); |
67 |
|
} |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
public SQLColumnSet getColumns() { |
74 |
0 |
return cols; |
75 |
|
} |
76 |
|
|
77 |
|
} |