Class: Dev::Database
- Inherits:
-
Object
- Object
- Dev::Database
- Defined in:
- lib/dev/Database.rb
Overview
create table Branches(Name text,Uri text,UNIQUE(Name));
Instance Attribute Summary collapse
-
#db ⇒ Object
Returns the value of attribute db.
Class Method Summary collapse
Instance Method Summary collapse
- #add_result(h) ⇒ Object
- #find_branches(pattern) ⇒ Object
- #get_branch_uri(name) ⇒ Object
- #get_results(where_hash) ⇒ Object
-
#initialize ⇒ Database
constructor
A new instance of Database.
- #set_branch_uri(name, uri) ⇒ Object
Constructor Details
#initialize ⇒ Database
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/dev/Database.rb', line 28 def initialize filename=Dev::Database.filename table_names=Dev::Database.get_table_names(filename) @db = SQLite3::Database.new filename @db.execute("create table Branches(Name text,Uri text,UNIQUE(Name));") if !table_names.include? "Branches" columns="" [:uri,:revision,:dir,:user,:machine,:ruby_version,:ruby_platform,:cmd,:status,:start_time,:end_time,:elapsed,:timeout,:timed_out,:output,:error].each { |s| columns="#{columns}," if columns.length > 0 columns="#{columns}#{s.to_s} text" } @db.execute("create table Results(#{columns});") if ! table_names.include? "Results" end |
Instance Attribute Details
#db ⇒ Object
Returns the value of attribute db.
13 14 15 |
# File 'lib/dev/Database.rb', line 13 def db @db end |
Class Method Details
.filename ⇒ Object
15 16 17 |
# File 'lib/dev/Database.rb', line 15 def self.filename return Dev::Environment.dev_root + "/dev.db" end |
.get_table_names(filename) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/dev/Database.rb', line 19 def self.get_table_names(filename) names=Array.new db = SQLite3::Database.new filename db.execute("select name from sqlite_master where type='table' ORDER BY name") do |row| names << row[0] end return names end |
Instance Method Details
#add_result(h) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dev/Database.rb', line 46 def add_result(h) columns="" values="" [:uri,:revision,:dir,:user,:machine,:ruby_version,:ruby_platform,:cmd,:status,:start_time,:end_time,:elapsed,:timeout,:timed_out,:output,:error].each { |s| sval="" sval=h[s].to_s if h.has_key?(s) sval.gsub("'","''") # need to escape single quotes for sqlite columns="#{columns}," if columns.length > 0 values="#{values}," if values.length > 0 columns="#{columns}#{s.to_s}" values="#{values}'#{sval}'" } #puts "insert or replace into Results (#{columns}) VALUES (#{values});" @db.execute("insert or replace into Results (#{columns}) VALUES (#{values});") end |
#find_branches(pattern) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/dev/Database.rb', line 101 def find_branches(pattern) names=Array.new sql="select Name from Branches where Name LIKE '#{pattern}';" sql="select Name from Branches;" if pattern.nil? || pattern.length==0 @db.execute(sql) do |row| names << row[0] if(row[0].length > 0) end return names end |
#get_branch_uri(name) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/dev/Database.rb', line 93 def get_branch_uri(name) uri="" @db.execute("select Uri from Branches where Name='#{name}';") do |row| uri=eval(row[0].to_s)[0] end return uri end |
#get_results(where_hash) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/dev/Database.rb', line 62 def get_results(where_hash) array=Array.new where="" where_hash.each{ |k,v| sval=v.to_s sval.gsub("'","''") # need to escape single quotes for sqlite where="#{where} AND " if where.length > 0 where="#{where}#{k.to_s}='#{sval}'" } columns="" [:uri,:revision,:dir,:user,:machine,:ruby_version,:ruby_platform,:cmd,:status,:start_time,:end_time,:elapsed,:timeout,:timed_out,:output,:error].each { |s| columns="#{columns}," if columns.length > 0 columns="#{columns}#{s.to_s}" } @db.execute("select #{columns} from Results where #{where};") do |row| h=Hash.new index = 0 [:uri,:revision,:dir,:user,:machine,:ruby_version,:ruby_platform,:cmd,:status,:start_time,:end_time,:elapsed,:timeout,:timed_out,:output,:error].each { |s| #puts "row[#{index}].to_s=#{row[index].to_s}" h[s]=row[index].to_s index=index+1 } array << h if !h.empty? end return array end |
#set_branch_uri(name, uri) ⇒ Object
42 43 44 |
# File 'lib/dev/Database.rb', line 42 def set_branch_uri(name,uri) @db.execute("insert or replace into Branches (Name,Uri) VALUES ('#{name}','#{uri}');") end |