Class: Sequel::Database

Inherits:
Object show all
Defined in:
lib/widget_list/sequel.rb

Direct Known Subclasses

WidgetListActiveRecord

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#db_typeObject

Returns the value of attribute db_type.



13
14
15
# File 'lib/widget_list/sequel.rb', line 13

def db_type
  @db_type
end

#errorsObject

Returns the value of attribute errors.



7
8
9
# File 'lib/widget_list/sequel.rb', line 7

def errors
  @errors
end

#final_countObject

Returns the value of attribute final_count.



16
17
18
# File 'lib/widget_list/sequel.rb', line 16

def final_count
  @final_count
end

#final_resultsObject

Returns the value of attribute final_results.



4
5
6
# File 'lib/widget_list/sequel.rb', line 4

def final_results
  @final_results
end

#last_errorObject

Returns the value of attribute last_error.



10
11
12
# File 'lib/widget_list/sequel.rb', line 10

def last_error
  @last_error
end

#last_sqlObject

Returns the value of attribute last_sql.



19
20
21
# File 'lib/widget_list/sequel.rb', line 19

def last_sql
  @last_sql
end

Instance Method Details

#_bind(sql = '', replace_in_query = {}) ⇒ Object

Parameters:

  • replace_in_query (Object) (defaults to: {})


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/widget_list/sequel.rb', line 85

def _bind(sql='',replace_in_query={})
  if !replace_in_query.empty? && replace_in_query.class.name == 'Hash'
    tmp = {}
    replace_in_query.each { |k, v|
      new_key = ':' + k.to_s
      tmp[ new_key ] = v
    }
    sql = WidgetList::Utils::fill(tmp, sql)
  end
  sql
end

#_convert_active_record_bind(sql = '', bind = []) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/widget_list/sequel.rb', line 37

def _convert_active_record_bind(sql='',bind=[])
  unless bind.empty?
    (bind||{}).each { |v|
      sql.sub!(/\?/,v.to_s)
    }
  end
end

#_convert_bind(bind = []) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/widget_list/sequel.rb', line 21

def _convert_bind(bind=[])
  parameters = ''
  unless bind.empty?
    all = []
    (bind||{}).each { |v|
      if v.class.name.downcase == 'string'
        all << "'" + v.gsub(/'/,"\\\\'") + "'"
      else
        all << v
      end
    }
    parameters = "," + all.join(' ,')
  end
  parameters
end

#_determine_type(sql_or_obj) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/widget_list/sequel.rb', line 71

def _determine_type(sql_or_obj)
  if sql_or_obj.class.name.downcase != 'string' && sql_or_obj.class.name.to_s.split('::').last.downcase  == 'dataset'
    sql = sql_or_obj.get_sql()
  elsif sql_or_obj.class.name.downcase == 'string'
    sql = sql_or_obj
  end
end

#_execObject

@final_results.each_with_index { |id,k|

  name = @final_results['NAME'][k]
  price = @final_results['PRICE'][k]
}


63
64
65
66
67
68
69
# File 'lib/widget_list/sequel.rb', line 63

def _exec
  if block_given?
    @final_count.times {|i|
      yield i
    }
  end
end

#_get_row_value(row, fieldName) ⇒ Object



97
98
99
# File 'lib/widget_list/sequel.rb', line 97

def _get_row_value(row,fieldName)
  row.send(fieldName).to_s
end

#_select(sql_or_obj, bind = [], replace_in_query = {}, active_record_model = false, group_match = false) ⇒ Object

Parameters:

  • sql_or_obj (Object or String)

    will either take raw SQL or a Sequel object

  • bind (Array) (defaults to: [])

    will be replacements for ? ? in your query. Must be in sequence

  • replace_in_query (Hash) (defaults to: {})

    will be a traditional php bind hash ‘BIND’=>‘value’. which will replace :BIND in the query. thanks mwild



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/widget_list/sequel.rb', line 108

def _select(sql_or_obj, bind=[], replace_in_query={}, active_record_model=false, group_match=false)
  # supporting either
  # if get_database._select('select * from items where name = ? AND price > ?', ['abc', 37]) > 0
  # or
  # if get_database._select(get_database[:items].filter(:name => 'abc')) > 0
  #
  sql = ''
  sql = _determine_type(sql_or_obj)

  if self.class.name != 'WidgetListActiveRecord'

    # build csv of bind to eval below (arguments need to be like this for raw SQL passed with bind in Sequel)
    #
    parameters = _convert_bind(bind)

    # escape anything incoming in raw SQL such as bound items to create the ruby string to pass
    #
    sql.gsub!(/'/,"\\\\'")
  else

    _convert_active_record_bind(sql, bind)

  end

  sql = _bind(sql,replace_in_query)

  # build rows array['COLUMN'][0] = 1234;
  #
  first   = 1
  cnt     = 0
  tmp     = nil
  @final_results = {}
  if Rails.env == 'development'
    Rails.logger.info(sql)
  end

  if self.class.name == 'WidgetListActiveRecord'
    begin

      if $is_mongo
        if !group_match.nil?
          params = []

          if group_match.key?('match') && !group_match['match'].empty?
            group_match['match'].each { |match|
              field = match.keys.first
              if active_record_model.respond_to?(:serializers) &&  active_record_model.serializers[field].type.to_s == 'Integer'
                if match[match.keys.first].class.name == 'Hash'
                  predicate      = match[field].keys.first
                  value_original = match[field][predicate]
                  match_final = {
                      field => { predicate =>
                                     WidgetList::List.parse_inputs_for_mongo_predicates(active_record_model, field, predicate, value_original)
                      }
                  }
                else
                  match_final = { field => WidgetList::List.parse_inputs_for_mongo_predicates(active_record_model, field, predicate, value_original) }
                end

              else
                match_final = { field => WidgetList::List.parse_inputs_for_mongo_predicates(active_record_model, field, predicate, match[field]) }
              end
              params << {
                  '$match' =>  match_final
              }
            }
          end

          params << group_match['group'] if group_match.key?('group')
          params << group_match['sort']  if group_match.key?('sort')
          params << group_match['skip']  if group_match.key?('skip')
          params << group_match['limit'] if group_match.key?('limit')

          active_record_model = active_record_model.collection.aggregate(params)
        end
      end

      if $is_mongo && sql == 'count'

        cnt = active_record_model.count
        @final_results['TOTAL'] = []
        @final_results['TOTAL'][0] = cnt

      else

        if $is_mongo
          results = active_record_model.all.to_a if active_record_model.respond_to?('all')
          results = active_record_model.to_a if active_record_model.respond_to?('to_a') && !group_match.nil?
        else
          results = active_record_model.find_by_sql(sql)
        end

        (results||[]).each { |row|
          cnt += 1
          row.attributes.keys.each { |fieldName|
            @final_results[fieldName.to_s.upcase] = [] unless @final_results.key?(fieldName.to_s.upcase)
            @final_results[fieldName.to_s.upcase] << ((row.send(fieldName).nil? && row.attributes[fieldName].nil?) ? '' : _get_row_value(row,fieldName))
          } if group_match.nil?

          row.each { |key,value|
            @final_results['CNT'] = [] unless @final_results.key?('CNT')
            if key == '_id' && !value.empty?
              value.each { |k,v|
                @final_results[k.to_s.upcase] = [] unless @final_results.key?(k.to_s.upcase)
              }
            end
            if key == 'cnt'
              @final_results['CNT'] << value
            elsif key == '_id'
              value.each { |k,v|
                @final_results[k.to_s.upcase] << v
              }
            end

          } if !group_match.nil?
        }
        @last_sql = sql_or_obj
      end

    rescue Exception => e
      cnt         = 0
      Rails.logger.info(e)
      @errors     = true
      @last_error = e.to_s
      @last_sql   = sql_or_obj
    end
  else
    eval("
      begin
        @errors = false
        self['" + sql + "' " +  parameters + "].each { |row|
          cnt += 1
          row.each { |k,v|
            if first == 1
              @final_results[k.to_s.upcase] = []
            end
            @final_results[k.to_s.upcase] << v
          }
          first = 0
        }
        @last_sql = self['" + sql + "' " +  parameters + "].get_sql
      rescue Exception => e
        Rails.logger.info(e)
        @errors = true
        @last_error = e.to_s
        @last_sql = '" + sql + "' + \"\n\n\n\" + ' With Bind => ' + bind.inspect + ' And  BindLegacy => ' + replace_in_query.inspect
      end
    ")
  end
  @final_count = cnt
  return cnt
end

#_update(sql_or_obj, bind = {}) ⇒ Object

probably not needed really



80
81
82
# File 'lib/widget_list/sequel.rb', line 80

def _update(sql_or_obj,bind={})

end