Class: HuggORM::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/hugg_orm/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, column_name = 'data') ⇒ Query

Returns a new instance of Query.



6
7
8
9
10
# File 'lib/hugg_orm/query.rb', line 6

def initialize(table, column_name = 'data')
  @table = table
  @column_name = column_name
  @values = []
end

Instance Attribute Details

#_commandObject (readonly)

Returns the value of attribute _command.



3
4
5
# File 'lib/hugg_orm/query.rb', line 3

def _command
  @_command
end

#_limitObject (readonly)

Returns the value of attribute _limit.



3
4
5
# File 'lib/hugg_orm/query.rb', line 3

def _limit
  @_limit
end

#_offsetObject (readonly)

Returns the value of attribute _offset.



3
4
5
# File 'lib/hugg_orm/query.rb', line 3

def _offset
  @_offset
end

#_orderObject (readonly)

Returns the value of attribute _order.



3
4
5
# File 'lib/hugg_orm/query.rb', line 3

def _order
  @_order
end

#_returningObject (readonly)

Returns the value of attribute _returning.



3
4
5
# File 'lib/hugg_orm/query.rb', line 3

def _returning
  @_returning
end

#_whereObject (readonly)

Returns the value of attribute _where.



3
4
5
# File 'lib/hugg_orm/query.rb', line 3

def _where
  @_where
end

#column_nameObject (readonly)

Returns the value of attribute column_name.



3
4
5
# File 'lib/hugg_orm/query.rb', line 3

def column_name
  @column_name
end

#tableObject (readonly)

Returns the value of attribute table.



3
4
5
# File 'lib/hugg_orm/query.rb', line 3

def table
  @table
end

#valuesObject (readonly)

Returns the value of attribute values.



3
4
5
# File 'lib/hugg_orm/query.rb', line 3

def values
  @values
end

Instance Method Details

#countObject



52
53
54
55
# File 'lib/hugg_orm/query.rb', line 52

def count
  @_command = "SELECT COUNT(*) FROM #{@table}"
  run.getvalue(0, 0).to_i
end

#deleteObject



47
48
49
50
# File 'lib/hugg_orm/query.rb', line 47

def delete
  @_command = "DELETE FROM #{@table}"
  run.cmd_tuples
end

#insert(data) ⇒ Object



22
23
24
25
26
27
# File 'lib/hugg_orm/query.rb', line 22

def insert(data)
  @_command = "INSERT INTO #{@table} (#{@column_name}) VALUES (#{next_placeholder})"
  @values << data.to_json
  pg_result = run
  pg_result.cmd_tuples
end

#limit(limit, offset = nil) ⇒ Object



64
65
66
67
68
69
# File 'lib/hugg_orm/query.rb', line 64

def limit(limit, offset = nil)
  @_limit = "LIMIT #{next_placeholder}"
  @values << limit
  self.offset(offset) if offset
  self
end

#offset(offset) ⇒ Object



71
72
73
74
75
# File 'lib/hugg_orm/query.rb', line 71

def offset(offset)
  @_offset = "OFFSET #{next_placeholder}"
  @values << offset
  self
end

#order(*args) ⇒ Object



77
78
79
80
81
82
# File 'lib/hugg_orm/query.rb', line 77

def order(*args)
  fields = []
  args.first.each_pair { |field, order| fields << "#{wrap_json(field)} #{order}" }
  @_order = "ORDER BY #{fields.join(', ')}"
  self
end

#runObject



12
13
14
# File 'lib/hugg_orm/query.rb', line 12

def run
  HuggORM::Connection.db.exec_params(to_sql, @values)
end

#selectObject



41
42
43
44
45
# File 'lib/hugg_orm/query.rb', line 41

def select
  @_command = "SELECT #{@column_name} FROM #{@table}"
  pg_result = run
  pg_result.map { |tuple| deserialize(tuple["#{@column_name}"]) }
end

#to_sqlObject



16
17
18
19
20
# File 'lib/hugg_orm/query.rb', line 16

def to_sql
  sql = "#{@_command} #{@_where} #{@_order} #{@_limit} #{@_offset} #{@_returning}".strip
  sql.gsub!(/\s{2,}/, ' ')
  sql
end

#update(data, return_data = false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hugg_orm/query.rb', line 29

def update(data, return_data = false)
  @_command = "UPDATE #{@table} SET #{@column_name} = #{@column_name} || #{next_placeholder}::jsonb"
  @values << data.to_json

  if return_data
    @_returning = "RETURNING #{@column_name}"
    return run.map { |tuple| deserialize(tuple["#{@column_name}"]) }
  end

  run.cmd_tuples
end

#where(conditions, values = []) ⇒ Object



57
58
59
60
61
62
# File 'lib/hugg_orm/query.rb', line 57

def where(conditions, values = [])
  values.size.times { |i| conditions.sub!(/\?/, next_placeholder(i + 1)) }
  @_where = "WHERE #{conditions}"
  @values += values
  self
end