Class: ROM::SQL::Gateway

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

Overview

SQL gateway

Constant Summary collapse

CONNECTION_EXTENSIONS =
{
  postgres: i[pg_array pg_json pg_enum]
}.freeze

Instance Attribute Summary collapse

Attributes included from Migration

#migrator

Instance Method Summary collapse

Methods included from Migration

#auto_migrate!, included, #migration, #pending_migrations?, #run_migrations

Constructor Details

#initialize(uri) ⇒ SQL::Gateway #initialize(uri, options) ⇒ SQL::Gateway #initialize(connection) ⇒ SQL::Gateway

Initialize an SQL gateway

Gateways are typically initialized via ROM::Configuration object, gateway constructor arguments such as URI and options are passed directly to this constructor

Overloads:

  • #initialize(uri) ⇒ SQL::Gateway

    Connects to a database via URI

    Examples:

    ROM.container(:sql, 'postgres://localhost/db_name')
    
  • #initialize(uri, options) ⇒ SQL::Gateway

    Connects to a database via URI and options

    Examples:

    ROM.container(:sql, 'postgres://localhost/db_name', extensions: %w[pg_enum])
    

    Options Hash (options):

    • :extensions (Array<Symbol>) —

      A list of connection extensions supported by Sequel

    • :user (String) —

      Database username

    • :password (String) —

      Database password

  • #initialize(connection) ⇒ SQL::Gateway

    Creates a gateway from an existing database connection. This works with Sequel connections exclusively.

    Examples:

    ROM.container(:sql, Sequel.connect(:sqlite))
    

See Also:



82
83
84
85
86
87
88
89
90
# File 'lib/rom/sql/gateway.rb', line 82

def initialize(uri, options = EMPTY_HASH)
  @connection = connect(uri, options)
  load_extensions(Array(options[:extensions]))
  Notifications.trigger('configuration.gateway.connected', connection: @connection)

  @options = options

  super
end

Instance Attribute Details

#logger ⇒ Object (readonly)



31
32
33
# File 'lib/rom/sql/gateway.rb', line 31

def logger
  @logger
end

#options ⇒ Object (readonly)



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

def options
  @options
end

Instance Method Details

#[](name) ⇒ Dataset

Return dataset with the given name

This returns a raw Sequel database



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

def [](name)
  connection[name]
end

#call(function, *args) ⇒ Object

Call a SQL function

Examples:

gateway.(:upper, 'John Doe') # => "JOHN DOE"


196
197
198
# File 'lib/rom/sql/gateway.rb', line 196

def call(function, *args)
  connection[Sequel.function(function, *args)].first.values.first
end

#create_table ⇒ Object

Create a table using the configured connection



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

def create_table(...)
  connection.create_table(...)
end

#database_type ⇒ Symbol

Underlying database type



181
182
183
# File 'lib/rom/sql/gateway.rb', line 181

def database_type
  @database_type ||= connection.database_type.to_sym
end

#dataset(name) ⇒ Dataset

Return dataset with the given name



140
141
142
# File 'lib/rom/sql/gateway.rb', line 140

def dataset(name)
  connection[name]
end

#dataset?(name) ⇒ Boolean

Check if a dataset exists



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

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

#disconnect ⇒ Object

Disconnect from the gateway's database



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

def disconnect
  connection.disconnect
end

#drop_table ⇒ Object

Drops a table



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

def drop_table(...)
  connection.drop_table(...)
end

#run(statement) ⇒ Object

Execute a statement



205
206
207
# File 'lib/rom/sql/gateway.rb', line 205

def run(statement)
  connection.run(statement)
end

#schema ⇒ Array

Returns a list of datasets inferred from table names



172
173
174
# File 'lib/rom/sql/gateway.rb', line 172

def schema
  @schema ||= connection.tables
end

#use_logger(logger) ⇒ Object

Setup a logger

Examples:

set a logger during configuration process

rom = ROM.container(:sql, 'sqlite::memory') do |config|
  config.gateways[:default].use_logger(Logger.new($stdout))
end

set logger after gateway has been established

rom = ROM.container(:sql, 'sqlite::memory')
rom.gateways[:default].use_logger(Logger.new($stdout))

See Also:

  • Sequel::Database#loggers


128
129
130
131
# File 'lib/rom/sql/gateway.rb', line 128

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