Instance methods for datasets that connect to a PostgreSQL database.
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
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
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
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 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
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
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 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 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
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
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
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
DISTINCT ON is a PostgreSQL extension
# File lib/sequel/adapters/shared/postgres.rb, line 823 def supports_distinct_on? true end
PostgreSQL supports modifying joined datasets
# File lib/sequel/adapters/shared/postgres.rb, line 828 def supports_modifying_joins? true end
# 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
PostgreSQL supports timezones in literal timestamps
# File lib/sequel/adapters/shared/postgres.rb, line 841 def supports_timestamp_timezones? true end
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
Generated with the Darkfish Rdoc Generator 2.