Module: ScopedSearch::QueryBuilder::Field
- Defined in:
- lib/scoped_search/query_builder.rb
Overview
This module gets included into the Field class to add SQL generation.
Instance Method Summary collapse
-
#construct_join_sql(key_relation, num) ⇒ Object
This method construct join statement for a key value table It assume the following table structure
---------------------------| main | | value | | key | | main_pk | | main_fk | | | | | | key_fk | | key_pk |---------------------------uniq name for the joins are needed in case that there is more than one condition on different keys in the same query. -
#construct_simple_join_sql(num) ⇒ Object
This method construct join statement for a key value table It assume the following table structure
-------------------| main | | key | | main_pk | | value | | | | main_fk |-------------------uniq name for the joins are needed in case that there is more than one condition on different keys in the same query. - #reflection_conditions(reflection) ⇒ Object
- #reflection_keys(reflection) ⇒ Object
- #to_ext_method_sql(key, operator, value, &block) ⇒ Object
-
#to_sql(operator = nil, &block) ⇒ Object
Return an SQL representation for this field.
Instance Method Details
#construct_join_sql(key_relation, num) ⇒ Object
This method construct join statement for a key value table
It assume the following table structure
---------- --------- --------
| main | | value | | key |
| main_pk | | main_fk | | |
| | | key_fk | | key_pk |
---------- --------- --------
uniq name for the joins are needed in case that there is more than one condition
on different keys in the same query.
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/scoped_search/query_builder.rb', line 382 def construct_join_sql(key_relation, num) join_sql = "" connection = klass.connection key = key_relation.to_s.singularize.to_sym key_table = definition.reflection_by_name(klass, key).table_name value_table = klass.table_name.to_s value_table_fk_key, key_table_pk = reflection_keys(definition.reflection_by_name(klass, key)) main_reflection = definition.reflection_by_name(definition.klass, relation) if main_reflection main_table = definition.klass.table_name main_table_pk, value_table_fk_main = reflection_keys(definition.reflection_by_name(definition.klass, relation)) join_sql = "\n INNER JOIN #{connection.quote_table_name(value_table)} #{value_table}_#{num} ON (#{main_table}.#{main_table_pk} = #{value_table}_#{num}.#{value_table_fk_main})" value_table = " #{value_table}_#{num}" end join_sql += "\n INNER JOIN #{connection.quote_table_name(key_table)} #{key_table}_#{num} ON (#{key_table}_#{num}.#{key_table_pk} = #{value_table}.#{value_table_fk_key}) " return join_sql end |
#construct_simple_join_sql(num) ⇒ Object
This method construct join statement for a key value table
It assume the following table structure
---------- ---------
| main | | key |
| main_pk | | value |
| | | main_fk |
---------- ---------
uniq name for the joins are needed in case that there is more than one condition
on different keys in the same query.
414 415 416 417 418 419 420 421 422 423 |
# File 'lib/scoped_search/query_builder.rb', line 414 def construct_simple_join_sql(num) connection = klass.connection key_value_table = klass.table_name main_table = definition.klass.table_name main_table_pk, value_table_fk_main = reflection_keys(definition.reflection_by_name(definition.klass, relation)) join_sql = "\n INNER JOIN #{connection.quote_table_name(key_value_table)} #{key_value_table}_#{num} ON (#{connection.quote_table_name(main_table)}.#{connection.quote_column_name(main_table_pk)} = #{key_value_table}_#{num}.#{connection.quote_column_name(value_table_fk_main)})" return join_sql end |
#reflection_conditions(reflection) ⇒ Object
432 433 434 435 436 437 438 |
# File 'lib/scoped_search/query_builder.rb', line 432 def reflection_conditions(reflection) return unless reflection conditions = reflection.[:conditions] conditions ||= "#{reflection.[:source]}_type = '#{reflection.[:source_type]}'" if reflection.[:source] && reflection.[:source_type] conditions ||= "#{reflection.try(:foreign_type)} = '#{reflection.klass}'" if reflection.[:polymorphic] " AND #{conditions}" if conditions end |
#reflection_keys(reflection) ⇒ Object
425 426 427 428 429 430 |
# File 'lib/scoped_search/query_builder.rb', line 425 def reflection_keys(reflection) pk = reflection.klass.primary_key fk = reflection.[:foreign_key] fk ||= reflection.foreign_key reflection.macro == :belongs_to ? [fk, pk] : [pk, fk] end |
#to_ext_method_sql(key, operator, value, &block) ⇒ Object
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/scoped_search/query_builder.rb', line 440 def to_ext_method_sql(key, operator, value, &block) raise ScopedSearch::QueryNotSupported, "'#{definition.klass}' doesn't respond to '#{ext_method}'" unless definition.klass.respond_to?(ext_method) begin conditions = definition.klass.send(ext_method.to_sym, key, operator, value) rescue StandardError => e raise ScopedSearch::QueryNotSupported, "external method '#{ext_method}' failed with error: #{e}" end raise ScopedSearch::QueryNotSupported, "external method '#{ext_method}' should return hash" unless conditions.kind_of?(Hash) sql = '' conditions.map do |notification, content| case notification when :include then yield(:include, content) when :joins then yield(:joins, content) when :conditions then sql = content when :parameter then content.map{|c| yield(:parameter, c)} end end return sql end |
#to_sql(operator = nil, &block) ⇒ Object
Return an SQL representation for this field. Also make sure that the relation which includes the search field is included in the SQL query.
This function may yield an :include that should be used in the ActiveRecord::Base#find call, to make sure that the field is available for the SQL query.
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/scoped_search/query_builder.rb', line 352 def to_sql(operator = nil, &block) # :yields: finder_option_type, value num = rand(1000000) connection = klass.connection if key_relation yield(:joins, construct_join_sql(key_relation, num) ) yield(:keycondition, "#{key_klass.table_name}_#{num}.#{connection.quote_column_name(key_field.to_s)} = ?") klass_table_name = relation ? "#{klass.table_name}_#{num}" : klass.table_name return "#{connection.quote_table_name(klass_table_name)}.#{connection.quote_column_name(field.to_s)}" elsif key_field yield(:joins, construct_simple_join_sql(num)) yield(:keycondition, "#{key_klass.table_name}_#{num}.#{connection.quote_column_name(key_field.to_s)} = ?") klass_table_name = relation ? "#{klass.table_name}_#{num}" : klass.table_name return "#{connection.quote_table_name(klass_table_name)}.#{connection.quote_column_name(field.to_s)}" elsif relation yield(:include, relation) end column_name = connection.quote_table_name(klass.table_name.to_s) + "." + connection.quote_column_name(field.to_s) column_name = "(#{column_name} >> #{offset*word_size} & #{2**word_size - 1})" if offset column_name end |