Sha256: 043a9b6be66ee6236a180c6969d8308862c5a4d5550fe58eeea3d9c3a96f7375
Contents?: true
Size: 1.74 KB
Versions: 13
Compression:
Stored size: 1.74 KB
Contents
class Card class Query module Sorting SORT_JOIN_TO_ITEM_MAP = { left: "left_id", right: "right_id" }.freeze def sort val return nil if @superquery sort_field = val[:return] || "db_content" item = val.delete(:item) || "left" if sort_field == "count" sort_by_count val, item elsif (join_field = SORT_JOIN_TO_ITEM_MAP[item.to_sym]) sq = join_cards(val, to_field: join_field, side: "LEFT", conditions_on_join: true) @mods[:sort] ||= "#{sq.table_alias}.#{sort_field}" else raise Card::Error::BadQuery, "sort item: #{item} not yet implemented" end end # EXPERIMENTAL! def sort_by_count val, item method_name = "sort_by_count_#{item}" unless respond_to?(method_name) raise Card::Error::BadQuery, "count with item: #{item} not yet implemented" end send method_name, val end def sort_by_count_referred_to val @mods[:sort] = "coalesce(count,0)" # needed for postgres cs = Query.new( return: "coalesce(count(*), 0) as count", group: "sort_join_field", superquery: self ) subselect = Query.new val.merge(return: "id", superquery: self) cs.add_condition "referer_id in (#{subselect.sql})" # FIXME: - SQL generated before SQL phase cs.joins << Join.new( from: cs, to: %w(card_references wr referee_id) ) cs.mods[:sort_join_field] = "#{cs.table_alias}.id as sort_join_field" # HACK! joins << Join.new( from: self, to: [cs, "srtbl", "sort_join_field"] ) end end end end
Version data entries
13 entries across 13 versions & 1 rubygems