Class: ROM::SQL::Gateway
- Inherits:
-
Gateway
- Object
- Gateway
- ROM::SQL::Gateway
- Extended by:
- Notifications
- 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
- #logger ⇒ Object readonly
-
#migrator ⇒ Object
included
from Migration
readonly
Returns the value of attribute migrator.
- #options ⇒ Object readonly
Instance Method Summary collapse
-
#[](name) ⇒ Dataset
Return dataset with the given name.
- #auto_migrate!(setup, options = EMPTY_HASH) ⇒ Object included from Migration
-
#call(function, *args) ⇒ Object
Call a SQL function.
-
#command(klass, relation:, **opts) ⇒ Command
Build an SQL-specific command.
-
#create_table ⇒ Object
Create a table using the configured connection.
-
#database_type ⇒ Symbol
Underlying database type.
-
#dataset(name) ⇒ Dataset
Return dataset with the given name.
-
#dataset?(name) ⇒ Boolean
Check if a dataset exists.
-
#disconnect ⇒ Object
Disconnect from the gateway's database.
-
#drop_table ⇒ Object
Drops a table.
-
#initialize(uri, options = EMPTY_HASH) ⇒ SQL::Gateway
constructor
Initialize an SQL gateway.
-
#migration(&block) ⇒ Object
included
from Migration
Migration DSL.
- #notifications ⇒ Object private
-
#pending_migrations? ⇒ Boolean
included
from Migration
Check if there are any pending migrations.
-
#run(statement) ⇒ Object
Execute a statement.
-
#run_migrations(options = {}) ⇒ Object
included
from Migration
Run migrations.
-
#schema ⇒ Array
Returns a list of datasets inferred from table names.
-
#use_logger(logger) ⇒ Object
Setup a logger.
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
85 86 87 88 89 90 91 92 93 |
# File 'lib/rom/sql/gateway.rb', line 85 def initialize(uri, = EMPTY_HASH) @connection = connect(uri, ) load_extensions(Array([:extensions])) notifications.trigger("sql.gateway.connected", connection: @connection) @options = super end |
Instance Attribute Details
#logger ⇒ Object (readonly)
35 36 37 |
# File 'lib/rom/sql/gateway.rb', line 35 def logger @logger end |
#migrator ⇒ Object (readonly) Originally defined in module Migration
Returns the value of attribute migrator.
#options ⇒ Object (readonly)
39 40 41 |
# File 'lib/rom/sql/gateway.rb', line 39 def @options end |
Instance Method Details
#[](name) ⇒ Dataset
Return dataset with the given name
This returns a raw Sequel database
116 117 118 |
# File 'lib/rom/sql/gateway.rb', line 116 def [](name) connection[name] end |
#auto_migrate!(setup, options = EMPTY_HASH) ⇒ Object Originally defined in module Migration
#call(function, *args) ⇒ Object
Call a SQL function
204 205 206 |
# File 'lib/rom/sql/gateway.rb', line 204 def call(function, *args) connection[Sequel.function(function, *args)].first.values.first end |
#command(klass, relation:, **opts) ⇒ Command
Build an SQL-specific command
222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/rom/sql/gateway.rb', line 222 def command(klass, relation:, **opts) return super unless relation.dataset.db.database_type == :postgres ext = if klass < Commands::Create Postgres::Commands::Create elsif klass < Commands::Update Postgres::Commands::Update end klass.include(ext) if ext super end |
#create_table ⇒ Object
Create a table using the configured connection
164 165 166 |
# File 'lib/rom/sql/gateway.rb', line 164 def create_table(...) connection.create_table(...) end |
#database_type ⇒ Symbol
Underlying database type
189 190 191 |
# File 'lib/rom/sql/gateway.rb', line 189 def database_type @database_type ||= connection.database_type.to_sym end |
#dataset(name) ⇒ Dataset
Return dataset with the given name
148 149 150 |
# File 'lib/rom/sql/gateway.rb', line 148 def dataset(name) connection[name] end |
#dataset?(name) ⇒ Boolean
Check if a dataset exists
157 158 159 |
# File 'lib/rom/sql/gateway.rb', line 157 def dataset?(name) schema.include?(name) end |
#disconnect ⇒ Object
Disconnect from the gateway's database
103 104 105 |
# File 'lib/rom/sql/gateway.rb', line 103 def disconnect connection.disconnect end |
#drop_table ⇒ Object
Drops a table
171 172 173 |
# File 'lib/rom/sql/gateway.rb', line 171 def drop_table(...) connection.drop_table(...) end |
#migration(&block) ⇒ Object Originally defined in module Migration
Migration DSL
#notifications ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
96 97 98 |
# File 'lib/rom/sql/gateway.rb', line 96 def notifications @notifications ||= Notifications.event_bus(:sql) end |
#pending_migrations? ⇒ Boolean Originally defined in module Migration
Check if there are any pending migrations
#run(statement) ⇒ Object
Execute a statement
213 214 215 |
# File 'lib/rom/sql/gateway.rb', line 213 def run(statement) connection.run(statement) end |
#run_migrations(options = {}) ⇒ Object Originally defined in module Migration
Run migrations
#schema ⇒ Array
Returns a list of datasets inferred from table names
180 181 182 |
# File 'lib/rom/sql/gateway.rb', line 180 def schema @schema ||= connection.tables end |
#use_logger(logger) ⇒ Object
Setup a logger
136 137 138 139 |
# File 'lib/rom/sql/gateway.rb', line 136 def use_logger(logger) @logger = logger connection.loggers << logger end |