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
- #has_result(h) ⇒ Object
-
#initialize ⇒ Database
constructor
A new instance of Database.
- #set_branch_uri(name, uri) ⇒ Object
Constructor Details
#initialize ⇒ Database
Returns a new instance of Database.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/dev/Database.rb', line 27 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" @db.close @db=nil end |
Instance Attribute Details
#db ⇒ Object
Returns the value of attribute db.
12 13 14 |
# File 'lib/dev/Database.rb', line 12 def db @db end |
Class Method Details
.filename ⇒ Object
14 15 16 |
# File 'lib/dev/Database.rb', line 14 def self.filename return Dev::Environment.dev_root + "/dev.db" end |
.get_table_names(filename) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/dev/Database.rb', line 18 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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dev/Database.rb', line 56 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 = SQLite3::Database.new Dev::Database.filename @db.execute("insert or replace into Results (#{columns}) VALUES (#{values});") @db.close @db=nil end |
#find_branches(pattern) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/dev/Database.rb', line 119 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 = SQLite3::Database.new Dev::Database.filename @db.execute(sql) do |row| names << row[0] if(row[0].length > 0) end @db.close @db=nil return names end |
#get_branch_uri(name) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/dev/Database.rb', line 108 def get_branch_uri(name) uri="" @db = SQLite3::Database.new Dev::Database.filename @db.execute("select Uri from Branches where Name='#{name}';") do |row| uri=eval(row[0].to_s)[0] end @db.close @db=nil return uri end |
#get_results(where_hash) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/dev/Database.rb', line 75 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 = SQLite3::Database.new Dev::Database.filename @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 @db.close @db=nil return array end |
#has_result(h) ⇒ Object
50 51 52 53 54 |
# File 'lib/dev/Database.rb', line 50 def has_result(h) results=get_results({:machine=>h[:machine],:user=>h[:user],:start_time=>h[:start_time]}) return true if results.length > 0 return false end |
#set_branch_uri(name, uri) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/dev/Database.rb', line 43 def set_branch_uri(name,uri) @db = SQLite3::Database.new Dev::Database.filename @db.execute("insert or replace into Branches (Name,Uri) VALUES ('#{name}','#{uri}');") @db.close @db=nil end |