Class: SqlLogic::SQLArray

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_logic/sql_array.rb

Instance Method Summary collapse

Constructor Details

#initialize(sql) ⇒ SQLArray

Returns a new instance of SQLArray.



3
4
5
6
7
8
9
# File 'lib/sql_logic/sql_array.rb', line 3

def initialize(sql)
  if sql.is_a?(Hash)
    @array = [sql, "AND", nil]
  elsif sql.is_a?(Array)
    @array = sql
  end
end

Instance Method Details

#+(h) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/sql_logic/sql_array.rb', line 34

def +(h)
  if h.is_a?(Hash)
    return SQLArray.new([self, "AND", h])
  elsif h.is_a?(SQLArray)
    return SQLArray.new([self, "AND", h])
  end
end

#-(h) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/sql_logic/sql_array.rb', line 42

def -(h)
  if h.is_a?(Hash)
    return SQLArray.new([self, "OR", h])
  elsif h.is_a?(SQLArray)
    return SQLArray.new([self, "OR", h])
  end
end

#to_sObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sql_logic/sql_array.rb', line 11

def to_s
  if @array[2]==nil
    @array[0]
  else
    if @array[0].blank?
      @array[2]
    else
      ["(", @array.join(" "), ")"].join
    end
  end
end

#to_sql(record = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/sql_logic/sql_array.rb', line 23

def to_sql(record=nil)
  if @array[2]==nil
    @array[0].to_sql(record)
  else
    sql_array = SQLArrayParam.new
    left_side, right_side = [@array[0].to_sql(record), @array[2].to_sql(record)]
    sql_array.merge!(left_side, right_side, @array[1])
    return sql_array.to_a
  end
end