Skip to content

Commit

Permalink
Merge pull request #14632 from matthewd/escape_bytea
Browse files Browse the repository at this point in the history
Use connection-specific bytea escaping
  • Loading branch information
rafaelfranca committed Apr 7, 2014
2 parents 83e9e19 + c4bdca1 commit 6bbbe0b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Expand Up @@ -4,14 +4,14 @@ class PostgreSQLAdapter < AbstractAdapter
module Quoting module Quoting
# Escapes binary strings for bytea input to the database. # Escapes binary strings for bytea input to the database.
def escape_bytea(value) def escape_bytea(value)
PGconn.escape_bytea(value) if value @connection.escape_bytea(value) if value
end end


# Unescapes bytea output from a database to the binary string it represents. # 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 # NOTE: This is NOT an inverse of escape_bytea! This is only to be used
# on escaped binary output from database drive. # on escaped binary output from database drive.
def unescape_bytea(value) def unescape_bytea(value)
PGconn.unescape_bytea(value) if value @connection.unescape_bytea(value) if value
end end


# Quotes PostgreSQL-specific data types for SQL input. # Quotes PostgreSQL-specific data types for SQL input.
Expand Down
17 changes: 17 additions & 0 deletions activerecord/test/cases/adapters/postgresql/bytea_test.rb
Expand Up @@ -70,6 +70,23 @@ def test_write_value
assert_equal(data, record.payload) assert_equal(data, record.payload)
end 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 def test_write_binary
data = File.read(File.join(File.dirname(__FILE__), '..', '..', '..', 'assets', 'example.log')) data = File.read(File.join(File.dirname(__FILE__), '..', '..', '..', 'assets', 'example.log'))
assert(data.size > 1) assert(data.size > 1)
Expand Down

0 comments on commit 6bbbe0b

Please sign in to comment.