01: /**
02: * Sequoia: Database clustering technology.
03: * Copyright (C) 2002-2004 French National Institute For Research In Computer
04: * Science And Control (INRIA).
05: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
06: * Contact: sequoia@continuent.org
07: *
08: * Licensed under the Apache License, Version 2.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: * Initial developer(s): Emmanuel Cecchet.
21: * Contributor(s): ______________________.
22: */package org.continuent.sequoia.controller.backend.rewriting;
23:
24: /**
25: * This class defines a SimpleRewritingRule
26: *
27: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
28: * @version 1.0
29: */
30: public class SimpleRewritingRule extends AbstractRewritingRule {
31:
32: private int queryPatternLength;
33:
34: /**
35: * Creates a new <code>SimpleRewritingRule.java</code> object
36: *
37: * @param queryPattern SQL pattern to match
38: * @param rewrite rewritten SQL query
39: * @param caseSensitive true if matching is case sensitive
40: * @param stopOnMatch true if rewriting must stop after this rule if it
41: * matches.
42: */
43: public SimpleRewritingRule(String queryPattern, String rewrite,
44: boolean caseSensitive, boolean stopOnMatch) {
45: super (queryPattern, caseSensitive ? rewrite : rewrite
46: .toLowerCase(), caseSensitive, stopOnMatch);
47: queryPatternLength = queryPattern.length();
48: }
49:
50: /**
51: * @see org.continuent.sequoia.controller.backend.rewriting.AbstractRewritingRule#rewrite(java.lang.String)
52: */
53: public String rewrite(String sqlQuery) {
54: // Check first if it is a match
55: int start;
56: if (isCaseSensitive)
57: start = sqlQuery.indexOf(queryPattern);
58: else
59: start = sqlQuery.toLowerCase().indexOf(
60: queryPattern.toLowerCase());
61: if (start == -1) { // No match
62: hasMatched = false;
63: return sqlQuery;
64: }
65: // Match, rewrite the query
66: hasMatched = true;
67: if (start == 0) {
68: if (queryPatternLength < sqlQuery.length())
69: // Match at the beginning of the pattern
70: return rewrite + sqlQuery.substring(queryPatternLength);
71: else
72: // The query was exactly the pattern
73: return rewrite;
74: } else {
75: if (start + queryPatternLength < sqlQuery.length())
76: return sqlQuery.substring(0, start)
77: + rewrite
78: + sqlQuery
79: .substring(start + queryPatternLength);
80: else
81: // Match at the end of the pattern
82: return sqlQuery.substring(0, start) + rewrite;
83: }
84: }
85:
86: }
|