class Purview::RawConnections::Base

Constants

BEGIN_TRANSACTION
COMMIT_TRANSACTION
ROLLBACK_TRANSACTION

Attributes

opts[R]
raw_connection[R]

Public Class Methods

connect(opts) click to toggle source
# File lib/purview/raw_connections/base.rb, line 4
def self.connect(opts)
  new(opts)
end
new(opts) click to toggle source
# File lib/purview/raw_connections/base.rb, line 8
def initialize(opts)
  @opts = opts
  @raw_connection = new_connection
end

Public Instance Methods

disconnect() click to toggle source
# File lib/purview/raw_connections/base.rb, line 13
def disconnect
  raw_connection.close
  @raw_connection = nil
  self
end
execute(sql, opts={}) click to toggle source
# 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_transaction() { || ... } click to toggle source
# 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

Private Instance Methods

database() click to toggle source
# File lib/purview/raw_connections/base.rb, line 44
def database
  opts[:database]
end
delete?(sql) click to toggle source
# File lib/purview/raw_connections/base.rb, line 48
def delete?(sql)
  !!(sql.to_s =~ /\ADELETE/i)
end
execute_sql(sql, opts={}) click to toggle source
# File lib/purview/raw_connections/base.rb, line 52
def execute_sql(sql, opts={})
  raise %{All "#{Base}(s)" must override the "execute_sql" method}
end
extract_rows(result) click to toggle source
# File lib/purview/raw_connections/base.rb, line 56
def extract_rows(result)
  raise %{All "#{Base}(s)" must override the "extract_rows" method}
end
extract_rows_affected(result) click to toggle source
# File lib/purview/raw_connections/base.rb, line 60
def extract_rows_affected(result)
  raise %{All "#{Base}(s)" must override the "extract_rows_affected" method}
end
host() click to toggle source
# File lib/purview/raw_connections/base.rb, line 64
def host
  opts[:host]
end
insert?(sql) click to toggle source
# File lib/purview/raw_connections/base.rb, line 68
def insert?(sql)
  !!(sql.to_s =~ /\AINSERT/i)
end
new_connection() click to toggle source
# File lib/purview/raw_connections/base.rb, line 72
def new_connection
  raise %{All "#{Base}(s)" must override the "new_connection" method}
end
password() click to toggle source
# File lib/purview/raw_connections/base.rb, line 76
def password
  opts[:password]
end
port() click to toggle source
# File lib/purview/raw_connections/base.rb, line 80
def port
  opts[:port]
end
select?(sql) click to toggle source
# File lib/purview/raw_connections/base.rb, line 84
def select?(sql)
  !!(sql.to_s =~ /\ASELECT/i)
end
structify_result(result) click to toggle source
# File lib/purview/raw_connections/base.rb, line 88
def structify_result(result)
  Purview::Structs::Result.new(
    :rows => structify_rows(extract_rows(result) || []),
    :rows_affected => extract_rows_affected(result)
  )
end
structify_row(row) click to toggle source
# File lib/purview/raw_connections/base.rb, line 95
def structify_row(row)
  Purview::Structs::Row.new(row)
end
structify_rows(rows) click to toggle source
# File lib/purview/raw_connections/base.rb, line 99
def structify_rows(rows)
  rows.map { |row| structify_row(row) }
end
update?(sql) click to toggle source
# File lib/purview/raw_connections/base.rb, line 103
def update?(sql)
  !!(sql.to_s =~ /\AUPDATE/i)
end
username() click to toggle source
# File lib/purview/raw_connections/base.rb, line 107
def username
  opts[:username]
end