Class: Rubyq::QueryBuilder
- Inherits:
-
Object
- Object
- Rubyq::QueryBuilder
- Defined in:
- lib/rubyq.rb
Instance Method Summary collapse
- #and_where(condition) ⇒ Object
- #delete(name) ⇒ Object
- #distinct(flag = true) ⇒ Object
- #from(name) ⇒ Object
- #get_query ⇒ Object
- #group_by(field) ⇒ Object
-
#initialize ⇒ QueryBuilder
constructor
A new instance of QueryBuilder.
- #or_where(condition) ⇒ Object
- #order_by(field, sort = nil) ⇒ Object
- #select(fields) ⇒ Object
- #set(field, value) ⇒ Object
- #update(name) ⇒ Object
- #where(condition) ⇒ Object
Constructor Details
#initialize ⇒ QueryBuilder
Returns a new instance of QueryBuilder.
5 6 7 |
# File 'lib/rubyq.rb', line 5 def initialize reset end |
Instance Method Details
#and_where(condition) ⇒ Object
59 60 61 62 63 |
# File 'lib/rubyq.rb', line 59 def and_where(condition) @parts[:where].push({ condition: condition, operator: 'AND' }) self end |
#delete(name) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/rubyq.rb', line 25 def delete(name) @type = :delete @parts[:from] = name self end |
#distinct(flag = true) ⇒ Object
33 34 35 36 37 |
# File 'lib/rubyq.rb', line 33 def distinct(flag = true) @parts[:distinct] = flag self end |
#from(name) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/rubyq.rb', line 39 def from(name) raise 'This method only for SELECT' unless @type == :select @parts[:from].push(name) self end |
#get_query ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rubyq.rb', line 83 def get_query query = case @type when :select then build_query_for_select when :update then build_query_for_update when :delete then build_query_for_delete end reset query end |
#group_by(field) ⇒ Object
77 78 79 80 81 |
# File 'lib/rubyq.rb', line 77 def group_by(field) @parts[:group_by].push(field) self end |
#or_where(condition) ⇒ Object
65 66 67 68 69 |
# File 'lib/rubyq.rb', line 65 def or_where(condition) @parts[:where].push({ condition: condition, operator: 'OR' }) self end |
#order_by(field, sort = nil) ⇒ Object
71 72 73 74 75 |
# File 'lib/rubyq.rb', line 71 def order_by(field, sort = nil) @parts[:order_by].push({ field: field, sort: sort }) self end |
#select(fields) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/rubyq.rb', line 9 def select(fields) @type = :select @parts[:select] = [*fields] self end |
#set(field, value) ⇒ Object
47 48 49 50 51 |
# File 'lib/rubyq.rb', line 47 def set(field, value) @parts[:set].push({ field: field, value: value }) self end |
#update(name) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/rubyq.rb', line 17 def update(name) @type = :update @parts[:from] = name self end |
#where(condition) ⇒ Object
53 54 55 56 57 |
# File 'lib/rubyq.rb', line 53 def where(condition) @parts[:where] = [ { condition: condition, operator: 'AND' } ] self end |