class Sequel::Dataset::PlaceholderLiteralizer::Recorder

Records the offsets at which the placeholder arguments are used in the SQL query.

Public Instance Methods

arg(v=(no_arg_given = true; @argn+=1)) click to toggle source

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
loader(pl, dataset, &block) click to toggle source

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
use(sql, arg, transformer) click to toggle source

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

prepared_sql_and_frags(dataset, prepared_args, &block) click to toggle source

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   frags.each(&:freeze)
121   frags.freeze
122   [prepared_sql, frags]
123 end
process(dataset) { |self, dataset| ... } click to toggle source

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   ds.opts[:sql].freeze
131   sql = ds.clone(:placeholder_literalizer=>self).sql
132 
133   last_offset = 0
134   fragments = @args.map do |used_sql, offset, arg, t|
135     raise Error, "placeholder literalizer argument literalized into different string than dataset returned" unless used_sql.equal?(sql)
136     a = [sql[last_offset...offset], arg, t]
137     last_offset = offset
138     a
139   end
140   final_sql = sql[last_offset..-1]
141 
142   arity = @argn+1
143   [ds, fragments, final_sql, arity]
144 end