class Ensql::SequelAdapter

Wraps a Sequel::Database to implement the {Adapter} interface for Sequel. You may want to utilize the relevant Sequel extensions to make the most of database-specific deserialization and other features. By default, uses the first database in Sequel::Databases. Other databases can be passed to the constructor.

require 'sequel'
DB = Sequel.connect('postgres://localhost/mydb')
DB.extend(:pg_json)
Ensql.adapter = Ensql::SequelAdapter.new(DB)

To stream rows, configure streaming on the connection and use {SQL.each_row}

DB = Sequel.connect('postgresql:/')
DB.extension(:pg_streaming)
DB.stream_all_queries = true
Ensql.adapter = Ensql::SequelAdapter.new(DB)
Ensql.sql("select * from large_table").each_row do  |row|
  # This now yields each row in single-row mode.
  # The connection cannot be used for other queries while this is streaming.
end

@see SUPPORTED_SEQUEL_VERSIONS

Attributes

db[R]

Public Class Methods

new(db = first_configured_database) click to toggle source

@param db [Sequel::Database]

# File lib/ensql/sequel_adapter.rb, line 65
def initialize(db = first_configured_database)
  @db = db
end
pool(db) click to toggle source

Wrap the raw connections from a Sequel::Database connection pool. This allows us to safely checkout the underlying database connection for use in a database specific adapter.

Ensql.adapter = MySqliteAdapter.new(SequelAdapter.pool)

@param db [Sequel::Database] @return [PoolWrapper] a pool adapter for raw connections

# File lib/ensql/sequel_adapter.rb, line 58
def self.pool(db)
  PoolWrapper.new do |client_block|
    db.pool.hold(&client_block)
  end
end

Public Instance Methods

fetch_count(sql) click to toggle source

@visibility private

# File lib/ensql/sequel_adapter.rb, line 82
def fetch_count(sql)
  db.execute_dui(sql)
end
fetch_each_row(sql) { |transform_keys(&:to_s)| ... } click to toggle source

@visibility private

# File lib/ensql/sequel_adapter.rb, line 75
def fetch_each_row(sql)
  return to_enum(:fetch_each_row, sql) unless block_given?

  db.fetch(sql) { |r| yield r.transform_keys(&:to_s) }
end
fetch_rows(sql) click to toggle source

@visibility private

# File lib/ensql/sequel_adapter.rb, line 70
def fetch_rows(sql)
  fetch_each_row(sql).to_a
end
literalize(value) click to toggle source

@visibility private

# File lib/ensql/sequel_adapter.rb, line 93
def literalize(value)
  db.literal(value)
end
run(sql) click to toggle source

@visibility private

# File lib/ensql/sequel_adapter.rb, line 87
def run(sql)
  db << sql
  nil
end

Private Instance Methods

first_configured_database() click to toggle source
# File lib/ensql/sequel_adapter.rb, line 101
def first_configured_database
  Sequel::DATABASES.first || raise(Error, "no database found in Sequel::DATABASES")
end