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/statements/configuration/returning.rb,
lib/db_mod/statements/default_method_settings.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.6'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Transaction

#end_transaction!, #start_transaction!, #transaction

Instance Attribute Details

#connPGconn (protected)

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

Returns:

  • (PGconn)

    for now, only PostgreSQL is supported



49
50
51
# File 'lib/db_mod.rb', line 49

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. This technique is required where it is not sufficient to simply define a module method on DbMod itself due to metaprogramming techniques requiring access to the module as self.

See DbMod::Create.setup and DbMod::Statements.setup

Parameters:

  • mod (Module)

    module which has had DbMod included

See Also:



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

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

  # Ensure that these definitions cascade when
  # submodules are included in subsequent submodules.
  class << mod
    define_method(:included) { |sub_mod| DbMod.included(sub_mod) }
  end
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.



88
89
90
91
92
# File 'lib/db_mod.rb', line 88

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

#db_connect!(options) ⇒ PGconn (private)

Create the database object itself.

Parameters:

Returns:

  • (PGconn)

    a new database connection



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/db_mod.rb', line 111

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:

See Also:



100
101
102
103
104
105
# File 'lib/db_mod.rb', line 100

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

Parameters:

  • sql (String)

    SQL query to execute

Returns:

  • (Object)

    SQL result set



64
65
66
67
68
69
# File 'lib/db_mod.rb', line 64

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