Class: HuggORM::Query
- Inherits:
-
Object
- Object
- HuggORM::Query
- Defined in:
- lib/hugg_orm/query.rb
Instance Attribute Summary collapse
-
#_command ⇒ Object
readonly
Returns the value of attribute _command.
-
#_limit ⇒ Object
readonly
Returns the value of attribute _limit.
-
#_offset ⇒ Object
readonly
Returns the value of attribute _offset.
-
#_order ⇒ Object
readonly
Returns the value of attribute _order.
-
#_returning ⇒ Object
readonly
Returns the value of attribute _returning.
-
#_where ⇒ Object
readonly
Returns the value of attribute _where.
-
#column_name ⇒ Object
readonly
Returns the value of attribute column_name.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
- #count ⇒ Object
- #delete ⇒ Object
-
#initialize(table, column_name = 'data') ⇒ Query
constructor
A new instance of Query.
- #insert(data) ⇒ Object
- #limit(limit, offset = nil) ⇒ Object
- #offset(offset) ⇒ Object
- #order(*args) ⇒ Object
- #run ⇒ Object
- #select ⇒ Object
- #to_sql ⇒ Object
- #update(data, return_data = false) ⇒ Object
- #where(conditions, values = []) ⇒ Object
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
#_command ⇒ Object (readonly)
Returns the value of attribute _command.
3 4 5 |
# File 'lib/hugg_orm/query.rb', line 3 def _command @_command end |
#_limit ⇒ Object (readonly)
Returns the value of attribute _limit.
3 4 5 |
# File 'lib/hugg_orm/query.rb', line 3 def _limit @_limit end |
#_offset ⇒ Object (readonly)
Returns the value of attribute _offset.
3 4 5 |
# File 'lib/hugg_orm/query.rb', line 3 def _offset @_offset end |
#_order ⇒ Object (readonly)
Returns the value of attribute _order.
3 4 5 |
# File 'lib/hugg_orm/query.rb', line 3 def _order @_order end |
#_returning ⇒ Object (readonly)
Returns the value of attribute _returning.
3 4 5 |
# File 'lib/hugg_orm/query.rb', line 3 def _returning @_returning end |
#_where ⇒ Object (readonly)
Returns the value of attribute _where.
3 4 5 |
# File 'lib/hugg_orm/query.rb', line 3 def _where @_where end |
#column_name ⇒ Object (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 |
#table ⇒ Object (readonly)
Returns the value of attribute table.
3 4 5 |
# File 'lib/hugg_orm/query.rb', line 3 def table @table end |
#values ⇒ Object (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
#count ⇒ Object
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 |
#delete ⇒ Object
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 |
#run ⇒ Object
12 13 14 |
# File 'lib/hugg_orm/query.rb', line 12 def run HuggORM::Connection.db.exec_params(to_sql, @values) end |
#select ⇒ Object
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_sql ⇒ Object
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 |