Class: Rooq::Context
- Inherits:
-
Object
- Object
- Rooq::Context
- Defined in:
- lib/rooq/context.rb
Overview
Context is the main entry point for executing queries. It wraps a Configuration and provides methods for query execution.
Inspired by jOOQ's DSLContext.
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
Class Method Summary collapse
-
.using(connection, dialect: nil) ⇒ Context
Create a context from a single connection.
-
.using_pool(pool, dialect: nil) ⇒ Context
Create a context from a connection pool.
Instance Method Summary collapse
-
#execute(query) ⇒ Result
Execute a query and return a Result object.
-
#fetch_all(query) ⇒ Array<Hash>
Execute a query and return all rows as an array with symbol keys.
-
#fetch_one(query) ⇒ Hash?
Execute a query and return a single row with symbol keys.
-
#initialize(configuration) ⇒ Context
constructor
Create a context with the given configuration.
-
#transaction { ... } ⇒ Object
Execute a block within a transaction.
Constructor Details
#initialize(configuration) ⇒ Context
Create a context with the given configuration.
41 42 43 |
# File 'lib/rooq/context.rb', line 41 def initialize(configuration) @configuration = configuration end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
37 38 39 |
# File 'lib/rooq/context.rb', line 37 def configuration @configuration end |
Class Method Details
.using(connection, dialect: nil) ⇒ Context
Create a context from a single connection.
49 50 51 |
# File 'lib/rooq/context.rb', line 49 def self.using(connection, dialect: nil) new(Configuration.from_connection(connection, dialect: dialect)) end |
.using_pool(pool, dialect: nil) ⇒ Context
Create a context from a connection pool.
57 58 59 |
# File 'lib/rooq/context.rb', line 57 def self.using_pool(pool, dialect: nil) new(Configuration.from_pool(pool, dialect: dialect)) end |
Instance Method Details
#execute(query) ⇒ Result
Execute a query and return a Result object.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rooq/context.rb', line 64 def execute(query) rendered = render_query(query) converted_params = parameter_converter.convert_all(rendered.params) raw_result = @configuration.connection_provider.with_connection do |connection| connection.exec_params(rendered.sql, converted_params) end Result.new(raw_result) end |
#fetch_all(query) ⇒ Array<Hash>
Execute a query and return all rows as an array with symbol keys.
88 89 90 |
# File 'lib/rooq/context.rb', line 88 def fetch_all(query) execute(query).to_a end |
#fetch_one(query) ⇒ Hash?
Execute a query and return a single row with symbol keys.
78 79 80 81 82 83 |
# File 'lib/rooq/context.rb', line 78 def fetch_one(query) result = execute(query) return nil if result.empty? result.first end |
#transaction { ... } ⇒ Object
Execute a block within a transaction. Commits on success, rolls back on error.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rooq/context.rb', line 96 def transaction(&block) @configuration.connection_provider.with_connection do |connection| if connection.respond_to?(:transaction) connection.transaction(&block) else begin connection.exec("BEGIN") result = yield connection.exec("COMMIT") result rescue StandardError connection.exec("ROLLBACK") raise end end end end |