Using Ruby DBI's prepare without a handle?
I need to use Ruby to create some SQL statements for a MySQL database. Ruby will never connect to the database. The SQL statement开发者_运维知识库s will be sent to another site and executed against the MySQL database.
Since the machine running Ruby does not have a connection to the database, is it possible to use DBI's prepare statement without creating a handle to the MySQL database?
ruby-dbi
does include an emulator for preparing statements when the dbd does not provide it. You can use it as follows:
require 'dbi'
st = DBI::SQL::PreparedStatement.new(nil, "Select * from table where x = ?")
st.bind(['test']) # => "Select * from table where x = test"
You can see the algorithm it uses here.
Edit: Based on the comments below a, dbi 0.2.2 version
require 'dbi'
class Quoter ; include DBI::SQL::BasicQuote ; end
st = DBI::SQL::PreparedStatement.new(Quoter.new, "Select * from table where x = ?")
st.bind(['test']) # => "Select * from table where x = 'test'"
Relevant source file is here.
精彩评论