cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r654880 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne: access/jdbc/RowDescriptorBuilder.java access/jdbc/SQLTemplateAction.java dba/oracle/OracleSQLTemplateAction.java
Date Fri, 09 May 2008 16:44:26 GMT
Author: aadamchik
Date: Fri May  9 09:44:26 2008
New Revision: 654880

URL: http://svn.apache.org/viewvc?rev=654880&view=rev
Log:
further improving Java type guessing algorithm for Oracle used in SQLTemplates

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/RowDescriptorBuilder.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/oracle/OracleSQLTemplateAction.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/RowDescriptorBuilder.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/RowDescriptorBuilder.java?rev=654880&r1=654879&r2=654880&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/RowDescriptorBuilder.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/RowDescriptorBuilder.java
Fri May  9 09:44:26 2008
@@ -189,11 +189,16 @@
     }
 
     public RowDescriptorBuilder overrideColumnType(String columnName, String type) {
+
         if (typeOverrides == null) {
             typeOverrides = new HashMap<String, String>();
         }
-
+        
         typeOverrides.put(columnName, type);
         return this;
     }
+    
+    public boolean isOverriden(String columnName) {
+        return typeOverrides != null && typeOverrides.containsKey(columnName);
+    }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java?rev=654880&r1=654879&r2=654880&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
Fri May  9 09:44:26 2008
@@ -218,6 +218,40 @@
         boolean iteratedResult = callback.isIteratedResult();
 
         ExtendedTypeMap types = adapter.getExtendedTypes();
+        RowDescriptorBuilder builder = configureRowDescriptorBuilder(compiled, resultSet);
+
+        JDBCResultIterator result = new JDBCResultIterator(
+                connection,
+                statement,
+                resultSet,
+                builder.getDescriptor(types),
+                query.getFetchLimit());
+
+        if (!iteratedResult) {
+            List<DataRow> resultRows = result.dataRows(false);
+            QueryLogger.logSelectCount(resultRows.size(), System.currentTimeMillis()
+                    - startTime);
+
+            callback.nextDataRows(query, resultRows);
+        }
+        else {
+            try {
+                result.setClosingConnection(true);
+                callback.nextDataRows(query, result);
+            }
+            catch (Exception ex) {
+                result.close();
+                throw ex;
+            }
+        }
+    }
+
+    /**
+     * @since 3.0
+     */
+    protected RowDescriptorBuilder configureRowDescriptorBuilder(
+            SQLStatement compiled,
+            ResultSet resultSet) throws SQLException {
         RowDescriptorBuilder builder = new RowDescriptorBuilder();
 
         // SQLTemplate #result columns take precedence over other ways to determine the
@@ -238,9 +272,13 @@
                     }
 
                     builder.overrideColumnType(column, attribute.getType());
+
+                    // note that some DB's (Oracle) also add default JDBC overrides for
+                    // DbAttributes that have no ObjAttributes (very common for PK's). If
+                    // we find that there is more than one such DB, we can make that a
+                    // default policy.
                 }
             }
-
         }
 
         if (query.getColumnNamesCapitalization() != null) {
@@ -254,30 +292,7 @@
             }
         }
 
-        JDBCResultIterator result = new JDBCResultIterator(
-                connection,
-                statement,
-                resultSet,
-                builder.getDescriptor(types),
-                query.getFetchLimit());
-
-        if (!iteratedResult) {
-            List<DataRow> resultRows = result.dataRows(false);
-            QueryLogger.logSelectCount(resultRows.size(), System.currentTimeMillis()
-                    - startTime);
-
-            callback.nextDataRows(query, resultRows);
-        }
-        else {
-            try {
-                result.setClosingConnection(true);
-                callback.nextDataRows(query, result);
-            }
-            catch (Exception ex) {
-                result.close();
-                throw ex;
-            }
-        }
+        return builder;
     }
 
     /**

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/oracle/OracleSQLTemplateAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/oracle/OracleSQLTemplateAction.java?rev=654880&r1=654879&r2=654880&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/oracle/OracleSQLTemplateAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/oracle/OracleSQLTemplateAction.java
Fri May  9 09:44:26 2008
@@ -40,9 +40,13 @@
 import java.util.Map;
 
 import org.apache.cayenne.access.OperationObserver;
+import org.apache.cayenne.access.jdbc.RowDescriptorBuilder;
 import org.apache.cayenne.access.jdbc.SQLStatement;
 import org.apache.cayenne.access.jdbc.SQLTemplateAction;
 import org.apache.cayenne.dba.DbAdapter;
+import org.apache.cayenne.dba.TypesMapping;
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.EntityResolver;
 import org.apache.cayenne.query.SQLTemplate;
 
@@ -52,8 +56,12 @@
  */
 class OracleSQLTemplateAction extends SQLTemplateAction {
 
-    OracleSQLTemplateAction(SQLTemplate query, DbAdapter adapter, EntityResolver resolver)
{
-        super(query, adapter, resolver);
+    protected DbEntity dbEntity;
+
+    OracleSQLTemplateAction(SQLTemplate query, DbAdapter adapter,
+            EntityResolver entityResolver) {
+        super(query, adapter, entityResolver);
+        this.dbEntity = query.getMetaData(entityResolver).getDbEntity();
     }
 
     @Override
@@ -81,6 +89,36 @@
                 startTime);
     }
 
+    /**
+     * @since 3.0
+     */
+    @Override
+    protected RowDescriptorBuilder configureRowDescriptorBuilder(
+            SQLStatement compiled,
+            ResultSet resultSet) throws SQLException {
+
+        RowDescriptorBuilder builder = super.configureRowDescriptorBuilder(
+                compiled,
+                resultSet);
+
+        // override numeric Java types based on JDBC defaults for DbAttributes, as Oracle
+        // ResultSetMetadata is not very precise about NUMERIC distinctions...
+        // (BigDecimal vs Long vs. Integer)
+        if (dbEntity != null) {
+            for (DbAttribute attribute : dbEntity.getAttributes()) {
+
+                if (!builder.isOverriden(attribute.getName())
+                        && TypesMapping.isNumeric(attribute.getType())) {
+
+                    builder.overrideColumnType(attribute.getName(), TypesMapping
+                            .getJavaBySqlType(attribute.getType()));
+                }
+            }
+        }
+
+        return builder;
+    }
+
     final class OracleResultSetWrapper implements ResultSet {
 
         private ResultSet delegate;
@@ -709,6 +747,7 @@
         public int getColumnType(int column) throws SQLException {
             int type = delegate.getColumnType(column);
 
+            // this only detects INTEGER but not BIGINT...
             if (type == Types.NUMERIC) {
                 int precision = delegate.getPrecision(column);
                 if ((precision == 10 || precision == 38)



Mime
View raw message