Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

escaping binary data encoding when inserting to sqlite3. Thanks Narus…

…e! [#6559 state:resolved]
  • Loading branch information...
commit 7222786c15eacbd432b2bce9798d3f3459f7e05f 1 parent 66c8c6c
Aaron Patterson tenderlove authored
8 activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
View
@@ -37,6 +37,14 @@ def self.sqlite3_connection(config) # :nodoc:
module ConnectionAdapters #:nodoc:
class SQLite3Adapter < SQLiteAdapter # :nodoc:
+ def quote(value, column = nil)
+ if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
+ s = column.class.string_to_binary(value).unpack("H*")[0]
+ "x'#{s}'"
+ else
+ super
+ end
+ end
# Returns the current database encoding format as a string, eg: 'UTF-8'
def encoding
19 activerecord/test/cases/adapters/sqlite/sqlite_adapter_test.rb
View
@@ -1,8 +1,13 @@
+# encoding: utf-8
require "cases/helper"
+require 'models/binary'
module ActiveRecord
module ConnectionAdapters
class SQLiteAdapterTest < ActiveRecord::TestCase
+ class DualEncoding < ActiveRecord::Base
+ end
+
def setup
@ctx = Base.sqlite3_connection :database => ':memory:',
:adapter => 'sqlite3',
@@ -15,6 +20,20 @@ def setup
eosql
end
+ def test_quote_binary_column_escapes_it
+ DualEncoding.connection.execute(<<-eosql)
+ CREATE TABLE dual_encodings (
+ id integer PRIMARY KEY AUTOINCREMENT,
+ name string,
+ data binary
+ )
+ eosql
+ str = "\x80".force_encoding("ASCII-8BIT")
+ binary = DualEncoding.new :name => 'いただきます!', :data => str
+ binary.save!
+ assert_equal str, binary.data
+ end
+
def test_execute
@ctx.execute "INSERT INTO items (number) VALUES (10)"
records = @ctx.execute "SELECT * FROM items"
Please sign in to comment.
Something went wrong with that request. Please try again.