Skip to content

Commit

Permalink
Fix literalization of SQL::Blobs in DataObjects and JDBC adapter's po…
Browse files Browse the repository at this point in the history
…stgresql subadapters when ruby 1.9 is used

This code previously expected String#[] to return a Fixnum, but it
returns a String in ruby 1.9.  Change the code so that it works in
both ruby 1.8 and ruby 1.9.  Refactor the literalization code in
the postgres adapters and subadapters so that that the shared
PostgreSQL adapter has code that doesn't depend on the underlying
adapter.  The native postgres adapter now overrides #literal to
use the versions present in the adapters, as does the JDBC
postgres adapter for strings.
  • Loading branch information
jeremyevans committed Jan 22, 2009
1 parent 754bb49 commit 5f80e6d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD

* Fix literalization of SQL::Blobs in DataObjects and JDBC adapter's postgresql subadapters when ruby 1.9 is used (jeremyevans)

* When using standard strings in the postgres adapter with the postgres-pr driver, use custom string escaping to prevent errors (jeremyevans)

* Before hooks now run in reverse order of being added, so later ones are run first (tamas)
Expand Down
14 changes: 0 additions & 14 deletions lib/sequel_core/adapters/do/postgres.rb
Expand Up @@ -86,20 +86,6 @@ def setup_connection(conn)
# Dataset subclass used for datasets that connect to PostgreSQL via DataObjects.
class Dataset < DataObjects::Dataset
include Sequel::Postgres::DatasetMethods

# Handle SQL::Blobs and Strings correctly.
def literal(v)
case v
when LiteralString
v
when SQL::Blob
"'#{v.gsub(/[\000-\037\047\134\177-\377]/){|b| "\\#{ b[0].to_s(8).rjust(3, '0') }"}}'"
when String
"'#{v.gsub("'", "''")}'"
else
super
end
end
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions lib/sequel_core/adapters/jdbc/postgresql.rb
Expand Up @@ -91,12 +91,14 @@ def prepare(*args)
ps
end

# Convert Java::JavaSql::Timestamps correctly, and handle SQL::Blobs
# correctly.
# Convert Java::JavaSql::Timestamps correctly, and handle Strings
# similar to the native postgres adapter.
def literal(v)
case v
when SQL::Blob
"'#{v.gsub(/[\000-\037\047\134\177-\377]/){|b| "\\#{ b[0].to_s(8).rjust(3, '0') }"}}'"
when LiteralString
v
when String
db.synchronize{|c| "'#{c.escape_string(v)}'"}
when Java::JavaSql::Timestamp
"TIMESTAMP #{literal(v.to_s)}"
else
Expand Down
14 changes: 14 additions & 0 deletions lib/sequel_core/adapters/postgres.rb
Expand Up @@ -325,6 +325,20 @@ def fetch_rows(sql)
end
end
end

# Literalize strings and blobs using code from the native adapter.
def literal(v)
case v
when LiteralString
v
when SQL::Blob
db.synchronize{|c| "'#{c.escape_bytea(v)}'"}
when String
db.synchronize{|c| "'#{c.escape_string(v)}'"}
else
super
end
end

if SEQUEL_POSTGRES_USES_PG

Expand Down
4 changes: 2 additions & 2 deletions lib/sequel_core/adapters/shared/postgres.rb
Expand Up @@ -646,9 +646,9 @@ def literal(v)
when LiteralString
v
when SQL::Blob
db.synchronize{|c| "'#{c.escape_bytea(v)}'"}
"'#{v.gsub(/[\000-\037\047\134\177-\377]/){|b| "\\#{("%o" % b[0..1].unpack("C")[0]).rjust(3, '0')}"}}'"
when String
db.synchronize{|c| "'#{c.escape_string(v)}'"}
"'#{v.gsub("'", "''")}'"
when Time
"#{v.strftime(PG_TIMESTAMP_FORMAT)}.#{sprintf("%06d",v.usec)}'"
when DateTime
Expand Down

0 comments on commit 5f80e6d

Please sign in to comment.