Class/Module Index [+]

Quicksearch

Sequel::Postgres::DatasetMethods

Instance methods for datasets that connect to a PostgreSQL database.

Public Class Methods

extended(obj) click to toggle source

Add the disable_insert_returning! mutation method

# File lib/sequel/adapters/shared/postgres.rb, line 712
def self.extended(obj)
  obj.def_mutation_method(:disable_insert_returning)
end
included(mod) click to toggle source

Add the disable_insert_returning! mutation method

# File lib/sequel/adapters/shared/postgres.rb, line 717
def self.included(mod)
  mod.def_mutation_method(:disable_insert_returning)
end

Public Instance Methods

analyze() click to toggle source

Return the results of an ANALYZE query as a string

# File lib/sequel/adapters/shared/postgres.rb, line 722
def analyze
  explain(:analyze=>true)
end
complex_expression_sql_append(sql, op, args) click to toggle source

Handle converting the ruby xor operator (^) into the PostgreSQL xor operator (#).

# File lib/sequel/adapters/shared/postgres.rb, line 728
def complex_expression_sql_append(sql, op, args)
  case op
  when :^
    j = XOR_OP
    c = false
    args.each do |a|
      sql << j if c
      literal_append(sql, a)
      c ||= true
    end
  else
    super
  end
end
disable_insert_returning() click to toggle source

Disable the use of INSERT RETURNING, even if the server supports it

# File lib/sequel/adapters/shared/postgres.rb, line 744
def disable_insert_returning
  clone(:disable_insert_returning=>true)
end
explain(opts={}) click to toggle source

Return the results of an EXPLAIN query as a string

# File lib/sequel/adapters/shared/postgres.rb, line 749
def explain(opts={})
  with_sql((opts[:analyze] ? EXPLAIN_ANALYZE : EXPLAIN) + select_sql).map(QUERY_PLAN).join(CRLF)
end
for_share() click to toggle source

Return a cloned dataset which will use FOR SHARE to lock returned rows.

# File lib/sequel/adapters/shared/postgres.rb, line 754
def for_share
  lock_style(:share)
end
full_text_search(cols, terms, opts = {}) click to toggle source

PostgreSQL specific full text search syntax, using tsearch2 (included in 8.3 by default, and available for earlier versions as an add-on).

# File lib/sequel/adapters/shared/postgres.rb, line 760
def full_text_search(cols, terms, opts = {})
  lang = opts[:language] || 'simple'
  terms = terms.join(' | ') if terms.is_a?(Array)
  filter("to_tsvector(?, ?) @@ to_tsquery(?, ?)", lang, full_text_string_join(cols), lang, terms)
end
insert(*values, &block) click to toggle source

Insert given values into the database.

# File lib/sequel/adapters/shared/postgres.rb, line 767
def insert(*values, &block)
  if @opts[:returning]
    super
  elsif !@opts[:sql] && supports_insert_select?
    returning(insert_pk).insert(*values){|r| return r.values.first}
  elsif (f = opts[:from]) && !f.empty?
    v = if values.size == 1
      values.first
    elsif values.size == 2 && values.all?{|v0| v0.is_a?(Array)}
      Hash[*values.first.zip(values.last).flatten]
    else
      values
    end
    execute_insert(insert_sql(*values), :table=>f.first, :values=>v)
  else
    super
  end
end
insert_select(*values) click to toggle source

Insert a record returning the record inserted

# File lib/sequel/adapters/shared/postgres.rb, line 787
def insert_select(*values)
  return unless supports_insert_select?
  returning.insert(*values){|r| return r}
end
lock(mode, opts={}) click to toggle source

Locks all tables in the dataset's FROM clause (but not in JOINs) with the specified mode (e.g. 'EXCLUSIVE'). If a block is given, starts a new transaction, locks the table, and yields. If a block is not given just locks the tables. Note that PostgreSQL will probably raise an error if you lock the table outside of an existing transaction. Returns nil.

# File lib/sequel/adapters/shared/postgres.rb, line 797
def lock(mode, opts={})
  if block_given? # perform locking inside a transaction and yield to block
    @db.transaction(opts){lock(mode, opts); yield}
  else
    @db.execute(LOCK % [source_list(@opts[:from]), mode], opts) # lock without a transaction
  end
  nil
end
multi_insert_sql(columns, values) click to toggle source

For PostgreSQL version > 8.2, allow inserting multiple rows at once.

# File lib/sequel/adapters/shared/postgres.rb, line 807
def multi_insert_sql(columns, values)
  return super if server_version < 80200
  
  # postgresql 8.2 introduces support for multi-row insert
  sql = LiteralString.new('VALUES ')
  expression_list_append(sql, values.map{|r| Array(r)})
  [insert_sql(columns, sql)]
end
supports_cte_in_subqueries?() click to toggle source

PostgreSQL supports using the WITH clause in subqueries if it supports using WITH at all (i.e. on PostgreSQL 8.4+).

# File lib/sequel/adapters/shared/postgres.rb, line 818
def supports_cte_in_subqueries?
  supports_cte?
end
supports_distinct_on?() click to toggle source

DISTINCT ON is a PostgreSQL extension

# File lib/sequel/adapters/shared/postgres.rb, line 823
def supports_distinct_on?
  true
end
supports_modifying_joins?() click to toggle source

PostgreSQL supports modifying joined datasets

# File lib/sequel/adapters/shared/postgres.rb, line 828
def supports_modifying_joins?
  true
end
supports_returning?(type) click to toggle source
# File lib/sequel/adapters/shared/postgres.rb, line 832
def supports_returning?(type)
  if type == :insert
    server_version >= 80200 && !opts[:disable_insert_returning]
  else
    server_version >= 80200
  end
end
supports_timestamp_timezones?() click to toggle source

PostgreSQL supports timezones in literal timestamps

# File lib/sequel/adapters/shared/postgres.rb, line 841
def supports_timestamp_timezones?
  true
end
supports_window_functions?() click to toggle source

PostgreSQL 8.4+ supports window functions

# File lib/sequel/adapters/shared/postgres.rb, line 846
def supports_window_functions?
  server_version >= 80400
end
window(name, opts) click to toggle source

Return a clone of the dataset with an addition named window that can be referenced in window functions.

# File lib/sequel/adapters/shared/postgres.rb, line 851
def window(name, opts)
  clone(:window=>(@opts[:window]||[]) + [[name, SQL::Window.new(opts)]])
end

Protected Instance Methods

_import(columns, values, opts={}) click to toggle source

If returned primary keys are requested, use RETURNING unless already set on the dataset. If RETURNING is already set, use existing returning values. If RETURNING is only set to return a single columns, return an array of just that column. Otherwise, return an array of hashes.

# File lib/sequel/adapters/shared/postgres.rb, line 861
def _import(columns, values, opts={})
  if server_version >= 80200
    if opts[:return] == :primary_key && !@opts[:returning]
      returning(insert_pk)._import(columns, values, opts)
    elsif @opts[:returning]
      statements = multi_insert_sql(columns, values)
      @db.transaction(opts.merge(:server=>@opts[:server])) do
        statements.map{|st| returning_fetch_rows(st)}
      end.first.map{|v| v.length == 1 ? v.values.first : v}
    else
      super
    end
  else
    super
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.