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