KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > sql > 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  *
23  * Free SoftwareFoundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.db.sql;
31
32 import com.caucho.log.Log;
33
34 import java.sql.SQLException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38 import java.util.regex.Pattern JavaDoc;
39
40 class LikeExpr extends Expr {
41   private static final Logger JavaDoc log = Log.open(LikeExpr.class);
42
43   private Expr _expr;
44   private String JavaDoc _pattern;
45   private Pattern JavaDoc _regexp;
46   private boolean _isNot;
47
48   LikeExpr(Expr expr, String JavaDoc pattern, boolean isNot)
49   {
50     _expr = expr;
51     _pattern = pattern;
52     _isNot = isNot;
53
54     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
55     for (int i = 0; i < pattern.length(); i++) {
56       char ch = pattern.charAt(i);
57
58       switch (ch) {
59       case '%':
60     sb.append(".*");
61     break;
62       case '_':
63     sb.append(".");
64     break;
65       case '.': case '\\': case '*': case '+': case '(': case ')':
66       case '[': case ']': case '?': case '^': case '$': case '|':
67     sb.append("\\");
68     sb.append(ch);
69     break;
70       default:
71     sb.append(ch);
72       }
73     }
74
75     try {
76       _regexp = Pattern.compile(sb.toString());
77     } catch (Exception JavaDoc e) {
78       log.log(Level.WARNING, e.toString(), e);
79     }
80   }
81
82   protected Expr bind(Query query)
83     throws SQLException JavaDoc
84   {
85     _expr = _expr.bind(query);
86
87     return this;
88   }
89
90   /**
91    * Returns the type of the expression.
92    */

93   public Class JavaDoc getType()
94   {
95     return boolean.class;
96   }
97
98   /**
99    * Returns the cost based on the given FromList.
100    */

101   public long subCost(ArrayList JavaDoc<FromItem> fromList)
102   {
103     return (_expr.subCost(fromList));
104   }
105
106   /**
107    * Evaluates the expression as a boolean.
108    */

109   public int evalBoolean(QueryContext context)
110     throws SQLException JavaDoc
111   {
112     if (_expr.isNull(context))
113       return UNKNOWN;
114
115     String JavaDoc value = _expr.evalString(context);
116
117     if (_regexp.matcher(value).matches())
118       return _isNot ? FALSE : TRUE;
119     else
120       return _isNot ? TRUE : FALSE;
121   }
122
123   public String JavaDoc evalString(QueryContext context)
124     throws SQLException JavaDoc
125   {
126     throw new SQLException JavaDoc("can't convert string to boolean");
127   }
128
129   /**
130    * Evaluates aggregate functions during the group phase.
131    *
132    * @param state the current database tuple
133    */

134   public void evalGroup(QueryContext context)
135     throws SQLException JavaDoc
136   {
137     _expr.evalGroup(context);
138   }
139
140   public String JavaDoc toString()
141   {
142     return "(" + _expr + " LIKE " + _pattern + ")";
143   }
144 }
145
Popular Tags