Permalink
Browse files

* changed to use dbi (instead of mysql)

* this module might be removed soon...
  • Loading branch information...
1 parent 0d2dd55 commit eb013849713fb589abbab9bfd5bc412c93ed8eca @ktym ktym committed Jun 26, 2002
Showing with 46 additions and 37 deletions.
  1. +46 −37 lib/bio/io/brdb.rb
View
@@ -17,53 +17,50 @@
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
-# $Id: brdb.rb,v 1.1 2001/12/19 12:20:19 katayama Exp $
+# $Id: brdb.rb,v 1.2 2002/06/26 00:12:29 k Exp $
#
begin
- require 'mysql'
+ require 'dbi'
rescue LoadError
- module Bio; class BRDB; end; end
end
module Bio
class BRDB
- def initialize(host = 'db.bioruby.org', user = 'bioruby',
- passwd = nil, db = 'brdb', *args)
- @brdb = Mysql.new(host, user, passwd, db, *args)
+ def initialize(*args)
+ @brdb = args
end
- def close
- @brdb.close
- end
-
- def self.get(*args)
- db = self.new
- db.get(*args)
+ def fetch(db_table, entry_id)
+ DBI.connect(*@brdb) do |dbh|
+ query = "select * from #{db_table} where id = ?"
+ dbh.execute(query, entry_id).fetch_all
+ end
end
- def get(db_table, entry_id)
- query = "select * from #{db_table} where id = \'#{entry_id}\'"
- @brdb.query(query).fetch_hash
+ def insert(db_table, values)
+ if values.is_a?(Array)
+ values = values.map{ |x| '"' + DBI.quote(x) + '"' }.join(",")
+ end
+ DBI.connect(*@brdb) do |dbh|
+ query = "insert into #{db_table} values (?);"
+ dbh.execute(query, values)
+ end
end
- def insert(db_table, ary)
- values = ary.map {|x| '"' + x + '"'}.join(",")
- query = "insert into #{db_table} values (#{values});"
- @brdb.query(query)
+ def update(db_table, entry_id, values)
+ if values.is_a?(Hash)
+ values = values.to_a.map{ |k, v| "#{k}='#{DBI.quote(v)}'" }.join(',')
+ end
+ DBI.connect(*@brdb) do |dbh|
+ query = "update #{db_table} set ? where id = ?"
+ dbh.execute(query, values, entry_id)
+ end
end
- def update(db_table, entry_id, hash)
- values = []
- hash.each do |k,v|
- val = Mysql.quote(v)
- values.push("#{k}='#{val}'")
- end
- values = values.join(',')
- query = "update #{db_table} set #{values} where id = #{entry_id}"
- @brdb.query(query)
+ def search(db_table, field, keyword)
end
end
@@ -72,6 +69,23 @@ def update(db_table, entry_id, hash)
if __FILE__ == $0
+ begin
+ require 'pp'
+ alias :p :pp
+ rescue LoadError
+ end
+
+ db = 'dbi:Mysql:host=db.bioruby.org;database=genbank'
+ user = 'root'
+
+ serv = Bio::BRDB.new(db, user)
+
+ serv.fetch('ent', 'AA2CG').each do |row|
+ p row.to_h
+ end
+ serv.fetch('ft', 'AA2CG').each do |row|
+ p row.to_h
+ end
end
@@ -81,14 +95,9 @@ def update(db_table, entry_id, hash)
--- Bio::BRDB.new(*args)
- The arguments are passed through to the Mysql.new(host=nil, user=nil,
- passwd=nil, db=nil, port=nil, sock=nil, flag=nil).
-
---- Bio::BRDB.close
-
---- Bio::BRDB.get(db_table, entry_id)
---- Bio::BRDB#get(db_table, entry_id)
+--- Bio::BRDB#close
+--- Bio::BRDB#fetch(db_table, entry_id)
+--- Bio::BRDB#update(db_table, entry_id, hash)
--- Bio::BRDB#insert(db_table, ary)
---- Bio::BRDB#update(db_table, hash)
=end

0 comments on commit eb01384

Please sign in to comment.