001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free SoftwareFoundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.db.sql;
031:
032: import com.caucho.log.Log;
033:
034: import java.sql.SQLException;
035: import java.util.ArrayList;
036: import java.util.logging.Level;
037: import java.util.logging.Logger;
038: import java.util.regex.Pattern;
039:
040: class LikeExpr extends Expr {
041: private static final Logger log = Log.open(LikeExpr.class);
042:
043: private Expr _expr;
044: private String _pattern;
045: private Pattern _regexp;
046: private boolean _isNot;
047:
048: LikeExpr(Expr expr, String pattern, boolean isNot) {
049: _expr = expr;
050: _pattern = pattern;
051: _isNot = isNot;
052:
053: StringBuilder sb = new StringBuilder();
054: for (int i = 0; i < pattern.length(); i++) {
055: char ch = pattern.charAt(i);
056:
057: switch (ch) {
058: case '%':
059: sb.append(".*");
060: break;
061: case '_':
062: sb.append(".");
063: break;
064: case '.':
065: case '\\':
066: case '*':
067: case '+':
068: case '(':
069: case ')':
070: case '[':
071: case ']':
072: case '?':
073: case '^':
074: case '$':
075: case '|':
076: sb.append("\\");
077: sb.append(ch);
078: break;
079: default:
080: sb.append(ch);
081: }
082: }
083:
084: try {
085: _regexp = Pattern.compile(sb.toString());
086: } catch (Exception e) {
087: log.log(Level.WARNING, e.toString(), e);
088: }
089: }
090:
091: protected Expr bind(Query query) throws SQLException {
092: _expr = _expr.bind(query);
093:
094: return this ;
095: }
096:
097: /**
098: * Returns the type of the expression.
099: */
100: public Class getType() {
101: return boolean.class;
102: }
103:
104: /**
105: * Returns the cost based on the given FromList.
106: */
107: public long subCost(ArrayList<FromItem> fromList) {
108: return (_expr.subCost(fromList));
109: }
110:
111: /**
112: * Evaluates the expression as a boolean.
113: */
114: public int evalBoolean(QueryContext context) throws SQLException {
115: if (_expr.isNull(context))
116: return UNKNOWN;
117:
118: String value = _expr.evalString(context);
119:
120: if (_regexp.matcher(value).matches())
121: return _isNot ? FALSE : TRUE;
122: else
123: return _isNot ? TRUE : FALSE;
124: }
125:
126: public String evalString(QueryContext context) throws SQLException {
127: throw new SQLException("can't convert string to boolean");
128: }
129:
130: /**
131: * Evaluates aggregate functions during the group phase.
132: *
133: * @param state the current database tuple
134: */
135: public void evalGroup(QueryContext context) throws SQLException {
136: _expr.evalGroup(context);
137: }
138:
139: public String toString() {
140: return "(" + _expr + " LIKE " + _pattern + ")";
141: }
142: }
|