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

Instance Method Summary collapse

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')

    Parameters:

    • uri (String, Symbol)

      connection URI

  • #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])

    Parameters:

    • uri (String, Symbol)

      connection URI

    • options (Hash)

      connection options

    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))

    Parameters:

    • connection (Sequel::Database)

      a connection instance

See Also:



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

def initialize(uri, options = EMPTY_HASH)
  @connection = connect(uri, options)
  load_extensions(Array(options[:extensions]))

  @options = options

  super
end

Instance Attribute Details

#loggerObject (readonly)



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

def logger
  @logger
end

#migratorObject (readonly) Originally defined in module Migration

Returns the value of attribute migrator.

#optionsObject (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

Parameters:

  • name (String, Symbol)

    The dataset name

Returns:

  • (Dataset)


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

def [](name)
  connection[name]
end

#auto_migrate!(conf, options = EMPTY_HASH) ⇒ Object Originally defined in module Migration

#call(function, *args) ⇒ Object

Call a SQL function

Examples:

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

Parameters:

  • function (Symbol)

    Function name

  • args (Array<Object>)

    Function arguments

Returns:

  • (Object)


194
195
196
# File 'lib/rom/sql/gateway.rb', line 194

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

#create_table(*args, &block) ⇒ Object

Create a table using the configured connection



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

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

#database_typeSymbol

Underlying database type

Returns:

  • (Symbol)


179
180
181
# File 'lib/rom/sql/gateway.rb', line 179

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

#dataset(name) ⇒ Dataset

Return dataset with the given name

Parameters:

  • name (String)

    a dataset name

Returns:

  • (Dataset)


138
139
140
# File 'lib/rom/sql/gateway.rb', line 138

def dataset(name)
  connection[name]
end

#dataset?(name) ⇒ Boolean

Check if a dataset exists

Parameters:

  • name (String)

    dataset name

Returns:

  • (Boolean)


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

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

#disconnectObject

Disconnect from the gateway's database



93
94
95
# File 'lib/rom/sql/gateway.rb', line 93

def disconnect
  connection.disconnect
end

#drop_table(*args, &block) ⇒ Object

Drops a table



161
162
163
# File 'lib/rom/sql/gateway.rb', line 161

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

#migration(&block) ⇒ Object Originally defined in module Migration

Migration DSL

See Also:

#pending_migrations?Boolean Originally defined in module Migration

Check if there are any pending migrations

Returns:

  • (Boolean)

See Also:

  • pending?

#run(statement) ⇒ Object

Execute a statement

Parameters:

  • statement (String)


203
204
205
# File 'lib/rom/sql/gateway.rb', line 203

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

#run_migrations(options = {}) ⇒ Object Originally defined in module Migration

Run migrations

Examples:

rom = ROM.container(:sql, ['sqlite::memory'])
rom.gateways[:default].run_migrations

Parameters:

  • options (Hash) (defaults to: {})

    The options used by Sequel migrator

#schemaArray

Returns a list of datasets inferred from table names

Returns:

  • (Array)

    array with table names



170
171
172
# File 'lib/rom/sql/gateway.rb', line 170

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))

Parameters:

  • logger (Object)

See Also:

  • Sequel::Database#loggers


126
127
128
129
# File 'lib/rom/sql/gateway.rb', line 126

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