class Sequel::Dataset::PlaceholderLiteralizer::Recorder
Records the offsets at which the placeholder arguments are used in the SQL
query.
Public Instance Methods
Return an Argument
with the specified position, or the next position. In general you shouldn't mix calls with an argument and calls without an argument for the same receiver.
# File lib/sequel/dataset/placeholder_literalizer.rb 87 def arg(v=(no_arg_given = true; @argn+=1)) 88 unless no_arg_given 89 @argn = v if @argn < v 90 end 91 Argument.new(self, v) 92 end
Yields the receiver and the dataset to the block, which should call arg
on the receiver for each placeholder argument, and return the dataset that you want to load.
# File lib/sequel/dataset/placeholder_literalizer.rb 80 def loader(dataset, &block) 81 PlaceholderLiteralizer.new(*process(dataset, &block)) 82 end
Record the offset at which the argument is used in the SQL
query, and any transforming block.
# File lib/sequel/dataset/placeholder_literalizer.rb 96 def use(sql, arg, transformer) 97 @args << [sql, sql.length, arg, transformer] 98 end
Private Instance Methods
Return an array with two elements, the first being an SQL
string with interpolated prepared argument placeholders (suitable for inspect), the the second being an array of SQL
fragments suitable for using for creating a Sequel::SQL::PlaceholderLiteralString
. Designed for use with emulated prepared statements.
# File lib/sequel/dataset/placeholder_literalizer.rb 108 def prepared_sql_and_frags(dataset, prepared_args, &block) 109 _, frags, final_sql, _ = process(dataset, &block) 110 111 frags = frags.map(&:first) 112 prepared_sql = String.new 113 frags.each_with_index do |sql, i| 114 prepared_sql << sql 115 prepared_sql << "$#{prepared_args[i]}" 116 end 117 if final_sql 118 frags << final_sql 119 prepared_sql << final_sql 120 end 121 122 [prepared_sql, frags] 123 end
Internals of loader
and prepared_sql_and_frags
.
# File lib/sequel/dataset/placeholder_literalizer.rb 126 def process(dataset) 127 @argn = -1 128 @args = [] 129 ds = yield self, dataset 130 sql = ds.clone(:placeholder_literalizer=>self).sql 131 132 last_offset = 0 133 fragments = @args.map do |used_sql, offset, arg, t| 134 raise Error, "placeholder literalizer argument literalized into different string than dataset returned" unless used_sql.equal?(sql) 135 a = [sql[last_offset...offset], arg, t] 136 last_offset = offset 137 a 138 end 139 final_sql = sql[last_offset..-1] 140 141 arity = @argn+1 142 [ds, fragments, final_sql, arity] 143 end