Class: PgDice::DatabaseConnection

Inherits:
Object
  • Object
show all
Includes:
LogHelper
Defined in:
lib/pgdice/database_connection.rb

Overview

Wrapper class around database connection handlers

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LogHelper

#blank?, log_duration, #squish

Constructor Details

#initialize(logger:, query_executor:, dry_run: false) ⇒ DatabaseConnection

Returns a new instance of DatabaseConnection.



11
12
13
14
15
# File 'lib/pgdice/database_connection.rb', line 11

def initialize(logger:, query_executor:, dry_run: false)
  @logger = logger
  @dry_run = dry_run
  @query_executor = query_executor
end

Instance Attribute Details

#dry_runObject (readonly)

Returns the value of attribute dry_run.



9
10
11
# File 'lib/pgdice/database_connection.rb', line 9

def dry_run
  @dry_run
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/pgdice/database_connection.rb', line 9

def logger
  @logger
end

#query_executorObject (readonly)

Returns the value of attribute query_executor.



9
10
11
# File 'lib/pgdice/database_connection.rb', line 9

def query_executor
  @query_executor
end

Instance Method Details

#execute(query) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pgdice/database_connection.rb', line 17

def execute(query)
  query = squish(query)

  if blank?(query) || dry_run
    logger.debug { "DatabaseConnection skipping query. Query: '#{query}'. Dry run: #{dry_run}" }
    return PgDice::PgResponse.new
  end

  logger.debug { "DatabaseConnection to execute query: #{query}" }
  PgDice::LogHelper.log_duration('Executing query', logger) do
    query_executor.call(query)
  end
end