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] }.freeze
Instance Attribute Summary collapse
- #logger ⇒ Object readonly
- #options ⇒ Object readonly
Attributes included from Migration
Instance Method Summary collapse
-
#[](name) ⇒ Dataset
Return dataset with the given name.
-
#call(function, *args) ⇒ Object
Call a SQL function.
-
#create_table(*args, &block) ⇒ 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(*args, &block) ⇒ Object
Drops a table.
-
#initialize(uri, options = EMPTY_HASH) ⇒ SQL::Gateway
constructor
Initialize an SQL gateway.
-
#run(statement) ⇒ Object
Execute a statement.
-
#schema ⇒ Array
Returns a list of datasets inferred from table names.
-
#use_logger(logger) ⇒ Object
Setup a logger.
Methods included from Migration
#auto_migrate!, included, #migration, #pending_migrations?, #run_migrations
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
81 82 83 84 85 86 87 88 89 |
# File 'lib/rom/sql/gateway.rb', line 81 def initialize(uri, = EMPTY_HASH) @connection = connect(uri, ) load_extensions(Array([:extensions])) Notifications.trigger("configuration.gateway.connected", connection: @connection) @options = super end |
Instance Attribute Details
#logger ⇒ Object (readonly)
31 32 33 |
# File 'lib/rom/sql/gateway.rb', line 31 def logger @logger end |
#options ⇒ Object (readonly)
35 36 37 |
# File 'lib/rom/sql/gateway.rb', line 35 def @options end |
Instance Method Details
#[](name) ⇒ Dataset
Return dataset with the given name
This returns a raw Sequel database
107 108 109 |
# File 'lib/rom/sql/gateway.rb', line 107 def [](name) connection[name] end |
#call(function, *args) ⇒ Object
Call a SQL function
195 196 197 |
# File 'lib/rom/sql/gateway.rb', line 195 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
155 156 157 |
# File 'lib/rom/sql/gateway.rb', line 155 def create_table(*args, &block) connection.create_table(*args, &block) end |
#database_type ⇒ Symbol
Underlying database type
180 181 182 |
# File 'lib/rom/sql/gateway.rb', line 180 def database_type @database_type ||= connection.database_type.to_sym end |
#dataset(name) ⇒ Dataset
Return dataset with the given name
139 140 141 |
# File 'lib/rom/sql/gateway.rb', line 139 def dataset(name) connection[name] end |
#dataset?(name) ⇒ Boolean
Check if a dataset exists
148 149 150 |
# File 'lib/rom/sql/gateway.rb', line 148 def dataset?(name) schema.include?(name) end |
#disconnect ⇒ Object
Disconnect from the gateway’s database
94 95 96 |
# File 'lib/rom/sql/gateway.rb', line 94 def disconnect connection.disconnect end |
#drop_table(*args, &block) ⇒ Object
Drops a table
162 163 164 |
# File 'lib/rom/sql/gateway.rb', line 162 def drop_table(*args, &block) connection.drop_table(*args, &block) end |
#run(statement) ⇒ Object
Execute a statement
204 205 206 |
# File 'lib/rom/sql/gateway.rb', line 204 def run(statement) connection.run(statement) end |
#schema ⇒ Array
Returns a list of datasets inferred from table names
171 172 173 |
# File 'lib/rom/sql/gateway.rb', line 171 def schema @schema ||= connection.tables end |
#use_logger(logger) ⇒ Object
Setup a logger
127 128 129 130 |
# File 'lib/rom/sql/gateway.rb', line 127 def use_logger(logger) @logger = logger connection.loggers << logger end |