Class: Sequel::Dataset::PlaceholderLiteralizer::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/dataset/placeholder_literalizer.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#arg(v = (no_arg_given = true; @argn+=1)) ⇒ Object

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.



101
102
103
104
105
106
# File 'lib/sequel/dataset/placeholder_literalizer.rb', line 101

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(dataset) ⇒ Object

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.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sequel/dataset/placeholder_literalizer.rb', line 79

def loader(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
  PlaceholderLiteralizer.new(ds.clone, fragments, final_sql, arity)
end

#use(sql, arg, transformer) ⇒ Object

Record the offset at which the argument is used in the SQL query, and any transforming



110
111
112
# File 'lib/sequel/dataset/placeholder_literalizer.rb', line 110

def use(sql, arg, transformer)
  @args << [sql, sql.length, arg, transformer]
end