Class: Djoini::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/djoini/table.rb

Overview

Holds basic table info and low-level operations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Table

Returns a new instance of Table.



6
7
8
9
10
11
12
# File 'lib/djoini/table.rb', line 6

def initialize(params)
  self.name = params.fetch(:name)
  self.db = Djoini.db
  self.columns = {}

  parse_table_info
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



4
5
6
# File 'lib/djoini/table.rb', line 4

def columns
  @columns
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/djoini/table.rb', line 4

def name
  @name
end

#primary_keyObject

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

#allObject



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_allObject



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