Skip to content

Commit

Permalink
escaping binary data encoding when inserting to sqlite3. Thanks Narus…
Browse files Browse the repository at this point in the history
…e! [#6559 state:resolved]
  • Loading branch information
tenderlove committed Mar 21, 2011
1 parent 66c8c6c commit 7222786
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions activerecord/test/cases/adapters/sqlite/sqlite_adapter_test.rb
@@ -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',
Expand All @@ -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"
Expand Down

0 comments on commit 7222786

Please sign in to comment.