Permalink
Browse files

When using standard strings in the postgres adapter with the postgres…

…-pr driver, use custom string escaping to prevent errors

postgres-pr is unmaintained AFAIK.  Instead of monkey patching to fix
the issue, change the escape_string method Sequel defines if
postgres-pr is used to check the force_standard_strings setting
and correctly escape if it is true (which it is by default).

postgres-pr has one other signficant problem, in that it raises
RuntimeErrors instead of PGErrors if there are errors in the SQL
syntax.  The fix isn't difficult, but as it would require modifying
an existing postgres-pr method, it's not one I'm comfortable making.
  • Loading branch information...
1 parent cebda44 commit 754bb498885fcbde4fd0a79a2ebcbb61ac973b31 @jeremyevans committed Jan 22, 2009
Showing with 7 additions and 1 deletion.
  1. +2 −0 CHANGELOG
  2. +1 −1 lib/sequel_core/adapters/postgres.rb
  3. +4 −0 spec/adapters/postgres_spec.rb
View
2 CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* 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)
* Add Firebird adapter, requires Firebird ruby driver located at http://github.com/wishdev/fb (wishdev)
View
2 lib/sequel_core/adapters/postgres.rb
@@ -15,7 +15,7 @@ class PGconn
# If there is no escape_string instead method, but there is an
# escape class method, use that instead.
def escape_string(str)
- self.class.escape(str)
+ Sequel::Postgres.force_standard_strings ? str.gsub("'", "''") : self.class.escape(str)
end
else
# Raise an error if no valid string escaping method can be found.
View
4 spec/adapters/postgres_spec.rb
@@ -260,6 +260,10 @@ def POSTGRES_DB.ret_commit
POSTGRES_DB['SELECT ? AS a', "\\dingo"].get(:a) == "\\dingo"
end
+ specify "should correctly escape strings with quotes" do
+ POSTGRES_DB['SELECT ? AS a', "\\'dingo"].get(:a) == "\\'dingo"
+ end
+
specify "should properly escape binary data" do
POSTGRES_DB['SELECT ? AS a', "\1\2\3".to_blob].get(:a) == "\1\2\3"
end

0 comments on commit 754bb49

Please sign in to comment.