Skip to content

Commit

Permalink
WIP preparing for template
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Oct 10, 2024
1 parent b711dad commit 3e2c1e9
Showing 1 changed file with 157 additions and 125 deletions.
282 changes: 157 additions & 125 deletions app/models/custom_field/order_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#++

module CustomField::OrderStatements
USER_ORDER_COLUMN = "ARRAY[users.lastname, users.firstname, users.mail]".freeze

# Returns a ORDER BY clause that can used to sort customized
# objects by their value of the custom field.
# Returns false, if the custom field can not be used for sorting.
Expand All @@ -40,17 +42,17 @@ def order_statement
def order_join_statement
case field_format
when "string", "date", "bool", "link"
join_for_order_by_string_sql
join_for_order_by_string
when "int"
join_for_order_by_int_sql
join_for_order_by_int
when "float"
join_for_order_by_float_sql
join_for_order_by_float
when "list"
join_for_order_by_list_sql
join_for_order_by_list
when "user"
join_for_order_by_user_sql
join_for_order_by_user
when "version"
join_for_order_by_version_sql
join_for_order_by_version
end
end

Expand All @@ -73,14 +75,14 @@ def group_by_statement
end

def group_by_join_statement
return join_for_group_by_list_sql if field_format == "list"
return join_for_group_by_list if field_format == "list"

order_join_statement
end

private

def join_for_order_by_string_sql
def join_for_order_by_string
<<-SQL.squish
LEFT OUTER JOIN (
SELECT DISTINCT ON (cv.customized_id) cv.customized_id, cv.value "value"
Expand All @@ -95,7 +97,7 @@ def join_for_order_by_string_sql
SQL
end

def join_for_order_by_int_sql
def join_for_order_by_int
<<-SQL.squish
LEFT OUTER JOIN (
SELECT DISTINCT ON (cv.customized_id) cv.customized_id, cv.value::decimal(60) "value"
Expand All @@ -110,7 +112,7 @@ def join_for_order_by_int_sql
SQL
end

def join_for_order_by_float_sql
def join_for_order_by_float
<<-SQL.squish
LEFT OUTER JOIN (
SELECT DISTINCT ON (cv.customized_id) cv.customized_id, cv.value::double precision "value"
Expand All @@ -125,139 +127,169 @@ def join_for_order_by_float_sql
SQL
end

def join_for_order_by_list_sql
def join_for_order_by_list
if multi_value?
<<-SQL.squish
LEFT OUTER JOIN (
SELECT cv.customized_id, array_agg(co.position ORDER BY co.position) "value"
FROM #{CustomValue.quoted_table_name} cv
INNER JOIN #{CustomOption.quoted_table_name} co
ON co.id = cv.value::bigint
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
GROUP BY cv.customized_id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
join_for_order_by_multi_value_list
else
<<-SQL.squish
LEFT OUTER JOIN (
SELECT DISTINCT ON (cv.customized_id) cv.customized_id, co.position "value"
FROM #{CustomValue.quoted_table_name} cv
INNER JOIN #{CustomOption.quoted_table_name} co
ON co.id = cv.value::bigint
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
ORDER BY cv.customized_id, cv.id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
join_for_order_by_single_value_list
end
end

def join_for_group_by_list_sql
def join_for_order_by_single_value_list
<<-SQL.squish
LEFT OUTER JOIN (
SELECT DISTINCT ON (cv.customized_id) cv.customized_id, co.position "value"
FROM #{CustomValue.quoted_table_name} cv
INNER JOIN #{CustomOption.quoted_table_name} co
ON co.id = cv.value::bigint
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
ORDER BY cv.customized_id, cv.id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
end

def join_for_order_by_multi_value_list
<<-SQL.squish
LEFT OUTER JOIN (
SELECT cv.customized_id, array_agg(co.position ORDER BY co.position) "value"
FROM #{CustomValue.quoted_table_name} cv
INNER JOIN #{CustomOption.quoted_table_name} co
ON co.id = cv.value::bigint
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
GROUP BY cv.customized_id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
end

def join_for_group_by_list
if multi_value?
<<-SQL.squish
LEFT OUTER JOIN (
SELECT cv.customized_id, array_to_string(array_agg(cv.value ORDER BY co.position), '.') "value"
FROM #{CustomValue.quoted_table_name} cv
INNER JOIN #{CustomOption.quoted_table_name} co
ON co.id = cv.value::bigint
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
GROUP BY cv.customized_id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
join_for_group_by_multi_value_list
else
<<-SQL.squish
LEFT OUTER JOIN (
SELECT DISTINCT ON (cv.customized_id) cv.customized_id, cv.value "value"
FROM #{CustomValue.quoted_table_name} cv
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
ORDER BY cv.customized_id, cv.id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
join_for_group_by_single_value_list
end
end

def join_for_order_by_user_sql
columns_array = "ARRAY[users.lastname, users.firstname, users.mail]"
def join_for_group_by_single_value_list
<<-SQL.squish
LEFT OUTER JOIN (
SELECT DISTINCT ON (cv.customized_id) cv.customized_id, cv.value "value"
FROM #{CustomValue.quoted_table_name} cv
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
ORDER BY cv.customized_id, cv.id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
end

def join_for_group_by_multi_value_list
<<-SQL.squish
LEFT OUTER JOIN (
SELECT cv.customized_id, array_to_string(array_agg(cv.value ORDER BY co.position), '.') "value"
FROM #{CustomValue.quoted_table_name} cv
INNER JOIN #{CustomOption.quoted_table_name} co
ON co.id = cv.value::bigint
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
GROUP BY cv.customized_id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
end

def join_for_order_by_user
if multi_value?
<<-SQL.squish
LEFT OUTER JOIN (
SELECT cv.customized_id, ARRAY_AGG(#{columns_array} ORDER BY #{columns_array}) "value"
FROM #{CustomValue.quoted_table_name} cv
INNER JOIN #{User.quoted_table_name} users
ON users.id = cv.value::bigint
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
GROUP BY cv.customized_id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
join_for_order_by_multi_value_user
else
<<-SQL.squish
LEFT OUTER JOIN (
SELECT DISTINCT ON (cv.customized_id) cv.customized_id, #{columns_array} "value"
FROM #{CustomValue.quoted_table_name} cv
INNER JOIN #{User.quoted_table_name} users
ON users.id = cv.value::bigint
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
ORDER BY cv.customized_id, cv.id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
join_for_order_by_single_value_user
end
end

def join_for_order_by_version_sql
def join_for_order_by_single_value_user
<<-SQL.squish
LEFT OUTER JOIN (
SELECT DISTINCT ON (cv.customized_id) cv.customized_id, #{USER_ORDER_COLUMN} "value"
FROM #{CustomValue.quoted_table_name} cv
INNER JOIN #{User.quoted_table_name} users
ON users.id = cv.value::bigint
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
ORDER BY cv.customized_id, cv.id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
end

def join_for_order_by_multi_value_user
<<-SQL.squish
LEFT OUTER JOIN (
SELECT cv.customized_id, ARRAY_AGG(#{USER_ORDER_COLUMN} ORDER BY #{USER_ORDER_COLUMN}) "value"
FROM #{CustomValue.quoted_table_name} cv
INNER JOIN #{User.quoted_table_name} users
ON users.id = cv.value::bigint
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
GROUP BY cv.customized_id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
end

def join_for_order_by_version
if multi_value?
<<-SQL.squish
LEFT OUTER JOIN (
SELECT cv.customized_id, array_agg(versions.name ORDER BY versions.name) "value"
FROM #{CustomValue.quoted_table_name} cv
INNER JOIN #{Version.quoted_table_name} versions
ON versions.id = cv.value::bigint
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
GROUP BY cv.customized_id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
join_for_order_by_multi_value_version
else
<<-SQL.squish
LEFT OUTER JOIN (
SELECT DISTINCT ON (cv.customized_id) cv.customized_id, versions.name "value"
FROM #{CustomValue.quoted_table_name} cv
INNER JOIN #{Version.quoted_table_name} versions
ON versions.id = cv.value::bigint
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
ORDER BY cv.customized_id, cv.id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
join_for_order_by_single_value_version
end
end

def join_for_order_by_single_value_version
<<-SQL.squish
LEFT OUTER JOIN (
SELECT DISTINCT ON (cv.customized_id) cv.customized_id, versions.name "value"
FROM #{CustomValue.quoted_table_name} cv
INNER JOIN #{Version.quoted_table_name} versions
ON versions.id = cv.value::bigint
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
ORDER BY cv.customized_id, cv.id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
end

def join_for_order_by_multi_value_version
<<-SQL.squish
LEFT OUTER JOIN (
SELECT cv.customized_id, array_agg(versions.name ORDER BY versions.name) "value"
FROM #{CustomValue.quoted_table_name} cv
INNER JOIN #{Version.quoted_table_name} versions
ON versions.id = cv.value::bigint
WHERE cv.customized_type = #{CustomValue.connection.quote(self.class.customized_class.name)}
AND cv.custom_field_id = #{id}
AND cv.value IS NOT NULL
AND cv.value != ''
GROUP BY cv.customized_id
) cf_order_#{id}
ON cf_order_#{id}.customized_id = #{self.class.customized_class.quoted_table_name}.id
SQL
end
end

0 comments on commit 3e2c1e9

Please sign in to comment.