Module: DbMod

Includes:
Transaction
Defined in:
lib/db_mod.rb,
lib/db_mod/create.rb,
lib/db_mod/version.rb,
lib/db_mod/exceptions.rb,
lib/db_mod/statements.rb,
lib/db_mod/transaction.rb,
lib/db_mod/exceptions/base.rb,
lib/db_mod/statements/prepared.rb,
lib/db_mod/statements/statement.rb,
lib/db_mod/exceptions/no_results.rb,
lib/db_mod/statements/parameters.rb,
lib/db_mod/statements/configuration.rb,
lib/db_mod/exceptions/too_many_results.rb,
lib/db_mod/statements/configuration/as.rb,
lib/db_mod/exceptions/connection_not_set.rb,
lib/db_mod/statements/configuration/as/csv.rb,
lib/db_mod/statements/configuration/single.rb,
lib/db_mod/statements/configuration/as/json.rb,
lib/db_mod/exceptions/already_in_transaction.rb,
lib/db_mod/statements/configuration/defaults.rb,
lib/db_mod/exceptions/bad_method_configuration.rb,
lib/db_mod/exceptions/duplicate_statement_name.rb,
lib/db_mod/statements/configuration/single/row.rb,
lib/db_mod/statements/configuration/single/value.rb,
lib/db_mod/statements/configuration/single/column.rb,
lib/db_mod/statements/configuration/single/required_row.rb,
lib/db_mod/statements/configuration/method_configuration.rb,
lib/db_mod/statements/configuration/single/required_value.rb

Overview

Version information

Defined Under Namespace

Modules: Create, Exceptions, Statements, Transaction

Constant Summary collapse

VERSION =

The current version of db_mod.

'0.0.5'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Transaction

#end_transaction!, #start_transaction!, #transaction

Instance Attribute Details

#connObject (protected)

Database object to be used for all database interactions in this module. Use #db_connect to initialize the object.



30
31
32
# File 'lib/db_mod.rb', line 30

def conn
  @conn
end

Class Method Details

.included(mod) ⇒ Object

When a module includes DbMod, we define some class-level functions specific to the module.



20
21
22
23
# File 'lib/db_mod.rb', line 20

def self.included(mod)
  DbMod::Create.setup(mod)
  DbMod::Statements.setup(mod)
end

Instance Method Details

#db_connect(options = {}) ⇒ Object (protected)

Create a new database connection to be used for all database interactions in this module.

Parameters:

  • options (Hash) (defaults to: {})

    database connection options

Options Hash (options):

  • :db (String)

    the name of the database to connect to

  • :host (String)

    the host server for the database. If not supplied a local posix socket connection will be attempted.

  • :port (Fixnum)

    port number the database server is listening on. Default is 5432.

  • :user (String)

    username for database authentication. If not supplied the name of the user running the script will be used (i.e. ENV)

  • :pass (String)

    password for database authentication. If not supplied then trusted authentication will be attempted.



57
58
59
60
61
# File 'lib/db_mod.rb', line 57

def db_connect(options = {})
  db_defaults! options
  @conn = db_connect! options
  self.class.prepare_all_statements(@conn)
end

#db_connect!(options) ⇒ Object (private)

Create the database object itself.

Parameters:



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/db_mod.rb', line 78

def db_connect!(options)
  PGconn.connect(
    options[:host],
    options[:port],
    '',
    '',
    options[:db],
    options[:user],
    options[:pass]
  )
end

#db_defaults!(options) ⇒ Object (private)

Load any missing options from defaults

Parameters:



68
69
70
71
72
73
# File 'lib/db_mod.rb', line 68

def db_defaults!(options)
  fail ArgumentError, 'database name :db not supplied' unless options[:db]
  options[:port] ||= 5432
  options[:user] ||= ENV['USER']
  options[:pass] ||= 'trusted?'
end

#query(sql) ⇒ Object (protected)

Shorthand for conn.query



33
34
35
36
37
38
# File 'lib/db_mod.rb', line 33

def query(sql)
  unless @conn
    fail DbMod::Exceptions::ConnectionNotSet, 'db_connect not called'
  end
  conn.query(sql)
end