001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency.core.impl.query;
010:
011: import com.completex.objective.components.persistency.*;
012: import com.completex.objective.components.persistency.core.DatabasePolicy;
013: import com.completex.objective.components.persistency.core.impl.AbstractResultSetWrapper;
014: import com.completex.objective.components.persistency.core.impl.LinkIterator;
015: import com.completex.objective.components.persistency.core.impl.PreparedStatementWrapper;
016: import com.completex.objective.components.persistency.type.CollectionFactory;
017: import com.completex.objective.util.PropertyMap;
018:
019: import java.sql.SQLException;
020: import java.util.*;
021:
022: /**
023: * @author Gennady Krizhevsky
024: */
025: public abstract class BaseQueryImpl implements Parent, BasicQuery,
026: Mappable {
027:
028: protected QueryDefinitionImpl queryDefinition = new QueryDefinitionImpl();
029: //
030: // Dynamic:
031: //
032: protected transient QueryFactoryCtl parentQueryFactory;
033: private transient int retrievedCount;
034: protected transient PreparedStatementWrapper statementWrapper;
035: protected transient AbstractResultSetWrapper resultSetWrapper;
036: protected transient int state = BasicQuery.STATE_NEW;
037: private transient boolean count;
038: private transient boolean bypassCache;
039: private transient String key;
040: protected String name;
041: protected transient AbstractParameters parameters;
042: protected transient Persistency persistency;
043: private transient Link link;
044:
045: private long offset;
046: private long pageSize;
047: public static final String TAG_QUERY_DEFINITION = "queryDefinition";
048: public static final String TAG_NAME = "name";
049: public static final String TAG_OFFSET = "offset";
050: public static final String TAG_PAGE_SIZE = "pageSize";
051: public static final String TAG_PARAMETERS = "parameters";
052: public static final String TAG_LINK = "link";
053:
054: protected BaseQueryImpl() {
055: createLinkIfNotExist();
056: }
057:
058: public Link toLink() {
059: return link;
060: }
061:
062: private Link createLinkIfNotExist() {
063: if (link == null) {
064: link = new Link(null, null, null);
065: }
066: return link;
067: }
068:
069: //
070: // Parent
071: //
072: public LinkIterator linkIterator() {
073: return toLink().linkIterator();
074: }
075:
076: public boolean hasChildren() {
077: return toLink().hasChildren();
078: }
079:
080: public Object getChildObject(String name) {
081: return toLink().getChildObject(name);
082: }
083:
084: public void setChildObject(String name, Object value) {
085: toLink().setChildObject(name, value);
086: }
087:
088: public Link[] inlineLinks() {
089: return toLink().inlineLinks();
090: }
091:
092: public void addChild(Link link) {
093: toLink().addChild(link);
094: }
095:
096: public Link child(String name) {
097: return toLink().child(name);
098: }
099:
100: public Link getChild(String name) {
101: return toLink().getChild(name);
102: }
103:
104: /**
105: *
106: * @param children
107: */
108: public void setChildren(LinkedHashMap children) {
109: toLink().setChildren(children);
110: }
111:
112: public LinkedHashMap getChildren() {
113: return toLink().getChildren();
114: }
115:
116: protected LinkedHashMap newChildren(Link parent) {
117: return toLink().newChildren(parent);
118: }
119:
120: //
121: // Parent end
122: //
123:
124: public void setParentQueryFactory(QueryFactoryCtl parentQueryFactory) {
125: this .parentQueryFactory = parentQueryFactory;
126: if (!isCompiled()) {
127: setPersistency(parentQueryFactory.getPersistency());
128: // queryDefinition.setQueryBuilder(parentQueryFactory.getQueryBuilder());
129: }
130: }
131:
132: public QueryFactoryCtl getParentQueryFactory() {
133: return parentQueryFactory;
134: }
135:
136: public boolean isCompiled() {
137: return queryDefinition.isCompiled();
138: }
139:
140: public boolean isExternalSetSql() {
141: return queryDefinition.isExternallySetSql();
142: }
143:
144: public void setSingularResultFactory(
145: PersistentObjectFactory singularResultFactory) {
146: queryDefinition.setSingularResultFactory(singularResultFactory);
147: }
148:
149: public String getSql() {
150: return queryDefinition.getSql();
151: }
152:
153: public BasicQuery setSql(String sql) {
154: queryDefinition.setSql(sql);
155: return this ;
156: }
157:
158: public BasicQuery setSqlInternal(String sql) {
159: queryDefinition.setSqlInternal(sql);
160: return this ;
161: }
162:
163: public int getRetrievedCount() {
164: return retrievedCount;
165: }
166:
167: public void setRetrievedCount(int retrievedCount) {
168: this .retrievedCount = retrievedCount;
169: }
170:
171: public boolean isResultCompound() {
172: return getSingularResultFactory() != null
173: && getSingularResultFactory().compound();
174: }
175:
176: public void nullifySingularResultFactory() {
177: queryDefinition.nullifySingularResultFactory();
178: }
179:
180: public PersistentObjectFactory getSingularResultFactory() {
181: return queryDefinition.getSingularResultFactory();
182: }
183:
184: public PreparedStatementWrapper getStatementWrapper() {
185: return statementWrapper;
186: }
187:
188: public void setStatementWrapper(
189: PreparedStatementWrapper statementWrapper) {
190: this .statementWrapper = statementWrapper;
191: }
192:
193: public AbstractResultSetWrapper getResultSetWrapper() {
194: return resultSetWrapper;
195: }
196:
197: public void setResultSetWrapper(
198: AbstractResultSetWrapper resultSetWrapper) {
199: this .resultSetWrapper = resultSetWrapper;
200: }
201:
202: public int getState() {
203: return state;
204: }
205:
206: public void setState(int state) {
207: this .state = state;
208: }
209:
210: public boolean isDisconnectedPageQuery() {
211: return queryDefinition.isDisconnectedPageQuery();
212: }
213:
214: public void setDisconnectedPageQuery(boolean disconnectedPageQuery) {
215: queryDefinition.setDisconnectedPageQuery(disconnectedPageQuery);
216: }
217:
218: public boolean hasMore() {
219: if (getPageSize() <= 0) {
220: throw new IllegalArgumentException(
221: "hasMore method is not supported for non-paginating queries");
222: }
223: return state != BasicQuery.STATE_CLOSED;
224: }
225:
226: public boolean isSelectConnectedForwardPageQuery() {
227: return getPageSize() > 0
228: && !queryDefinition.isDisconnectedPageQuery() && !count;
229: }
230:
231: public boolean isSelectDisconnectedPageQuery() {
232: return getPageSize() > 0
233: && queryDefinition.isDisconnectedPageQuery() && !count;
234: }
235:
236: public Collection newMultipleResult() {
237: try {
238: return getMultipleResultFactory().newCollection();
239: } catch (Exception e) {
240: new RuntimeException(
241: "Cannot get newQuery of MultipleResult", e);
242: }
243: return null;
244: }
245:
246: public CollectionFactory getMultipleResultFactory() {
247: return queryDefinition.getMultipleResultFactory();
248: }
249:
250: public void setMultipleResultFactory(
251: CollectionFactory multipleResultFactory) {
252: queryDefinition.setMultipleResultFactory(multipleResultFactory);
253: }
254:
255: public SelectQueryBuilder getQueryBuilder() {
256: return queryDefinition.getQueryBuilder();
257: }
258:
259: // public void setQueryBuilder(SelectQueryBuilder queryBuilder) {
260: // queryDefinition.setQueryBuilder(queryBuilder);
261: // }
262:
263: public Persistency getPersistency() {
264: return persistency;
265: }
266:
267: public void setPersistency(Persistency persistency) {
268: if (persistency != null) {
269: this .persistency = persistency;
270: queryDefinition.setDatabasePolicy(persistency
271: .getDatabasePolicy());
272: } else {
273: throw new IllegalArgumentException("persistency == null");
274: }
275: }
276:
277: public void setExcludedFields(Set excludedFields) {
278: queryDefinition.setExcludedFields(excludedFields);
279: }
280:
281: public CompoundColumnFilter columnFilters() {
282: return queryDefinition.columnFilters();
283: }
284:
285: public CompoundColumnFilter getCompoundColumnFilter() {
286: return queryDefinition.getCompoundColumnFilter();
287: }
288:
289: public void setCompoundColumnFilter(
290: CompoundColumnFilter columnFilter) {
291: queryDefinition.setCompoundColumnFilter(columnFilter);
292: }
293:
294: public void setCount(boolean count) {
295: this .count = count;
296: }
297:
298: public boolean isCount() {
299: return count;
300: }
301:
302: public boolean isBypassCache() {
303: return bypassCache;
304: }
305:
306: public void setBypassCache(boolean bypassCache) {
307: this .bypassCache = bypassCache;
308: }
309:
310: public boolean isUseColumnNames() {
311: return queryDefinition.isUseColumnNames();
312: }
313:
314: public void setUseColumnNames(boolean useColumnNames) {
315: queryDefinition.setUseColumnNames(useColumnNames);
316: }
317:
318: public String getName() {
319: return name;
320: }
321:
322: public void setName(String name) {
323: if (this .name == null) {
324: this .name = name;
325: } else {
326: throw new IllegalArgumentException(
327: "Cannot change immutable field [name] from ["
328: + this .name + "] to [" + name + "]");
329: }
330: }
331:
332: public Object toKey() {
333: if (key == null) {
334: StringBuffer buffer = new StringBuffer(queryDefinition
335: .getSql());
336: buffer
337: .append("|")
338: .append(
339: getSingularResultFactory() == null ? PersistentObject.class
340: .getName()
341: : getSingularResultFactory()
342: .getClass().getName());
343: key = buffer.toString();
344: }
345: return key;
346: }
347:
348: public boolean isClosed() {
349: return state == BasicQuery.STATE_CLOSED;
350: }
351:
352: public QueryDefinition getQueryDefinition() {
353: return queryDefinition;
354: }
355:
356: protected Parameter getParameter0(int index) {
357: return parameters.get(index);
358: }
359:
360: protected BasicQuery setParameters0(Object[] parameters) {
361: this .parameters = toParameters(parameters);
362: return this ;
363: }
364:
365: protected abstract AbstractParameters toParameters(
366: Object[] parameters);
367:
368: protected BasicQuery setParameters0(AbstractParameters parameters) {
369: this .parameters = parameters;
370: return this ;
371: }
372:
373: public int parametersSize() {
374: return parameters == null ? 0 : parameters.size();
375: }
376:
377: protected void reset0() throws OdalPersistencyException {
378: try {
379: if (resultSetWrapper != null) {
380: resultSetWrapper.close();
381: }
382: if (statementWrapper != null) {
383: statementWrapper.close();
384: }
385: } catch (SQLException e) {
386: throw new OdalPersistencyException(e);
387: }
388: }
389:
390: public DatabasePolicy getDatabasePolicy() {
391: return getPersistency() == null ? null : getPersistency()
392: .getDatabasePolicy();
393: }
394:
395: public void decompile() {
396: queryDefinition.decompile();
397: }
398:
399: protected void postClose() throws OdalPersistencyException {
400: }
401:
402: public void close() throws OdalPersistencyException {
403: if (state == STATE_ACTIVE) {
404: reset0();
405: state = STATE_CLOSED;
406: }
407: postClose();
408: }
409:
410: public long getPageSize() {
411: return pageSize;
412: }
413:
414: public BasicQuery setPageSize(long pageSize) {
415: this .pageSize = pageSize;
416: return this ;
417: }
418:
419: public BasicQuery setPage(Page page) {
420: setPageSize(page.getSize());
421: setOffset(page.getOffset());
422: return this ;
423: }
424:
425: public long getOffset() {
426: return offset;
427: }
428:
429: public void setOffset(long offset) {
430: this .offset = offset;
431: }
432:
433: public Map toMap() {
434: HashMap map = new HashMap();
435: map.put(TAG_QUERY_DEFINITION, queryDefinition.toMap());
436: map.put(TAG_NAME, name);
437: map.put(TAG_OFFSET, String.valueOf(offset));
438: map.put(TAG_PAGE_SIZE, String.valueOf(pageSize));
439: if (parameters != null) {
440: map.put(TAG_PARAMETERS, parameters.toList());
441: }
442:
443: if (hasChildren()) {
444: map.put(TAG_LINK, link.toMap());
445: }
446: return map;
447: }
448:
449: public void fromMap(Map map) {
450: PropertyMap propertyMap = PropertyMap.toPropertyMap(map, true);
451: Map queryDefinitionMap = propertyMap.getPropertyMap(
452: TAG_QUERY_DEFINITION, true);
453: queryDefinition = new QueryDefinitionImpl(queryDefinitionMap);
454: name = propertyMap.getProperty(TAG_NAME);
455: offset = propertyMap.getLong(TAG_OFFSET);
456: pageSize = propertyMap.getLong(TAG_PAGE_SIZE);
457:
458: List list = propertyMap.getList(TAG_PARAMETERS);
459: if (list != null) {
460: parameters = new Parameters();
461: parameters.fromList(list);
462: }
463:
464: Map linkMap = propertyMap.getMap(TAG_LINK);
465: if (linkMap != null) {
466: try {
467: link = new Link(linkMap);
468: } catch (RuntimeException e) {
469: throw e;
470: }
471: }
472: }
473:
474: }
|