KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > expression > booleanvalueexpression > predicates > characterlikepredicate


1 package com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.predicates;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.serversystem.*;
6 import com.daffodilwoods.daffodildb.server.sql99.common.*;
7 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*;
8 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.table.*;
9 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
10 import com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary.*;
11 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.*;
12 import com.daffodilwoods.daffodildb.server.sql99.token.*;
13 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
14 import com.daffodilwoods.daffodildb.utils.comparator.*;
15 import com.daffodilwoods.daffodildb.utils.field.*;
16 import com.daffodilwoods.database.resource.*;
17 import com.daffodilwoods.database.utility.*;
18
19 /**
20  * Class represents Like Predicate.
21  * <p>Title: </p>
22  * <p>Description: </p>
23  * <p>Copyright: Copyright (c) 2003</p>
24  * <p>Company: </p>
25  * @author unascribed
26  * @version 1.0
27  */

28 public class characterlikepredicate extends PredicateAbstract implements com.daffodilwoods.daffodildb.utils.parser.StatementExecuter,
29     likepredicate, IntegerPool, ExecutionPlanConstants, SimpleConstants {
30
31    public SRESERVEDWORD1206543922escapecharacter
32        _OptSRESERVEDWORD1206543922escapecharacter0;
33    public characterpattern _characterpattern1;
34    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439222;
35    public SRESERVEDWORD1206543922 _OptSRESERVEDWORD12065439223;
36    public charactermatchvalue _charactermatchvalue4;
37
38    /**
39     * Boolean represents whether subquery is present in query or not.
40     */

41    private Boolean JavaDoc subQueryFlag;
42    private boolean firstClob, secondClob, thirdClob;
43    private ColumnDetails[] cd1, cd2, cd3;
44
45    /**
46     * Methods Calculates cost of solving this predicate on the rows passed.
47     * @param rowCount, rows is the total no of rows on which predicate will be solved
48     * @param index, flag indicating Index is choosen or not.
49     * @return
50     * @throws DException
51     */

52    public double getCost(long rowCount, boolean index) throws DException {
53       return rowCount;
54    }
55
56    /**
57     * Returns Qualified Condition in case of qualified join.
58     * TableDetails passed represents the tables involved on right side of LOJ and
59     * on left side of ROJ.
60     * Algo:
61     * 1) We extract unique tables involved in the predicate.
62     * 2) If tables involved are greater than 1 or less than 1, then it means
63     * predicate involves two tables or zero tables, means predicate is to be
64     * solved as onCondition in qualified join.
65     * 3) If tables involed are exactly one, then we check whether that table
66     * lies in the array of tables passed, if it lies then we can shift
67     * predicate to singleTableLevel otherwise not.
68     * 4) For predicate to shift at singleTable Level we make BVESingleTablePLan
69     * otheriwse we make onCondition
70     * For Example:
71     * A LOJ B on A.a1 Like 'a%'
72     * In the above example, Table involved in predicate is one, i.e. A and it
73     * lies on the left side of LOJ, then we solve this condition above LOJ as onCondition
74     *
75     * A LOJ B on B.b1 Like 'b%'
76     * In the above example, Table involved in predicate is one, i.e. B and it
77     * lies on the right side of LOJ,
78     * then we solve shift this condition to single Table Level
79     *
80     * A LOJ B on B.b1 Like A.a1
81     * In the above example, Table involved in predicate is two, then we solve
82     * this condition above LOJ as onCondition
83     * @param tableDetails
84     * @return
85     * @throws DException
86     */

87    public _QualifiedBVE getQualifiedBVE(TableDetails[] tableDetails) throws DException {
88      ArrayList uniqueTabldeDetailsLsit = getUniqueTableDetails(new ArrayList(),columnDetails);
89       if (uniqueTabldeDetailsLsit.size() != 1) {
90          return new QualifiedBVE(null, BVEPlanMerger.getBooleanFactor(this));
91       }
92       QualifiedBVE qualifiedBVE = getQualifiedBVEifBelongsToSingleTablePlan(
93           tableDetails, uniqueTabldeDetailsLsit);
94       if (qualifiedBVE == null) {
95          qualifiedBVE = new QualifiedBVE(null, BVEPlanMerger.getBooleanFactor(this));
96       }
97       return qualifiedBVE;
98    }
99
100    /**
101     * Returns Qualified Plan in case when Predicate can be shifted to single Table Level, otherwise return
102     * null.
103     * Passed Tables belongs to Qualified Plan.
104     * Algo:
105     * Passed Plan Tables is matched against Tables involved in the predicate.
106     * If it matches then this predicate can be shifted to Single Table Level, otherwise predicate is solved
107     * as onCondition.
108     * @param planTableDeails
109     * @param uniqueTabldeDetailsLsit
110     * @return
111     * @throws DException
112     */

113    private QualifiedBVE getQualifiedBVEifBelongsToSingleTablePlan(TableDetails[] planTableDeails, ArrayList uniqueTabldeDetailsLsit) throws DException {
114       TableDetails conditionTableDetail = (TableDetails) uniqueTabldeDetailsLsit.
115           get(0);
116       for (int j = 0, length = planTableDeails.length; j < length; j++) {
117          if (planTableDeails[j] == conditionTableDetail) {
118             BVESingleTablePlan bveSingleTablePlan = new BVESingleTablePlan(
119                 BVEPlanMerger.getBooleanFactor(this), conditionTableDetail);
120             return new QualifiedBVE(new BVESingleTablePlan[] {bveSingleTablePlan}
121                                     , null);
122          }
123       }
124       return null;
125    }
126
127    /**
128     * Returns List of unique tables involved in the predicate.
129     * @return
130     * @throws DException
131
132    private ArrayList getUniqueTableDetailsList() throws DException {
133       int cLength = columnDetails.length;
134       ArrayList checkTableList = new ArrayList();
135       for (int i = 0; i < cLength; i++) {
136          int type = columnDetails[i].getType();
137          if (type == REFERENCE) {
138             TableDetails tableDetails = columnDetails[i].getTableDetails();
139             if (!checkTableList.contains(tableDetails)) {
140                checkTableList.add(tableDetails);
141             }
142          }
143          if (type == SCALARFUNCTION || type == CASEEXPRESSION) {
144             checkTableList.clear();
145             return checkTableList;
146          }
147       }
148       return checkTableList;
149    }
150 */

151
152    /**
153     * Returns Type of this predicate.
154     * @return
155     * @throws DException
156     */

157    public int getPredicateType() throws DException {
158       if (_OptSRESERVEDWORD12065439223 != null) {
159          return OperatorConstants.NOTLIKE;
160       }
161       return OperatorConstants.LIKE;
162    }
163
164    /**
165     * Method decides whether this predicate can be solved by Index or not.
166     * Cases for which Like Predicate can be solved as NonIndexed Condition :
167     * 1) If predicate involves subquery.
168     * 2) If predicate involves any question mark.
169     * 3) If predicate involves any expression like a + 1. In this case Columns
170     * present in predicate is greater than 2.
171     * 4) If predicate involves any column other than CONSTANT in character pattern.
172     * 5) If predicate involves any column other than REFERENCE in character match value.
173     * 6) If first character of character pattern is a special character '% or _'.
174     * This is because as then we are
175     * not able to return correct type to move in BTREE.
176     * 7) If NOT is present in the predicate
177     *
178     * Cases for which Like Predicate can be solved as Indexed Condition :
179     * If Predicate involves the REFERENCE column in character match value and
180     * CONSTANT type column in character pattern.
181     * In case when predicate can be solved by index then we Make Range Predicate
182     * as solvable by Index and Like Predicate is choosen as NonIndex Condition
183     * E.g a1 like 'A%'. Then range predicate a1 >= 'A' and a1 < 'B' is solved by
184     * index and a1 like 'A%' is solved as non index.
185     *
186     * @param allColumnPredicates
187     * @throws DException
188     */

189    public void setColumnPredicates(_AllColumnPredicates allColumnPredicates) throws
190        DException {
191       boolean clob = checkForDatatype();
192       if (clob || checkForSubQuery() || _OptSRESERVEDWORD12065439223 != null || checkForQuestionInColumn() || checkForOtherCases() || checkForFirstCharacterInPattern()) {
193          allColumnPredicates.addToNonIndex(BVEPlanMerger.getBooleanFactor(this));
194          return;
195       }
196       if (_OptSRESERVEDWORD1206543922escapecharacter0 == null) {
197          setColumnPredicatesInNormalCase(allColumnPredicates);
198          return;
199       }
200       setColumnPredicatesInEscapeCase(allColumnPredicates);
201    }
202
203    /**
204     * Returns the SingleColumnPredicate array containing the predicate which can
205     * be solvable by index.
206     * Property is initialised in single column predicate to ensure if this predicate
207     * is not solvable by index then it is not choosen in the final execution plan,
208     * this work is done in AllColumnnPredicate class when
209     * we are making nonIndexed Condition for predicates not solvable by index.
210     * @param cp1
211     * @return
212     * @throws DException
213     */

214    private SingleColumnPredicate[] getSingleColumnPredicates(predicate cp1) throws DException {
215       SingleColumnPredicate scp = new SingleColumnPredicate();
216       scp.setPredicate(cp1);
217       scp.setLikePredicate();
218       scp.setColumnName(columnDetails[0].getColumn());
219       return new SingleColumnPredicate[] {scp};
220    }
221
222    /**
223     * Initializes the Single column predicate in case when escape character is not present.
224     * We make Range Predicate as this predicate can be solvable by index.
225     * Algo:
226     * 1) we call run of character pattern to get values in terms of byte[].
227     * 2) we make collect the bytes in a new array till we find first special character '_' or '%'
228     * 3) we increment last byte of newly formed array of byte in step 2 to make a range
229     * 4) in the last step we initializes the allcolumnpredicate wuth the range predicate.
230     *
231     * For Example:
232     * A Like 'abc%def'
233     * for this newly formed byte array will be 'abc' and after incrementing last array is 'abd'
234     * so range will be A >= 'abc' and A < 'abd'.
235     * @param allColumnPredicates
236     * @throws DException
237     */

238    private void setColumnPredicatesInNormalCase(_AllColumnPredicates allColumnPredicates) throws DException {
239       if (!checkForConstants(cd2)) {
240          allColumnPredicates.addToNonIndex(BVEPlanMerger.getBooleanFactor(this));
241       }
242       FieldBase fBase = (FieldBase) _characterpattern1.run(null);
243       byte[] bytes = fBase.getBufferRange().getBytes();
244       byte[] comparableBytes = null;
245       for (int i = 0; i < bytes.length; i++) {
246          byte b = bytes[i];
247          if (b == 37 || b == 95) { //'_' or '%'
248
break;
249          }
250          comparableBytes = comparableBytes == null ? getNewByte(b)
251              : addInByteArray(b, comparableBytes);
252       }
253       byte[] newcomparableBytes = new byte[comparableBytes.length];
254       for(int i = 0;i< comparableBytes.length;i++){
255         if(comparableBytes[i]>=65 && comparableBytes[i]<=90){
256           newcomparableBytes[i] = (byte)(comparableBytes[i]+32);
257           }
258           else
259           newcomparableBytes[i] = (byte)(comparableBytes[i]);
260       }
261       byte[] b = incrementByte(comparableBytes);
262       FieldBase fbLeft = new FieldStringLiteral(com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs.getString(newcomparableBytes), CHARACTER);
263       FieldBase fbRight = new FieldStringLiteral(com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs.getString(b), CHARACTER);
264       setColumnPredicatesByFormingRangePredicate(fbLeft, fbRight, allColumnPredicates);
265    }
266
267    /**
268     * Initializes the All Column Predicate with the two fieldBases passed.
269     * Steps :
270     * 1) We initialises two predicates with the help of _charactermatchvalue4 and two predicates passed.
271     * 2) In the second step we add this two predicates as indexed condition in allColumnPredicates.
272     * 3) In the last step we add this predicate as NonIndexed Condition.
273     * @param fBaseLeft
274     * @param fBaseRight
275     * @param allColumnPredicates
276     * @throws DException
277     */

278    private void setColumnPredicatesByFormingRangePredicate(FieldBase fBaseLeft, FieldBase fBaseRight, _AllColumnPredicates allColumnPredicates) throws DException {
279       CharacterPredicate cp1 = new CharacterPredicate(_charactermatchvalue4._charactermatchvalue0, OperatorConstants.GREATERTHANEQUALTO, fBaseLeft, columnDetails);
280       CharacterPredicate cp2 = new CharacterPredicate(_charactermatchvalue4._charactermatchvalue0, OperatorConstants.LESSTHAN, fBaseRight, columnDetails);
281       allColumnPredicates.addSinglePredicate(getSingleColumnPredicates(cp1));
282       allColumnPredicates.addSinglePredicate(getSingleColumnPredicates(cp2));
283       allColumnPredicates.addToNonIndex(BVEPlanMerger.getBooleanFactor(this));
284    }
285
286    /**
287     * Initializes the Single column predicate in case when escape character is present.
288     * Characters involved in Escape sequence shud be equal to 1, otherwise exception is thrown.
289     * We make Range Predicate excluding escape character as this predicate can be solvable by index.
290     * Algo:
291     * 1) we call run of character pattern to get values in terms of byte[].
292     * 2) we make collect the bytes in a new array till we find first special character '_' or '%' provided
293     * this special character is not escape character.
294     * 3) we increment last byte of newly formed array of byte in step 2 to make a range
295     * 4) in the last step we initializes the allcolumnpredicate with the range predicate.
296     *
297     * For Example:
298     * A Like 'abc%def' escape 'b'
299     * for this newly formed byte array will be 'ac' and after incrementing last array is 'ad' as because
300     * escape character is excluded from search
301     * so range will be A >= 'ac' and A < 'ad'.
302     * @param allColumnPredicates
303     * @throws DException
304     */

305    private void setColumnPredicatesInEscapeCase(_AllColumnPredicates allColumnPredicates) throws DException {
306       if (!checkForConstants(cd2) || !checkForConstants(cd3)) {
307          allColumnPredicates.addToNonIndex(BVEPlanMerger.getBooleanFactor(this));
308       }
309       FieldBase fBase = (FieldBase) _characterpattern1.run(null);
310       FieldBase fBase2 = (FieldBase) _OptSRESERVEDWORD1206543922escapecharacter0.run(null);
311       byte[] bytes = fBase.getBufferRange().getBytes();
312       byte[] comparableBytes = null;
313       byte[] esc = fBase2.getBufferRange().getBytes();
314       if (esc.length > 1 || esc.length == 0) {
315          throw new DException("DSE1103", null);
316       }
317       boolean flag = false;
318       for (int k = 0; k < bytes.length; k++) {
319          byte b = bytes[k];
320          if (b == esc[0] && !flag) {
321             flag = true;
322             if (k + 1 < bytes.length && (bytes[k + 1] == 37 || bytes[k + 1] == 95)) {
323                ++k;
324             } else {
325                continue;
326             }
327          }
328          if (b == 37 || b == 95) {
329             break;
330          }
331          comparableBytes = comparableBytes == null ? getNewByte(bytes[k])
332              : addInByteArray(bytes[k], comparableBytes);
333       }
334       if (comparableBytes == null) { // for case '%a' as loop breaks on % and comparableBytes array will be null
335
allColumnPredicates.addToNonIndex(BVEPlanMerger.getBooleanFactor(this));
336          return;
337       }
338       byte[] newcomparableBytes = new byte[comparableBytes.length];
339       for(int i = 0;i< comparableBytes.length;i++){
340         if(comparableBytes[i]>=65 && comparableBytes[i]<90){
341           newcomparableBytes[i] = (byte)(comparableBytes[i]+32);
342           }
343           else{
344           newcomparableBytes[i] = (byte)(comparableBytes[i]);
345       }
346       }
347       byte[] b = incrementByte(comparableBytes);
348       FieldBase fbLeft = new FieldStringLiteral(com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs.getString(newcomparableBytes), CHARACTER);
349       FieldBase fbRight = new FieldStringLiteral(com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs.getString(b), CHARACTER);
350       setColumnPredicatesByFormingRangePredicate(fbLeft, fbRight, allColumnPredicates);
351    }
352
353    /**
354     * Increaments last byte in the byte array passed and returns array
355     * @param comparableBytes
356     * @return
357     * @throws DException
358     */

359    private byte[] incrementByte(byte[] comparableBytes) throws DException {
360       byte[] bytes = new byte[comparableBytes.length];
361       for (int i = 0; i < comparableBytes.length - 1; i++) {
362         if(comparableBytes[i]>=65 && comparableBytes[i] <=90)
363          bytes[i] = (byte)(comparableBytes[i]+32);
364         else
365           bytes[i]=comparableBytes[i];
366       }
367       if(comparableBytes[comparableBytes.length - 1]>=65 && comparableBytes[comparableBytes.length - 1]<=90)
368       bytes[comparableBytes.length - 1] = (byte) ( comparableBytes[comparableBytes.length - 1] + 33);
369       else
370       bytes[comparableBytes.length - 1] = (byte) ((int)comparableBytes[comparableBytes.length - 1] + 1);
371       return bytes;
372    }
373
374    /**
375     * Returns byte array containing passed byte
376     * @param b
377     * @return
378     * @throws DException
379     */

380    private byte[] getNewByte(byte b) throws DException {
381       byte[] b1 = new byte[1];
382       b1[0] = b;
383       return b1;
384    }
385
386    /**
387     * Returns byte array after adding byte passed in the passed byte array.
388     * @param b
389     * @param comparableBytes
390     * @return
391     * @throws DException
392     */

393    private byte[] addInByteArray(byte b, byte[] comparableBytes) throws DException {
394       byte[] newB = new byte[comparableBytes.length + 1];
395       for (int i = 0; i < comparableBytes.length; i++) {
396          newB[i] = comparableBytes[i];
397       }
398       newB[comparableBytes.length] = b;
399       return newB;
400    }
401
402    /**
403     * Checks for first character in the pattern as special character '_' or '%'
404     * If it is special character then true is retuned otherwise false is returned.
405     * Exception is also thrown if predicate contains any column other than character
406     * @return
407     * @throws DException
408     */

409    private boolean checkForFirstCharacterInPattern() throws DException {
410       if (columnDetails[0].getType() == REFERENCE && columnDetails[1].getType() == CONSTANT && _OptSRESERVEDWORD1206543922escapecharacter0 == null) {
411          FieldBase fBase = (FieldBase) _characterpattern1.run(null);
412          byte[] bytes = fBase.getBufferRange().getBytes();
413          return bytes.length == 0 || bytes[0] == 37 || bytes[0] == 95;
414       }
415       return false;
416    }
417
418    /**
419     * Throws Exception if predicate contains any column other than character
420     * @throws DException
421     */

422    private boolean checkForDatatype() throws DException {
423       for (int i = 0; i < columnDetails.length; i++) {
424          if (columnDetails[i].getQuestion()) {
425             continue;
426          }
427          int dataType = columnDetails[i].getDatatype();
428          if (dataType == CHARACTER || dataType == VARCHAR || dataType == CHAR || dataType == CHARACTERVARYING) {
429             continue;
430          }
431          if (dataType == CLOB || dataType == CHARACTERLARGEOBJECT || dataType == CHARLARGEOBJECT || dataType == LONGVARCHAR) {
432             return true;
433          }
434          throw new DException("DSE3575", new Object JavaDoc[] {StaticClass.getDataTypeName(dataType)});
435       }
436       return false;
437    }
438
439    /**
440     * In this some cases are checked in which we can't use this predicate in
441     * indexing. These are -
442     * 1. When more than two columns are involved.
443     * 2. When Two Columns are involved then
444     * A. Both columns are constants
445     * B. One of the column is part of Outer query.
446     * @return
447     * @throws DException
448     */

449
450    private boolean checkForOtherCases() throws DException {
451       if (_OptSRESERVEDWORD1206543922escapecharacter0 == null && columnDetails.length != 2) {
452          return true;
453       }
454       return! (columnDetails[0].getType() == REFERENCE && columnDetails[1].getType() == CONSTANT) || columnDetails[1].getUnderLyingReference();
455    }
456
457    /**
458     * Chceks for any Question mark in predicate
459     * @return
460     * @throws DException
461     */

462
463    private boolean checkForQuestionInColumn() throws DException {
464       for (int i = 0; i < columnDetails.length; i++) {
465          if (columnDetails[i].getType() == CONSTANT && columnDetails[i].getQuestion()) {
466             return true;
467          }
468       }
469       return false;
470    }
471
472    /**
473     * Returns a BVEConstants member telling the BVEPlanType of instance on which
474     * this method is invoked.<p>
475     * If this method finds any column of GROUPING type then it returns
476     * BVEConstants.BVEAGGREGATE.<p>
477     * If it is not an aggregate plan, then it check for ALLTABLEPLAN
478     * type by checking existence of CASE Expression OR SubQuery and
479     * ComplexInnerQuery. If founds any then returns BVEConstants.BVEALLTABLEPLAN.<p>
480     * If both cases fail it check for table count for this predicate,<p>
481     * If table count is One, returns BVEConstants.BVESINGLETABLEPLAN;<p>
482     * If Two, returns BVEConstants.BVEJOINRELATION;<p>
483     * Otherwise BVEConstants.BVEALLTABLEPLAN.<p>
484     * @param tableList An empty ArrayList which is filled in this method with
485     * table names used in this predicate.
486     * @return BVEConstants.MEMBER.
487     * @throws DException
488     */

489    private int getBVEPlanType(ArrayList tableList) throws DException {
490       subQueryFlag = new Boolean JavaDoc(checkForSubQuery());
491       for (int i = 0; i < columnDetails.length; i++) {
492          int type = columnDetails[i].getType();
493          if (type == GROUPING) {
494             return BVEConstants.BVEAGGREGATEPLAN;
495          }
496          if (type == SCALARFUNCTION || type == CASEEXPRESSION || type == FUNCTIONAL || type == USERFUNCTION ) {
497             return BVEConstants.BVEALLTABLEPLAN;
498          }
499          if (type != CONSTANT) {
500             TableDetails tableDetail = columnDetails[i].getTableDetails();
501             if (tableDetail != null && !tableList.contains(tableDetail)) {
502                tableList.add(tableDetail);
503             }
504          }
505       }
506       if (checkForSubQuery() && checkForComplexInnerQuery()) {
507          return BVEConstants.BVEALLTABLEPLAN;
508       }
509       int size = tableList.size();
510       if (size == 1) {
511          return BVEConstants.BVESINGLETABLEPLAN;
512       } else if (size == 2) {
513          return BVEConstants.BVEJOINRELATION;
514       } else {
515          return BVEConstants.BVEALLTABLEPLAN;
516       }
517    }
518
519    /**
520     * Returns _BVEPlan based on the type of instance of this Object.<p>
521     * <pre>
522     * instance type returned plan
523     * -------------------------------------------------------------------------
524     * BVEConstants.BVEAGGREGATEPLAN BVEAggregatePlan
525     * BVEConstants.BVEALLTABLEPLAN BVEAllTablePlan
526     * BVEConstants.BVESINGLETABLEPLAN BVESingleTablePlan
527     * BVEConstants.BVEJOINRELATION BVEAllTablePlan
528     *
529     * <pre>
530     * In case of BVEJOINRELATION, a _JoinRelation is wrapped in
531     * AllTablesJoinRelation and AllTablesJoinRelation in BVEAllTablePlan.
532     * @return
533     * @throws DException
534     */

535    public _BVEPlan getExecutionPlan() throws DException {
536       ArrayList tableList = new ArrayList();
537       int type = getBVEPlanType(tableList);
538       switch (type) {
539          case BVEConstants.BVEAGGREGATEPLAN:
540             return new BVEAggregatePlan(BVEPlanMerger.getBooleanFactor(this));
541          case BVEConstants.BVEALLTABLEPLAN:
542             return new BVEAllTablePlan(null, null, BVEPlanMerger.getBooleanFactor(this));
543          case BVEConstants.BVESINGLETABLEPLAN:
544             return new BVESingleTablePlan(BVEPlanMerger.getBooleanFactor(this),
545                                           (TableDetails) tableList.get(0));
546          case BVEConstants.BVEJOINRELATION:
547             TableDetails[] tableDetails = (TableDetails[]) tableList.toArray(new
548                 TableDetails[0]);
549             _JoinRelation sr = new SimpleRelation(tableDetails,
550                                                   BVEPlanMerger.getBooleanFactor(this));
551             AllTablesJoinRelation atjr = new AllTablesJoinRelation(new
552                 _JoinRelation[] {sr});
553             return new BVEAllTablePlan(null, atjr, null);
554       }
555       throw new DException("DSE3547", new Object JavaDoc[] {this.toString()});
556    }
557
558    /**
559     * ComplexInnerQuery means that subQuery contains the column of outer query.
560     * In this case this condition is treated as remaining condition.
561     * @return
562     * @throws DException
563     */

564
565    private boolean checkForComplexInnerQuery() throws DException {
566       _Reference[] references = getReferences(null);
567       int length = references.length;
568       for (int i = 0; i < length; i++) {
569          if (references[i].getReferenceType() == SUBQUERY) {
570             if ( ( (subquery) references[i]).getUnderlyingReferences() != null) {
571                return true;
572             }
573          }
574       }
575       return false;
576    }
577
578    /**
579     * Run Method returns value 0,-1 or 1 on the basis whether predicate solves on
580     * the current record or not.
581     * Steps:
582     * 1) On Calling run we retrieves values for character match value and character pattern.
583     * 2) Thereafter, appropriate comparator is initialised by catching NPE for the first time.
584     * Comparator is decided on the basis of Byte Comparison usage.
585     * If Bytes Comparison is allowed
586     * Without Escape
587     * LikeComparator is initialised
588     * With Escape
589     * Comparator is decided on the basis of Array or single element value of escape
590     * Single Element
591     * EscapeComparator is initialised
592     * Array Containing Single Element (in case of subquery)
593     * EscapeSpecialComparator is initialised
594     *
595     * If Bytes Comparison is not allowed
596     * Without Escape
597     * SimpleLikeComparator is initialised
598     * With Escape
599     * Comparator is decided on the basis of Array or single element value of escape
600     * Single Element
601     * SimpleEscapeComparator is initialised
602     * Array Containing Single Element (in case of subquery)
603     * SimpleEscapeSpecialComparator is initialised
604     * 3) On the basis of compare value from comparator appropriate FiledBase is returned
605     * 4) if NOT presents and compare value is 0 then 1 is returned otherwise 0 is returned
606     * @param object
607     * @return
608     * @throws com.daffodilwoods.database.resource.DException
609     */

610    public Object JavaDoc run(Object JavaDoc object) throws com.daffodilwoods.database.resource.
611
       DException {
612       Object JavaDoc fieldBase1 = _charactermatchvalue4.run(object);
613       Object JavaDoc fieldBase2 = _characterpattern1.run(object);
614       int cmp = -1;
615       try {
616          cmp = comparator.compare(fieldBase1, fieldBase2);
617       } catch (NullPointerException JavaDoc ex) {
618          ByteComparison bc1 = _charactermatchvalue4.getByteComparison(object);
619          ByteComparison bc2 = _characterpattern1.getByteComparison(object);
620          initializeClob();
621          comparator = _OptSRESERVEDWORD1206543922escapecharacter0 == null ?
622              getAppropriateComparatorNormal(bc1, bc2, fieldBase1, fieldBase2, null)
623              : getAppropriateComparatorEscape(bc1, bc2, _OptSRESERVEDWORD1206543922escapecharacter0.getByteComparison(object), fieldBase1, fieldBase2, null, object);
624          cmp = comparator.compare(fieldBase1, fieldBase2);
625       } catch (DException ex1) {
626          throw ex1;
627
628       }
629       return _OptSRESERVEDWORD12065439223 == null ? cmp == 0 ? IntegerPool.Integer0 : IntegerPool.Integer1
630           : cmp == 0 ? IntegerPool.Integer1 : IntegerPool.Integer0;
631    }
632
633    /**
634     * Returns Appropriate Comparator for Normal Case
635     * @param byteComparison1
636     * @param byteComparison2
637     * @param value1
638     * @param value2
639     * @param serverSession
640     * @return
641     * @throws DException
642     */

643
644    public SuperComparator getAppropriateComparatorNormal(ByteComparison byteComparison1, ByteComparison byteComparison2, Object JavaDoc value1, Object JavaDoc value2, _ServerSession serverSession) throws DException {
645       boolean canNotUseByteComparison = !byteComparison1.canUseByteComparison() || !byteComparison2.canUseByteComparison();
646       SuperComparator comparator = canNotUseByteComparison ? new SimpleLikeComparator(firstClob, secondClob) :
647           (SuperComparator)new LikeComparator(firstClob, secondClob);
648       return getAppropriateComparator(value1, value2, serverSession, comparator);
649    }
650
651    /**
652     * Returns Appropriate Comparator for Escape Case
653     * @param byteComparison1
654     * @param byteComparison2
655     * @param value1
656     * @param value2
657     * @param serverSession
658     * @return
659     * @throws DException
660     */

661    public SuperComparator getAppropriateComparatorEscape(ByteComparison byteComparison1, ByteComparison byteComparison2, ByteComparison byteComparison3, Object JavaDoc value1, Object JavaDoc value2, _ServerSession serverSession, Object JavaDoc object) throws DException {
662       boolean canNotUseByteComparison = !byteComparison1.canUseByteComparison() || !byteComparison2.canUseByteComparison() || !byteComparison3.canUseByteComparison();
663       boolean singleElement = false;
664       Object JavaDoc value = _OptSRESERVEDWORD1206543922escapecharacter0.run(object);
665       FieldBase[] fieldBases = null;
666       try {
667          fieldBases = GeneralPurposeStaticClass.changeIntoFieldBase( (Object JavaDoc[]) value);
668       } catch (ClassCastException JavaDoc ex) {
669          singleElement = true;
670       }
671       SuperComparator comparator = null;
672       comparator = canNotUseByteComparison ? singleElement ? new SimpleEscapeComparator(_OptSRESERVEDWORD1206543922escapecharacter0, object, firstClob, secondClob, thirdClob)
673           : (SuperComparator)new SimpleEscapeSpecialComparator(_OptSRESERVEDWORD1206543922escapecharacter0, object, firstClob, secondClob, thirdClob) :
674           singleElement ? (SuperComparator)new EscapeComparator(_OptSRESERVEDWORD1206543922escapecharacter0, object, firstClob, secondClob, thirdClob)
675           : (SuperComparator)new EscapeSpecialComparator(_OptSRESERVEDWORD1206543922escapecharacter0, object, firstClob, secondClob, thirdClob);
676       return getAppropriateComparator(value1, value2, serverSession, comparator);
677    }
678
679    /**
680     * Decides Appropriate Comparator on the basis of following combination
681     * Array - Single
682     * Array - Array
683     * Single - Array
684     * Single - Single
685     * Array and Single are values of _charactermatchvalue4 and _characterpattern1 which are passed as an argument
686     * Array is in case of subQuery
687     * Single is in case of simple column
688     * @param byteComparison1
689     * @param byteComparison2
690     * @param value1
691     * @param value2
692     * @param serverSession
693     * @return
694     * @throws DException
695     */

696    private SuperComparator getAppropriateComparator(Object JavaDoc value1, Object JavaDoc value2, _ServerSession serverSession, SuperComparator comparator) throws DException {
697       boolean leftSingleElement = false;
698       boolean rightSingleElement = false;
699       FieldBase[] fieldBases1 = null;
700       try {
701          fieldBases1 = GeneralPurposeStaticClass.changeIntoFieldBase( (Object JavaDoc[]) value1);
702       } catch (ClassCastException JavaDoc ex) {
703          leftSingleElement = true;
704       }
705       FieldBase[] fieldBases2 = null;
706       try {
707          fieldBases2 = GeneralPurposeStaticClass.changeIntoFieldBase( (Object JavaDoc[]) value2);
708       } catch (ClassCastException JavaDoc ex) {
709          rightSingleElement = true;
710       }
711       if (leftSingleElement && rightSingleElement) {
712          return comparator;
713       }
714       if (leftSingleElement && !rightSingleElement) {
715          return new CJoDpnqbsbups(new SuperComparator[] {comparator});
716       }
717       if (!leftSingleElement && rightSingleElement) {
718          return new CJoTqfdjbmDpnqbsbups(new SuperComparator[] {comparator});
719       }
720       return getJoinComparator(fieldBases1, fieldBases2, serverSession, comparator);
721    }
722
723    private SuperComparator getJoinComparator(Object JavaDoc[] fieldBase1, Object JavaDoc[] fieldBase2, _ServerSession serverSession, SuperComparator comparator) throws DException {
724       int length = fieldBase1.length;
725       SuperComparator[] comparators = new SuperComparator[length];
726       for (int i = 0; i < length; i++) {
727          comparators[i] = comparator;
728       }
729       return new CKpjoDpnqbsbups(comparators);
730    }
731
732    /**
733     * This method is called from Browser, when Parameterized query is fired from Browser.
734     * With the help of ParameterInfo Browser open ups daialog box(labelled by its corresponding name)
735     * for each question mark to put the values of question mark.
736     * @return
737     * @throws DException
738     */

739
740    public ParameterInfo[] getParameterInfo() throws DException {
741       boolean isSubQuery = false;
742       ParameterInfo[] p1 = _charactermatchvalue4.getParameterInfo();
743       ParameterInfo[] p2 = _characterpattern1.getParameterInfo();
744       ParameterInfo[] p3 = _OptSRESERVEDWORD1206543922escapecharacter0 != null ? _OptSRESERVEDWORD1206543922escapecharacter0.getParameterInfo() : null;
745       ArrayList aList = new ArrayList(5);
746       int count = 0, maxDataType = -1;
747       if(p1.length == p2.length){
748          for (int i = 0; i < p2.length; i++) {
749             if (p2[i].getSubQuery()) {
750               ParameterInfo[] array = p2[i].getParameterInfoArray();
751                if(array !=null)
752                  aList.addAll(Arrays.asList(array));
753                  isSubQuery = true;
754            } else if (p2[i].getQuestionMark()) {
755                if (!p1[i].getQuestionMark()) {
756                   p2[i].setName(p1[i].getName() + count++);
757                   p2[i].setDataType(p1[i].getDataType());
758                }
759                aList.add(p2[i]);
760             }
761             int t = p2[i].getDataType();
762             if (maxDataType < t) {
763                maxDataType = t;
764             }
765          }
766       }
767       else{
768          for (int i = 0; i < p2.length; i++) {
769             if (p2[i].getSubQuery()) {
770               ParameterInfo[] array = p2[i].getParameterInfoArray();
771                if(array != null)
772                aList.addAll(Arrays.asList(array));
773                isSubQuery = true;
774             } else if (p2[i].getQuestionMark()) {
775                aList.add(p2[i]);
776             }
777          }
778       }
779       if(p3 != null){
780          if(p3.length == p1.length){
781             for (int i = 0; i < p3.length; i++) {
782                if (p3[i].getSubQuery()) {
783                  ParameterInfo[] array = p3[i].getParameterInfoArray();
784                  if(array != null)
785                   aList.addAll(Arrays.asList(array));
786                   isSubQuery = true;
787                } else if (p3[i].getQuestionMark()) {
788                   if (!p1[i].getQuestionMark()) {
789                      p3[i].setName(p1[i].getName() + count++);
790                      p3[i].setDataType(p1[i].getDataType());
791                   }
792                   aList.add(p3[i]);
793                }
794                int t = p3[i].getDataType();
795                if (maxDataType < t) {
796                   maxDataType = t;
797                }
798             }
799          }
800          else{
801             for (int i = 0; i < p3.length; i++) {
802                if (p3[i].getSubQuery()) {
803                  ParameterInfo[] array = p3[i].getParameterInfoArray();
804                  if(array != null)
805                   aList.addAll(Arrays.asList(array));
806                   isSubQuery = true;
807                } else if (p3[i].getQuestionMark()) {
808                   aList.add(p3[i]);
809                }
810             }
811          }
812       }
813       for (int i = p1.length - 1; i >= 0; i--) {
814          if (p1[i].getQuestionMark()) {
815             aList.add(0, p1[i]);
816          }
817          int t = p2[i].getDataType();
818          if (maxDataType < t) {
819             maxDataType = t;
820          }
821       }
822       return (ParameterInfo[]) aList.toArray(new ParameterInfo[0]);
823    }
824
825    public AbstractRowValueExpression[] getChilds() {
826       AbstractRowValueExpression[] childs =
827           _OptSRESERVEDWORD1206543922escapecharacter0 == null
828           ? new AbstractRowValueExpression[] {
829           (AbstractRowValueExpression) (_charactermatchvalue4),
830           (AbstractRowValueExpression) (_characterpattern1)}
831           : new AbstractRowValueExpression[] {
832           (AbstractRowValueExpression) (_charactermatchvalue4),
833           (AbstractRowValueExpression) (_characterpattern1),
834           (AbstractRowValueExpression)
835           _OptSRESERVEDWORD1206543922escapecharacter0};
836       return childs;
837    }
838
839    public void setDefaultValues(_VariableValueOperations variableValueOperation) throws
840        DException {
841    }
842
843    /**
844     * This method performs the semantic checking of cardinality and initiates
845     * checking of underlying children. Some Runtime checking like checking of
846     * dataType are performed at runtime.
847     * @param parent
848     * @return
849     * @throws DException
850     */

851
852    public com.daffodilwoods.daffodildb.server.sql99.utils._Reference[]
853        checkSemantic(com.daffodilwoods.daffodildb.server.serversystem.
854
                     _ServerSession parent) throws DException {
855       _Reference[] references = GeneralPurposeStaticClass.getJointReferences(
856           _charactermatchvalue4.checkSemantic(parent),
857           _characterpattern1.checkSemantic(parent));
858       references = GeneralPurposeStaticClass.getJointReferences(references, _OptSRESERVEDWORD1206543922escapecharacter0 == null ? null : _OptSRESERVEDWORD1206543922escapecharacter0.checkSemantic(parent));
859       checkDataTypes(parent);
860       return references;
861    }
862
863    private void checkDataTypes(_ServerSession session) throws DException {
864       int[] dt1 = _charactermatchvalue4.getByteComparison(session).getDataTypes();
865       int[] dt2 = _characterpattern1.getByteComparison(session).getDataTypes();
866       try {
867         checkForCharacterDatatype(dt1,dt2);
868          if (_OptSRESERVEDWORD1206543922escapecharacter0 != null) {
869             int dt3 = _OptSRESERVEDWORD1206543922escapecharacter0.getByteComparison(session).getDataTypes()[0];
870             Check.checkForDatatype(dt1[0], dt3);
871          }
872       } catch (DException ex) {
873         if (!(ex.getDseCode().equals("DSE4114") || ex.getDseCode().equals("DSE6001"))) { // done by kaushik regarding changes in check
874
throw ex;
875          }
876       }
877    }
878
879    private void initializeClob() throws DException {
880       firstClob = checkForCLOB(cd1);
881       secondClob = checkForCLOB(cd2);
882       if (cd3 != null) {
883          thirdClob = checkForCLOB(cd3);
884       }
885    }
886
887    public ColumnDetails[] getColumnDetails() throws DException {
888       cd1 = _charactermatchvalue4.getColumnDetails();
889       cd2 = _characterpattern1.getColumnDetails();
890       if (_OptSRESERVEDWORD1206543922escapecharacter0 != null) {
891          cd3 = _OptSRESERVEDWORD1206543922escapecharacter0.getColumnDetails();
892       }
893       columnDetails = GeneralPurposeStaticClass.getCombinedCDArray(cd1, cd2);
894       columnDetails = GeneralPurposeStaticClass.getCombinedCDArray(columnDetails, cd3);
895       for (int i = 0; i < columnDetails.length; i++) {
896          columnDetails[i].setAsBelongToAllowedPredicateColumn();
897       }
898       return columnDetails;
899    }
900
901    private boolean checkForCLOB(ColumnDetails[] cd) throws DException {
902       for (int i = 0; i < cd.length; i++) {
903          if (cd[i].getQuestion()) {
904             continue;
905          }
906          int dataType = cd[i].getDatatype();
907          if (dataType == CLOB || dataType == CHARACTERLARGEOBJECT || dataType == CHARLARGEOBJECT || dataType == LONGVARCHAR) {
908             return true;
909          }
910       }
911       return false;
912    }
913
914    private boolean checkForConstants(ColumnDetails[] cd) throws DException {
915       for (int i = 0; i < cd.length; i++) {
916          if (cd[i].getType() != CONSTANT) {
917             return false;
918          }
919       }
920       return true;
921    }
922
923    public String JavaDoc toString() {
924       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
925       sb.append(" ");
926       sb.append(_charactermatchvalue4);
927       sb.append(" ");
928       if (_OptSRESERVEDWORD12065439223 != null) {
929          sb.append(_OptSRESERVEDWORD12065439223);
930       }
931       sb.append(" ");
932       sb.append(_SRESERVEDWORD12065439222);
933       sb.append(" ");
934       sb.append(_characterpattern1);
935       sb.append(" ");
936       if (_OptSRESERVEDWORD1206543922escapecharacter0 != null) {
937          sb.append(_OptSRESERVEDWORD1206543922escapecharacter0);
938       }
939       return sb.toString();
940    }
941
942    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
943       characterlikepredicate tempClass = new characterlikepredicate();
944       if (_OptSRESERVEDWORD1206543922escapecharacter0 != null) {
945          tempClass._OptSRESERVEDWORD1206543922escapecharacter0 = (
946              SRESERVEDWORD1206543922escapecharacter)
947              _OptSRESERVEDWORD1206543922escapecharacter0.clone();
948       }
949       tempClass._characterpattern1 = (characterpattern) _characterpattern1.clone();
950       tempClass._SRESERVEDWORD12065439222 = (SRESERVEDWORD1206543922)
951           _SRESERVEDWORD12065439222.clone();
952       if (_OptSRESERVEDWORD12065439223 != null) {
953          tempClass._OptSRESERVEDWORD12065439223 = (SRESERVEDWORD1206543922)
954              _OptSRESERVEDWORD12065439223.clone();
955       }
956       tempClass._charactermatchvalue4 = (charactermatchvalue)
957           _charactermatchvalue4.clone();
958       try {
959          tempClass.getColumnDetails();
960       } catch (DException ex) {
961          throw new RuntimeException JavaDoc(ex.getMessage());
962       }
963       return tempClass;
964    }
965
966    private void checkForCharacterDatatype(int[] dt1,int dt2[]) throws DException{
967        for (int i = 0; i < dt1.length; i++) {
968          if (dt1[i] == -1 || dt2[i] == -1) {
969            continue;
970          }
971          /*Commented by Sandeep Kadiyan to Solve BUG 12046 */
972            if(!((dt1[i]>=16 && dt1[i]<=20) || dt1[i]==33 || dt1[i]==36 || dt1[i]==37 || dt1[i]==41))
973               throw new DException("DSE6004",new Object JavaDoc[]{StaticClass.getDataTypeName(dt1[i])});
974           if(!((dt2[i]>=16 && dt2[i]<=20) || dt2[i]==33 || dt2[i]==36 || dt2[i]==37 || dt2[i]==41))
975                 throw new DException("DSE6004",new Object JavaDoc[]{StaticClass.getDataTypeName(dt2[i])});
976         }
977      }
978    }
979
Popular Tags