module Sequel::Extensions::Combine
Constants
- AGGREGATED_ROW_ALIAS
- ALLOWED_TYPES
Public Instance Methods
combine(options)
click to toggle source
# File lib/sequel/extensions/combine.rb, line 11 def combine(options) raise Sequel::DatabaseError, "Invalid adapter. PostgreSQL driver not found." unless Sequel::Postgres::USES_PG column_mappings = options.map { |type, relations| combine_columns(type, relations) } column_mapping = column_mappings.reduce({}, :merge) select_append do column_mapping.map { |column_name, query| query.as(column_name.to_sym) } end end
Private Instance Methods
aggregate_many(dataset)
click to toggle source
# File lib/sequel/extensions/combine.rb, line 36 def aggregate_many(dataset) dataset.select { COALESCE(array_to_json(array_agg(row_to_json(AGGREGATED_ROW_ALIAS))), "[]") } end
aggregate_one(dataset)
click to toggle source
# File lib/sequel/extensions/combine.rb, line 40 def aggregate_one(dataset) dataset.select { row_to_json(AGGREGATED_ROW_ALIAS) } end
combine_columns(type, relations)
click to toggle source
# File lib/sequel/extensions/combine.rb, line 23 def combine_columns(type, relations) return {} unless ALLOWED_TYPES.include?(type) relations.each_with_object({}) do |(relation_name, (dataset, key_mappings)), columns| base_query = dataset.where(key_mappings).from_self(alias: AGGREGATED_ROW_ALIAS) case type when :many columns[relation_name] = aggregate_many(base_query) when :one columns[relation_name] = aggregate_one(base_query) end end end