Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Use connection-specific bytea escaping

In our normal usage, it's rare for this to make a difference... but is
more technically correct.

As well as a spec that proves this is a good idea, let's also add a more
sane-looking one that just covers basic to_sql functionality. There
aren't many places where we actually use escape_bytea, but that's one
that won't be going away.
  • Loading branch information...
commit c4bdca19a7a61eb12c75c4a3225e54b69b486a15 1 parent 67e6706
@matthewd matthewd authored
4 activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
@@ -4,14 +4,14 @@ class PostgreSQLAdapter < AbstractAdapter
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
# 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
# Quotes PostgreSQL-specific data types for SQL input.
17 activerecord/test/cases/adapters/postgresql/bytea_test.rb
@@ -70,6 +70,23 @@ def test_write_value
assert_equal(data, record.payload)
+ 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
+ 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 =, '..', '..', '..', 'assets', 'example.log'))
assert(data.size > 1)
Please sign in to comment.
Something went wrong with that request. Please try again.