Module: OrientSupport::Support

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

Instance Method Summary collapse

Instance Method Details

#compose_where(*arg, &b) ⇒ Object



19
20
21
22
23
# File 'lib/support/orientquery.rb', line 19

def compose_where *arg , &b
  arg = arg.flatten
  return "" if arg.blank? || arg.size == 1 && arg.first.blank?
  "where " + generate_sql_list( arg , &b)
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'"


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/support/orientquery.rb', line 32

def generate_sql_list attributes = {}, &b
  fill = block_given? ? yield : 'and'
  a= case attributes 
      when ::Hash
        attributes.map do |key, value|
          case value
          when ActiveOrient::Model
            "#{key} = #{value.rrid}"
          when Numeric
            "#{key} = #{value}"
          when ::Array
            "#{key} in [#{value.to_orient}]"
          when Range
            "#{key} between #{value.first} and #{value.last} " 
          when DateTime
            "#{key} = date(\'#{value.strftime("%Y%m%d%H%M%S")}\',\'yyyyMMddHHmmss\')"
          when Date
            "#{key} = date(\'#{value.to_s}\',\'yyyy-MM-dd\')"
          else #  String, Symbol, Time, Trueclass, Falseclass ...
            "#{key} = \'#{value.to_s}\'"
          end
        end.join(" #{fill} ")
      when ::Array
        attributes.map{|y| generate_sql_list y, &b }.join( " #{fill} " )
      when String
        attributes
      end    
end