1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
package ca.spaz.sql; |
5 |
|
|
6 |
|
import java.sql.*; |
7 |
|
import java.util.*; |
8 |
|
import java.util.ArrayList; |
9 |
|
|
10 |
|
import org.apache.log4j.Logger; |
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
7 |
public class SQLSelect extends SQLSelectableStatement { |
21 |
|
|
22 |
|
|
23 |
|
|
24 |
13 |
private static final Logger logger = Logger.getLogger(SQLSelect.class); |
25 |
|
|
26 |
11 |
protected List items = new ArrayList(); |
27 |
|
|
28 |
11 |
protected ResultSet results = null; |
29 |
|
|
30 |
11 |
private SQLSelect subSelection = null; |
31 |
|
|
32 |
11 |
private String[] joinedTables = null; |
33 |
|
|
34 |
11 |
private List orders = new ArrayList(); |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
public SQLSelect(String tableName) { |
43 |
11 |
super(tableName, true, class="keyword">true, false); |
44 |
11 |
} |
45 |
|
|
46 |
|
public SQLSelect(SQLSelect subSelection) { |
47 |
0 |
super("", true, class="keyword">true, false); |
48 |
0 |
this.subSelection = subSelection; |
49 |
0 |
} |
50 |
|
|
51 |
|
public SQLSelect(String[] joinedTables) { |
52 |
0 |
super("", true, class="keyword">true, false); |
53 |
0 |
this.joinedTables = joinedTables; |
54 |
0 |
} |
55 |
|
|
56 |
|
public void addOrderBy(String orderClause) { |
57 |
0 |
orders.add(orderClause); |
58 |
0 |
} |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
public void addSelection(String field) { |
67 |
0 |
items.add(field); |
68 |
0 |
} |
69 |
|
|
70 |
|
public void addSelection(String field, String as) { |
71 |
0 |
items.add(field + " as " + as); |
72 |
0 |
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
protected ResultSet doExecuteQuery(Connection con) throws SQLException { |
78 |
10 |
Statement stmt = con.createStatement(); |
79 |
10 |
String query = this.getQueryString(); |
80 |
10 |
if (logger.isDebugEnabled()) { |
81 |
0 |
logger.debug("executeQuery() - Statement to be executed: " + query); |
82 |
|
} |
83 |
|
|
84 |
10 |
results = stmt.executeQuery(query); |
85 |
10 |
return results; |
86 |
|
} |
87 |
|
|
88 |
|
private String getOrder() { |
89 |
10 |
StringBuffer sb = new StringBuffer(); |
90 |
10 |
if (orders.size() > 0) { |
91 |
0 |
sb.append(" ORDER BY "); |
92 |
0 |
for (int i = 0; i < orders.size(); i++) { |
93 |
0 |
Object w = orders.get(i); |
94 |
0 |
sb.append(w.toString()); |
95 |
0 |
if (i < orders.size() - 1) { |
96 |
0 |
sb.append(","); |
97 |
|
} |
98 |
|
} |
99 |
|
} |
100 |
10 |
return sb.toString(); |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
protected String getQueryString() { |
107 |
10 |
StringBuffer sb = new StringBuffer(); |
108 |
10 |
sb.append("SELECT "); |
109 |
10 |
if (items.size() == 0) { |
110 |
10 |
sb.append(" * "); |
111 |
3 |
} else { |
112 |
0 |
for (int i = 0; i < items.size(); i++) { |
113 |
0 |
String term = (String) items.get(i); |
114 |
0 |
if (term != null) { |
115 |
0 |
sb.append(term); |
116 |
0 |
if (i < items.size() - 1) { |
117 |
0 |
sb.append(", "); |
118 |
|
} |
119 |
|
} |
120 |
|
} |
121 |
|
} |
122 |
10 |
sb.append(" FROM "); |
123 |
10 |
if (null != joinedTables) { |
124 |
0 |
for (int i = 0; i < joinedTables.length; i++) { |
125 |
0 |
sb.append(joinedTables[i]); |
126 |
0 |
if (i < joinedTables.length - 1) { |
127 |
0 |
sb.append(", "); |
128 |
|
} |
129 |
|
} |
130 |
7 |
} else if (null != subSelection) { |
131 |
0 |
sb.append("("); |
132 |
0 |
sb.append(subSelection.toString()); |
133 |
0 |
sb.append(")"); |
134 |
0 |
} else { |
135 |
10 |
sb.append(getTableName()); |
136 |
|
} |
137 |
10 |
sb.append(getWhere()); |
138 |
10 |
sb.append(getOrder()); |
139 |
10 |
return sb.toString(); |
140 |
|
} |
141 |
|
|
142 |
|
} |