Title: [625] trunk/activerecord-jdbc: Improve Derby string quoting performance
Revision
625
Author
olabini
Date
2007-06-14 08:10:14 -0400 (Thu, 14 Jun 2007)

Log Message

Improve Derby string quoting performance

Modified Paths


Diff

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


--- trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_derby.rb	2007-06-14 09:52:10 UTC (rev 624)
+++ trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_derby.rb	2007-06-14 12:10:14 UTC (rev 625)
@@ -411,10 +411,6 @@
       end
     end
 
-    def quote_string(s)
-      s.gsub(/'/, "''") # ' (for ruby-mode)
-    end
-
     def native_database_types #:nodoc:
       {
         :primary_key=>"int generated by default as identity NOT NULL PRIMARY KEY", 

Modified: trunk/activerecord-jdbc/src/java/JDBCDerbySpec.java (624 => 625)


--- trunk/activerecord-jdbc/src/java/JDBCDerbySpec.java	2007-06-14 09:52:10 UTC (rev 624)
+++ trunk/activerecord-jdbc/src/java/JDBCDerbySpec.java	2007-06-14 12:10:14 UTC (rev 625)
@@ -28,14 +28,46 @@
 
 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 JDBCDerbySpec {
     public static void load(Ruby runtime, RubyModule jdbcSpec) {
         RubyModule derby = jdbcSpec.defineModuleUnder("Derby");
         CallbackFactory cf = runtime.callbackFactory(JDBCDerbySpec.class);
+        derby.defineFastMethod("quote_string",cf.getFastSingletonMethod("quote_string",IRubyObject.class));
     }
+
+    private final static ByteList TWO_SINGLE = 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++) {
+            switch (bl.bytes[i]) {
+            case '\'': 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, TWO_SINGLE);
+            i+=1;
+        }
+        if(replacementFound) {
+            return recv.getRuntime().newStringShared(bl);
+        } else {
+            return string;
+        }
+    }
 }
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to