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: /**
036: * Condition
037: *
038: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
039: */
040: public class Condition implements BraceElement {
041:
042: private Comparator comparator;
043: private Value sLeft;
044: private Value sRight;
045: private int id;
046:
047: public enum Comparator {
048: LIKE("like"), NOT_LIKE("not like"), EQUAL("="), NOT_EQUAL("!="), LESS(
049: "<"), GREATER(">"), GREATER_OR_EQUAL(">="), LESS_OR_EQUAL(
050: "<="), NOT_GREATER("!>"), NOT_LESS("!<"), IS("is"), IS_NOT(
051: "is not"), IN("in"), NOT_IN("not in"), NEAR(null) /* no SQL equivalent */, IS_CHILD_OF(
052: null), IS_DIRECT_CHILD_OF(null);
053:
054: private String sql;
055:
056: Comparator(String sql) {
057: this .sql = sql;
058: }
059:
060: public String getSql() {
061: return sql;
062: }
063: }
064:
065: /**
066: * Constructor.
067: * <p />
068: * At least one value has to be an constant or an FxSqlParserException is thrown.
069: *
070: * @param stmt the statement
071: * @param vleft the left value
072: * @param comparator the comparator
073: * @param vright the right value
074: * @throws SqlParserException if a error occured
075: */
076: protected Condition(FxStatement stmt, Value vleft,
077: Comparator comparator, Value vright)
078: throws SqlParserException {
079: this .comparator = comparator;
080: this .sLeft = vleft;
081: this .sRight = vright;
082: this .id = stmt.getNewBraceElementId();
083: if (vleft instanceof Property && vright instanceof Property) {
084: String sParam = vleft + String.valueOf(comparator) + vright;
085: throw new SqlParserException(
086: "ex.sqlSearch.invalidConditionNoConst", sParam);
087: }
088: if (vleft instanceof Property) {
089: if (((Property) vleft).getPropertyName().equals("*")
090: && vleft.hasFunction()) {
091: throw new SqlParserException(
092: "ex.sqlSearch.parser.fulltextSearchMayNotHaveFunct",
093: vleft.getFunctionsStart() + ".."
094: + vleft.getFunctionsEnd());
095: }
096: }
097: if (vright instanceof Property) {
098: if (((Property) vright).getPropertyName().equals("*")
099: && vright.hasFunction()) {
100: throw new SqlParserException(
101: "ex.sqlSearch.parser.fulltextSearchMayNotHaveFunct",
102: vright.getFunctionsStart() + ".."
103: + vright.getFunctionsEnd());
104: }
105: }
106: }
107:
108: /**
109: * Returns the id
110: * @return the id
111: */
112: public int getId() {
113: return this .id;
114: }
115:
116: public BraceElement[] getElements() {
117: return new BraceElement[0];
118: }
119:
120: public String toString() {
121: return (sLeft == null ? "null" : sLeft.getValue() + " ")
122: + String.valueOf(comparator)
123: + (sRight == null ? "" : " " + sRight.getValue());
124: }
125:
126: public Value getLValueInfo() {
127: return this .sLeft;
128: }
129:
130: public Value getRValueInfo() {
131: return this .sRight;
132: }
133:
134: public Comparator getComperator() {
135: return this .comparator;
136: }
137:
138: public String getSqlComperator() {
139: return " " + this .comparator.getSql() + " ";
140: }
141:
142: /**
143: * Returns true if the condition is always true.
144: *
145: * @return true if the condition is always true.
146: * @throws SqlParserException if a error occured
147: */
148: public boolean isAlwaysTrue() throws SqlParserException {
149: if (!(sLeft instanceof Constant && sRight instanceof Constant)) {
150: return false;
151: }
152: if (comparator == Comparator.EQUAL) {
153: return sLeft.equals(sRight);
154: }
155: throw new SqlParserException(
156: "ex.sqlSearch.connotUseComperator", comparator, (sLeft
157: + " " + comparator + " " + sRight));
158: }
159:
160: /**
161: * Returns true if the condition is always false.
162: *
163: * @return true if the condition is always false.
164: * @throws SqlParserException if a error occured
165: */
166: public boolean isAlwaysFalse() throws SqlParserException {
167: if (!(sLeft instanceof Constant && sRight instanceof Constant)) {
168: return false;
169: }
170: if (comparator == Comparator.EQUAL) {
171: return !sLeft.equals(sRight);
172: }
173: throw new SqlParserException(
174: "ex.sqlSearch.connotUseComperator", comparator, (sLeft
175: + " " + comparator + " " + sRight));
176: }
177:
178: public Constant getConstant() {
179: if (this .sLeft instanceof Constant) {
180: return (Constant) sLeft;
181: } else if (this .sRight instanceof Constant) {
182: return (Constant) sRight;
183: } else {
184: return null;
185: }
186: }
187:
188: public Property getProperty() {
189: if (this .sLeft instanceof Property) {
190: return (Property) sLeft;
191: } else if (this .sRight instanceof Property) {
192: return (Property) sRight;
193: } else {
194: return null;
195: }
196: }
197:
198: }
|