Class: Rhom::RhomDbAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/rhom/rhom_db_adapter.rb

Constant Summary collapse

@@database =
nil
@@inside_transaction =
false

Class Method Summary collapse

Class Method Details

.closeObject

closes the database if and only if it is open



38
39
40
41
42
43
44
45
46
# File 'lib/rhom/rhom_db_adapter.rb', line 38

def close
  if @@database
    @@database.close
    @@database = nil
  else
    return false
  end
  return true
end

.commitObject



57
58
59
60
61
62
63
64
# File 'lib/rhom/rhom_db_adapter.rb', line 57

def commit
    begin
      @@inside_transaction = false
      @@database.commit
    rescue Exception => e
      puts "exception when commit transaction"
    end
end

.delete_all_from_table(table = nil) ⇒ Object

deletes all rows from a given table



213
214
215
216
217
218
219
# File 'lib/rhom/rhom_db_adapter.rb', line 213

def delete_all_from_table(table=nil)
  query = nil
  if table
    query = "delete from #{table}"
  end
  execute_sql query
end

.delete_from_table(table = nil, condition = nil) ⇒ Object

deletes rows from a table which satisfy condition (hash) example usage is the following: delete_from_table(‘object_values’,“object”=>“some-object”) this would execute the following sql: delete from object_values where object=“some-object”



204
205
206
207
208
209
210
# File 'lib/rhom/rhom_db_adapter.rb', line 204

def delete_from_table(table=nil,condition=nil)
  query = nil
  if table and condition
    query = "delete from #{table} where #{where_str(condition)}"
  end
  execute_sql query
end

.destroy_table(table) ⇒ Object

deletes all rows from a given table by recreating db-file and save all other tables



222
223
224
225
226
227
228
# File 'lib/rhom/rhom_db_adapter.rb', line 222

def destroy_table(table)
  query = nil
  if table
    query = "destroy #{table}"
  end
  execute_sql query
end

.execute_sql(sql = nil) ⇒ Object

execute a sql statement optionally, disable the factory processing which returns the result array directly



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rhom/rhom_db_adapter.rb', line 78

def execute_sql(sql=nil)
  result = []
  if sql
    #puts "RhomDbAdapter: Executing query - #{sql}"
    # Make sure we lock the sync engine's mutex
    # before we perform a database transaction.
    # This prevents concurrency issues.
    begin
      SyncEngine.lock_sync_mutex unless @@inside_transaction
      result = @@database.execute sql
      SyncEngine.unlock_sync_mutex unless @@inside_transaction
    rescue Exception => e
      #puts "exception when running query: #{e}"
      # make sure we unlock even if there's an error!
      if @@inside_transaction
        raise
      else
        SyncEngine.unlock_sync_mutex
      end    
    end
  end
  #puts "result is: #{result.inspect}"
  result
end

.get_value_for_sql_stmt(value) ⇒ Object

generates a value for sql statement



142
143
144
145
146
147
148
149
150
# File 'lib/rhom/rhom_db_adapter.rb', line 142

def get_value_for_sql_stmt(value)
  if value.nil? or value == 'NULL'
    "NULL"
  elsif value.is_a?(String)
    "'#{value}'"
  else
    "'#{value.to_s}'"
  end
end

.insert_into_table(table = nil, values = nil) ⇒ Object

inserts a single row into the database takes the table name and values (hash) as arguments exmaple usage is the following: insert_into_table(‘object_values, “source_id”=>1,“object”=>“some-object”,“update_type”=>’delete’) this would execute the following sql: insert into object_values (source_id,object,update_type) values (1,‘some-object’,‘delete’);



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rhom/rhom_db_adapter.rb', line 182

def insert_into_table(table=nil,values=nil)
  query = nil
  cols = ""
  vals = ""
  if table and values
    values.each do |key,val|
      value = get_value_for_sql_stmt(val)+","
      cols << "#{key},"
      vals << value
    end
    cols = cols[0..cols.length - 2]
    vals = vals[0..vals.length - 2]
    query = "insert into #{table} (#{cols}) values (#{vals})"
  end
  execute_sql query
end

.open(dbfile = nil) ⇒ Object

maintains a single database connection



31
32
33
34
35
# File 'lib/rhom/rhom_db_adapter.rb', line 31

def open(dbfile=nil)
  unless @@database or dbfile.nil?
    @@database = SQLite3::Database.new(dbfile)
  end
end

.rollbackObject



66
67
68
69
70
71
72
73
# File 'lib/rhom/rhom_db_adapter.rb', line 66

def rollback
    begin
      @@inside_transaction = false
      @@database.rollback
    rescue Exception => e
      puts "exception when rollback transaction"
    end
end

.select_from_table(table = nil, columns = nil, condition = nil, params = nil, select_arr = nil) ⇒ Object

support for select statements this function takes table name, columns (as a comma-separated list), condition (as a hash), and params (as a hash) example usage is the following: select_from_table(‘object_values’, ‘*’, “source_id”=>2,“update_type”=>‘query’,

{"order by"=>'object'})

this would return all columns where source_id = 2 and update_type = ‘query’ ordered by the “object” column



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rhom/rhom_db_adapter.rb', line 160

def select_from_table(table=nil,columns=nil,condition=nil,params=nil,select_arr=nil)
  query = nil
  if table and columns and condition
    if params and params['distinct']
      query = "select distinct #{columns} from #{table} where #{where_str(condition,select_arr)}"
    elsif params and params['order by']
      query = "select #{columns} from #{table} where #{where_str(condition,select_arr)} order by #{params['order by']}"
    else
      query = "select #{columns} from #{table} where #{where_str(condition,select_arr)}"
    end
  elsif table and columns
    query = "select #{columns} from #{table}"                     
  end
  execute_sql query
end

.select_str(select_arr) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/rhom/rhom_db_adapter.rb', line 117

def select_str(select_arr)
  select_str = ""
  select_arr.each do |attrib|
    select_str << "'#{attrib}'" + ","
  end
  select_str.length > 2 ? select_str[0..select_str.length-2] : select_str
end

.start_transactionObject



48
49
50
51
52
53
54
55
# File 'lib/rhom/rhom_db_adapter.rb', line 48

def start_transaction
    begin
      @@inside_transaction = true
      @@database.start_transaction
    rescue Exception => e
      puts "exception when start_transaction"
    end
end

.string_from_key_vals(values, delim) ⇒ Object

generates key/value list



132
133
134
135
136
137
138
139
# File 'lib/rhom/rhom_db_adapter.rb', line 132

def string_from_key_vals(values, delim)
  vals = ""
  values.each do |key,value|
    op = value.nil? ? 'is ' : '= '
    vals << " \"#{key}\" #{op} #{get_value_for_sql_stmt(value)} #{delim}"
  end
  vals
end

.update_into_table(table = nil, values = nil, condition = nil) ⇒ Object

updates values (hash) in a given table which satisfy condition (hash) example usage is the following: update_into_table(‘object_values’,“value”=>“Electronics”,“attrib”=>“industry”) this executes the following sql: update table object_values set value=‘Electronics’ where object=‘some-object’ and attrib=‘industry’;



235
236
237
238
239
240
241
242
# File 'lib/rhom/rhom_db_adapter.rb', line 235

def update_into_table(table=nil,values=nil,condition=nil)
  query = nil
  vals = values.nil? ? nil : vals_str(values)
  if table and condition and vals
    query = "update #{table} set #{vals} where #{where_str(condition)}"
  end
  execute_sql query
end

.vals_str(values) ⇒ Object

generates value clause based on hash



126
127
128
129
# File 'lib/rhom/rhom_db_adapter.rb', line 126

def vals_str(values)
  vals = string_from_key_vals(values, ",")
  vals[0..vals.length - 2]
end

.where_str(condition, select_arr = nil) ⇒ Object

generates where clause based on hash



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rhom/rhom_db_adapter.rb', line 104

def where_str(condition,select_arr=nil)
  where_str = ""
  if condition
    where_str += string_from_key_vals(condition,"and")
    where_str = where_str[0..where_str.length - 5]
  end
  
  if select_arr and select_arr.length > 0
    where_str += " and attrib in (#{select_str(select_arr)})"
  end
  where_str
end