Permalink
Browse files

Fix literalization of SQL::Blobs in DataObjects and JDBC adapter's po…

…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...
1 parent 754bb49 commit 5f80e6d9beb6a6625cc78d227f75c7049c9f49bf @jeremyevans committed Jan 22, 2009
View
@@ -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)
@@ -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
@@ -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
@@ -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
@@ -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

0 comments on commit 5f80e6d

Please sign in to comment.