class SeekParty::SeekPartyQueryBuilder
Attributes
params[RW]
sp_query[RW]
Public Class Methods
new(params)
click to toggle source
# File lib/seek_party/seek_party_query_builder.rb, line 7 def initialize(params) @params = params @sp_query = SPQuery.new(params: params) end
Public Instance Methods
build_query(sp_attributes)
click to toggle source
# File lib/seek_party/seek_party_query_builder.rb, line 12 def build_query(sp_attributes) return unless params build_base_queries(sp_attributes) @sp_query.build_final_query end
Private Instance Methods
build_base_queries(spattribute)
click to toggle source
# File lib/seek_party/seek_party_query_builder.rb, line 30 def build_base_queries(spattribute) spattribute.attributes.each do |attribute| if params[:search].present? full_column_name = spattribute.get_full_column_name(attribute) cast_column_name = cast_according_to_adapter(full_column_name) sanitized_sql = sanitize_sql(["#{cast_column_name} LIKE ?", "%#{params[:search].downcase}%"]) @sp_query.set_attribute_query(attribute, sanitized_sql) end # If there are other params being used other than :search # it means a where clause is needed. # TODO: Figure out a way to work with date intervals, too. spattribute.attributes.each do |attribute_deep| next unless params[attribute_deep.to_sym] build_subquery_string(attribute, attribute_deep) end end end
build_equals_query(attribute)
click to toggle source
# File lib/seek_party/seek_party_query_builder.rb, line 60 def build_equals_query(attribute) sanitize_sql(["#{cast_according_to_adapter(attribute)} = ?", @params[attribute.to_sym].to_s.downcase]) end
build_subquery_string(attribute, attribute_deep)
click to toggle source
# File lib/seek_party/seek_party_query_builder.rb, line 22 def build_subquery_string(attribute, attribute_deep) if params[:search].present? @sp_query.add_attribute_query(attribute, " AND #{build_equals_query(attribute_deep)}") else @sp_query.set_attribute_query(attribute_deep, build_equals_query(attribute_deep)) end end
cast_according_to_adapter(column_name)
click to toggle source
# File lib/seek_party/seek_party_query_builder.rb, line 50 def cast_according_to_adapter(column_name) if db_sqlite3? "LOWER(CAST(#{column_name} AS TEXT))" elsif db_postgresql? "LOWER(#{column_name}::VARCHAR)" else raise "SeekParty does not support #{ActiveRecord::Base.connection.class}." end end
db_postgresql?()
click to toggle source
# File lib/seek_party/seek_party_query_builder.rb, line 74 def db_postgresql? ActiveRecord::Base.connection.instance_of? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter rescue StandardError false end
db_sqlite3?()
click to toggle source
# File lib/seek_party/seek_party_query_builder.rb, line 68 def db_sqlite3? ActiveRecord::Base.connection.instance_of? ActiveRecord::ConnectionAdapters::SQLite3Adapter rescue StandardError false end
sanitize_sql(sql_array)
click to toggle source
# File lib/seek_party/seek_party_query_builder.rb, line 64 def sanitize_sql(sql_array) ActiveRecord::Base::sanitize_sql_array(sql_array) end