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)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Migration

create, included, #migration, 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:



64
65
66
67
68
69
70
71
72
# File 'lib/rom/sql/gateway.rb', line 64

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)
  @schema = connection.tables

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

Instance Attribute Details

#loggerObject (readonly)

Return optionally configured logger

Returns:

  • (Object)

    logger



27
28
29
# File 'lib/rom/sql/gateway.rb', line 27

def logger
  @logger
end

#schemaArray (readonly)

Returns a list of datasets inferred from table names

Returns:

  • (Array)

    array with table names



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

def schema
  @schema
end

Class Method Details

.database_file?(scheme) ⇒ Boolean

Parameters:

  • scheme (String, Symbol)

Returns:

  • (Boolean)


39
40
41
# File 'lib/rom/sql/gateway.rb', line 39

def self.database_file?(scheme)
  scheme.to_s.include?('sqlite')
end

Instance Method Details

#[](name) ⇒ Dataset

Return dataset with the given name

Parameters:

  • name (String)

    dataset name

Returns:

  • (Dataset)


88
89
90
# File 'lib/rom/sql/gateway.rb', line 88

def [](name)
  connection[name]
end

#dataset(name) ⇒ Dataset

Return dataset with the given name

Parameters:

  • name (String)

    a dataset name

Returns:

  • (Dataset)


111
112
113
# File 'lib/rom/sql/gateway.rb', line 111

def dataset(name)
  connection[name]
end

#dataset?(name) ⇒ Boolean

Check if dataset exists

Parameters:

  • name (String)

    dataset name

Returns:

  • (Boolean)


120
121
122
# File 'lib/rom/sql/gateway.rb', line 120

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

#disconnectObject

Disconnect from database



77
78
79
# File 'lib/rom/sql/gateway.rb', line 77

def disconnect
  connection.disconnect
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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rom/sql/gateway.rb', line 132

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

#use_logger(logger) ⇒ Object

Setup a logger

Parameters:

  • logger (Object)

See Also:

  • Sequel::Database#loggers


99
100
101
102
# File 'lib/rom/sql/gateway.rb', line 99

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