Class: Rhom::RhomDbAdapter
- Defined in:
- lib/rhom/rhom_db_adapter.rb,
lib/rhom/rhom_db_adapterME.rb
Constant Summary collapse
- @@database =
nil
Class Method Summary collapse
-
.close ⇒ Object
closes the database if and only if it is open.
-
.delete_all_from_table(table = nil) ⇒ Object
deletes all rows from a given table.
-
.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”.
-
.execute_sql ⇒ Object
execute a sql statement optionally, disable the factory processing which returns the result array directly.
-
.get_value_for_sql_stmt(value) ⇒ Object
generates a value for sql statement.
-
.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’);.
-
.open(dbfile = nil) ⇒ Object
maintains a single database connection.
-
.select_from_table(table = nil, columns = nil, condition = nil, params = 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’, by”=>‘object’) this would return all columns where source_id = 2 and update_type = ‘query’ ordered by the “object” column.
- .select_str(select_arr) ⇒ Object
-
.string_from_key_vals(values, delim) ⇒ Object
generates key/value list.
-
.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’;.
-
.vals_str(values) ⇒ Object
generates value clause based on hash.
-
.where_str(condition, select_arr = nil) ⇒ Object
generates where clause based on hash.
Class Method Details
.close ⇒ Object
closes the database if and only if it is open
40 41 42 43 44 45 46 47 48 |
# File 'lib/rhom/rhom_db_adapter.rb', line 40 def close if @@database #and not @@database.closed? @@database.close @@database = nil else return false end return true end |
.delete_all_from_table(table = nil) ⇒ Object
deletes all rows from a given table
189 190 191 192 193 194 195 |
# File 'lib/rhom/rhom_db_adapter.rb', line 189 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”
180 181 182 183 184 185 186 |
# File 'lib/rhom/rhom_db_adapter.rb', line 180 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 |
.execute_sql ⇒ Object
execute a sql statement optionally, disable the factory processing which returns the result array directly
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rhom/rhom_db_adapter.rb', line 53 def execute_sql(sql=nil) result = [] if sql #puts 'query is ' + 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 # execute sql statement inside of transaction # result is returned as an array of hashes # @@database.transaction unless @@database.transaction_active? # @@database.results_as_hash = true result = @@database.execute sql # @@database.commit SyncEngine::unlock_sync_mutex rescue Exception => e puts "exception when running query: #{e}" # make sure we unlock even if there's an error! SyncEngine::unlock_sync_mutex end end #puts "returned #{result.length.to_s} records..." result end |
.get_value_for_sql_stmt(value) ⇒ Object
generates a value for sql statement
118 119 120 121 122 123 124 125 126 |
# File 'lib/rhom/rhom_db_adapter.rb', line 118 def get_value_for_sql_stmt(value) if value.is_a?(String) "'#{value}'" elsif value.nil? "NULL" else "#{value}" 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’);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/rhom/rhom_db_adapter.rb', line 158 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
32 33 34 35 36 37 |
# File 'lib/rhom/rhom_db_adapter.rb', line 32 def open(dbfile=nil) #puts "DB name = " + dbfile.inspect unless @@database or dbfile.nil? @@database = SQLite3::Database.new(dbfile) end end |
.select_from_table(table = nil, columns = nil, condition = nil, params = 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
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/rhom/rhom_db_adapter.rb', line 136 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
94 95 96 97 98 99 100 |
# File 'lib/rhom/rhom_db_adapter.rb', line 94 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 |
.string_from_key_vals(values, delim) ⇒ Object
generates key/value list
109 110 111 112 113 114 115 |
# File 'lib/rhom/rhom_db_adapter.rb', line 109 def string_from_key_vals(values, delim) vals = "" values.each do |key,value| vals << " #{key} = #{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’;
202 203 204 205 206 207 208 209 |
# File 'lib/rhom/rhom_db_adapter.rb', line 202 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
103 104 105 106 |
# File 'lib/rhom/rhom_db_adapter.rb', line 103 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
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rhom/rhom_db_adapter.rb', line 80 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 #puts "where_str: #{where_str}" if where_str where_str end |