 |
Forums |
Admin Start New Thread
By: Andrew R Jackson
RE: Quoting and the Postgres DBD [ reply ] 2009-04-02 22:11
|
Looking at a pre-reorg version of the DBI (which I have working elsewhere for a long time), and looking at the commented out quote() method for the current mysql-dbd [in database.rb], perhaps this might be of interest to those wanting the DBI interface working again as documented:
def quote(value)
case value
when String
value = value.gsub(/'/, "''") # ' (for ruby-mode)
"'#{value}'"
when DBI::Binary
value = quote(value.to_s)
when NilClass
"NULL"
when TrueClass
"'1'"
when FalseClass
"'0'"
when Array
value.collect { |v| quote(v) }.join(", ")
when DBI::Date, DBI::Time, DBI::Timestamp, ::Date
"'#{value.to_s}'"
when ::Time
"'#{value.rfc2822}'"
else
value.to_s
end
end
Of course, adjustments for your specific DBD may be needed, and you realize if there were problems with this old quoting...well, you'll be happy about the consistency...
|
By: Andrew R Jackson
RE: Quoting and the Postgres DBD [ reply ] 2009-04-02 21:18
|
Same problem with the MySQL DBD.
Somebody hated the quote() method of the pre-overhaul version and commented them out.
If the PG DBD is anything like the MySQL DDB, the quote() method is there--it's even got a bit more code in it since before the reorg--but commented out.
No comment provided as to why to axe it and break existing usages, just the code is commented out.
Maybe uncomment the method?
|
By: Mike Pomraning
RE: Quoting and the Postgres DBD [ reply ] 2009-03-27 03:24
|
I didn't see anything immediately in the changelogs or in diffs between ruby-dbi versions to justify the removal of quote(), and the DBI::DatabaseHandle docs suggest that David's expectation is proper.
"Ruby/DBI" tracker patch #24932 addresses in terms of the ruby-pg driver's escape_bytea() routine. Both Pg.quote() and Pg::Database#quote() are supported.
|
By: David Evans
Quoting and the Postgres DBD [ reply ] 2009-03-26 11:07
|
Hi, all.
d = DBI.connect('DBI:Pg:...', ...)
d.quote('foo')
should work, yes? It doesn't:
irb(main):010:0> d.quote('foo')
NoMethodError: undefined method `quote' for #<DBI::DBD::Pg::Database:0x101ed74>
but this does:
irb(main):011:0> DBI::DBD::Pg.quote('foo')
=> "E'foo'"
Should DBI::DBD::Pg#quote move into DBI::DBD::Pg::Database? I haven't tried with other DBDs; can file a bug report if appropriate.
DBI version 0.4.1, the pg DBD is 0.3.7.
Cheers!
|
|
 |