Method: Sequel::Dataset#with

Defined in:
lib/sequel/dataset/query.rb

#with(name, dataset, opts = OPTS) ⇒ Object

Add a common table expression (CTE) with the given name and a dataset that defines the CTE. A common table expression acts as an inline view for the query.

Options:

:args

Specify the arguments/columns for the CTE, should be an array of symbols.

:recursive

Specify that this is a recursive CTE

:materialized

Set to false to force inlining of the CTE, or true to force not inlining the CTE (PostgreSQL 12+/SQLite 3.35+).

DB[:items].with(:items, DB[:syx].where(Sequel[:name].like('A%')))
# WITH items AS (SELECT * FROM syx WHERE (name LIKE 'A%' ESCAPE '\')) SELECT * FROM items

Raises:



1132
1133
1134
1135
1136
1137
1138
1139
1140
# File 'lib/sequel/dataset/query.rb', line 1132

def with(name, dataset, opts=OPTS)
  raise(Error, 'This dataset does not support common table expressions') unless supports_cte?
  if hoist_cte?(dataset)
    s, ds = hoist_cte(dataset)
    s.with(name, ds, opts)
  else
    clone(:with=>((@opts[:with]||EMPTY_ARRAY) + [Hash[opts].merge!(:name=>name, :dataset=>dataset)]).freeze)
  end
end