Class: Condition::Storage::RailsDb
- Inherits:
-
Object
- Object
- Condition::Storage::RailsDb
- Defined in:
- lib/condition/storage/rails_db.rb
Constant Summary collapse
- LOG_NAME =
'SQL'
Instance Method Summary collapse
- #all(param_item) ⇒ Object
- #commit ⇒ Object
- #delete(param_item) ⇒ Object
- #exec_after(param_item) ⇒ Object
-
#initialize(db: nil) ⇒ RailsDb
constructor
A new instance of RailsDb.
- #insert(param_item, default) ⇒ Object
Constructor Details
#initialize(db: nil) ⇒ RailsDb
Returns a new instance of RailsDb.
6 7 8 |
# File 'lib/condition/storage/rails_db.rb', line 6 def initialize(db: nil) @conn = db.nil? ? ActiveRecord::Base.connection : db end |
Instance Method Details
#all(param_item) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/condition/storage/rails_db.rb', line 14 def all(param_item) sql = "SELECT * FROM #{param_item.name}" res = exec(sql) #res.to_hash convert_keys(res) end |
#commit ⇒ Object
10 11 12 |
# File 'lib/condition/storage/rails_db.rb', line 10 def commit @conn.commit_db_transaction end |
#delete(param_item) ⇒ Object
21 22 23 24 |
# File 'lib/condition/storage/rails_db.rb', line 21 def delete(param_item) sql = "DELETE FROM #{param_item.name}" exec sql end |
#exec_after(param_item) ⇒ Object
48 49 50 51 52 |
# File 'lib/condition/storage/rails_db.rb', line 48 def exec_after(param_item) param_item..each do |key| exec key end end |
#insert(param_item, default) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/condition/storage/rails_db.rb', line 26 def insert(param_item, default) default_item = default.item(param_item.name) if default prms = default_item ? default_item.params.merge(param_item.params) : param_item.params i1 = '' prms.each_pair do |k, v| if i1 != '' i1 = i1 + ', ' end i1 = i1 + k.to_s end param_item.values.each do |it| prms = default_item ? default_item.value.merge(it) : it ary = [] prms.each_pair do |k, v| ary << quote_value(v) end i2 = ary.join(',') sql = "INSERT INTO #{param_item.name.to_s} (#{i1}) VALUES (#{i2})" exec sql end end |