1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sf.webmancer.db;
23
24 import java.util.HashMap;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Vector;
29
30 import net.sf.webmancer.util.ContractChecker;
31 import net.sf.webmancer.util.immutable.ImmutableList;
32 import net.sf.webmancer.util.immutable.ImmutableMap;
33
34
35
36
37
38 public class Query implements IQuery {
39
40
41
42 private Map<String, IAttribute> attributes;
43
44
45
46
47 private List<IRestrictor> restrictors;
48
49
50
51
52 private List<ICondition> conditions;
53
54
55
56
57 private long limit;
58
59
60
61
62 private long offset;
63
64
65
66
67 private IAttribute orderingAttribute;
68
69
70
71
72 private boolean orderingIsAscending;
73
74
75
76
77 private IDatabaseOperationPerformer performer;
78
79
80
81
82
83 public Query(IDatabaseOperationPerformer performer) {
84 super();
85 ContractChecker.mustNotBeNull(performer, "performer");
86 this.performer = performer;
87 this.attributes = new HashMap<String, IAttribute>();
88 this.restrictors = new Vector<IRestrictor>();
89 this.conditions = new LinkedList<ICondition>();
90 this.limit = 0;
91 this.orderingAttribute = null;
92 this.orderingIsAscending = true;
93 }
94
95
96
97
98 public void addAttribute(IAttribute attribute) {
99 ContractChecker.mustNotBeNull(attribute, "attribute");
100 this.attributes.put(attribute.getId(), attribute);
101 }
102
103
104
105
106 public long queryCount() {
107 return performer.performQueryCount(this.getConditions());
108 }
109
110
111
112
113 public IData queryResult() {
114 return performer.performQueryResult(this.getAttributes(), this.getConditions(), this.getLimit(), this.getOffset(), this.getOrderingAttribute(), this.isOrderingAscending());
115 }
116
117
118
119
120 public long delete() {
121 return performer.performDelete(this.getConditions());
122 }
123
124
125
126
127 public long update(Map<IAttribute, ICell> row) {
128 return performer.performUpdate(row, this.getConditions());
129 }
130
131
132
133
134 public void insert(Map<IAttribute, ICell> row) {
135 performer.performInsert(row);
136 }
137
138 public void insert(IData data) {
139 performer.performInsert(data);
140 }
141
142
143
144
145 public void registerRestrictor(IRestrictor restrictor) {
146 ContractChecker.mustNotBeNull(restrictor, "restrictor");
147 this.restrictors.add(restrictor);
148 }
149
150
151
152
153 public void restrictByCondition(ICondition condition) {
154 ContractChecker.mustNotBeNull(condition, "condition");
155 this.conditions.add(condition);
156 }
157
158
159
160
161 public void restrictLimit(long limit) {
162 ContractChecker.mustNotBeNegative(limit, "limit");
163 this.limit = limit;
164 }
165
166
167
168
169 public void restrictOffset(long offset) {
170 ContractChecker.mustNotBeNegative(offset, "offset");
171 this.offset = offset;
172 }
173
174
175
176
177 public void restrictOrdering(IAttribute attribute, boolean ascending) {
178 ContractChecker.mustNotBeNull(attribute, "attribute");
179 this.orderingAttribute = attribute;
180 this.orderingIsAscending = ascending;
181 }
182
183
184
185
186 protected void callRestrictors(RestrictionType type) {
187 for (IRestrictor restrictor : this.restrictors) {
188 restrictor.restrict(this, type);
189 }
190 }
191
192
193
194
195
196
197 protected Map<String, IAttribute> getAttributes() {
198 return new ImmutableMap<String, IAttribute>(this.attributes);
199 }
200
201
202
203
204
205
206 public void setAttributes(Map<String, IAttribute> attributes) {
207 ContractChecker.mustNotBeNull(attributes, "attributes");
208 this.attributes = attributes;
209 }
210
211
212
213
214
215
216 protected List<ICondition> getConditions() {
217 return new ImmutableList<ICondition>(this.conditions);
218 }
219
220
221
222
223
224
225 public void setConditions(List<ICondition> conditions) {
226 this.conditions = conditions;
227 }
228
229
230
231
232
233
234 protected long getLimit() {
235 return this.limit;
236 }
237
238
239
240
241
242
243 public void setLimit(long limit) {
244 ContractChecker.mustNotBeNegative(limit, "limit");
245 this.limit = limit;
246 }
247
248
249
250
251
252
253 protected long getOffset() {
254 return this.offset;
255 }
256
257
258
259
260
261
262 public void setOffset(long offset) {
263 ContractChecker.mustNotBeNegative(offset, "offset");
264 this.offset = offset;
265 }
266
267
268
269
270
271
272 protected IAttribute getOrderingAttribute() {
273 return this.orderingAttribute;
274 }
275
276
277
278
279
280
281 public void setOrderingAttribute(IAttribute attribute) {
282 ContractChecker.mustNotBeNull(attribute, "attribute");
283 this.orderingAttribute = attribute;
284 }
285
286
287
288
289
290
291 protected boolean isOrderingAscending() {
292 return this.orderingIsAscending;
293 }
294
295
296
297
298
299
300 public void setOrderingAscending(boolean ascending) {
301 this.orderingIsAscending = ascending;
302 }
303
304 }