Title: [531] trunk/activerecord-jdbc: Fix some more Oracle errors, including bad implementation of inserts
Revision
531
Author
olabini
Date
2007-05-03 09:37:26 -0400 (Thu, 03 May 2007)

Log Message

Fix some more Oracle errors, including bad implementation of inserts

Modified Paths


Diff

Modified: trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_oracle.rb (530 => 531)


--- trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_oracle.rb	2007-05-02 18:28:22 UTC (rev 530)
+++ trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_oracle.rb	2007-05-03 13:37:26 UTC (rev 531)
@@ -71,20 +71,11 @@
       else # Assume the sql contains a bind-variable for the id
         id_value = select_one("select #{sequence_name}.nextval id from dual")['id'].to_i
         log(sql, name) { 
-          execute_prepared_insert(sql,id_value)
+          @connection.execute_id_insert(sql,id_value)
         }
       end
       id_value
     end
-    
-    def execute_prepared_insert(sql, id)
-      @stmts ||= {}
-      @stmts[sql] ||= @connection.ps(sql)
-      stmt = @stmts[sql]
-      stmt.setLong(1,id)
-      stmt.executeUpdate
-      id
-    end
 
     def _execute(sql, name = nil)
       log_no_bench(sql, name) do

Modified: trunk/activerecord-jdbc/src/java/JdbcAdapterInternalService.java (530 => 531)


--- trunk/activerecord-jdbc/src/java/JdbcAdapterInternalService.java	2007-05-02 18:28:22 UTC (rev 530)
+++ trunk/activerecord-jdbc/src/java/JdbcAdapterInternalService.java	2007-05-03 13:37:26 UTC (rev 531)
@@ -31,6 +31,7 @@
 
 import java.sql.Connection;
 import java.sql.DatabaseMetaData;
+import java.sql.PreparedStatement;
 import java.sql.ResultSetMetaData;
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -72,7 +73,7 @@
         cJdbcConn.defineFastMethod("execute_update",cf.getFastSingletonMethod("execute_update", IRubyObject.class));
         cJdbcConn.defineFastMethod("execute_query",cf.getFastOptSingletonMethod("execute_query")); 
         cJdbcConn.defineFastMethod("execute_insert",cf.getFastSingletonMethod("execute_insert", IRubyObject.class));
-        cJdbcConn.defineFastMethod("ps",cf.getFastSingletonMethod("prepareStatement", IRubyObject.class));
+        cJdbcConn.defineFastMethod("execute_id_insert",cf.getFastSingletonMethod("execute_id_insert", IRubyObject.class, IRubyObject.class));
         cJdbcConn.defineFastMethod("primary_keys",cf.getFastSingletonMethod("primary_keys", IRubyObject.class));
         cJdbcConn.defineFastMethod("set_native_database_types",cf.getFastSingletonMethod("set_native_database_types"));
         cJdbcConn.defineFastMethod("native_database_types",cf.getFastSingletonMethod("native_database_types"));
@@ -313,8 +314,16 @@
         return runtime.newArray(keyNames);
     }
 
-    public static IRubyObject prepareStatement(IRubyObject recv, IRubyObject sql) {
-        return recv.getInstanceVariable("@connection").callMethod(recv.getRuntime().getCurrentContext(),"prepareStatement",sql);
+    public static IRubyObject execute_id_insert(IRubyObject recv, IRubyObject sql, IRubyObject id) throws SQLException {
+        Connection c = (Connection)recv.dataGetStruct();
+        PreparedStatement ps = c.prepareStatement(sql.toString());
+        try {
+            ps.setLong(1,RubyNumeric.fix2long(id));
+            ps.executeUpdate();
+        } finally {
+            ps.close();
+        }
+        return id;
     }
 
     public static IRubyObject execute_update(IRubyObject recv, IRubyObject sql) throws SQLException {
@@ -416,6 +425,10 @@
             results.add(row);
         }
  
+        try {
+            rs.close();
+        } catch(Exception e) {}
+
         return runtime.newArray(results);
     }
 
@@ -448,6 +461,10 @@
             results.add(row);
         }
  
+        try {
+            rs.close();
+        } catch(Exception e) {}
+
         return runtime.newArray(results);
     }
 
@@ -488,6 +505,10 @@
             }
         }
  
+        try {
+            rs.close();
+        } catch(Exception e) {}
+
         return runtime.newArray(results);
     }
 
@@ -532,12 +553,18 @@
     }
 
     public static IRubyObject unmarshal_id_result(Ruby runtime, ResultSet rs) throws SQLException {
-        if(rs.next()) {
-            if(rs.getMetaData().getColumnCount() > 0) {
-                return runtime.newFixnum(rs.getLong(1));
+        try {
+            if(rs.next()) {
+                if(rs.getMetaData().getColumnCount() > 0) {
+                    return runtime.newFixnum(rs.getLong(1));
+                }
             }
+            return runtime.getNil();
+        } finally {
+            try {
+                rs.close();
+            } catch(Exception e) {}
         }
-        return runtime.getNil();
     }
 
     private final static ByteList ZERO = new ByteList(new byte[]{'\\','0'});
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to