Skip to content

Commit

Permalink
add resulting join sqls from template in comment
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Oct 18, 2024
1 parent 3dcfa9c commit 89a556f
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/models/custom_field/order_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ def group_by_join_statement

def can_be_used_for_grouping? = field_format.in?(%w[list date bool int float string link])

# Template for all the join statements.
#
# For single value custom fields thejoin ensures single value for every
# customized object using DISTINCT ON and selecting first value by id of
# custom value:
#
# LEFT OUTER JOIN (
# SELECT DISTINCT ON (cv.customized_id), cv.customized_id, xxx "value"
# FROM custom_values cv
# WHERE …
# ORDER BY cv.customized_id, cv.id
# ) cf_order_NNN ON cf_order_NNN.customized_id = …
#
# For multi value custom fields the GROUP BY and value aggregate function
# ensure single value for every customized object:
#
# LEFT OUTER JOIN (
# SELECT cv.customized_id, ARRAY_AGG(xxx ORDERY BY yyy) "value"
# FROM custom_values cv
# WHERE …
# GROUP BY cv.customized_id, cv.id
# ) cf_order_NNN ON cf_order_NNN.customized_id = …
#
def join_for_order_sql(value:, add_select: nil, join: nil, multi_value: false)
<<-SQL.squish
LEFT OUTER JOIN (
Expand Down

0 comments on commit 89a556f

Please sign in to comment.