00001 /** 00002 * C-JDBC: Clustered JDBC. 00003 * Copyright (C) 2002-2005 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 /** 00028 * This class defines a SimpleRewritingRule 00029 * 00030 * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a> 00031 * @version 1.0 00032 */ 00033 public class SimpleRewritingRule extends AbstractRewritingRule 00034 { 00035 00036 private int queryPatternLength; 00037 00038 /** 00039 * Creates a new <code>SimpleRewritingRule.java</code> object 00040 * 00041 * @param queryPattern SQL pattern to match 00042 * @param rewrite rewritten SQL query 00043 * @param caseSensitive true if matching is case sensitive 00044 * @param stopOnMatch true if rewriting must stop after this rule if it 00045 * matches. 00046 */ 00047 public SimpleRewritingRule(String queryPattern, String rewrite, 00048 boolean caseSensitive, boolean stopOnMatch) 00049 { 00050 super(queryPattern, caseSensitive ? rewrite : rewrite.toLowerCase(), 00051 caseSensitive, stopOnMatch); 00052 queryPatternLength = queryPattern.length(); 00053 } 00054 00055 /** 00056 * @see org.objectweb.cjdbc.controller.backend.rewriting.AbstractRewritingRule#rewrite(java.lang.String) 00057 */ 00058 public String rewrite(String sqlQuery) 00059 { 00060 // Check first if it is a match 00061 int start; 00062 if (isCaseSensitive) 00063 start = sqlQuery.indexOf(queryPattern); 00064 else 00065 start = sqlQuery.toLowerCase().indexOf(queryPattern.toLowerCase()); 00066 if (start == -1) 00067 { // No match 00068 hasMatched = false; 00069 return sqlQuery; 00070 } 00071 // Match, rewrite the query 00072 hasMatched = true; 00073 if (start == 0) 00074 { 00075 if (queryPatternLength < sqlQuery.length()) 00076 // Match at the beginning of the pattern 00077 return rewrite + sqlQuery.substring(queryPatternLength); 00078 else 00079 // The query was exactly the pattern 00080 return rewrite; 00081 } 00082 else 00083 { 00084 if (start + queryPatternLength < sqlQuery.length()) 00085 return sqlQuery.substring(0, start) + rewrite 00086 + sqlQuery.substring(start + queryPatternLength); 00087 else 00088 // Match at the end of the pattern 00089 return sqlQuery.substring(0, start) + rewrite; 00090 } 00091 } 00092 00093 }