Class: Rooq::Executor
- Inherits:
-
Object
- Object
- Rooq::Executor
- Defined in:
- lib/rooq/executor.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#dialect ⇒ Object
readonly
Returns the value of attribute dialect.
-
#hooks ⇒ Object
readonly
Returns the value of attribute hooks.
Instance Method Summary collapse
- #execute(query) ⇒ Object
- #fetch_all(query) ⇒ Object
- #fetch_one(query) ⇒ Object
-
#initialize(connection, dialect: Dialect::PostgreSQL.new) ⇒ Executor
constructor
A new instance of Executor.
- #on_after_execute(&block) ⇒ Object
- #on_before_execute(&block) ⇒ Object
Constructor Details
#initialize(connection, dialect: Dialect::PostgreSQL.new) ⇒ Executor
Returns a new instance of Executor.
7 8 9 10 11 |
# File 'lib/rooq/executor.rb', line 7 def initialize(connection, dialect: Dialect::PostgreSQL.new) @connection = connection @dialect = dialect @hooks = ExecutorHooks.new end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
5 6 7 |
# File 'lib/rooq/executor.rb', line 5 def connection @connection end |
#dialect ⇒ Object (readonly)
Returns the value of attribute dialect.
5 6 7 |
# File 'lib/rooq/executor.rb', line 5 def dialect @dialect end |
#hooks ⇒ Object (readonly)
Returns the value of attribute hooks.
5 6 7 |
# File 'lib/rooq/executor.rb', line 5 def hooks @hooks end |
Instance Method Details
#execute(query) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rooq/executor.rb', line 13 def execute(query) rendered = query.to_sql(@dialect) hooks.before_execute&.call(rendered) result = @connection.exec_params(rendered.sql, rendered.params) hooks.after_execute&.call(rendered, result) result end |
#fetch_all(query) ⇒ Object
32 33 34 35 |
# File 'lib/rooq/executor.rb', line 32 def fetch_all(query) result = execute(query) result.to_a end |
#fetch_one(query) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/rooq/executor.rb', line 25 def fetch_one(query) result = execute(query) return nil if result.ntuples.zero? result[0] end |
#on_after_execute(&block) ⇒ Object
42 43 44 45 |
# File 'lib/rooq/executor.rb', line 42 def on_after_execute(&block) @hooks = @hooks.with_after_execute(block) self end |
#on_before_execute(&block) ⇒ Object
37 38 39 40 |
# File 'lib/rooq/executor.rb', line 37 def on_before_execute(&block) @hooks = @hooks.with_before_execute(block) self end |