Module: Yaml::QueryBuilder

Included in:
Orms::ActiveRecordVersion2
Defined in:
lib/yaml/query_builder.rb,
lib/yaml/query_builder/sql/mysql.rb,
lib/yaml/query_builder/sql/postgresql.rb

Defined Under Namespace

Modules: Sql

Instance Method Summary collapse

Instance Method Details

#__attempt_to_load__(klass) ⇒ Object



125
126
127
# File 'lib/yaml/query_builder.rb', line 125

def __attempt_to_load__(klass)
   klass.constantize
end

#__build_individual_yaml_conditions__(field, k, v, skip_key = false) ⇒ Object



29
30
31
32
33
# File 'lib/yaml/query_builder.rb', line 29

def __build_individual_yaml_conditions__(field, k,v, skip_key = false)
  return "1 = 1" if v.is_a?(Yaml::NotSerializedField)
  key_s = skip_key ? '' : "#{k}: "
  "#{field} LIKE '%#{key_s}#{v.is_a?(Symbol) ? ':' + v.to_s : __serialize__to_yaml_value__(v)}%' "
end

#__build_yaml_attributes__(field, yaml_conditions, skip_key = false) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/yaml/query_builder.rb', line 45

def __build_yaml_attributes__(field, yaml_conditions, skip_key = false)
  current_conditions = ''
  __filter_yaml_attributes_to_check_on__(yaml_conditions).each do |k,v|
    conditions = __resolve_yaml_conditions_by_structure__(field, k, v, skip_key)
    current_conditions = __join_yaml_conditions__(current_conditions, conditions)
  end
  current_conditions
end

#__build_yaml_conditions__(yaml_conditions) ⇒ Object



12
13
14
15
16
# File 'lib/yaml/query_builder.rb', line 12

def __build_yaml_conditions__(yaml_conditions)
  yaml_conditions.inject("") do |conditions, (serialized_field,v)|
    __join_yaml_conditions__(conditions, __build_yaml_attributes__(serialized_field, v))
  end
end

#__build_yaml_conditions_for_list__(field, k, v) ⇒ Object



24
25
26
# File 'lib/yaml/query_builder.rb', line 24

def __build_yaml_conditions_for_list__(field, k,v)
  v.map { |item| __resolve_yaml_conditions_by_structure__(field, k, item, true) }.join('AND ')
end

#__check_yaml_nested_hierarchy__(value, yaml_conditions) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/yaml/query_builder.rb', line 61

def __check_yaml_nested_hierarchy__(value, yaml_conditions)
  return true if value.nil? && yaml_conditions.nil? || yaml_conditions == '*'
  return false if value.nil? || yaml_conditions.nil?
  unless yaml_conditions.is_a?(Hash) || yaml_conditions.is_a?(Array)
    object_built_from_conds = value.is_a?(String) ? __yaml_load_object_recursively__(value) : value
    return yaml_conditions == object_built_from_conds
  end
  return __check_yaml_nested_hierarchy_on_list__(value, yaml_conditions) if yaml_conditions.is_a?(Array)
  yaml_conditions.inject(true) do |accept, (field, conditions)|
    accept &= begin
      nested_value = __yaml_method_or_key__(value, field)
      if conditions.is_a?(Hash)
        (conditions[:class].blank? || __yaml_same_class__(conditions[:class].to_s, nested_value)) &&
        __check_yaml_nested_hierarchy__(nested_value, __filter_yaml_attributes_to_check_on__(conditions.symbolize_keys!))
      else
        __check_yaml_nested_hierarchy__(nested_value, conditions)
      end
    end
  end
end

#__check_yaml_nested_hierarchy_on_list__(value, yaml_conditions) ⇒ Object



54
55
56
57
58
59
# File 'lib/yaml/query_builder.rb', line 54

def __check_yaml_nested_hierarchy_on_list__(value, yaml_conditions)
  return false if !value.is_a?(Array) || value.length != yaml_conditions.length
  yaml_conditions.zip(value).inject(true) do |result, (cond, nested_value)|
      result &= __check_yaml_nested_hierarchy__(nested_value, cond)
  end
end

#__filter_yaml_attributes_to_check_on__(yaml_conditions) ⇒ Object



136
137
138
# File 'lib/yaml/query_builder.rb', line 136

def __filter_yaml_attributes_to_check_on__(yaml_conditions)
  yaml_conditions.reject { |k,v| k.to_s == 'class' }
end

#__include_db_adapter_if_necessary__Object



4
5
6
7
8
9
10
# File 'lib/yaml/query_builder.rb', line 4

def __include_db_adapter_if_necessary__
  adapter_name = self.connection.adapter_name
  adapter_capitalized = adapter_name.upcase[0].chr + adapter_name.downcase[1..-1]
  adapter_module = ::Yaml::QueryBuilder::Sql.const_get("#{adapter_capitalized}Adapter".to_sym)
  self.send(:include, adapter_module)
  @db_adapter_included = true
end

#__join_yaml_conditions__(current_conditions, conditions) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/yaml/query_builder.rb', line 140

def __join_yaml_conditions__(current_conditions, conditions)
  if current_conditions.blank?
    conditions
  elsif conditions.blank?
    current_conditions
  else
    [ "(#{conditions})", "(#{current_conditions})" ].join(' AND ')
  end
end

#__resolve_yaml_conditions_by_structure__(field, k, v, skip_key = false) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/yaml/query_builder.rb', line 35

def __resolve_yaml_conditions_by_structure__(field, k, v, skip_key = false)
  if v.is_a?(Hash)
    __build_yaml_attributes__(field, v, skip_key)
  elsif v.is_a?(Array)
    __build_yaml_conditions_for_list__(field, k, v)
  else
    __build_individual_yaml_conditions__(field, k, v, skip_key)
  end
end

#__serialize__to_yaml_value__(v) ⇒ Object



18
19
20
21
22
# File 'lib/yaml/query_builder.rb', line 18

def __serialize__to_yaml_value__(v)
  return '' if v.nil?
  v_s = v.to_s
  v_s == '*' ? "" : v_s
end

#__yaml_deserialize__(source) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/yaml/query_builder.rb', line 112

def __yaml_deserialize__(source)
  handler = YAML.load(source) rescue nil

  unless handler.present?
    if handler.nil? && source =~ ParseObjectFromYaml
      handler_class = $1
    end
    __attempt_to_load__(handler_class || handler.class) rescue nil
    handler = YAML.load(source)
  end
  handler
end

#__yaml_load_object_recursively__(object) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/yaml/query_builder.rb', line 97

def __yaml_load_object_recursively__(object)
  yaml_object = __yaml_deserialize__(object)
  if yaml_object.respond_to?(:value) && yaml_object.respond_to?(:type_id) && yaml_object.type_id == 'struct'
    yaml_object = yaml_object.value
    yaml_object = yaml_object.keys.inject(yaml_object) do |yobject, key|
      yobject[key] = yobject[key].is_a?(String) ? __yaml_load_object_recursively__(yobject[key]) : yobject[key]
      yobject
    end
    yaml_object[:class] = yaml_object.class.to_s
    yaml_object
  else
    yaml_object
  end
end

#__yaml_method_or_key__(object, key) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/yaml/query_builder.rb', line 82

def __yaml_method_or_key__(object, key)
  value = if object.is_a?(Hash)
    object[key.to_sym] || object[key.to_s]
  elsif object.respond_to?(key.to_sym)
    object.send(key)
  end
  if value.is_a?(String)
    __yaml_load_object_recursively__(value)
  else
    value
  end
rescue
    nil
end

#__yaml_same_class__(class_string, nested_value) ⇒ Object



129
130
131
132
133
134
# File 'lib/yaml/query_builder.rb', line 129

def __yaml_same_class__(class_string, nested_value)
  klazz_s = class_string.gsub('Struct::', '')
  klazz =  klazz_s.constantize rescue nil
  struct_klazz = nested_value.class.to_s[/^Struct::(.*)$/, 1]
  klazz.present? && nested_value.is_a?(klazz) || struct_klazz.present? && struct_klazz == klazz_s
end