001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.platforms.stores.base;
009:
010: //base classes
011: import java.math.BigDecimal;
012: import java.sql.PreparedStatement;
013: import java.sql.ResultSet;
014: import java.sql.SQLException;
015: import java.util.ArrayList;
016: import java.util.HashSet;
017: import java.util.Iterator;
018:
019: //project specific classes
020: import org.jfolder.common.utils.misc.MiscHelper;
021: import org.jfolder.platforms.stores.base.SystemStatement;
022:
023: //other classes
024:
025: public class WhereStatement extends SystemStatement {
026:
027: //
028: public final static Integer EQUALS = new Integer(0);
029: public final static Integer GREATER_THAN = new Integer(1);
030: public final static Integer GREATER_THAN_OR_EQUAL = new Integer(2);
031: public final static Integer NOT_EQUAL = new Integer(3);
032: public final static Integer LESS_THAN = new Integer(4);
033: public final static Integer LESS_THAN_OR_EQUAL = new Integer(5);
034: //
035: public final static Integer LIKE = new Integer(6);
036: public final static Integer NOT_LIKE = new Integer(7);
037: public final static Integer IS_NULL = new Integer(8);
038: public final static Integer IS_NOT_NULL = new Integer(9);
039: //
040: public final static Integer IN = new Integer(10);
041: public final static Integer NOT_IN = new Integer(11);
042:
043: //
044: public final static String AND_CONJUNCTION = "AND";
045: public final static String OR_CONJUNCTION = "OR";
046:
047: //
048: //
049: private ArrayList whereColumnNames = null;
050: private ArrayList whereColumnMetaTypes = null;
051: private ArrayList whereColumnTables = null;
052: //
053: private ArrayList whereColumnAlternateNames = null;
054: private ArrayList whereColumnAlternateMetaTypes = null;
055: private ArrayList whereColumnAlternateTables = null;
056: private ArrayList whereColumnAlternateValues = null;
057: //
058: private ArrayList whereColumnComparisons = null;
059: //
060: private HashSet whereColumnTablesSet = null;
061:
062: //
063: private String currentWhereConjunction = null;
064: //
065: private ArrayList whereConjunctionSeries = null;
066: //
067: private ArrayList whereParenthesisSeries = null;
068:
069: //
070: private HashSet allColumnTablesSet = null;
071: //
072: private HashSet blockedForeignKeys = null;
073:
074: protected WhereStatement() {
075: //
076: this .whereColumnNames = new ArrayList();
077: this .whereColumnMetaTypes = new ArrayList();
078: this .whereColumnTables = new ArrayList();
079: //
080: this .whereColumnAlternateNames = new ArrayList();
081: this .whereColumnAlternateMetaTypes = new ArrayList();
082: this .whereColumnAlternateTables = new ArrayList();
083: this .whereColumnAlternateValues = new ArrayList();
084: //
085: this .whereColumnComparisons = new ArrayList();
086: //
087: this .whereColumnTablesSet = new HashSet();
088:
089: //
090: this .currentWhereConjunction = AND_CONJUNCTION;
091: //
092: this .whereConjunctionSeries = new ArrayList();
093: //
094: this .whereParenthesisSeries = new ArrayList();
095: //
096: this .allColumnTablesSet = new HashSet();
097: //
098: this .blockedForeignKeys = new HashSet();
099: }
100:
101: public void blockForeignKey(CreateStatement inCs, String inColumn) {
102: this .blockedForeignKeys.add(inCs.getName() + "-" + inColumn);
103: }
104:
105: public boolean isForeignKeyBlocked(CreateStatement inCs,
106: String inColumn) {
107: return this .blockedForeignKeys.contains(inCs.getName() + "-"
108: + inColumn);
109: }
110:
111: //
112: public void addToAllTables(CreateStatement inWcs) {
113: this .allColumnTablesSet.add(inWcs);
114: }
115:
116: public Iterator getAllTablesIterator() {
117: return this .allColumnTablesSet.iterator();
118: }
119:
120: public boolean isPresentWithinAllTables(String inTableName) {
121:
122: boolean outValue = false;
123:
124: Iterator iter = getAllTablesIterator();
125:
126: while (iter.hasNext()) {
127: CreateStatement nextTable = (CreateStatement) iter.next();
128:
129: outValue |= (inTableName.equals(nextTable.getName()));
130: }
131:
132: return outValue;
133: }
134:
135: //
136: public void useAndConjunction() {
137: this .currentWhereConjunction = AND_CONJUNCTION;
138: }
139:
140: public void useOrConjunction() {
141: this .currentWhereConjunction = OR_CONJUNCTION;
142: }
143:
144: //
145: public void addWhereColumn(String inName1, Integer inMetaType1,
146: CreateStatement inTable1, String inName2,
147: Integer inMetaType2, CreateStatement inTable2,
148: Integer inComparison, Integer inParenthesis) {
149: //
150: this .whereColumnNames.add(inName1);
151: this .whereColumnMetaTypes.add(inMetaType1);
152: this .whereColumnTables.add(inTable1);
153: //
154: this .whereColumnAlternateNames.add(inName2);
155: this .whereColumnAlternateMetaTypes.add(inMetaType2);
156: this .whereColumnAlternateTables.add(inTable2);
157: this .whereColumnAlternateValues.add(null);
158: //
159: this .whereColumnComparisons.add(inComparison);
160: //
161: this .whereColumnTablesSet.add(inTable1);
162: this .whereColumnTablesSet.add(inTable2);
163:
164: //
165: this .whereConjunctionSeries.add(this .currentWhereConjunction);
166: //
167: this .whereParenthesisSeries.add(inParenthesis);
168:
169: //
170: this .allColumnTablesSet.add(inTable1);
171: this .allColumnTablesSet.add(inTable2);
172: }
173:
174: public void addWhereColumn(String inName, Integer inMetaType,
175: CreateStatement inTable, Object inValue,
176: Integer inComparison, Integer inParenthesis) {
177: //
178: this .whereColumnNames.add(inName);
179: this .whereColumnMetaTypes.add(inMetaType);
180: this .whereColumnTables.add(inTable);
181: //
182: this .whereColumnAlternateNames.add(null);
183: this .whereColumnAlternateMetaTypes.add(null);
184: this .whereColumnAlternateTables.add(null);
185: this .whereColumnAlternateValues.add(inValue);
186: //
187: this .whereColumnComparisons.add(inComparison);
188: //
189: this .whereColumnTablesSet.add(inTable);
190:
191: //
192: this .whereConjunctionSeries.add(this .currentWhereConjunction);
193: //
194: this .whereParenthesisSeries.add(inParenthesis);
195:
196: //
197: this .allColumnTablesSet.add(inTable);
198: }
199:
200: //
201: public int getWhereColumnCount() {
202: return this .whereColumnNames.size();
203: }
204:
205: //
206: public String getWhereColumnName(int inIndex) {
207: return (String) this .whereColumnNames.get(inIndex);
208: }
209:
210: public CreateStatement getWhereColumnTable(int inIndex) {
211: return (CreateStatement) this .whereColumnTables.get(inIndex);
212: }
213:
214: public Integer getWhereColumnMetaType(int inIndex) {
215: return (Integer) this .whereColumnMetaTypes.get(inIndex);
216: }
217:
218: //
219: public CreateStatement getWhereColumnAlternateTable(int inIndex) {
220: return (CreateStatement) this .whereColumnAlternateTables
221: .get(inIndex);
222: }
223:
224: public Object getWhereColumnAlternateValue(int inIndex) {
225: //MiscHelper.println("WhereStatement alternateValues = "
226: // + this.whereColumnAlternateValues);
227: return this .whereColumnAlternateValues.get(inIndex);
228: }
229:
230: public Integer getWhereColumnAlternateMetaType(int inIndex) {
231: return (Integer) this .whereColumnAlternateMetaTypes
232: .get(inIndex);
233: }
234:
235: public String getWhereColumnAlternateName(int inIndex) {
236: return (String) this .whereColumnAlternateNames.get(inIndex);
237: }
238:
239: //
240: public Integer getWhereColumnComparison(int inIndex) {
241: return (Integer) this .whereColumnComparisons.get(inIndex);
242: }
243:
244: public String getWhereColumnConjunction(int inIndex) {
245: return (String) this .whereConjunctionSeries.get(inIndex);
246: }
247:
248: //
249: public int getWhereColumnParenthesis(int inIndex) {
250:
251: int outValue = 0;
252:
253: Integer parenthesis = (Integer) this .whereParenthesisSeries
254: .get(inIndex);
255: if (parenthesis != null) {
256: outValue = parenthesis.intValue();
257: }
258:
259: return outValue;
260: }
261:
262: public int getLastWhereColumnParenthesis() {
263:
264: int outValue = 0;
265:
266: for (int i = 0; i < getWhereColumnCount(); i++) {
267: outValue = outValue - getWhereColumnParenthesis(i);
268: }
269:
270: return outValue;
271: }
272:
273: //
274: public ArrayList getAllTables() {
275:
276: ArrayList outValue = new ArrayList();
277:
278: Iterator iter = getAllTablesIterator();
279: while (iter.hasNext()) {
280: outValue.add(iter.next());
281: }
282:
283: return outValue;
284: }
285:
286: }
|