Title: [613] trunk/activerecord-jdbc: Added eparation of Java speed implementations, also create a better structure_dump impl for Derby

Diff

Modified: trunk/activerecord-jdbc/lib/active_record/connection_adapters/jdbc_adapter.rb (612 => 613)


--- trunk/activerecord-jdbc/lib/active_record/connection_adapters/jdbc_adapter.rb	2007-06-06 04:42:22 UTC (rev 612)
+++ trunk/activerecord-jdbc/lib/active_record/connection_adapters/jdbc_adapter.rb	2007-06-07 09:00:12 UTC (rev 613)
@@ -196,7 +196,7 @@
     end
 
     class JdbcConnection
-      attr_reader :adapter
+      attr_reader :adapter, :connection
       
       def initialize(config)
         @config = config.symbolize_keys!

Modified: trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_derby.rb (612 => 613)


--- trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_derby.rb	2007-06-06 04:42:22 UTC (rev 612)
+++ trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_derby.rb	2007-06-07 09:00:12 UTC (rev 613)
@@ -161,8 +161,8 @@
     COLUMN_TYPE_STMT = "SELECT COLUMNDATATYPE, COLUMNDEFAULT FROM SYS.SYSCOLUMNS WHERE REFERENCEID = '%s' AND COLUMNNAME = '%s'"
 
     AUTO_INC_STMT = "SELECT AUTOINCREMENTSTART, AUTOINCREMENTINC, COLUMNNAME, REFERENCEID, COLUMNDEFAULT FROM SYS.SYSCOLUMNS WHERE REFERENCEID = '%s' AND COLUMNNAME = '%s'"
+    AUTO_INC_STMT2 = "SELECT AUTOINCREMENTSTART, AUTOINCREMENTINC, COLUMNNAME, REFERENCEID, COLUMNDEFAULT FROM SYS.SYSCOLUMNS WHERE REFERENCEID = (SELECT T.TABLEID FROM SYS.SYSTABLES T WHERE T.TABLENAME = '%s') AND COLUMNNAME = '%s'"
 
-
     def add_quotes(name)
       return name unless name
       %Q{"#{name}"}
@@ -196,6 +196,43 @@
       end
       false
     end
+
+    def reinstate_auto_increment(name, refid, coldef)
+      stmt = AUTO_INC_STMT % [refid, strip_quotes(name)]
+      data = ""
+      if data
+        start = data['autoincrementstart']
+        if start
+          coldef << " GENERATED " << (data['columndefault'].nil? ? "ALWAYS" : "BY DEFAULT ")
+          coldef << "AS IDENTITY (START WITH "
+          coldef << start
+          coldef << ", INCREMENT BY "
+          coldef << data['autoincrementinc']
+          coldef << ")"
+          return true
+        end
+      end
+      false
+    end
+
+    def auto_increment_stmt(tname, cname)
+      stmt = AUTO_INC_STMT2 % [tname, strip_quotes(cname)]
+      data = ""
+      if data
+        start = data['autoincrementstart']
+        if start
+          coldef = ""
+          coldef << " GENERATED " << (data['columndefault'].nil? ? "ALWAYS" : "BY DEFAULT ")
+          coldef << "AS IDENTITY (START WITH "
+          coldef << start
+          coldef << ", INCREMENT BY "
+          coldef << data['autoincrementinc']
+          coldef << ")"
+          return coldef
+        end
+      end
+      ""
+    end
     
     def create_column(name, refid, colno)
       stmt = COLUMN_TYPE_STMT % [refid, strip_quotes(name)]
@@ -212,29 +249,48 @@
       coldef
     end
     
+    SIZEABLE = %w(VARCHAR CLOB BLOB)
+    
     def structure_dump #:nodoc:
-      data = ""
-      execute("select tablename, tableid from sys.systables where schemaid not in (select schemaid from sys.sysschemas where schemaname LIKE 'SYS%')").each do |tbl|
-        tid = tbl["tableid"]
-        tname = tbl["tablename"]
-        data << "CREATE TABLE #{tname} (\n"
+      definition=""
+      rs = @connection.connection.meta_data.getTables(nil,nil,nil,["TABLE"].to_java(:string))
+      while rs.next
+        tname = rs.getString(3)
+        definition << "CREATE TABLE #{tname} (\n"
+        rs2 = @connection.connection.meta_data.getColumns(nil,nil,tname,nil)
         first_col = true
-        execute(COLUMN_INFO_STMT % tid).each do |col|
-          col_name = add_quotes(col['columnname']);
-          create_col_string = create_column(col_name, col['referenceid'],col['columnnumber'].to_i)
+        while rs2.next
+          col_name = add_quotes(rs2.getString(4));
+          default = ""
+          d1 = rs2.getString(13)
+          if d1 =~ /^GENERATED_/
+            default = auto_increment_stmt(tname, col_name)
+          elsif d1
+            default = " DEFAULT #{d1}"
+          end
+          
+          type = rs2.getString(6)
+          col_size = rs2.getString(7)
+          nulling = (rs2.getString(18) == 'NO' ? " NOT NULL" : "")
+          create_col_string = add_quotes(expand_double_quotes(strip_quotes(col_name))) + 
+            " " + 
+            type +
+            (SIZEABLE.include?(type) ? "(#{col_size})" : "") + 
+            nulling +
+            default
           if !first_col
             create_col_string = ",\n #{create_col_string}"
           else
             create_col_string = " #{create_col_string}"
           end
-
-          data << create_col_string
-
+          
+          definition << create_col_string
+          
           first_col = false
         end
-        data << ");\n\n"
+        definition << ");\n\n"
       end
-      data
+      definition
     end
     
     # Support for removing columns added via derby bug issue:
@@ -317,10 +373,6 @@
       end
     end
     
-    def tables
-      super.reject{|t| t =~ /^sys/i }
-    end
-    
     def quote(value, column = nil) # :nodoc:
       return value.to_s if column && column.type == :primary_key
 

Modified: trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_mysql.rb (612 => 613)


--- trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_mysql.rb	2007-06-06 04:42:22 UTC (rev 612)
+++ trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_mysql.rb	2007-06-07 09:00:12 UTC (rev 613)
@@ -58,11 +58,6 @@
     def quote_column_name(name) #:nodoc:
         "`#{name}`"
     end
-
-    # from active_record/vendor/mysql.rb
-    def quote_string(str) #:nodoc:
-      @connection.mysql_quote_string(str)
-    end
     
     def quoted_true
         "1"

Added: trunk/activerecord-jdbc/src/java/JDBCDerbySpec.java (0 => 613)


--- trunk/activerecord-jdbc/src/java/JDBCDerbySpec.java	                        (rev 0)
+++ trunk/activerecord-jdbc/src/java/JDBCDerbySpec.java	2007-06-07 09:00:12 UTC (rev 613)
@@ -0,0 +1,41 @@
+/***** BEGIN LICENSE BLOCK *****
+ * Version: CPL 1.0/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Common Public
+ * License Version 1.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Software distributed under the License is distributed on an "AS
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * rights and limitations under the License.
+ *
+ * Copyright (C) 2007 Ola Bini <[EMAIL PROTECTED]>
+ * 
+ * Alternatively, the contents of this file may be used under the terms of
+ * either of the GNU General Public License Version 2 or later (the "GPL"),
+ * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the CPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the CPL, the GPL or the LGPL.
+ ***** END LICENSE BLOCK *****/
+
+import org.jruby.Ruby;
+import org.jruby.RubyModule;
+
+import org.jruby.runtime.CallbackFactory;
+import org.jruby.runtime.ThreadContext;
+import org.jruby.runtime.builtin.IRubyObject;
+
+public class JDBCDerbySpec {
+    public static void load(Ruby runtime, RubyModule jdbcSpec) {
+        RubyModule derby = jdbcSpec.defineModuleUnder("Derby");
+        CallbackFactory cf = runtime.callbackFactory(JDBCDerbySpec.class);
+    }
+}

Added: trunk/activerecord-jdbc/src/java/JDBCMySQLSpec.java (0 => 613)


--- trunk/activerecord-jdbc/src/java/JDBCMySQLSpec.java	                        (rev 0)
+++ trunk/activerecord-jdbc/src/java/JDBCMySQLSpec.java	2007-06-07 09:00:12 UTC (rev 613)
@@ -0,0 +1,82 @@
+/***** BEGIN LICENSE BLOCK *****
+ * Version: CPL 1.0/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Common Public
+ * License Version 1.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Software distributed under the License is distributed on an "AS
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * rights and limitations under the License.
+ *
+ * Copyright (C) 2007 Ola Bini <[EMAIL PROTECTED]>
+ * 
+ * Alternatively, the contents of this file may be used under the terms of
+ * either of the GNU General Public License Version 2 or later (the "GPL"),
+ * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the CPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the CPL, the GPL or the LGPL.
+ ***** END LICENSE BLOCK *****/
+
+import org.jruby.Ruby;
+import org.jruby.RubyModule;
+import org.jruby.RubyString;
+
+import org.jruby.runtime.CallbackFactory;
+import org.jruby.runtime.ThreadContext;
+import org.jruby.runtime.builtin.IRubyObject;
+
+import org.jruby.util.ByteList;
+
+public class JDBCMySQLSpec {
+    public static void load(Ruby runtime, RubyModule jdbcSpec) {
+        RubyModule mysql = jdbcSpec.defineModuleUnder("MySQL");
+        CallbackFactory cf = runtime.callbackFactory(JDBCMySQLSpec.class);
+        mysql.defineFastMethod("quote_string",cf.getFastSingletonMethod("quote_string",IRubyObject.class));
+    }
+
+    private final static ByteList ZERO = new ByteList(new byte[]{'\\','0'});
+    private final static ByteList NEWLINE = new ByteList(new byte[]{'\\','n'});
+    private final static ByteList CARRIAGE = new ByteList(new byte[]{'\\','r'});
+    private final static ByteList ZED = new ByteList(new byte[]{'\\','Z'});
+    private final static ByteList DBL = new ByteList(new byte[]{'\\','"'});
+    private final static ByteList SINGLE = new ByteList(new byte[]{'\\','\''});
+    private final static ByteList ESCAPE = new ByteList(new byte[]{'\\','\\'});
+
+    public static IRubyObject quote_string(IRubyObject recv, IRubyObject string) {
+        boolean replacementFound = false;
+        ByteList bl = ((RubyString) string).getByteList();
+        
+        for(int i = bl.begin; i < bl.begin + bl.realSize; i++) {
+            ByteList rep = null;
+            switch (bl.bytes[i]) {
+            case 0: rep = ZERO; break;
+            case '\n': rep = NEWLINE; break;
+            case '\r': rep = CARRIAGE; break;
+            case 26: rep = ZED; break;
+            case '"': rep = DBL; break;
+            case '\'': rep = SINGLE; break;
+            case '\\': rep = ESCAPE; break;
+            default: continue;
+            }
+            
+            // On first replacement allocate a different bytelist so we don't manip original 
+            if(!replacementFound) {
+                bl = new ByteList(bl);
+                replacementFound = true;
+            }
+
+            bl.replace(i, 1, rep);
+            i+=1;
+        }
+        return recv.getRuntime().newStringShared(bl);
+    }
+}

Modified: trunk/activerecord-jdbc/src/java/JdbcAdapterInternalService.java (612 => 613)


--- trunk/activerecord-jdbc/src/java/JdbcAdapterInternalService.java	2007-06-06 04:42:22 UTC (rev 612)
+++ trunk/activerecord-jdbc/src/java/JdbcAdapterInternalService.java	2007-06-07 09:00:12 UTC (rev 613)
@@ -82,8 +82,12 @@
         cJdbcConn.defineFastMethod("rollback",cf.getFastSingletonMethod("rollback"));
         cJdbcConn.defineFastMethod("database_name",cf.getFastSingletonMethod("database_name"));
         cJdbcConn.defineFastMethod("columns",cf.getFastOptSingletonMethod("columns"));
-        cJdbcConn.defineFastMethod("mysql_quote_string",cf.getFastSingletonMethod("mysql_quote_string",IRubyObject.class));
         cJdbcConn.defineFastMethod("tables",cf.getFastOptSingletonMethod("tables"));
+
+        RubyModule jdbcSpec = runtime.getOrCreateModule("JdbcSpec");
+        JDBCMySQLSpec.load(runtime, jdbcSpec);
+        JDBCDerbySpec.load(runtime, jdbcSpec);
+
         return true;
     }
 
@@ -107,7 +111,7 @@
     public static IRubyObject tables(IRubyObject recv, IRubyObject[] args) throws SQLException, IOException {
         Ruby runtime = recv.getRuntime();
         String catalog = null, schemapat = null, tablepat = null;
-        String[] types = null;
+        String[] types = new String[]{"TABLE"};
         if (args != null) {
             if (args.length > 0) {
                 catalog = convertToStringOrNull(args[0]);
@@ -600,43 +604,6 @@
         }
     }
 
-    private final static ByteList ZERO = new ByteList(new byte[]{'\\','0'});
-    private final static ByteList NEWLINE = new ByteList(new byte[]{'\\','n'});
-    private final static ByteList CARRIAGE = new ByteList(new byte[]{'\\','r'});
-    private final static ByteList ZED = new ByteList(new byte[]{'\\','Z'});
-    private final static ByteList DBL = new ByteList(new byte[]{'\\','"'});
-    private final static ByteList SINGLE = new ByteList(new byte[]{'\\','\''});
-    private final static ByteList ESCAPE = new ByteList(new byte[]{'\\','\\'});
-
-    public static IRubyObject mysql_quote_string(IRubyObject recv, IRubyObject string) {
-        boolean replacementFound = false;
-        ByteList bl = ((RubyString) string).getByteList();
-        
-        for(int i = bl.begin; i < bl.begin + bl.realSize; i++) {
-            ByteList rep = null;
-            switch (bl.bytes[i]) {
-            case 0: rep = ZERO; break;
-            case '\n': rep = NEWLINE; break;
-            case '\r': rep = CARRIAGE; break;
-            case 26: rep = ZED; break;
-            case '"': rep = DBL; break;
-            case '\'': rep = SINGLE; break;
-            case '\\': rep = ESCAPE; break;
-            default: continue;
-            }
-            
-            // On first replacement allocate a different bytelist so we don't manip original 
-            if(!replacementFound) {
-                bl = new ByteList(bl);
-                replacementFound = true;
-            }
-
-            bl.replace(i, 1, rep);
-            i+=1;
-        }
-        return recv.getRuntime().newStringShared(bl);
-    }
-    
     private static String convertToStringOrNull(IRubyObject obj) {
         if (obj.isNil()) {
             return null;
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to