001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019 020package lucee.runtime.sql.old; 021 022import java.io.BufferedReader; 023import java.io.ByteArrayInputStream; 024import java.io.FileReader; 025import java.sql.SQLException; 026import java.util.Vector; 027 028// Referenced classes of package Zql: 029// ZExpression, ZConstant, ZExp, ZTuple, 030// ZqlParser 031 032public final class ZEval 033{ 034 035 public ZEval() 036 { 037 } 038 039 public boolean eval(ZTuple ztuple, ZExp zexp) 040 throws SQLException 041 { 042 if(ztuple == null || zexp == null) 043 throw new SQLException("ZEval.eval(): null argument or operator"); 044 if(!(zexp instanceof ZExpression)) 045 throw new SQLException("ZEval.eval(): only expressions are supported"); 046 ZExpression zexpression = (ZExpression)zexp; 047 String s = zexpression.getOperator(); 048 if(s.equals("AND")) 049 { 050 boolean flag = true; 051 for(int i = 0; i < zexpression.nbOperands(); i++) 052 flag &= eval(ztuple, zexpression.getOperand(i)); 053 054 return flag; 055 } 056 if(s.equals("OR")) 057 { 058 boolean flag1 = false; 059 for(int j = 0; j < zexpression.nbOperands(); j++) 060 flag1 |= eval(ztuple, zexpression.getOperand(j)); 061 062 return flag1; 063 } 064 if(s.equals("NOT")) 065 return !eval(ztuple, zexpression.getOperand(0)); 066 if(s.equals("=")) 067 return evalCmp(ztuple, zexpression.getOperands()) == 0.0D; 068 if(s.equals("!=")) 069 return evalCmp(ztuple, zexpression.getOperands()) != 0.0D; 070 if(s.equals("<>")) 071 return evalCmp(ztuple, zexpression.getOperands()) != 0.0D; 072 if(s.equals("#")) 073 throw new SQLException("ZEval.eval(): Operator # not supported"); 074 if(s.equals(">")) 075 return evalCmp(ztuple, zexpression.getOperands()) > 0.0D; 076 if(s.equals(">=")) 077 return evalCmp(ztuple, zexpression.getOperands()) >= 0.0D; 078 if(s.equals("<")) 079 return evalCmp(ztuple, zexpression.getOperands()) < 0.0D; 080 if(s.equals("<=")) 081 return evalCmp(ztuple, zexpression.getOperands()) <= 0.0D; 082 if(s.equals("BETWEEN") || s.equals("NOT BETWEEN")) 083 { 084 ZExpression zexpression1 = new ZExpression("AND", new ZExpression(">=", zexpression.getOperand(0), zexpression.getOperand(1)), new ZExpression("<=", zexpression.getOperand(0), zexpression.getOperand(2))); 085 if(s.equals("NOT BETWEEN")) 086 return !eval(ztuple, ( (zexpression1))); 087 return eval(ztuple, ( (zexpression1))); 088 } 089 if(s.equals("LIKE") || s.equals("NOT LIKE")) 090 throw new SQLException("ZEval.eval(): Operator (NOT) LIKE not supported"); 091 if(s.equals("IN") || s.equals("NOT IN")) 092 { 093 ZExpression zexpression2 = new ZExpression("OR"); 094 for(int k = 1; k < zexpression.nbOperands(); k++) 095 zexpression2.addOperand(new ZExpression("=", zexpression.getOperand(0), zexpression.getOperand(k))); 096 097 if(s.equals("NOT IN")) 098 return !eval(ztuple, ( (zexpression2))); 099 return eval(ztuple, ( (zexpression2))); 100 } 101 if(s.equals("IS NULL")) 102 { 103 if(zexpression.nbOperands() <= 0 || zexpression.getOperand(0) == null) 104 return true; 105 ZExp zexp1 = zexpression.getOperand(0); 106 if(zexp1 instanceof ZConstant) 107 return ((ZConstant)zexp1).getType() == 1; 108 throw new SQLException("ZEval.eval(): can't eval IS (NOT) NULL"); 109 } 110 if(s.equals("IS NOT NULL")) 111 { 112 ZExpression zexpression3 = new ZExpression("IS NULL"); 113 zexpression3.setOperands(zexpression.getOperands()); 114 return !eval(ztuple, ((zexpression3))); 115 } 116 throw new SQLException("ZEval.eval(): Unknown operator " + s); 117 118 } 119 120 double evalCmp(ZTuple ztuple, Vector vector) 121 throws SQLException 122 { 123 if(vector.size() < 2) 124 throw new SQLException("ZEval.evalCmp(): Trying to compare less than two values"); 125 if(vector.size() > 2) 126 throw new SQLException("ZEval.evalCmp(): Trying to compare more than two values"); 127 Object obj = null; 128 Object obj1 = null; 129 obj = evalExpValue(ztuple, (ZExp)vector.elementAt(0)); 130 obj1 = evalExpValue(ztuple, (ZExp)vector.elementAt(1)); 131 if((obj instanceof String) || (obj1 instanceof String)) 132 return (obj.equals(obj1) ? 0 : -1); 133 if((obj instanceof Number) && (obj1 instanceof Number)) 134 return ((Number)obj).doubleValue() - ((Number)obj1).doubleValue(); 135 throw new SQLException("ZEval.evalCmp(): can't compare (" + obj.toString() + ") with (" + obj1.toString() + ")"); 136 } 137 138 double evalNumericExp(ZTuple ztuple, ZExpression zexpression) 139 throws SQLException 140 { 141 if(ztuple == null || zexpression == null || zexpression.getOperator() == null) 142 throw new SQLException("ZEval.eval(): null argument or operator"); 143 String s = zexpression.getOperator(); 144 Object obj = evalExpValue(ztuple, zexpression.getOperand(0)); 145 if(!(obj instanceof Double)) 146 throw new SQLException("ZEval.evalNumericExp(): expression not numeric"); 147 Double double1 = (Double)obj; 148 if(s.equals("+")) 149 { 150 double d = double1.doubleValue(); 151 for(int i = 1; i < zexpression.nbOperands(); i++) 152 { 153 Object obj1 = evalExpValue(ztuple, zexpression.getOperand(i)); 154 d += ((Number)obj1).doubleValue(); 155 } 156 157 return d; 158 } 159 if(s.equals("-")) 160 { 161 double d1 = double1.doubleValue(); 162 if(zexpression.nbOperands() == 1) 163 return -d1; 164 for(int j = 1; j < zexpression.nbOperands(); j++) 165 { 166 Object obj2 = evalExpValue(ztuple, zexpression.getOperand(j)); 167 d1 -= ((Number)obj2).doubleValue(); 168 } 169 170 return d1; 171 } 172 if(s.equals("*")) 173 { 174 double d2 = double1.doubleValue(); 175 for(int k = 1; k < zexpression.nbOperands(); k++) 176 { 177 Object obj3 = evalExpValue(ztuple, zexpression.getOperand(k)); 178 d2 *= ((Number)obj3).doubleValue(); 179 } 180 181 return d2; 182 } 183 if(s.equals("/")) 184 { 185 double d3 = double1.doubleValue(); 186 for(int l = 1; l < zexpression.nbOperands(); l++) 187 { 188 Object obj4 = evalExpValue(ztuple, zexpression.getOperand(l)); 189 d3 /= ((Number)obj4).doubleValue(); 190 } 191 192 return d3; 193 } 194 if(s.equals("**")) 195 { 196 double d4 = double1.doubleValue(); 197 for(int i1 = 1; i1 < zexpression.nbOperands(); i1++) 198 { 199 Object obj5 = evalExpValue(ztuple, zexpression.getOperand(i1)); 200 d4 = Math.pow(d4, ((Number)obj5).doubleValue()); 201 } 202 203 return d4; 204 } 205 throw new SQLException("ZEval.evalNumericExp(): Unknown operator " + s); 206 207 } 208 209 public Object evalExpValue(ZTuple ztuple, ZExp zexp) 210 throws SQLException 211 { 212 Object obj = null; 213 if(zexp instanceof ZConstant) 214 { 215 ZConstant zconstant = (ZConstant)zexp; 216 switch(zconstant.getType()) 217 { 218 case 0: // '\0' 219 Object obj1 = ztuple.getAttValue(zconstant.getValue()); 220 if(obj1 == null) 221 throw new SQLException("ZEval.evalExpValue(): unknown column " + zconstant.getValue()); 222 try 223 { 224 obj = new Double(obj1.toString()); 225 } 226 catch(NumberFormatException numberformatexception) 227 { 228 obj = obj1; 229 } 230 break; 231 232 case 2: // '\002' 233 obj = new Double(zconstant.getValue()); 234 break; 235 236 case 1: // '\001' 237 case 3: // '\003' 238 default: 239 obj = zconstant.getValue(); 240 break; 241 } 242 } else 243 if(zexp instanceof ZExpression) 244 obj = new Double(evalNumericExp(ztuple, (ZExpression)zexp)); 245 return obj; 246 } 247 248 public static void main(String args[]) 249 { 250 try 251 { 252 BufferedReader bufferedreader = new BufferedReader(new FileReader("test.db")); 253 String s = bufferedreader.readLine(); 254 ZTuple ztuple = new ZTuple(s); 255 ZqlParser zqlparser = new ZqlParser(); 256 ZEval zeval = new ZEval(); 257 while((s = bufferedreader.readLine()) != null) 258 { 259 ztuple.setRow(s); 260 BufferedReader bufferedreader1 = new BufferedReader(new FileReader("test.sql")); 261 String s1; 262 while((s1 = bufferedreader1.readLine()) != null) 263 { 264 zqlparser.initParser(new ByteArrayInputStream(s1.getBytes())); 265 ZExp zexp = zqlparser.readExpression(); 266 System.out.print(s + ", " + s1 + ", "); 267 System.out.println(zeval.eval(ztuple, zexp)); 268 } 269 bufferedreader1.close(); 270 } 271 bufferedreader.close(); 272 } 273 catch(Exception exception) 274 { 275 276 } 277 } 278}