Class: FC::DbBase
- Inherits:
-
Object
- Object
- FC::DbBase
- Defined in:
- lib/fc/base.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.table_fields ⇒ Object
Returns the value of attribute table_fields.
-
.table_name ⇒ Object
Returns the value of attribute table_name.
Instance Attribute Summary collapse
-
#database_fields ⇒ Object
Returns the value of attribute database_fields.
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
-
.create_from_fiels(data) ⇒ Object
make instance on fields hash.
-
.find(id) ⇒ Object
get element by id.
-
.set_table(name, fields) ⇒ Object
define table name and fields.
-
.where(cond = "1", *params) ⇒ Object
get elements array by sql condition (possibles ‘?’ placeholders).
Instance Method Summary collapse
-
#delete ⇒ Object
delete object from DB.
-
#initialize(params = {}) ⇒ DbBase
constructor
A new instance of DbBase.
-
#reload ⇒ Object
reload object from DB.
-
#save ⇒ Object
save changed fields.
Constructor Details
#initialize(params = {}) ⇒ DbBase
11 12 13 14 15 |
# File 'lib/fc/base.rb', line 11 def initialize(params = {}) self.class.table_fields.each {|key| self.send("#{key}=", params[key] || params[key.to_sym]) } @id = (params["id"] || params[:id]).to_i if params["id"] || params[:id] @database_fields = params[:database_fields] || {} end |
Class Attribute Details
.table_fields ⇒ Object
Returns the value of attribute table_fields.
8 9 10 |
# File 'lib/fc/base.rb', line 8 def table_fields @table_fields end |
.table_name ⇒ Object
Returns the value of attribute table_name.
8 9 10 |
# File 'lib/fc/base.rb', line 8 def table_name @table_name end |
Instance Attribute Details
#database_fields ⇒ Object
Returns the value of attribute database_fields.
5 6 7 |
# File 'lib/fc/base.rb', line 5 def database_fields @database_fields end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/fc/base.rb', line 5 def id @id end |
Class Method Details
.create_from_fiels(data) ⇒ Object
make instance on fields hash
29 30 31 32 33 |
# File 'lib/fc/base.rb', line 29 def self.create_from_fiels(data) # use only defined in set_table fields database_fields = data.select{|key, val| self.table_fields.include?(key.to_s)} self.new(database_fields.merge({:id => data["id"].to_s, :database_fields => database_fields})) end |
.find(id) ⇒ Object
get element by id
36 37 38 39 40 |
# File 'lib/fc/base.rb', line 36 def self.find(id) r = FC::DB.query("SELECT * FROM #{self.table_name} WHERE id=#{id.to_i}") raise "Record not found (#{self.table_name}.id=#{id})" if r.count == 0 self.create_from_fiels(r.first) end |
.set_table(name, fields) ⇒ Object
define table name and fields
22 23 24 25 26 |
# File 'lib/fc/base.rb', line 22 def self.set_table(name, fields) @table_name = name self.table_fields = fields.split(',').map{|e| e.gsub(' ','')} self.table_fields.each{|e| attr_accessor e.to_sym} end |
.where(cond = "1", *params) ⇒ Object
get elements array by sql condition (possibles ‘?’ placeholders)
43 44 45 46 47 48 |
# File 'lib/fc/base.rb', line 43 def self.where(cond = "1", *params) i = -1 sql = "SELECT * FROM #{self.table_name} WHERE #{cond.gsub('?'){i+=1; "'#{Mysql2::Client.escape(params[i].to_s)}'"}}" r = FC::DB.query(sql) r.map{|data| self.create_from_fiels(data)} end |
Instance Method Details
#delete ⇒ Object
delete object from DB
83 84 85 |
# File 'lib/fc/base.rb', line 83 def delete FC::DB.query("DELETE FROM #{self.class.table_name} WHERE id=#{@id.to_i}") if @id end |
#reload ⇒ Object
reload object from DB
75 76 77 78 79 80 |
# File 'lib/fc/base.rb', line 75 def reload raise "Can't reload object without id" if !@id || @id.to_i == 0 new_obj = self.class.find(@id) self.database_fields = new_obj.database_fields self.class.table_fields.each {|key| self.send("#{key}=", new_obj.send(key)) } end |
#save ⇒ Object
save changed fields
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/fc/base.rb', line 51 def save sql = @id.to_i != 0 ? "UPDATE #{self.class.table_name} SET " : "INSERT IGNORE INTO #{self.class.table_name} SET " fields = [] self.class.table_fields.each do |key| db_val = @database_fields[key] val = self.send(key) val = 1 if val == true val = 0 if val == false if val.to_s != db_val.to_s || val.nil? && !db_val.nil? || !val.nil? && db_val.nil? fields << "#{key}=#{val ? (val.class == String ? "'#{FC::DB.connect.escape(val)}'" : val.to_i) : 'NULL'}" end end if fields.length > 0 sql << fields.join(',') sql << " WHERE id=#{@id.to_i}" if @id FC::DB.query(sql) @id = FC::DB.connect.last_id unless @id self.class.table_fields.each do |key| @database_fields[key] = self.send(key) end end end |