class Sequel::Postgres::Dataset

  1. lib/sequel/adapters/postgres.rb
Parent: Postgres

Dataset class for PostgreSQL datasets that use the pg, postgres, or postgres-pr driver.

Constants

APOS = Sequel::Dataset::APOS  
DatasetClass = self  
PREPARED_ARG_PLACEHOLDER = LiteralString.new('$').freeze  

Public Instance methods

call (type, bind_vars=OPTS, *values, &block)

Execute the given type of statement with the hash of values.

[show source]
# File lib/sequel/adapters/postgres.rb, line 731
def call(type, bind_vars=OPTS, *values, &block)
  ps = to_prepared_statement(type, values)
  ps.extend(BindArgumentMethods)
  ps.call(bind_vars, &block)
end
fetch_rows (sql)

Yield all rows returned by executing the given SQL and converting the types.

[show source]
# File lib/sequel/adapters/postgres.rb, line 624
def fetch_rows(sql)
  return cursor_fetch_rows(sql){|h| yield h} if @opts[:cursor]
  execute(sql){|res| yield_hash_rows(res, fetch_rows_set_cols(res)){|h| yield h}}
end
prepare (type, name=nil, *values)

Prepare the given type of statement with the given name, and store it in the database to be called later.

[show source]
# File lib/sequel/adapters/postgres.rb, line 739
def prepare(type, name=nil, *values)
  ps = to_prepared_statement(type, values)
  ps.extend(PreparedStatementMethods)
  if name
    ps.prepared_statement_name = name
    db.set_prepared_statement(name, ps)
  end
  ps
end
prepared_arg_placeholder ()

PostgreSQL uses $N for placeholders instead of ?, so use a $ as the placeholder.

[show source]
# File lib/sequel/adapters/postgres.rb, line 753
def prepared_arg_placeholder
  PREPARED_ARG_PLACEHOLDER
end
use_cursor (opts=OPTS)

Uses a cursor for fetching records, instead of fetching the entire result set at once. Can be used to process large datasets without holding all rows in memory (which is what the underlying drivers do by default). Options:

  • :rows_per_fetch - the number of rows per fetch (default 1000). Higher numbers result in fewer queries but greater memory use.

  • :cursor_name - the name assigned to the cursor (default ‘sequel_cursor’). Nested cursors require different names.

Usage:

DB[:huge_table].use_cursor.each{|row| p row}
DB[:huge_table].use_cursor(:rows_per_fetch=>10000).each{|row| p row}
DB[:huge_table].use_cursor(:cursor_name=>'my_cursor').each{|row| p row}

This is untested with the prepared statement/bound variable support, and unlikely to work with either.

[show source]
# File lib/sequel/adapters/postgres.rb, line 647
def use_cursor(opts=OPTS)
  clone(:cursor=>{:rows_per_fetch=>1000, :cursor_name => 'sequel_cursor'}.merge(opts))
end