You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tv...@apache.org on 2010/10/31 18:07:46 UTC

svn commit: r1029419 - /db/torque/village/trunk/src/java/com/workingdogs/village/Schema.java

Author: tv
Date: Sun Oct 31 17:07:46 2010
New Revision: 1029419

URL: http://svn.apache.org/viewvc?rev=1029419&view=rev
Log:
Work around TORQUE-36
Use cached metadata when running selects. Metdata can be loaded into the cache with Schema.initSchemas(Connection)

Modified:
    db/torque/village/trunk/src/java/com/workingdogs/village/Schema.java

Modified: db/torque/village/trunk/src/java/com/workingdogs/village/Schema.java
URL: http://svn.apache.org/viewvc/db/torque/village/trunk/src/java/com/workingdogs/village/Schema.java?rev=1029419&r1=1029418&r2=1029419&view=diff
==============================================================================
--- db/torque/village/trunk/src/java/com/workingdogs/village/Schema.java (original)
+++ db/torque/village/trunk/src/java/com/workingdogs/village/Schema.java Sun Oct 31 17:07:46 2010
@@ -22,11 +22,16 @@ package com.workingdogs.village;
 import java.io.ByteArrayOutputStream;
 import java.io.PrintWriter;
 import java.sql.Connection;
+import java.sql.DatabaseMetaData;
 import java.sql.PreparedStatement;
+import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
 
 /**
  * The Schema object represents the <a href="Column.html">Columns</a> in a database table. It contains a collection of <a
@@ -74,6 +79,62 @@ public final class Schema
     }
 
     /**
+     * Initialize all table schemas reachable from this connection
+     * 
+     * @param conn a database connection
+     * @throws SQLException if retrieving the database meta data is unsuccessful
+     */
+    public static void initSchemas(Connection conn) throws SQLException
+    {
+        ResultSet allCol = null;
+        
+        try
+        {
+            DatabaseMetaData databaseMetaData  = conn.getMetaData();
+            String connURL = databaseMetaData.getURL();
+            allCol = databaseMetaData.getColumns(
+                    conn.getCatalog(), null, null, null);
+
+            while(true)
+            {
+                Schema schema = new Schema();
+                
+                schema.setAttributes("*");
+                schema.singleTable = true;
+                schema.populate(allCol);
+
+                if (schema.numberOfColumns > 0)
+                {
+                    String keyValue = connURL + schema.tableName;
+                    
+                    synchronized (schemaCache)
+                    {
+                        schemaCache.put(keyValue, schema);
+                    }
+                }
+                else
+                {
+                    break;
+                }
+            }
+        }
+        finally
+        {
+            if (allCol != null)
+            {
+                try
+                {
+                    allCol.close();
+                }
+                catch (SQLException e)
+                {
+                    //Do nothing
+                }               
+            }
+        }
+    }
+    
+    /**
      * Creates a Schema with all columns
      *
      * @param conn
@@ -134,7 +195,7 @@ public final class Schema
 	            	// query is never executed on the server - only prepared
 	                if (stmt != null)
 	                {
-	                    tableSchema = new Schema();
+	                    tableSchema = this;
 	                    tableSchema.setTableName(tableName);
 	                    tableSchema.setAttributes(columnsAttribute);
 	                    tableSchema.populate(stmt.getMetaData(), tableName, null);
@@ -532,6 +593,61 @@ public final class Schema
     }
 
     /**
+     * Internal method which populates this Schema object with Columns.
+     *
+     * @param meta The meta data of the database connection used to build this Schema.
+     *
+     * @exception SQLException
+     */
+    void populate(ResultSet dbMeta)
+            throws SQLException
+    {
+        List cols = new ArrayList();
+        String tableName = null;
+        columnNumberByName = new Hashtable();
+        
+        while (dbMeta.next())
+        {
+            if (tableName == null)
+            {
+                tableName = dbMeta.getString(3); // table name
+                setTableName(tableName);
+            }
+            else if (!tableName.equals(dbMeta.getString(3))) // not same table name
+            {
+                dbMeta.previous(); // reset result set pointer
+                break;
+            }
+            
+            Column c = new Column();
+            
+            c.populate(tableName,
+                dbMeta.getString(4), // column name
+                dbMeta.getString(6), // Data source dependent type name
+                dbMeta.getInt(5), // SQL type from java.sql.Types
+                dbMeta.getInt(11) == DatabaseMetaData.columnNullable); // is NULL allowed.
+            
+            cols.add(c);
+            
+            Integer position = new Integer(dbMeta.getInt(17)); // ordinal number
+            columnNumberByName.put(c.name(), position);
+            columnNumberByName.put(tableName + "." + c.name(), position);
+        }
+        
+        if (!cols.isEmpty())
+        {
+            this.numberOfColumns = cols.size();
+            columns = new Column[numberOfColumns() + 1];
+            
+            int i = 1;
+            for (Iterator col = cols.iterator(); col.hasNext();)
+            {
+                columns[i++] = (Column)col.next();
+            }
+        }
+    }
+
+    /**
      * Sets the columns to select from the table
      *
      * @param attributes comma separated list of column names



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org