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

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Thu May 15 18:53:18 EDT 2008


Author: galder.zamarreno at jboss.com
Date: 2008-05-15 18:53:17 -0400 (Thu, 15 May 2008)
New Revision: 5859

Modified:
   core/trunk/src/main/java/org/jboss/cache/loader/AdjListJDBCCacheLoaderConfig.java
   core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoader.java
   core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoaderConfig.java
Log:
[JBCACHE-1342] Missing getters and setters added to enable individual property configuration. Variables containing sql sentences are checked for null in which case they're reconstructed enabling both setProperties() method construction and MC style, individual property construction.

Modified: core/trunk/src/main/java/org/jboss/cache/loader/AdjListJDBCCacheLoaderConfig.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/loader/AdjListJDBCCacheLoaderConfig.java	2008-05-15 22:32:10 UTC (rev 5858)
+++ core/trunk/src/main/java/org/jboss/cache/loader/AdjListJDBCCacheLoaderConfig.java	2008-05-15 22:53:17 UTC (rev 5859)
@@ -37,6 +37,10 @@
    private String updateNodeSql;
    private String updateTableSql;
    private String connectionFactoryClass;
+   private String primaryKey;
+   private String fqnType;
+   private String nodeType;
+   private String parentColumn;
    protected String table;
    protected String nodeColumn;
    protected String fqnColumn;
@@ -68,6 +72,10 @@
 
    public String getCreateTableDDL()
    {
+      if (createTableDDL == null)
+      {
+         setCreateTableDDL(constructCreateTableDDL());
+      }
       return createTableDDL;
    }
 
@@ -90,6 +98,10 @@
 
    public String getDeleteAllSql()
    {
+      if (deleteAllSql == null)
+      {
+         setDeleteAllSql(constructDeleteAllSql());
+      }
       return deleteAllSql;
    }
 
@@ -101,6 +113,10 @@
 
    public String getDeleteNodeSql()
    {
+      if (deleteNodeSql == null)
+      {
+         setDeleteNodeSql(constructDeleteNodeSql());
+      }
       return deleteNodeSql;
    }
 
@@ -134,6 +150,10 @@
 
    public String getInsertNodeSql()
    {
+      if (insertNodeSql == null)
+      {
+         setInsertNodeSql(constructInsertNodeSql());
+      }
       return insertNodeSql;
    }
 
@@ -145,6 +165,10 @@
 
    public String getSelectChildFqnsSql()
    {
+      if (selectChildFqnsSql == null)
+      {
+         setSelectChildFqnsSql(constructSelectChildNamesSql());
+      }
       return selectChildFqnsSql;
    }
 
@@ -156,6 +180,10 @@
 
    public String getSelectNodeSql()
    {
+      if (selectNodeSql == null)
+      {
+         setSelectNodeSql(constructSelectNodeSql());
+      }
       return selectNodeSql;
    }
 
@@ -189,6 +217,10 @@
 
    public String getDropTableDDL()
    {
+      if (dropTableDDL == null)
+      {
+         setDropTableDDL(constructDropTableDDL());
+      }
       return dropTableDDL;
    }
 
@@ -200,6 +232,10 @@
 
    public String getSelectChildNamesSql()
    {
+      if (selectChildNamesSql == null)
+      {
+         setSelectChildNamesSql(constructSelectChildNamesSql());
+      }
       return selectChildNamesSql;
    }
 
@@ -211,6 +247,10 @@
 
    public String getUpdateNodeSql()
    {
+      if (updateNodeSql == null)
+      {
+         setUpdateNodeSql(constructUpdateNodeSql());
+      }
       return updateNodeSql;
    }
 
@@ -263,7 +303,73 @@
       testImmutability("connectionFactoryClass");
       this.connectionFactoryClass = connectionFactoryClass;
    }
+   
+   public String getPrimaryKey()
+   {
+      return primaryKey;
+   }
 
+   public void setPrimaryKey(String primaryKey)
+   {
+      testImmutability("primaryKey");
+      this.primaryKey = primaryKey;
+   }
+
+   public String getFqnType()
+   {
+      return fqnType;
+   }
+
+   public void setFqnType(String fqnType)
+   {
+      testImmutability("fqnType");
+      this.fqnType = fqnType;
+   }
+
+   public String getNodeType()
+   {
+      return nodeType;
+   }
+
+   public void setNodeType(String nodeType)
+   {
+      testImmutability("nodeType");
+      this.nodeType = nodeType;
+   }
+
+   public String getParentColumn()
+   {
+      return parentColumn;
+   }
+
+   public void setParentColumn(String parentColumn)
+   {
+      testImmutability("parentColumn");
+      this.parentColumn = parentColumn;
+   }
+
+   public String getNodeColumn()
+   {
+      return nodeColumn;
+   }
+
+   public void setNodeColumn(String nodeColumn)
+   {
+      testImmutability("nodeColumn");
+      this.nodeColumn = nodeColumn;
+   }
+
+   public String getFqnColumn()
+   {
+      return fqnColumn;
+   }
+
+   public void setFqnColumn(String fqnColumn)
+   {
+      testImmutability("fqnColumn");
+      this.fqnColumn = fqnColumn;
+   }
+
    @Override
    public void setProperties(Properties props)
    {
@@ -297,30 +403,36 @@
       this.dropTable = prop == null ? false : Boolean.valueOf(prop);
 
       this.table = props.getProperty("cache.jdbc.table.name", "jbosscache");
-      String primaryKey = props.getProperty("cache.jdbc.table.primarykey", "jbosscache_pk");
+      primaryKey = props.getProperty("cache.jdbc.table.primarykey", "jbosscache_pk");
       fqnColumn = props.getProperty("cache.jdbc.fqn.column", "fqn");
-      String fqnType = props.getProperty("cache.jdbc.fqn.type", "varchar(255)");
+      fqnType = props.getProperty("cache.jdbc.fqn.type", "varchar(255)");
       nodeColumn = props.getProperty("cache.jdbc.node.column", "node");
-      String nodeType = props.getProperty("cache.jdbc.node.type", "blob");
-      String parentColumn = props.getProperty("cache.jdbc.parent.column", "parent");
+      nodeType = props.getProperty("cache.jdbc.node.type", "blob");
+      parentColumn = props.getProperty("cache.jdbc.parent.column", "parent");
 
-      selectChildNamesSql = "select " + fqnColumn + " from " + table + " where " + parentColumn + "=?";
-      deleteNodeSql = "delete from " + table + " where " + fqnColumn + "=?";
-      deleteAllSql = "delete from " + table;
-      selectChildFqnsSql = "select " + fqnColumn + " from " + table + " where " + parentColumn + "=?";
-      insertNodeSql = "insert into " +
-            table +
-            " (" +
-            fqnColumn +
-            ", " +
-            nodeColumn +
-            ", " +
-            parentColumn +
-            ") values (?, ?, ?)";
-      updateNodeSql = "update " + table + " set " + nodeColumn + "=? where " + fqnColumn + "=?";
-      selectNodeSql = "select " + nodeColumn + " from " + table + " where " + fqnColumn + "=?";
+      selectChildNamesSql = constructSelectChildNamesSql();
+      deleteNodeSql = constructDeleteNodeSql();
+      deleteAllSql = constructDeleteAllSql();
+      /* select child fqns and select child names sql is the same */
+      selectChildFqnsSql = constructSelectChildNamesSql();
+      insertNodeSql = constructInsertNodeSql();
+      updateNodeSql = constructUpdateNodeSql();
+      selectNodeSql = constructSelectNodeSql();
 
-      createTableDDL = "create table " +
+      createTableDDL = constructCreateTableDDL();
+
+      dropTableDDL = constructDropTableDDL();
+      connectionFactoryClass = props.getProperty("cache.jdbc.connection.factory", "org.jboss.cache.loader.NonManagedConnectionFactory");
+   }
+
+   private String constructDropTableDDL()
+   {
+      return "drop table " + table;
+   }
+
+   private String constructCreateTableDDL()
+   {
+      return "create table " +
             table +
             "(" +
             fqnColumn +
@@ -335,9 +447,6 @@
             " " +
             fqnType +
             ", constraint " + primaryKey + " primary key (" + fqnColumn + "))";
-
-      dropTableDDL = "drop table " + table;
-      connectionFactoryClass = props.getProperty("cache.jdbc.connection.factory", "org.jboss.cache.loader.NonManagedConnectionFactory");
    }
 
    @Override
@@ -365,7 +474,11 @@
                && Util.safeEquals(table, other.table)
                && Util.safeEquals(updateNodeSql, other.updateNodeSql)
                && Util.safeEquals(updateTableSql, other.updateTableSql)
-               && Util.safeEquals(connectionFactoryClass, other.connectionFactoryClass);
+               && Util.safeEquals(connectionFactoryClass, other.connectionFactoryClass)
+               && Util.safeEquals(primaryKey, other.primaryKey)
+               && Util.safeEquals(nodeType, other.nodeType)
+               && Util.safeEquals(fqnType, other.fqnType)
+               && Util.safeEquals(parentColumn, other.parentColumn);
       }
 
       return false;
@@ -394,6 +507,10 @@
       result = 31 * result + (updateNodeSql == null ? 0 : updateNodeSql.hashCode());
       result = 31 * result + (updateTableSql == null ? 0 : updateTableSql.hashCode());
       result = 31 * result + (connectionFactoryClass == null ? 0 : connectionFactoryClass.hashCode());
+      result = 31 * result + (primaryKey == null ? 0 : primaryKey.hashCode());
+      result = 31 * result + (nodeType == null ? 0 : nodeType.hashCode());
+      result = 31 * result + (fqnType == null ? 0 : fqnType.hashCode());
+      result = 31 * result + (parentColumn == null ? 0 : parentColumn.hashCode());
 
       return result;
    }
@@ -404,4 +521,41 @@
       return (AdjListJDBCCacheLoaderConfig) super.clone();
    }
 
+   private String constructSelectNodeSql()
+   {
+      return "select " + nodeColumn + " from " + table + " where " + fqnColumn + "=?";
+   }
+
+   private String constructUpdateNodeSql()
+   {
+      return "update " + table + " set " + nodeColumn + "=? where " + fqnColumn + "=?";
+   }
+
+   private String constructDeleteAllSql()
+   {
+      return "delete from " + table;
+   }
+
+   private String constructDeleteNodeSql()
+   {
+      return "delete from " + table + " where " + fqnColumn + "=?";
+   }
+
+   private String constructSelectChildNamesSql()
+   {
+      return "select " + fqnColumn + " from " + table + " where " + parentColumn + "=?";
+   }
+
+   private String constructInsertNodeSql()
+   {
+      return "insert into " +
+            table +
+            " (" +
+            fqnColumn +
+            ", " +
+            nodeColumn +
+            ", " +
+            parentColumn +
+            ") values (?, ?, ?)";
+   }   
 }
\ No newline at end of file

Modified: core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoader.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoader.java	2008-05-15 22:32:10 UTC (rev 5858)
+++ core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoader.java	2008-05-15 22:53:17 UTC (rev 5859)
@@ -267,6 +267,11 @@
       ResultSet rs = null;
       try
       {
+         if (getLogger().isDebugEnabled())
+         {
+            getLogger().debug("executing sql: " + config.getNodeCountSql());
+         }
+
          conn = cf.getConnection();
          ps = conn.prepareStatement(config.getNodeCountSql());
          rs = ps.executeQuery();

Modified: core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoaderConfig.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoaderConfig.java	2008-05-15 22:32:10 UTC (rev 5858)
+++ core/trunk/src/main/java/org/jboss/cache/loader/JDBCCacheLoaderConfig.java	2008-05-15 22:53:17 UTC (rev 5859)
@@ -10,6 +10,7 @@
  * Builds the different SQLs needed by <tt>JDBCCacheLoader</tt>.
  *
  * @author Mircea.Markus at iquestint.com
+ * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
  * @version 1.0
  */
 public class JDBCCacheLoaderConfig extends AdjListJDBCCacheLoaderConfig
@@ -19,10 +20,13 @@
 
    private static final Log log = LogFactory.getLog(JDBCCacheLoaderConfig.class);
 
-   private String deleteNode;
-   private String recursiveChildren;
+   private String deleteNodeSql;
+   private String recursiveChildrenSql;
    private String nodeCountSql;
+   private String sqlConcat;
 
+   private String startingWith;
+   private String appendSepparator;
 
    public JDBCCacheLoaderConfig(IndividualCacheLoaderConfig base)
    {
@@ -39,27 +43,28 @@
    public void setProperties(Properties props)
    {
       super.setProperties(props);
-      String sqlConcat = props.getProperty("cache.jdbc.sql-concat");
-      if (sqlConcat == null)
-      {
-         log.info("Missiing JDBCCacheLoader config 'cache.jdbc.sql-concat', using default value:'concat(1,2)'");
-         sqlConcat = "concat(1,2)";
-      }
-      String startingWith = sqlConcat.replace('1', '?').replace("2", "'%'"); //concat(?, '%')
-      String appendSepparator = sqlConcat.replace("1", fqnColumn).replace("2", "'/'"); //concat(fqnColumn, '/')
-      deleteNode = "delete from " + table + " where " + appendSepparator + " like " + startingWith;
-      recursiveChildren = "select " + fqnColumn + "," + nodeColumn + " from " + table + " where " + appendSepparator + " like " + startingWith;
-      nodeCountSql = "select count(*) from " + table;
+      sqlConcat = props.getProperty("cache.jdbc.sql-concat");
+
+      disectSqlConcat();
+      
+      deleteNodeSql = constructDeleteNodeSql();
+      recursiveChildrenSql = constructRecursiveChildrenSql();
+      nodeCountSql = constructNodeCountSql();
    }
 
-
    /**
     * Returns the sql string for removing a node and all its children.
     */
    @Override
    public String getDeleteNodeSql()
    {
-      return deleteNode;
+      if (startingWith == null || appendSepparator == null || deleteNodeSql == null)
+      {
+         disectSqlConcat();
+         setDeleteNodeSql(constructDeleteNodeSql());
+      }
+
+      return deleteNodeSql;
    }
 
    /**
@@ -67,15 +72,93 @@
     */
    public String getRecursiveChildrenSql()
    {
-      return recursiveChildren;
+      if (startingWith == null || appendSepparator == null || recursiveChildrenSql == null)
+      {
+         disectSqlConcat();
+         setRecursiveChildrenSql(constructRecursiveChildrenSql());
+      }
+
+      return recursiveChildrenSql;
    }
 
+   public void setRecursiveChildrenSql(String recursiveChildrenSql)
+   {
+      this.recursiveChildrenSql = recursiveChildrenSql;
+   }
 
    /**
     * Returns an sql that will count all the persisted node.
     */
    public String getNodeCountSql()
    {
+      if (startingWith == null || appendSepparator == null || nodeCountSql == null)
+      {
+         disectSqlConcat();
+         setNodeCountSql(constructNodeCountSql());
+      }
+
       return nodeCountSql;
    }
-}
+
+   public void setNodeCountSql(String nodeCountSql)
+   {
+      this.nodeCountSql = nodeCountSql;
+   }
+
+   public String getSqlConcat()
+   {
+      return sqlConcat;
+   }
+
+   public void setSqlConcat(String sqlConcat)
+   {
+      testImmutability("sqlConcat");
+      this.sqlConcat = sqlConcat;
+   }
+
+   public String getStartingWith()
+   {
+      return startingWith;
+   }
+
+   public void setStartingWith(String startingWith)
+   {
+      this.startingWith = startingWith;
+   }
+
+   public String getAppendSepparator()
+   {
+      return appendSepparator;
+   }
+
+   public void setAppendSepparator(String appendSepparator)
+   {
+      this.appendSepparator = appendSepparator;
+   }
+
+   private void disectSqlConcat()
+   {
+      if (sqlConcat == null)
+      {
+         log.info("Missiing JDBCCacheLoader config 'cache.jdbc.sql-concat', using default value:'concat(1,2)'");
+         sqlConcat = "concat(1,2)";
+      }
+      startingWith = sqlConcat.replace('1', '?').replace("2", "'%'"); //concat(?, '%')
+      appendSepparator = sqlConcat.replace("1", fqnColumn).replace("2", "'/'"); //concat(fqnColumn, '/')      
+   }
+
+   private String constructNodeCountSql()
+   {
+      return "select count(*) from " + table;
+   }
+
+   private String constructRecursiveChildrenSql()
+   {
+      return "select " + fqnColumn + "," + nodeColumn + " from " + table + " where " + appendSepparator + " like " + startingWith;
+   }
+
+   private String constructDeleteNodeSql()
+   {
+      return "delete from " + table + " where " + appendSepparator + " like " + startingWith;
+   }   
+}
\ No newline at end of file




More information about the jbosscache-commits mailing list