You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by lr...@apache.org on 2007/07/25 22:24:10 UTC

svn commit: r559580 - in /incubator/tuscany/java/das/rdb/src/main: java/org/apache/tuscany/das/rdb/impl/ resources/

Author: lresende
Date: Wed Jul 25 13:24:09 2007
New Revision: 559580

URL: http://svn.apache.org/viewvc?view=rev&rev=559580
Log:
Tuscany-1353 - Applying patch from Amita

Modified:
    incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/BaseCommandImpl.java
    incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ConnectionImpl.java
    incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/InsertCommandImpl.java
    incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/Statement.java
    incubator/tuscany/java/das/rdb/src/main/resources/config.xsd

Modified: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/BaseCommandImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/BaseCommandImpl.java?view=diff&rev=559580&r1=559579&r2=559580
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/BaseCommandImpl.java (original)
+++ incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/BaseCommandImpl.java Wed Jul 25 13:24:09 2007
@@ -24,27 +24,39 @@
 import org.apache.tuscany.das.rdb.config.wrapper.MappingWrapper;
 
 public abstract class BaseCommandImpl {
-
     protected MappingWrapper configWrapper = new MappingWrapper();
 
+    //when no config in method params, useGetGeneratedKeys will default to True
+    //as DBMS metadata method is not reliable in all cases
     public void setConnection(Connection connection) {
         setConnection(new ConnectionImpl(connection));
     }
 
     public void setConnection(Connection connection, Config config) {
         boolean managed = true;
-        if (config != null && config.getConnectionInfo() != null) {
-            managed = config.getConnectionInfo().isManagedtx();
+        String generatedKeysSupported = null;
+        
+        if (config != null){
+        	if(config.getConnectionInfo() != null) {
+        		managed = config.getConnectionInfo().isManagedtx();
+        	}
+        
+            generatedKeysSupported = config.getGeneratedKeysSupported();
+        	
+        	setConnection(connection, managed, generatedKeysSupported);
+        }
+        else{
+        	setConnection(connection);
         }
-        setConnection(connection, managed);
     }
 
-    public void setConnection(Connection connection, boolean manageTransaction) {
+    public void setConnection(Connection connection, boolean manageTransaction, String generatedKeysSupported) {
         ConnectionImpl c = new ConnectionImpl(connection);
         c.setManageTransactions(manageTransaction);
+        c.setGeneratedKeysSupported(generatedKeysSupported);
         setConnection(c);
     }
-
+    
     public abstract void setConnection(ConnectionImpl c);
 
 }

Modified: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ConnectionImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ConnectionImpl.java?view=diff&rev=559580&r1=559579&r2=559580
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ConnectionImpl.java (original)
+++ incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ConnectionImpl.java Wed Jul 25 13:24:09 2007
@@ -36,19 +36,12 @@
 
     private boolean managingTransaction = true;
 
-    private final boolean useGetGeneratedKeys;
-
+    private String generatedKeysSupported = null;
+    
     public ConnectionImpl(Connection connection) {
         this.connection = connection;
 
         try {
-            DatabaseMetaData dbmd = connection.getMetaData();
-
-            if (dbmd.getDatabaseProductName().indexOf("Oracle") >= 0  || dbmd.getDatabaseProductName().indexOf("PostgreSQL") >= 0) {
-                this.useGetGeneratedKeys = false;
-            } else {
-                this.useGetGeneratedKeys = true;
-            }
             if (connection.getAutoCommit()) {
                 throw new RuntimeException("AutoCommit must be off");
             }
@@ -62,6 +55,45 @@
         return connection;
     }
 
+    public String getGeneratedKeysSupported() {
+        return this.generatedKeysSupported;
+    }
+    
+    public void setGeneratedKeysSupported(String useGetGeneratedKeys){
+        this.generatedKeysSupported = useGetGeneratedKeys;
+    }    
+    
+    public boolean isGeneratedKeysSupported() {
+        try{
+            if(this.generatedKeysSupported == null){
+                DatabaseMetaData dbmsMetadata = this.connection.getMetaData();
+                boolean supportsGetGeneratedKeys = dbmsMetadata.supportsGetGeneratedKeys();
+                if(supportsGetGeneratedKeys){
+                    this.generatedKeysSupported = "true";
+                }
+                //currently DERBY partially supports this feature and thus returns FALSE,
+                //this hardcoding is needed as the partial support is enough for DAS
+                //we can remove this later, when DERBY change the behaviour of it's "supportsGetGeneratedKeys"
+                else if(dbmsMetadata.getDatabaseProductName().indexOf("Derby") > 0){                    
+                    this.generatedKeysSupported = "true";
+                }
+                else{
+                    this.generatedKeysSupported = "false";
+                }
+            }           
+        }catch(Exception e){//can be from supportsGetGeneratedKeys or due to absense of supportsGetGeneratedKeys
+            if (this.logger.isDebugEnabled()) {
+                this.logger.debug("exception setiing useGetGeneratedKeys false");
+            }
+            this.generatedKeysSupported = "false";
+        }
+        
+        if (this.logger.isDebugEnabled()) {
+            this.logger.debug("returning useGetGeneratedKeys():"+ this.generatedKeysSupported);
+        }
+        return Boolean.parseBoolean(this.generatedKeysSupported);
+    }
+    
     public void cleanUp() {
         try {
             if (managingTransaction) {
@@ -91,9 +123,10 @@
     public PreparedStatement prepareStatement(String queryString, String[] returnKeys) throws SQLException {
         if (this.logger.isDebugEnabled()) {
             this.logger.debug("Preparing Statement: " + queryString);
+            this.logger.debug("Boolean value for use gen key: " + this.generatedKeysSupported);
         }
 
-        if (useGetGeneratedKeys) {
+        if (isGeneratedKeysSupported()) {
             return connection.prepareStatement(queryString, Statement.RETURN_GENERATED_KEYS);
         } else if (returnKeys.length > 0) {
             return connection.prepareStatement(queryString, returnKeys);
@@ -101,7 +134,7 @@
 
         return connection.prepareStatement(queryString);
     }
-
+    
     public PreparedStatement preparePagedStatement(String queryString) throws SQLException {
         if (this.logger.isDebugEnabled()) {
             this.logger.debug("Preparing Statement: " + queryString);
@@ -117,9 +150,5 @@
 
     public CallableStatement prepareCall(String queryString) throws SQLException {
         return connection.prepareCall(queryString);
-    }
-
-    public boolean useGetGeneratedKeys() {
-        return this.useGetGeneratedKeys;
     }
 }

Modified: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/InsertCommandImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/InsertCommandImpl.java?view=diff&rev=559580&r1=559579&r2=559580
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/InsertCommandImpl.java (original)
+++ incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/InsertCommandImpl.java Wed Jul 25 13:24:09 2007
@@ -57,7 +57,11 @@
 
     public int getGeneratedKey() {
         try {
-            return statement.getGeneratedKey().intValue();
+        	Integer key = statement.getGeneratedKey();
+        	if(key != null)
+        		return key.intValue();
+        	else
+        		throw new RuntimeException("Could not obtain generated key!");
         } catch (SQLException ex) {
             throw new RuntimeException(ex);
         }

Modified: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/Statement.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/Statement.java?view=diff&rev=559580&r1=559579&r2=559580
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/Statement.java (original)
+++ incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/Statement.java Wed Jul 25 13:24:09 2007
@@ -204,7 +204,7 @@
 
     public Integer getGeneratedKey() throws SQLException {
 
-        if (getConnection().useGetGeneratedKeys()) {
+    	if (getConnection().isGeneratedKeysSupported()) {
             ResultSet rs = preparedStatement.getGeneratedKeys();
             if (rs.next()) {
                 return new Integer(rs.getInt(1));

Modified: incubator/tuscany/java/das/rdb/src/main/resources/config.xsd
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/resources/config.xsd?view=diff&rev=559580&r1=559579&r2=559580
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/resources/config.xsd (original)
+++ incubator/tuscany/java/das/rdb/src/main/resources/config.xsd Wed Jul 25 13:24:09 2007
@@ -37,7 +37,15 @@
       </xsd:sequence>
       <xsd:attribute name="uri" type="xsd:string"/>
       <xsd:attribute name="dataObjectModel" type="xsd:string"/>
-      <xsd:attribute name="databaseSchemaNameSupported" type="xsd:boolean" default="false"/>      
+      <xsd:attribute name="databaseSchemaNameSupported" type="xsd:boolean" default="false"/>
+      <xsd:attribute name="generatedKeysSupported">
+         <xsd:simpleType>
+            <xsd:restriction base="xsd:string">
+                <xsd:enumeration value="true"/>
+                <xsd:enumeration value="false"/>
+            </xsd:restriction>
+         </xsd:simpleType>
+      </xsd:attribute>
    </xsd:complexType>
 
    <xsd:complexType name="ConnectionProperties">



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org