Class: Bizside::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/bizside/query_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQueryBuilder

Returns a new instance of QueryBuilder.



6
7
8
9
10
# File 'lib/bizside/query_builder.rb', line 6

def initialize
  @sql = ''
  @params = []
  @indent = 0
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/bizside/query_builder.rb', line 4

def params
  @params
end

#sqlObject (readonly)

Returns the value of attribute sql.



4
5
6
# File 'lib/bizside/query_builder.rb', line 4

def sql
  @sql
end

Instance Method Details

#and(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bizside/query_builder.rb', line 34

def and(*args)
  append_sql('and (')

  if block_given?
    @indent += 1
    yield self
    @indent -= 1
  else
    append(*args)
  end

  append_sql(')')
end

#append(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bizside/query_builder.rb', line 12

def append(*args)
  if args.size == 1
    if args[0].is_a?(Array)
      append(*args[0])
    elsif args[0].is_a?(QueryBuilder)
      append(args[0].to_array)
    else
      append_sql(args[0])
    end
  else
    args.each_with_index do |arg, i|
      if i == 0
        append_sql(arg)
      else
        @params << arg
      end
    end
  end

  self
end

#or(*args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bizside/query_builder.rb', line 48

def or(*args)
  append_sql('or (')

  if block_given?
    @indent += 1
    yield self
    @indent -= 1
  else
    append(*args)
  end

  append_sql(')')
end

#to_aObject



66
67
68
# File 'lib/bizside/query_builder.rb', line 66

def to_a
  to_array
end

#to_arrayObject



62
63
64
# File 'lib/bizside/query_builder.rb', line 62

def to_array
  [@sql] + @params
end