001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Emmanuel Cecchet.
020: * Contributor(s): ______________________.
021: */package org.continuent.sequoia.controller.backend.rewriting;
022:
023: import org.continuent.sequoia.common.xml.DatabasesXmlTags;
024:
025: /**
026: * This class defines a AbstractRewritingRule to rewrite SQL requests for a
027: * specific backend.
028: *
029: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
030: * @version 1.0
031: */
032: public abstract class AbstractRewritingRule {
033: protected String queryPattern;
034: protected String rewrite;
035: protected boolean isCaseSensitive;
036: protected boolean stopOnMatch;
037: protected boolean hasMatched;
038:
039: /**
040: * Creates a new <code>AbstractRewritingRule</code> object
041: *
042: * @param queryPattern SQL pattern to match
043: * @param rewrite rewritten SQL query
044: * @param caseSensitive true if matching is case sensitive
045: * @param stopOnMatch true if rewriting must stop after this rule if it
046: * matches.
047: */
048: public AbstractRewritingRule(String queryPattern, String rewrite,
049: boolean caseSensitive, boolean stopOnMatch) {
050: this .queryPattern = queryPattern;
051: this .rewrite = rewrite;
052: this .isCaseSensitive = caseSensitive;
053: this .stopOnMatch = stopOnMatch;
054: this .hasMatched = false;
055: }
056:
057: /**
058: * Returns true if the query given in the last call to rewrite has matched
059: * this rule.
060: * <p>1. call rewrite(query)
061: * <p>2. call hasMatched() to know if query matched this rule.
062: *
063: * @return true if the query matched this rule.
064: * @see #rewrite(String)
065: */
066: public boolean hasMatched() {
067: return hasMatched;
068: }
069:
070: /**
071: * Rewrite the given query according to the rule. Note that this method does
072: * not check if the given query matches the rule or not. You must call
073: * matches(String) before calling this method.
074: *
075: * @param sqlQuery request to rewrite
076: * @return the rewritten SQL query according to the rule.
077: * @see AbstractRewritingRule#hasMatched
078: */
079: public abstract String rewrite(String sqlQuery);
080:
081: /**
082: * Returns the isCaseSensitive value.
083: *
084: * @return Returns the isCaseSensitive.
085: */
086: public boolean isCaseSensitive() {
087: return isCaseSensitive;
088: }
089:
090: /**
091: * Returns the queryPattern value.
092: *
093: * @return Returns the queryPattern.
094: */
095: public String getQueryPattern() {
096: return queryPattern;
097: }
098:
099: /**
100: * Returns the rewrite value.
101: *
102: * @return Returns the rewrite.
103: */
104: public String getRewrite() {
105: return rewrite;
106: }
107:
108: /**
109: * Returns the stopOnMatch value.
110: *
111: * @return Returns the stopOnMatch.
112: */
113: public boolean isStopOnMatch() {
114: return stopOnMatch;
115: }
116:
117: /**
118: * Get xml information about this AbstractRewritingRule.
119: *
120: * @return xml formatted information on this AbstractRewritingRule.
121: */
122: public String getXml() {
123: return "<" + DatabasesXmlTags.ELT_RewritingRule + " "
124: + DatabasesXmlTags.ATT_queryPattern + "=\""
125: + queryPattern + "\" " + DatabasesXmlTags.ATT_rewrite
126: + "=\"" + rewrite + "\" "
127: + DatabasesXmlTags.ATT_caseSensitive + "=\""
128: + isCaseSensitive + "\" "
129: + DatabasesXmlTags.ATT_stopOnMatch + "=\""
130: + stopOnMatch + "\"/>";
131: }
132:
133: }
|