Class: Purview::RawConnections::Base

Inherits:
Object
  • Object
show all
Includes:
Mixins::Helpers, Mixins::Logger
Defined in:
lib/purview/raw_connections/base.rb

Direct Known Subclasses

JDBC::Base, Mysql2, PG, SQLite3, TinyTds

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Logger

#logger, #logger_opts, #logger_type, #with_context_logging

Methods included from Mixins::Helpers

#blank?, #coalesced, #filter_blank_values, #filter_nil_values, #present?, #timestamp, #with_timestamp, #zero?

Constructor Details

#initialize(opts) ⇒ Base

Returns a new instance of Base.



8
9
10
11
# File 'lib/purview/raw_connections/base.rb', line 8

def initialize(opts)
  @opts = opts
  @raw_connection = new_connection
end

Class Method Details

.connect(opts) ⇒ Object



4
5
6
# File 'lib/purview/raw_connections/base.rb', line 4

def self.connect(opts)
  new(opts)
end

Instance Method Details

#disconnectObject



13
14
15
16
17
# File 'lib/purview/raw_connections/base.rb', line 13

def disconnect
  raw_connection.close
  @raw_connection = nil
  self
end

#execute(sql, opts = {}) ⇒ Object



19
20
21
22
23
# File 'lib/purview/raw_connections/base.rb', line 19

def execute(sql, opts={})
  logger.debug("Executing: #{sql}")
  result = execute_sql(sql, opts)
  structify_result(result)
end

#with_transactionObject



25
26
27
28
29
30
31
# File 'lib/purview/raw_connections/base.rb', line 25

def with_transaction
  execute_sql(BEGIN_TRANSACTION)
  yield.tap { |result| execute_sql(COMMIT_TRANSACTION) }
rescue
  execute_sql(ROLLBACK_TRANSACTION)
  raise
end