00001 /** 00002 * C-JDBC: Clustered JDBC. 00003 * Copyright (C) 2002-2004 French National Institute For Research In Computer 00004 * Science And Control (INRIA). 00005 * Contact: c-jdbc@objectweb.org 00006 * 00007 * This library is free software; you can redistribute it and/or modify it 00008 * under the terms of the GNU Lesser General Public License as published by the 00009 * Free Software Foundation; either version 2.1 of the License, or any later 00010 * version. 00011 * 00012 * This library is distributed in the hope that it will be useful, but WITHOUT 00013 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00014 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 00015 * for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public License 00018 * along with this library; if not, write to the Free Software Foundation, 00019 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00020 * 00021 * Initial developer(s): Emmanuel Cecchet. 00022 * Contributor(s): ______________________. 00023 */ 00024 00025 package org.objectweb.cjdbc.controller.backend.rewriting; 00026 00027 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 00028 00029 /** 00030 * This class defines a AbstractRewritingRule to rewrite SQL requests for a 00031 * specific backend. 00032 * 00033 * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a> 00034 * @version 1.0 00035 */ 00036 public abstract class AbstractRewritingRule 00037 { 00038 protected String queryPattern; 00039 protected String rewrite; 00040 protected boolean isCaseSensitive; 00041 protected boolean stopOnMatch; 00042 protected boolean hasMatched; 00043 00044 /** 00045 * Creates a new <code>AbstractRewritingRule</code> object 00046 * 00047 * @param queryPattern SQL pattern to match 00048 * @param rewrite rewritten SQL query 00049 * @param caseSensitive true if matching is case sensitive 00050 * @param stopOnMatch true if rewriting must stop after this rule if it 00051 * matches. 00052 */ 00053 public AbstractRewritingRule(String queryPattern, String rewrite, 00054 boolean caseSensitive, boolean stopOnMatch) 00055 { 00056 this.queryPattern = queryPattern; 00057 this.rewrite = rewrite; 00058 this.isCaseSensitive = caseSensitive; 00059 this.stopOnMatch = stopOnMatch; 00060 this.hasMatched = false; 00061 } 00062 00063 /** 00064 * Returns true if the query given in the last call to rewrite has matched 00065 * this rule. 00066 * <p>1. call rewrite(query) 00067 * <p>2. call hasMatched() to know if query matched this rule. 00068 * 00069 * @return true if the query matched this rule. 00070 * @see #rewrite(String) 00071 */ 00072 public boolean hasMatched() 00073 { 00074 return hasMatched; 00075 } 00076 00077 /** 00078 * Rewrite the given query according to the rule. Note that this method does 00079 * not check if the given query matches the rule or not. You must call 00080 * matches(String) before calling this method. 00081 * 00082 * @param sqlQuery request to rewrite 00083 * @return the rewritten SQL query according to the rule. 00084 * @see AbstractRewritingRule#hasMatched 00085 */ 00086 public abstract String rewrite(String sqlQuery); 00087 00088 /** 00089 * Returns the isCaseSensitive value. 00090 * 00091 * @return Returns the isCaseSensitive. 00092 */ 00093 public boolean isCaseSensitive() 00094 { 00095 return isCaseSensitive; 00096 } 00097 00098 /** 00099 * Returns the queryPattern value. 00100 * 00101 * @return Returns the queryPattern. 00102 */ 00103 public String getQueryPattern() 00104 { 00105 return queryPattern; 00106 } 00107 00108 /** 00109 * Returns the rewrite value. 00110 * 00111 * @return Returns the rewrite. 00112 */ 00113 public String getRewrite() 00114 { 00115 return rewrite; 00116 } 00117 00118 /** 00119 * Returns the stopOnMatch value. 00120 * 00121 * @return Returns the stopOnMatch. 00122 */ 00123 public boolean isStopOnMatch() 00124 { 00125 return stopOnMatch; 00126 } 00127 00128 /** 00129 * Get xml information about this AbstractRewritingRule. 00130 * 00131 * @return xml formatted information on this AbstractRewritingRule. 00132 */ 00133 public String getXml() 00134 { 00135 return "<" + DatabasesXmlTags.ELT_RewritingRule + " " 00136 + DatabasesXmlTags.ATT_queryPattern + "=\"" + queryPattern + "\" " 00137 + DatabasesXmlTags.ATT_rewrite + "=\"" + rewrite + "\" " 00138 + DatabasesXmlTags.ATT_caseSensitive + "=\"" + isCaseSensitive + "\" " 00139 + DatabasesXmlTags.ATT_stopOnMatch + "=\"" + stopOnMatch + "\"/>"; 00140 } 00141 00142 }