Title: [803] trunk/activerecord-jdbc: Raise an exception in columns_internal if the table doesn't exist,
Revision
803
Author
nicksieger
Date
2007-11-14 14:04:39 -0500 (Wed, 14 Nov 2007)

Log Message

Raise an exception in columns_internal if the table doesn't exist,
rather than silently returning an empty array

Modified Paths

Diff

Modified: trunk/activerecord-jdbc/src/java/jdbc_adapter/JdbcAdapterInternalService.java (802 => 803)


--- trunk/activerecord-jdbc/src/java/jdbc_adapter/JdbcAdapterInternalService.java	2007-11-14 00:17:33 UTC (rev 802)
+++ trunk/activerecord-jdbc/src/java/jdbc_adapter/JdbcAdapterInternalService.java	2007-11-14 19:04:39 UTC (rev 803)
@@ -278,14 +278,10 @@
         throw wrap(recv, toWrap);
     }
 
-    public static IRubyObject tables(final IRubyObject recv, IRubyObject[] args) {
-        final Ruby runtime     = recv.getRuntime();
-        final String catalog   = getCatalog(args);
-        final String schemapat = getSchemaPattern(args);
-        final String tablepat  = getTablePattern(args);
-        final String[] types   = getTypes(args);
-
-        return withConnectionAndRetry(recv, new SQLBlock() {
+    private static SQLBlock tableLookupBlock(final Ruby runtime, 
+            final String catalog, final String schemapat, 
+            final String tablepat, final String[] types) {
+        return new SQLBlock() {
             public IRubyObject call(Connection c) throws SQLException {
                 ResultSet rs = null;
                 try {
@@ -319,9 +315,19 @@
                     try { rs.close(); } catch (Exception e) { }
                 }
             }
-        });
+        };
     }
 
+    public static IRubyObject tables(final IRubyObject recv, IRubyObject[] args) {
+        final Ruby runtime     = recv.getRuntime();
+        final String catalog   = getCatalog(args);
+        final String schemapat = getSchemaPattern(args);
+        final String tablepat  = getTablePattern(args);
+        final String[] types   = getTypes(args);
+        return withConnectionAndRetry(recv, tableLookupBlock(runtime, catalog,
+                schemapat, tablepat, types));
+    }
+
     private static String getCatalog(IRubyObject[] args) {
         if (args != null && args.length > 0) {
             return convertToStringOrNull(args[0]);
@@ -420,14 +426,17 @@
                     boolean isDerby = clzName.indexOf("derby") != -1;
                     boolean isOracle = clzName.indexOf("oracle") != -1 || clzName.indexOf("oci") != -1;
                     String schemaName = null;
+
                     if(args.length>2) {
                         schemaName = args[2].toString();
                     }
+
                     if(metadata.storesUpperCaseIdentifiers()) {
                         table_name = table_name.toUpperCase();
                     } else if(metadata.storesLowerCaseIdentifiers()) {
                         table_name = table_name.toLowerCase();
                     }
+
                     if(schemaName == null && (isDerby || isOracle)) {
                         ResultSet schemas = metadata.getSchemas();
                         String username = metadata.getUserName();
@@ -440,6 +449,12 @@
                         schemas.close();
                     }
 
+                    RubyArray matchingTables = (RubyArray) tableLookupBlock(recv.getRuntime(), 
+                            c.getCatalog(), schemaName, table_name, getTypes(null)).call(c);
+                    if (matchingTables.isEmpty()) {
+                        throw new SQLException("Table " + table_name + " does not exist");
+                    }
+
                     results = metadata.getColumns(c.getCatalog(),schemaName,table_name,null);
                     return unmarshal_columns(recv, metadata, results);
                 } finally {

Modified: trunk/activerecord-jdbc/test/simple.rb (802 => 803)


--- trunk/activerecord-jdbc/test/simple.rb	2007-11-14 00:17:33 UTC (rev 802)
+++ trunk/activerecord-jdbc/test/simple.rb	2007-11-14 19:04:39 UTC (rev 803)
@@ -141,6 +141,13 @@
     assert_equal 1, Entry.count
     assert ActiveRecord::Base.connected?
   end
+
+  class Animal < ActiveRecord::Base; end
+  def test_fetching_columns_for_nonexistent_table_should_raise
+    assert_raises(ActiveRecord::ActiveRecordError) do
+      Animal.columns
+    end
+  end
 end
 
 module MultibyteTestMethods
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to