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(pl, dataset, &block) 81 pl.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 frags << final_sql 118 prepared_sql << final_sql 119 120 [prepared_sql, frags] 121 end
Internals of loader
and prepared_sql_and_frags
.
# File lib/sequel/dataset/placeholder_literalizer.rb 124 def process(dataset) 125 @argn = -1 126 @args = [] 127 ds = yield self, dataset 128 sql = ds.clone(:placeholder_literalizer=>self).sql 129 130 last_offset = 0 131 fragments = @args.map do |used_sql, offset, arg, t| 132 raise Error, "placeholder literalizer argument literalized into different string than dataset returned" unless used_sql.equal?(sql) 133 a = [sql[last_offset...offset], arg, t] 134 last_offset = offset 135 a 136 end 137 final_sql = sql[last_offset..-1] 138 139 arity = @argn+1 140 [ds, fragments, final_sql, arity] 141 end