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.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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" @db.close @db=nil 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
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/dev/Database.rb', line 57 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
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/dev/Database.rb', line 120 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
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/dev/Database.rb', line 109 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
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 107 |
# File 'lib/dev/Database.rb', line 76 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
51 52 53 54 55 |
# File 'lib/dev/Database.rb', line 51 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
44 45 46 47 48 49 |
# File 'lib/dev/Database.rb', line 44 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 |