[jbosscache-commits] JBoss Cache SVN: r8192 - core/trunk/src/main/java/org/jboss/cache/loader.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Tue Aug 18 06:44:01 EDT 2009


Author: manik.surtani at jboss.com
Date: 2009-08-18 06:44:01 -0400 (Tue, 18 Aug 2009)
New Revision: 8192

Modified:
   core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoader.java
   core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoaderConfig.java
Log:
[JBCACHE-1534] (JDBCCacheLoader does not escape wildcard characters in generated LIKE clause) Patch submitted by Andrew Duckworth

Modified: core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoader.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoader.java	2009-08-18 10:43:24 UTC (rev 8191)
+++ core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoader.java	2009-08-18 10:44:01 UTC (rev 8192)
@@ -95,6 +95,8 @@
     */
    public Object put(Fqn name, Object key, Object value) throws Exception
    {
+   	  if (getLogger().isTraceEnabled())
+      	getLogger().trace("put name=" + name + " key=" + key + " value=" + value);
       lock.acquireLock(name, true);
       try
       {
@@ -135,6 +137,8 @@
     */
    public void put(Fqn name, Map attributes) throws Exception
    {
+   	  if (getLogger().isTraceEnabled())
+      	getLogger().trace("put name=" + name + " attr=" + attributes);
       lock.acquireLock(name, true);
       Map toStore = attributes;
       if (toStore != null && !(toStore instanceof HashMap)) toStore = new HashMap(attributes);
@@ -311,9 +315,22 @@
 
    private String getFqnWildcardString(String fqnString, Fqn fqn)
    {
-      return fqnString + (fqn.isRoot() ? "" : Fqn.SEPARATOR) + '%';
+      return escapeSqlLikeParameter(fqnString + (fqn.isRoot() ? "" : Fqn.SEPARATOR)) + '%';
    }
 
+   /**
+    * Escape all characters occurring in the FQN string that have significance to SQL within a LIKE clause. Escape character '^'
+    * must match value in JDBCCacheLoaderConfig.constructRecursiveChildrenSql. This method
+    * doubles any occurrence of the escape character '^' and replaces any occurrence of wildcard characters '_' '%' with escaped version e.g. "^_"
+    * 
+    * @param res    the fqn as a String
+    * @return       the escaped result
+    */
+   private String escapeSqlLikeParameter(String res)
+   {
+       return res.replaceAll("([_%^])", "^$1");
+   }
+
    private Map<Object, Object> readAttributes(ResultSet rs, int index) throws SQLException
    {
       Map<Object, Object> result;
@@ -340,6 +357,8 @@
 
    private void addNewSubtree(Fqn name, Map attributes) throws Exception
    {
+      if (getLogger().isTraceEnabled())
+        getLogger().trace("addNewSubtree name=" + name + " attr=" + attributes);
       Fqn currentNode = name;
       do
       {

Modified: core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoaderConfig.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoaderConfig.java	2009-08-18 10:43:24 UTC (rev 8191)
+++ core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoaderConfig.java	2009-08-18 10:44:01 UTC (rev 8192)
@@ -149,12 +149,12 @@
 
    private String constructRecursiveChildrenSql()
    {
-      return "SELECT " + fqnColumn + "," + nodeColumn + " FROM " + table + " WHERE " + fqnColumn + " = ? OR " + fqnColumn + " LIKE ?";
+      return "SELECT " + fqnColumn + "," + nodeColumn + " FROM " + table + " WHERE " + fqnColumn + " = ? OR " + fqnColumn + " LIKE ? ESCAPE '^'";
    }
 
    @Override
    protected String constructDeleteNodeSql()
    {
-      return "DELETE FROM " + table + " WHERE " + fqnColumn + " = ? OR " + fqnColumn + " LIKE ?";
+      return "DELETE FROM " + table + " WHERE " + fqnColumn + " = ? OR " + fqnColumn + " LIKE ? ESCAPE '^'";
    }
 }
\ No newline at end of file



More information about the jbosscache-commits mailing list