Class: Dev::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/dev/Database.rb

Overview

create table Branches(Name text,Uri text,UNIQUE(Name));

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase

Returns a new instance of Database.



27
28
29
30
31
32
33
34
35
36
37
38
39
# 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"
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



12
13
14
# File 'lib/dev/Database.rb', line 12

def db
  @db
end

Class Method Details

.filenameObject



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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dev/Database.rb', line 51

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



106
107
108
109
110
111
112
113
114
# File 'lib/dev/Database.rb', line 106

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



98
99
100
101
102
103
104
# File 'lib/dev/Database.rb', line 98

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



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
92
93
94
95
96
# File 'lib/dev/Database.rb', line 67

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

#has_result(h) ⇒ Object



45
46
47
48
49
# File 'lib/dev/Database.rb', line 45

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



41
42
43
# File 'lib/dev/Database.rb', line 41

def set_branch_uri(name,uri)
  @db.execute("insert or replace into Branches (Name,Uri) VALUES ('#{name}','#{uri}');")
end