Module: OrientSupport::Support

Included in:
ActiveOrient::API, ActiveOrient::OrientDB, ModelClass, Array, Hash, MatchConnection, MatchStatement, OrientQuery
Defined in:
lib/support/orientquery.rb

Instance Method Summary collapse

Instance Method Details

#as(a = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/support/orientquery.rb', line 93

def as a=nil
  if a
    @q[:as] = a   # subsequent calls overwrite older entries
  else
    if @q[:as].blank?
      nil
    else
      "as: #{ @q[:as] }"
    end
  end
end

#compose_where(*arg, &b) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/support/orientquery.rb', line 20

def compose_where *arg , &b
      arg = arg.flatten.compact

      unless arg.blank? 
g= generate_sql_list( arg , &b)
"where #{g}" unless g.empty?
      end
end

#generate_sql_list(attributes = {}, &b) ⇒ Object

designs a list of “Key = Value” pairs combined by “and” or the binding provided by the block

ORD.generate_sql_list  where: 25 , upper: '65' 
 => "where = 25 and upper = '65'"
ORD.generate_sql_list(  con_id: 25 , symbol: :G) { ',' } 
 => "con_id = 25 , symbol = 'G'"

If »NULL« should be addressed, { key: nil } is translated to “key = NULL” (used by set: in update and upsert), { key: [nil] } is translated to “key is NULL” ( used by where )



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/support/orientquery.rb', line 40

def generate_sql_list attributes = {},  &b
  fill = block_given? ? yield : 'and'
  case attributes 
  when ::Hash
    attributes.map do |key, value|
      case value
      when nil
        "#{key} =  NULL"
      when ::Array
        if value == [nil]
        "#{key} is NULL"
        else  
        "#{key} in #{value.to_orient}"
        end
      when Range
        "#{key} between #{value.first} and #{value.last} " 
      else #  String, Symbol, Time, Trueclass, Falseclass ...
        "#{key} = #{value.to_or}"
      end
    end.join(" #{fill} ")
  when ::Array
    attributes.map{|y| generate_sql_list y, &b }.join( " #{fill} " )
  when String
    attributes
  when Symbol, Numeric
    attributes.to_s
  end    
end

#where(value = nil) ⇒ Object

:nodoc:



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/support/orientquery.rb', line 80

def where  value=nil     # :nodoc:
  if value.present?
    if value.is_a?( Hash ) && value.size >1
                    value.each {| a,b| where( {a => b} ) }
    else
      @q[:where] <<  value
    end
    self
  elsif @q[:where].present?
    "where #{ generate_sql_list( @q[:where] ){ @fill || 'and' } }"
  end
end

#while_s(value = nil) ⇒ Object

while and where depend on @q, a struct



72
73
74
75
76
77
78
79
# File 'lib/support/orientquery.rb', line 72

def while_s  value=nil     # :nodoc:
  if value.present?
    @q[:while] << value
    self
  elsif @q[:while].present?
    "while #{ generate_sql_list( @q[:while] ) }"
  end
end