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

Class Attribute Summary collapse

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:

    • connection (Sequel::Database)

      a connection instance

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

    • :inferrable_relations (Array<Symbol>)

      A list of dataset names that should be inferred. If this is set explicitly to an empty array relations won’t be inferred at all

    • :not_inferrable_relations (Array<Symbol>)

      A list of dataset names that should NOT be inferred

    • :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:



90
91
92
93
94
95
96
97
98
99
# File 'lib/rom/sql/gateway.rb', line 90

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

  @options = options

  super

  self.class.instance = self
end

Class Attribute Details

.instanceObject

FIXME: get rid of this and figure out a nicer way of handling migration DSL we want to have global access ONLY when running migration tasks



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

def instance
  @instance
end

Instance Attribute Details

#loggerObject (readonly)



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

def logger
  @logger
end

#migratorObject (readonly) Originally defined in module Migration

Returns the value of attribute migrator.

#optionsObject (readonly)



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

def options
  @options
end

Instance Method Details

#[](name) ⇒ Dataset

Return dataset with the given name

Thsi returns a raw Sequel database

Parameters:

  • name (String, Symbol)

    The dataset name

Returns:

  • (Dataset)


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

def [](name)
  connection[name]
end

#create_table(*args, &block) ⇒ Object

Create a table using the configured connection



190
191
192
# File 'lib/rom/sql/gateway.rb', line 190

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)


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

def dataset(name)
  connection[name]
end

#dataset?(name) ⇒ Boolean

Check if a dataset exists

Parameters:

  • name (String)

    dataset name

Returns:

  • (Boolean)


158
159
160
# File 'lib/rom/sql/gateway.rb', line 158

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

#disconnectObject

Disconnect from the gateway’s database



104
105
106
# File 'lib/rom/sql/gateway.rb', line 104

def disconnect
  connection.disconnect
end

#drop_table(*args, &block) ⇒ Object

Drops a table



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

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

#extend_command_class(klass, dataset) ⇒ Object

Extend the command class with database-specific behavior

Note: Currently, only postgres is supported.

Parameters:

  • klass (Class)

    Command class

  • dataset (Sequel::Dataset)

    A dataset that will be used



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rom/sql/gateway.rb', line 170

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

#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_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



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

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


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

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