1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

escaping binary data encoding when inserting to sqlite3. Thanks Naruse! [#6559 state:resolved]

This commit is contained in:
Aaron Patterson 2011-03-21 14:48:43 -07:00
parent ea8fcfb729
commit 88636f7195
2 changed files with 27 additions and 0 deletions

View file

@ -34,6 +34,14 @@ module ActiveRecord
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

View file

@ -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 @@ module ActiveRecord
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"