class DataObjects::Command
Abstract base class for adapter-specific Command subclasses
Attributes
connection[R]
The Connection on which the command will be run
Public Class Methods
new(connection, text)
click to toggle source
Create a new Command object on the specified connection
# File lib/data_objects/command.rb, line 9 def initialize(connection, text) raise ArgumentError.new("+connection+ must be a DataObjects::Connection") unless DataObjects::Connection === connection @connection, @text = connection, text end
Public Instance Methods
execute_non_query(*args)
click to toggle source
Execute this command and return no dataset
# File lib/data_objects/command.rb, line 15 def execute_non_query(*args) raise NotImplementedError.new end
execute_reader(*args)
click to toggle source
Execute this command and return a DataObjects::Reader for a dataset
# File lib/data_objects/command.rb, line 20 def execute_reader(*args) raise NotImplementedError.new end
set_types(column_types)
click to toggle source
Assign an array of types for the columns to be returned by this command
# File lib/data_objects/command.rb, line 25 def set_types(column_types) raise NotImplementedError.new end
to_s()
click to toggle source
Display the command text
# File lib/data_objects/command.rb, line 30 def to_s @text end
Private Instance Methods
escape_sql(args)
click to toggle source
Escape a string of SQL with a set of arguments. The first argument is assumed to be the SQL to escape, the remaining arguments (if any) are assumed to be values to escape and interpolate.
Examples¶ ↑
escape_sql("SELECT * FROM zoos") # => "SELECT * FROM zoos" escape_sql("SELECT * FROM zoos WHERE name = ?", "Dallas") # => "SELECT * FROM zoos WHERE name = `Dallas`" escape_sql("SELECT * FROM zoos WHERE name = ? AND acreage > ?", "Dallas", 40) # => "SELECT * FROM zoos WHERE name = `Dallas` AND acreage > 40"
Warning¶ ↑
This method is meant mostly for adapters that don't support bind-parameters.
# File lib/data_objects/command.rb, line 54 def escape_sql(args) return @text if args.empty? sql = @text.dup vars = args.dup replacements = 0 mismatch = false sql.gsub!(/'[^']*'|"[^"]*"|`[^`]*`|\?/) do |x| next x unless x == '?' replacements += 1 if vars.empty? mismatch = true else var = vars.shift connection.quote_value(var) end end if !vars.empty? || mismatch raise ArgumentError, "Binding mismatch: #{args.size} for #{replacements}" else sql end end