Module: DB

Included in:
DataSet
Defined in:
lib/sqlloader/database.rb

Overview

Copyright 2012 Ludovico Fischer

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Instance Method Summary collapse

Instance Method Details

#get_db_connection(options) ⇒ Object

Yields a database connection configured with the supplied options. This method will attempt to close the connection once the block terminates.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sqlloader/database.rb', line 20

def get_db_connection(options)
  dbname = options[:dbname]
  unless dbname
    abort  "You must specify a database name"
  end
  user = options.fetch(:user, dbname)
  password = options[:password]
  port = options.fetch(:port, 5432)
  begin
    db_connection = PG.connect(:host => 'localhost', :port => port, :dbname => dbname, :user => user, :password => password)
    yield db_connection
  ensure
    db_connection.finish unless db_connection.nil?
  end
end