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;
010:
011: import com.completex.objective.components.persistency.Query;
012: import com.completex.objective.components.persistency.QueryDefinition;
013: import com.completex.objective.components.persistency.Mappable;
014: import com.completex.objective.components.persistency.core.impl.query.QueryDefinitionImpl;
015: import com.completex.objective.util.PropertyMap;
016:
017: import java.util.List;
018: import java.util.Map;
019: import java.util.HashMap;
020: import java.io.Serializable;
021:
022: /**
023: * Represents SQL union query. Generally it is not thread safe.
024: *
025: * @author Gennady Krizhevsky
026: */
027: public interface Union {
028:
029: public static final UnionMode UNION = UnionMode.UNION;
030: public static final UnionMode UNION_ALL = UnionMode.UNION_ALL;
031:
032: /**
033: * Adds another query to the union with "UNION" clause
034: *
035: * @param query
036: * @return self
037: */
038: Union union(QueryDefinition query);
039:
040: /**
041: * Adds another query to the union with "UNION ALL" clause
042: *
043: * @param query
044: * @return self
045: */
046: Union unionAll(QueryDefinition query);
047:
048: /**
049: * Adds another query to the union with UnionMode dependent clause
050: *
051: * @param query
052: * @return self
053: */
054: Union union(QueryDefinition query, UnionMode union);
055:
056: /**
057: * Sets order by clause that has union scope rather than query one
058: *
059: * @param orderBy
060: * @return self
061: */
062: Union setOrderBy(String[] orderBy);
063:
064: /**
065: *
066: * @return true if it contains any sub-queries (same as unionSize() > 0)
067: */
068: boolean hasEntries();
069:
070: /**
071: *
072: * @return number of union sub-queries
073: */
074: int unionSize();
075:
076: /**
077: *
078: * @return union's Order By columns
079: */
080: String[] getOrderBy();
081:
082: /**
083: * Adds column expression to the order by clause. All the expressions will be used when
084: * compiling separated by commas
085: *
086: * @param columnExpression
087: * @param direction - ascending or descending
088: * @return self
089: */
090: Union addToOrderBy(String columnExpression,
091: Query.OrderDirection direction);
092:
093: /**
094: * Adds column expression to the order by clause with default sort direction.
095: *
096: * @param columnExpression
097: * @return self
098: */
099: Union addToOrderBy(String columnExpression);
100:
101: /**
102: * Sets array of column expressions to the order by clause. All the expressions will be used when
103: * compiling separated by commas
104: *
105: * @param orderBy
106: * @return self
107: */
108: Union setOrderBy(List orderBy);
109:
110: /**
111: *
112: * @return true if Union is compiled
113: */
114: boolean isCompiled();
115:
116: /**
117: * Decompiles union allowing for the modifications
118: *
119: * @return self
120: */
121: Union decompile();
122:
123: /**
124: * Decompiles union. After that it becomes immutable until decompile() is used.
125: *
126: * @param databasePolicy
127: * @return self
128: */
129: Union compile(DatabasePolicy databasePolicy);
130:
131: /**
132: *
133: * @param index
134: * @return index-th union part
135: * @throws IndexOutOfBoundsException if the index is out of range (index
136: * < 0 || index >= size()).
137: */
138: UnionEntry getUnionEntry(int index);
139:
140: /**
141: *
142: * @return true if there is order by clause
143: */
144: boolean hasOrderBy();
145:
146: /**
147: * Utility class representing Union sub-query
148: */
149: static class UnionEntry implements Serializable, Mappable {
150: private QueryDefinition queryDefinition;
151: private UnionMode unionMode;
152: private static final String TAG_QUERY_DEFINITION = "queryDefinition";
153: private static final String TAG_UNION_MODE = "unionMode";
154:
155: public UnionEntry(QueryDefinition queryDefinition,
156: UnionMode unionMode) {
157: this .queryDefinition = queryDefinition;
158: this .unionMode = unionMode;
159: }
160:
161: public QueryDefinition getQueryDefinition() {
162: return queryDefinition;
163: }
164:
165: public UnionMode getUnionMode() {
166: return unionMode;
167: }
168:
169: public Map toMap() {
170: HashMap map = new HashMap();
171: map.put(TAG_QUERY_DEFINITION, ((Mappable) queryDefinition)
172: .toMap());
173: map.put(TAG_UNION_MODE, unionMode.getName());
174: return map;
175: }
176:
177: public void fromMap(Map map) {
178: PropertyMap propertyMap = PropertyMap.toPropertyMap(map);
179: Map queryDefinitionMap = propertyMap.getPropertyMap(
180: TAG_QUERY_DEFINITION, true);
181: queryDefinition = new QueryDefinitionImpl(
182: queryDefinitionMap);
183: String unionModeName = propertyMap.getProperty(
184: TAG_UNION_MODE, true);
185: unionMode = UnionMode.name2mode(unionModeName);
186: }
187: }
188:
189: }
|