module Sequel::Plugins::PgRow

  1. lib/sequel/extensions/_model_pg_row.rb
  2. lib/sequel/plugins/pg_row.rb
  3. show all

The pg_row plugin allows you to use Sequel::Model classes as composite type classes, via the pg_row extension. So if you have an address table:

DB.create_table(:address) do
  String :street
  String :city
  String :zip

and a company table with an address:

DB.create_table(:company) do
  String :name
  address :address

You can create a Sequel::Model for the address table, and load the plugin, which registers the row type:

class Address < Sequel::Model(:address)
  plugin :pg_row

Then when you select from the company table (even using a plain dataset), it will return address values as instances of Address:

# => {:name=>'MS', :address=>
#  Address.load(:street=>'123 Foo St', :city=>'Bar Town', :zip=>'12345')}

If you want a lot of your models to be used as row types, you can load the plugin into Sequel::Model itself:

Sequel::Model.plugin :pg_row

And then call register_row_type in the class


Note that automatic conversion only works with the native postgres adapter. For other adapters that connect to PostgreSQL, you need to call the conversion proc manually.

In addition to returning row-valued/composite types as instances of Sequel::Model, this also lets you use model instances in datasets when inserting, updating, and filtering:

DB[:company].insert(:name=>'MS', :address=>
  Address.load(:street=>'123 Foo St', :city=>'Bar Town', :zip=>'12345'))


Public Class

  1. configure


CAST = '::'.freeze  
ROW = 'ROW'.freeze  

Public Class methods

configure (model)

When loading the extension, make sure the database has the pg_row extension loaded, load the custom database extensions, and automatically register the row type if the model has a dataset.

[show source]
# File lib/sequel/plugins/pg_row.rb, line 60
def self.configure(model)
  model.db.extension(:pg_row, :_model_pg_row)
  model.register_row_type if model.instance_variable_get(:@dataset)