001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.sqlParser;
034:
035: import java.util.ArrayList;
036:
037: /**
038: * Brace
039: *
040: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
041: */
042: public class Brace implements BraceElement {
043:
044: private ArrayList<BraceElement> conditions;
045: private Brace parent;
046: private String type;
047: private FxStatement stmt;
048: private int id;
049:
050: protected Brace(FxStatement stmt) {
051: this .conditions = new ArrayList<BraceElement>(25);
052: this .parent = null;
053: this .stmt = stmt;
054: this .id = stmt.getNewBraceElementId();
055: }
056:
057: public int getId() {
058: return this .id;
059: }
060:
061: private void checkTbl(Value val) throws SqlParserException {
062: if (!(val instanceof Property)) {
063: return;
064: }
065: Property pr = (Property) val;
066: if (stmt.getTableByAlias(pr.getTableAlias()) == null) {
067: throw new SqlParserException("ex.sqlSearch.unknownTable",
068: pr.getTableAlias());
069: }
070: }
071:
072: /**
073: * Adds a element (condition or sub-brace) to the this brace.
074: *
075: * @param ele the element to add
076: * @throws SqlParserException if the condition is invalid
077: */
078: protected void addElement(BraceElement ele)
079: throws SqlParserException {
080: if (ele instanceof Condition) {
081: Condition co = (Condition) ele;
082: checkTbl(co.getLValueInfo());
083: checkTbl(co.getRValueInfo());
084: }
085: if (ele instanceof Brace) {
086: ((Brace) ele).parent = this ;
087: }
088: conditions.add(ele);
089: }
090:
091: protected void addElements(BraceElement eles[])
092: throws SqlParserException {
093: for (BraceElement ele : eles) {
094: addElement(ele);
095: }
096: }
097:
098: public int getLevel() {
099: int level = 0;
100: Brace p = this ;
101: while (p.parent != null) {
102: p = p.parent;
103: level++;
104: }
105: return level;
106: }
107:
108: public int getSize() {
109: return this .conditions.size();
110: }
111:
112: public Brace getParent() {
113: return this .parent;
114: }
115:
116: /**
117: * Returns the statement this brace belongs to.
118: *
119: * @return the statement this brace belongs to
120: */
121: public FxStatement getStatement() {
122: return stmt;
123: }
124:
125: protected BraceElement removeLastElement() {
126: BraceElement be = this .conditions
127: .remove(this .conditions.size() - 1);
128: if (be instanceof Brace) {
129: ((Brace) be).parent = null;
130: }
131: return be;
132: }
133:
134: protected boolean removeElement(BraceElement ele) {
135: boolean removed = this .conditions.remove(ele);
136: if (removed && ele instanceof Brace) {
137: ((Brace) ele).parent = null;
138: }
139: return removed;
140: }
141:
142: protected BraceElement[] removeAllElements() {
143: BraceElement result[] = conditions
144: .toArray(new BraceElement[conditions.size()]);
145: conditions.clear();
146: return result;
147: }
148:
149: /**
150: * Returns the type of the table.
151: *
152: * @return the type of the table
153: */
154: public String getType() {
155: return this .type;
156: }
157:
158: public boolean isOr() {
159: return (this .type != null && this .type.equalsIgnoreCase("or"));
160: }
161:
162: public boolean isAnd() {
163: return (this .type != null && this .type.equalsIgnoreCase("and"));
164: }
165:
166: protected void setType(String value) {
167: this .type = value;
168: }
169:
170: /**
171: * Returns the amount of conditons and sub-braces in this brace.
172: *
173: * @return the amount of conditons and sub-braces in this brace
174: */
175: public int size() {
176: return conditions.size();
177: }
178:
179: public BraceElement[] getElements() {
180: return conditions.toArray(new BraceElement[conditions.size()]);
181: }
182:
183: public BraceElement getElementAt(int pos) {
184: return conditions.get(pos);
185: }
186:
187: public String toString() {
188: return "Brace[type=" + type + ";" + super .toString() + "]";
189: }
190:
191: protected boolean processAndOr(String andOr,
192: boolean insideforcedBrace) throws SqlParserException {
193: if (type != null && !type.equals(andOr)) {
194: if (insideforcedBrace) {
195: insideforcedBrace = false;
196: stmt.endSubBrace();
197: } else {
198: if (andOr.equalsIgnoreCase("and")) {
199: insideforcedBrace = true;
200: BraceElement ele = removeLastElement();
201: System.out.println("#remAdd:" + ele);
202: Brace newBrace = stmt.startSubBrace();
203: newBrace.addElement(ele);
204: newBrace.setType(andOr);
205: } else {
206: BraceElement ele[] = removeAllElements();
207: Brace newBrace = new Brace(stmt);
208: newBrace.addElements(ele);
209: newBrace.type = this.type;
210: this.addElement(newBrace);
211: this.type = andOr;
212: }
213: }
214: }
215: setType(andOr);
216: return insideforcedBrace;
217: }
218: }
|