KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > ql > LikeExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.ql;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.util.CharBuffer;
33
34 /**
35  * A like expression
36  */

37 class LikeExpr extends Expr {
38   // value expression
39
private Expr _value;
40   // match pattern
41
private Expr _pattern;
42   // escape string
43
private String JavaDoc _escape;
44   // true if this is a negative between
45
private boolean _isNot;
46
47   /**
48    * Creates a like expression.
49    *
50    * @param value the value expression
51    * @param pattern the matching pattern
52    * @param escape the escape pattern
53    * @param isNot if true, this the like is negated
54    */

55   LikeExpr(Expr value, Expr pattern, String JavaDoc escape, boolean isNot)
56     throws ConfigException
57   {
58     _value = value;
59     _pattern = pattern;
60     _escape = escape;
61     _isNot = isNot;
62
63     evalTypes();
64   }
65
66   /**
67    * Evaluates the types for the expression
68    */

69   void evalTypes()
70     throws ConfigException
71   {
72     if (getJavaType() != null)
73       return;
74
75     if (! _value.isString())
76       throw error(L.l("LIKE value `{0}' must be a string expression",
77                       _value));
78
79     if (! _pattern.isString())
80       throw error(L.l("LIKE pattern `{0}' must be a string expression.",
81                       _pattern));
82
83     setJavaType(boolean.class);
84   }
85   
86
87   /**
88    * Prints the where SQL for this expression
89    *
90    * @param gen the java code generator
91    */

92   void generateWhere(CharBuffer cb)
93   {
94     _value.generateWhereSubExpr(cb);
95
96     if (_isNot)
97       cb.append(" NOT LIKE ");
98     else
99       cb.append(" LIKE ");
100
101     _pattern.generateWhereSubExpr(cb);
102
103     if (_escape != null) {
104       cb.append(" ESCAPE ");
105       cb.append(_escape);
106     }
107   }
108
109   public String JavaDoc toString()
110   {
111     String JavaDoc not = _isNot ? "NOT " : "";
112
113     if (_escape != null)
114       return not + _pattern + " ESCAPE " + _escape;
115     else
116       return not + _pattern;
117   }
118 }
119
Popular Tags