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, line 86
def arg(v=(no_arg_given = true; @argn+=1))
  unless no_arg_given
    @argn = v if @argn < v
  end
  Argument.new(self, v)
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, line 79
def loader(pl, dataset, &block)
  pl.new(*process(dataset, &block))
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, line 95
def use(sql, arg, transformer)
  @args << [sql, sql.length, arg, transformer]
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, line 107
def prepared_sql_and_frags(dataset, prepared_args, &block)
  _, frags, final_sql, _ = process(dataset, &block)

  frags = frags.map(&:first)
  prepared_sql = String.new
  frags.each_with_index do |sql, i|
    prepared_sql << sql
    prepared_sql << "$#{prepared_args[i]}"
  end
  frags << final_sql
  prepared_sql << final_sql

  [prepared_sql, frags]
end
process(dataset) { |self, dataset| ... } click to toggle source

Internals of loader and prepared_sql_and_frags.

# File lib/sequel/dataset/placeholder_literalizer.rb, line 123
def process(dataset)
  @argn = -1
  @args = []
  ds = yield self, dataset
  sql = ds.clone(:placeholder_literalizer=>self).sql

  last_offset = 0
  fragments = @args.map do |used_sql, offset, arg, t|
    raise Error, "placeholder literalizer argument literalized into different string than dataset returned" unless used_sql.equal?(sql)
    a = [sql[last_offset...offset], arg, t]
    last_offset = offset
    a
  end
  final_sql = sql[last_offset..-1]

  arity = @argn+1
  [ds, fragments, final_sql, arity]
end