-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_helper.rb
65 lines (53 loc) · 1.75 KB
/
db_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'fileutils'
require 'pp'
require 'rubygems'
require 'dbi'
require 'dbd/Pg'
require 'yaml'
# DbHelper - manages connections to the ModEncode chado database conveniently
# Change RAILS_ROOT as necessary for the location of your config files
RAILS_ROOT = "/var/www/submit" unless defined?(RAILS_ROOT)
module DbHelper
# Get database info
def database
# TODO : make more portable
if File.exists? "#{RAILS_ROOT}/config/idf2chadoxml_database.yml" then
db_definition = open("#{RAILS_ROOT}/config/idf2chadoxml_database.yml") { |f| YAML.load(f.read)
}
dbinfo = Hash.new
dbinfo[:dsn] = db_definition['ruby_dsn']
dbinfo[:user] = db_definition['user']
dbinfo[:password] = db_definition['password']
return dbinfo
else
raise Exception.new("You need an idf2chadoxml_database.yml file in your config/ directory with at least a Ruby DBI dsn.")
end
end
# Connect to database
def open_handle
dbinfo = database
db = DBI.connect(dbinfo[:dsn], dbinfo[:user], dbinfo[:password])
db.execute "BEGIN TRANSACTION"
db
end
def close_handle(db)
db.execute "ROLLBACK"
db.disconnect
end
# Test for demonstrating connection to database
def runIt
puts "running it!"
no_db_commits = false
# Connect to database
dbinfo = database
db = DBI.connect(dbinfo[:dsn], dbinfo[:user], dbinfo[:password])
db.execute("BEGIN TRANSACTION") if no_db_commits
sth_last_data_id = db.prepare("SELECT last_value FROM generic_chado.data_data_id_seq")
sth_last_data_id.execute unless no_db_commits
last_id = sth_last_data_id.fetch_hash["last_value"] unless no_db_commits
puts last_id.inspect
db.execute("ROLLBACK") if no_db_commits
db.disconnect
puts "ran."
end
end