Class: ROM::SQL::Gateway

Inherits:
Gateway
  • Object
show all
Includes:
Options, Migration
Defined in:
lib/rom/sql/gateway.rb

Overview

SQL gateway

Examples:

db = Sequel.connect(:sqlite)
gateway = ROM::SQL::Gateway.new(db)

users = gateway.dataset(:users)

Constant Summary collapse

CONNECTION_EXTENSIONS =
{
  postgres: %i(pg_array pg_json)
}.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Migration

create, included, #migration, #pending_migrations?, run, #run_migrations

Constructor Details

#connect(uri, options) ⇒ Gateway #connect(connection) ⇒ Gateway

SQL gateway interface

Examples:

gateway = ROM::SQL::Gateway.new('postgres://localhost/rom')

# or reuse connection
DB = Sequel.connect('postgres://localhost/rom')
gateway = ROM::SQL::Gateway.new(DB)

Overloads:

  • #connect(uri, options) ⇒ Gateway

    Connects to database via uri passing options

    Parameters:

    • uri (String, Symbol)

      connection URI

    • options (Hash)

      connection options

  • #connect(connection) ⇒ Gateway

    Re-uses connection instance

    Parameters:

    • connection (Sequel::Database)

      a connection instance



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rom/sql/gateway.rb', line 58

def initialize(uri, options = {})
  repo_options = self.class.option_definitions.names
  conn_options = options.reject { |k, _| repo_options.include?(k) }

  @connection = connect(uri, conn_options)
  load_extensions(Array(options[:extensions]))

  super(uri, options.reject { |k, _| conn_options.keys.include?(k) })

  self.class.instance = self
end

Class Attribute Details

.instanceObject



23
24
25
# File 'lib/rom/sql/gateway.rb', line 23

def instance
  @instance
end

Instance Attribute Details

#loggerObject (readonly)

Return optionally configured logger

Returns:

  • (Object)

    logger



35
36
37
# File 'lib/rom/sql/gateway.rb', line 35

def logger
  @logger
end

Instance Method Details

#[](name) ⇒ Dataset

Return dataset with the given name

Parameters:

  • name (String)

    dataset name

Returns:

  • (Dataset)


84
85
86
# File 'lib/rom/sql/gateway.rb', line 84

def [](name)
  connection[name]
end

#create_table(*args, &block) ⇒ Object

Create a table using the configured connection



148
149
150
# File 'lib/rom/sql/gateway.rb', line 148

def create_table(*args, &block)
  connection.create_table(*args, &block)
end

#dataset(name) ⇒ Dataset

Return dataset with the given name

Parameters:

  • name (String)

    a dataset name

Returns:

  • (Dataset)


107
108
109
# File 'lib/rom/sql/gateway.rb', line 107

def dataset(name)
  connection[name]
end

#dataset?(name) ⇒ Boolean

Check if dataset exists

Parameters:

  • name (String)

    dataset name

Returns:

  • (Boolean)


116
117
118
# File 'lib/rom/sql/gateway.rb', line 116

def dataset?(name)
  schema.include?(name)
end

#disconnectObject

Disconnect from database



73
74
75
# File 'lib/rom/sql/gateway.rb', line 73

def disconnect
  connection.disconnect
end

#drop_table(*args, &block) ⇒ Object

Drops a table



155
156
157
# File 'lib/rom/sql/gateway.rb', line 155

def drop_table(*args, &block)
  connection.drop_table(*args, &block)
end

#extend_command_class(klass, dataset) ⇒ Object

Extend database-specific behavior

Note: Currently, only postgres is supported.

Parameters:

  • klass (Class)

    command class

  • dataset (Object)

    a dataset that will be used



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rom/sql/gateway.rb', line 128

def extend_command_class(klass, dataset)
  type = dataset.db.database_type

  if type == :postgres
    ext =
      if klass < Commands::Create
        Commands::Postgres::Create
      elsif klass < Commands::Update
        Commands::Postgres::Update
      end

    klass.send(:include, ext) if ext
  end

  klass
end

#schemaArray

Returns a list of datasets inferred from table names

Returns:

  • (Array)

    array with table names



164
165
166
# File 'lib/rom/sql/gateway.rb', line 164

def schema
  @schema ||= connection.tables
end

#use_logger(logger) ⇒ Object

Setup a logger

Parameters:

  • logger (Object)

See Also:

  • Sequel::Database#loggers


95
96
97
98
# File 'lib/rom/sql/gateway.rb', line 95

def use_logger(logger)
  @logger = logger
  connection.loggers << logger
end