Title: [455] trunk/activerecord-jdbc: Make a native string-quote for MySQL, like MRI's got
Revision
455
Author
olabini
Date
2007-04-15 14:52:27 -0400 (Sun, 15 Apr 2007)

Log Message

Make a native string-quote for MySQL, like MRI's got

Modified Paths


Diff

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


--- trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_mysql.rb	2007-04-15 07:55:51 UTC (rev 454)
+++ trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_mysql.rb	2007-04-15 18:52:27 UTC (rev 455)
@@ -45,10 +45,10 @@
     def quote(value, column = nil)
       if column && column.type == :primary_key
         value.to_s
-      elsif value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
+      elsif column && String === value && column.type == :binary && column.class.respond_to?(:string_to_binary)
         s = column.class.string_to_binary(value).unpack("H*")[0]
         "x'#{s}'"
-      elsif value.kind_of?(BigDecimal)
+      elsif BigDecimal === value
         "'#{value.to_s("F")}'"
       else
         super
@@ -61,15 +61,7 @@
 
     # from active_record/vendor/mysql.rb
     def quote_string(str) #:nodoc:
-      str.gsub(/([\0\n\r\032\'\"\\])/) do
-        case $1
-        when "\0" then "\\0"
-        when "\n" then "\\n"
-        when "\r" then "\\r"
-        when "\032" then "\\Z"
-        else "\\"+$1
-        end
-      end
+      @connection.mysql_quote_string(str)
     end
     
     def quoted_true

Modified: trunk/activerecord-jdbc/src/java/JdbcAdapterInternalService.java (454 => 455)


--- trunk/activerecord-jdbc/src/java/JdbcAdapterInternalService.java	2007-04-15 07:55:51 UTC (rev 454)
+++ trunk/activerecord-jdbc/src/java/JdbcAdapterInternalService.java	2007-04-15 18:52:27 UTC (rev 455)
@@ -81,6 +81,7 @@
         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.defineMethod("tables",cf.getSingletonMethod("tables"));
         return true;
     }
@@ -529,4 +530,40 @@
         }
         return runtime.getNil();
     }
+
+    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 _str) {
+        RubyString str = (RubyString)_str;
+        boolean r = false;
+        ByteList bl = str.getByteList();
+        for(int i=bl.begin,j=i+bl.realSize;i<j;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;
+            }
+            if(!r) {
+                bl = new ByteList(bl);
+                r = true;
+            }
+            j+=1;
+            bl.replace(i, 1, rep);
+            i+=1;
+        }
+        return recv.getRuntime().newStringShared(bl);
+    }
 }
_______________________________________________
Jruby-extras-devel mailing list
[EMAIL PROTECTED]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to