Class: ROM::SQL::Gateway
- Inherits:
-
Gateway
- Object
- Gateway
- ROM::SQL::Gateway
- 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 pg_hstore) }.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.
-
#create_table(*args, &block) ⇒ Object
Create a table using the configured connection.
-
#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(*args, &block) ⇒ Object
Drops a table.
-
#extend_command_class(klass, dataset) ⇒ Object
Extend the command class with database-specific behavior.
-
#initialize(uri, options = EMPTY_HASH) ⇒ SQL::Gateway
constructor
Initialize an SQL gateway.
-
#migration(&block) ⇒ Object
included
from Migration
Migration DSL.
-
#pending_migrations? ⇒ Boolean
included
from Migration
Check if there are any pending migrations.
-
#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
86 87 88 89 90 91 92 93 |
# File 'lib/rom/sql/gateway.rb', line 86 def initialize(uri, = EMPTY_HASH) @connection = connect(uri, ) load_extensions(Array([:extensions])) @options = super end |
Instance Attribute Details
#logger ⇒ Object (readonly)
28 29 30 |
# File 'lib/rom/sql/gateway.rb', line 28 def logger @logger end |
#migrator ⇒ Object (readonly) Originally defined in module Migration
Returns the value of attribute migrator.
#options ⇒ Object (readonly)
32 33 34 |
# File 'lib/rom/sql/gateway.rb', line 32 def @options end |
Instance Method Details
#[](name) ⇒ Dataset
Return dataset with the given name
Thsi returns a raw Sequel database
111 112 113 |
# File 'lib/rom/sql/gateway.rb', line 111 def [](name) connection[name] end |
#create_table(*args, &block) ⇒ Object
Create a table using the configured connection
184 185 186 |
# File 'lib/rom/sql/gateway.rb', line 184 def create_table(*args, &block) connection.create_table(*args, &block) end |
#dataset(name) ⇒ Dataset
Return dataset with the given name
143 144 145 |
# File 'lib/rom/sql/gateway.rb', line 143 def dataset(name) connection[name] end |
#dataset?(name) ⇒ Boolean
Check if a dataset exists
152 153 154 |
# File 'lib/rom/sql/gateway.rb', line 152 def dataset?(name) schema.include?(name) end |
#disconnect ⇒ Object
Disconnect from the gateway’s database
98 99 100 |
# File 'lib/rom/sql/gateway.rb', line 98 def disconnect connection.disconnect end |
#drop_table(*args, &block) ⇒ Object
Drops a table
191 192 193 |
# File 'lib/rom/sql/gateway.rb', line 191 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.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/rom/sql/gateway.rb', line 164 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
#pending_migrations? ⇒ Boolean Originally defined in module Migration
Check if there are any pending migrations
#run_migrations(options = {}) ⇒ Object Originally defined in module Migration
Run migrations
#schema ⇒ Array
Returns a list of datasets inferred from table names
200 201 202 |
# File 'lib/rom/sql/gateway.rb', line 200 def schema @schema ||= connection.tables end |
#use_logger(logger) ⇒ Object
Setup a logger
131 132 133 134 |
# File 'lib/rom/sql/gateway.rb', line 131 def use_logger(logger) @logger = logger connection.loggers << logger end |