Class: Djoini::Table
- Inherits:
-
Object
- Object
- Djoini::Table
- Defined in:
- lib/djoini/table.rb
Overview
Holds basic table info and low-level operations
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#primary_key ⇒ Object
readonly
Returns the value of attribute primary_key.
Instance Method Summary collapse
- #all ⇒ Object
- #delete(params) ⇒ Object
- #delete_all ⇒ Object
-
#initialize(params) ⇒ Table
constructor
A new instance of Table.
- #insert(params) ⇒ Object
- #update(params) ⇒ Object
- #where(params) ⇒ Object
Constructor Details
Instance Attribute Details
#columns ⇒ Object
Returns the value of attribute columns.
4 5 6 |
# File 'lib/djoini/table.rb', line 4 def columns @columns end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/djoini/table.rb', line 4 def name @name end |
#primary_key ⇒ Object
Returns the value of attribute primary_key.
4 5 6 |
# File 'lib/djoini/table.rb', line 4 def primary_key @primary_key end |
Instance Method Details
#all ⇒ Object
40 41 42 |
# File 'lib/djoini/table.rb', line 40 def all db.exec "select * from #{name}" end |
#delete(params) ⇒ Object
44 45 46 |
# File 'lib/djoini/table.rb', line 44 def delete(params) db.exec "delete from #{name} where #{bind_vars(params, ', ')}" end |
#delete_all ⇒ Object
48 49 50 |
# File 'lib/djoini/table.rb', line 48 def delete_all db.exec "delete from #{name}" end |
#insert(params) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/djoini/table.rb', line 14 def insert(params) check_keys params _field_names = params.keys.join(', ') _values = params.values.map { |a| %('#{a}') }.join(', ') db.exec "insert into #{name} (#{_field_names}) values (#{_values}) returning #{primary_key}" end |
#update(params) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/djoini/table.rb', line 25 def update(params) check_keys params[:fields] db.exec "update #{name} set #{bind_vars(params[:fields], ', ')} where #{bind_vars(params[:where], ' AND ')}" end |
#where(params) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/djoini/table.rb', line 33 def where(params) check_keys params db.exec "select * from #{name} where #{bind_vars(params, ' AND ')}" end |