Class Sequel::Dataset::PlaceholderLiteralizer
In: lib/sequel/dataset/placeholder_literalizer.rb
Parent: Object

PlaceholderLiteralizer allows you to record the application of arbitrary changes to a dataset with placeholder arguments, recording where those placeholder arguments are used in the query. When running the query, the literalization process is much faster as Sequel can skip most of the work it normal has to do when literalizing a dataset.

Basically, this enables optimizations that allow Sequel to cache the SQL produced for a given dataset, so that it doesn‘t need to recompute that information every time.

Example:

  loader = Sequel::Dataset::PlaceholderLiteralizer.loader(DB[:items]) do |pl, ds|
    ds.where(:id=>pl.arg).exclude(:name=>pl.arg).limit(1)
  end
  loader.first(1, "foo")
  # SELECT * FROM items WHERE ((id = 1) AND (name != 'foo')) LIMIT 1
  loader.first(2, "bar")
  # SELECT * FROM items WHERE ((id = 2) AND (name != 'bar')) LIMIT 1

Caveats:

Note that this method does not handle all possible cases. For example:

  loader = Sequel::Dataset::PlaceholderLiteralizer.loader(DB[:items]) do |pl, ds|
    ds.join(pl.arg, :item_id=>:id)
  end
  loader(:cart_items)

Will not qualify the item_id column with cart_items. In this type of situation it‘s best to add a table alias when joining:

  loader = Sequel::Dataset::PlaceholderLiteralizer.loader(DB[:items]) do |pl, ds|
    ds.join(Sequel.as(pl.arg, :t), :item_id=>:id)
  end
  loader(:cart_items)

There are other similar cases that are not handled, mainly when Sequel changes the SQL produced depending on the types of the arguments.

Methods

all   each   first   get   loader   new   sql   with_dataset  

Classes and Modules

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

Public Class methods

Create a PlaceholderLiteralizer by yielding a Recorder and dataset to the given block, recording the offsets at which the recorders arguments are used in the query.

[Source]

     # File lib/sequel/dataset/placeholder_literalizer.rb, line 116
116:       def self.loader(dataset, &block)
117:         Recorder.new.loader(dataset, &block)
118:       end

Save the dataset, array of SQL fragments, and ending SQL string.

[Source]

     # File lib/sequel/dataset/placeholder_literalizer.rb, line 121
121:       def initialize(dataset, fragments, final_sql, arity)
122:         @dataset = dataset
123:         @fragments = fragments
124:         @final_sql = final_sql
125:         @arity = arity
126:       end

Public Instance methods

Return an array of all objects by running the SQL query for the given arguments. If a block is given, yields all objects to the block after loading them.

[Source]

     # File lib/sequel/dataset/placeholder_literalizer.rb, line 137
137:       def all(*args, &block)
138:         @dataset.with_sql_all(sql(*args), &block)
139:       end

Run the SQL query for the given arguments, yielding each returned row to the block.

[Source]

     # File lib/sequel/dataset/placeholder_literalizer.rb, line 142
142:       def each(*args, &block)
143:         @dataset.with_sql_each(sql(*args), &block)
144:       end

Run the SQL query for the given arguments, returning the first row.

[Source]

     # File lib/sequel/dataset/placeholder_literalizer.rb, line 147
147:       def first(*args)
148:         @dataset.with_sql_first(sql(*args))
149:       end

Run the SQL query for the given arguments, returning the first value. For this to make sense, the dataset should return a single row with a single value (or no rows).

[Source]

     # File lib/sequel/dataset/placeholder_literalizer.rb, line 153
153:       def get(*args)
154:         @dataset.with_sql_single_value(sql(*args))
155:       end

Return the SQL query to use for the given arguments.

[Source]

     # File lib/sequel/dataset/placeholder_literalizer.rb, line 158
158:       def sql(*args)
159:         raise Error, "wrong number of arguments (#{args.length} for #{@arity})" unless args.length == @arity
160:         s = ''
161:         ds = @dataset
162:         @fragments.each do |sql, i, transformer|
163:           s << sql
164:           if i.is_a?(Integer)
165:             v = args.fetch(i)
166:             v = transformer.call(v) if transformer
167:           else
168:             v = i.call
169:           end
170:           ds.literal_append(s, v)
171:         end
172:         if sql = @final_sql
173:           s << sql
174:         end
175:         s
176:       end

Return a new PlaceholderLiteralizer with a modified dataset. This yields the receiver‘s dataset to the block, and the block should return the new dataset to use.

[Source]

     # File lib/sequel/dataset/placeholder_literalizer.rb, line 131
131:       def with_dataset
132:         dup.instance_exec{@dataset = yield @dataset; self}
133:       end

[Validate]