mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #14632 from matthewd/escape_bytea
Use connection-specific bytea escaping
This commit is contained in:
commit
6bbbe0b651
2 changed files with 19 additions and 2 deletions
|
@ -4,14 +4,14 @@ module ActiveRecord
|
|||
module Quoting
|
||||
# Escapes binary strings for bytea input to the database.
|
||||
def escape_bytea(value)
|
||||
PGconn.escape_bytea(value) if value
|
||||
@connection.escape_bytea(value) if value
|
||||
end
|
||||
|
||||
# Unescapes bytea output from a database to the binary string it represents.
|
||||
# NOTE: This is NOT an inverse of escape_bytea! This is only to be used
|
||||
# on escaped binary output from database drive.
|
||||
def unescape_bytea(value)
|
||||
PGconn.unescape_bytea(value) if value
|
||||
@connection.unescape_bytea(value) if value
|
||||
end
|
||||
|
||||
# Quotes PostgreSQL-specific data types for SQL input.
|
||||
|
|
|
@ -70,6 +70,23 @@ class PostgresqlByteaTest < ActiveRecord::TestCase
|
|||
assert_equal(data, record.payload)
|
||||
end
|
||||
|
||||
def test_via_to_sql
|
||||
data = "'\u001F\\"
|
||||
record = ByteaDataType.create(payload: data)
|
||||
sql = ByteaDataType.where(payload: data).select(:payload).to_sql
|
||||
result = @connection.query(sql)
|
||||
assert_equal([[data]], result)
|
||||
end
|
||||
|
||||
def test_via_to_sql_with_complicating_connection
|
||||
Thread.new do
|
||||
other_conn = ActiveRecord::Base.connection
|
||||
other_conn.execute('SET standard_conforming_strings = off')
|
||||
end.join
|
||||
|
||||
test_via_to_sql
|
||||
end
|
||||
|
||||
def test_write_binary
|
||||
data = File.read(File.join(File.dirname(__FILE__), '..', '..', '..', 'assets', 'example.log'))
|
||||
assert(data.size > 1)
|
||||
|
|
Loading…
Reference in a new issue