Method: Audrey::Engine::SQLite3::Query::Q0#hash_pairs

Defined in:
lib/audrey/engine/sqlite3/query/q0.rb

#hash_pairs(wheres, params) ⇒ Object


hash_pairs



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/audrey/engine/sqlite3/query/q0.rb', line 254

def hash_pairs(wheres, params)
	# $tm.hrm
	
	# loop through fields
	@fquery.fields.each do |k, vals|
		# key
		krs = Audrey::Util.randstr(40)
		params[krs] = k
		
		# if we're looking for a null value
		is_null = false
		
		# initialize clause
		clause = '(hkey=:' + krs + ')'
		
		# if specific values are wanted
		unless [*vals].include?(Audrey::Query::Q0::Defined)
			send_vals = []
			
			# force to array
			# Note: just using [*vals] doesn't work because if vals is nil
			# then [*vals] is an empty array
			if not vals.is_a?(Array)
				vals = [vals]
			end
			
			vals.each do |val|
				if val.nil?
					is_null = true
				else
					vrs = Audrey::Util.randstr(40)
					params[vrs] = val
					send_vals.push ':' + vrs
				end
			end
			
			# ors
			ors = []
			
			# send_vals
			if send_vals.any?
				ors.push 'scalar in (' + send_vals.join(', ') + ')'
			end
			
			# if null
			if is_null
				ors.push 'scalar is null'
			end
			
			# add scalar conditions to clause
			clause += ' and (' + ors.join(') or (') + ')'
		end
		
		# if looking for a null value, add condition in which the parent
		# element is selected if no such key/value pair exists
		if is_null
			null_clause = "((select count(*) from hash_pairs hp where parent=hv.parent and hkey=:#{krs} ) = 0)"
			clause = "(#{clause}) or #{null_clause}"
		end
		
		# add where clause
		wheres.push clause
	end
end