Class Sequel::Postgres::Dataset
In: lib/sequel/adapters/postgres.rb
Parent: Sequel::Dataset

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

Methods

Included Modules

Sequel::Postgres::DatasetMethods

Classes and Modules

Module Sequel::Postgres::Dataset::ArgumentMapper
Module Sequel::Postgres::Dataset::BindArgumentMethods
Module Sequel::Postgres::Dataset::PreparedStatementMethods

Constants

PREPARED_ARG_PLACEHOLDER = LiteralString.new('$').freeze

Public Instance methods

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

[Source]

     # File lib/sequel/adapters/postgres.rb, line 412
412:         def call(type, bind_vars={}, *values, &block)
413:           ps = to_prepared_statement(type, values)
414:           ps.extend(BindArgumentMethods)
415:           ps.call(bind_vars, &block)
416:         end

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

[Source]

     # File lib/sequel/adapters/postgres.rb, line 304
304:       def fetch_rows(sql, &block)
305:         return cursor_fetch_rows(sql, &block) if @opts[:cursor]
306:         execute(sql){|res| yield_hash_rows(res, fetch_rows_set_cols(res), &block)}
307:       end

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

[Source]

     # File lib/sequel/adapters/postgres.rb, line 420
420:         def prepare(type, name=nil, *values)
421:           ps = to_prepared_statement(type, values)
422:           ps.extend(PreparedStatementMethods)
423:           if name
424:             ps.prepared_statement_name = name
425:             db.prepared_statements[name] = ps
426:           end
427:           ps
428:         end

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

[Source]

     # File lib/sequel/adapters/postgres.rb, line 434
434:         def prepared_arg_placeholder
435:           PREPARED_ARG_PLACEHOLDER
436:         end

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.

Usage:

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

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

[Source]

     # File lib/sequel/adapters/postgres.rb, line 324
324:       def use_cursor(opts={})
325:         clone(:cursor=>{:rows_per_fetch=>1000}.merge(opts))
326:       end

[Validate]