Class: Rooq::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/rooq/executor.rb

Direct Known Subclasses

ValidatingExecutor

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/rooq/executor.rb', line 5

def connection
  @connection
end

#dialectObject (readonly)

Returns the value of attribute dialect.



5
6
7
# File 'lib/rooq/executor.rb', line 5

def dialect
  @dialect
end

#hooksObject (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