Skip to content

Commit

Permalink
extract summary building logic to separate function for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
mathemancer committed Aug 23, 2024
1 parent 50174dc commit 6a7e991
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions db/sql/00_msar.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3821,26 +3821,39 @@ ORDER BY conkey, target_oid, confkey;
$$ LANGUAGE SQL STABLE RETURNS NULL ON NULL INPUT;


CREATE OR REPLACE FUNCTION msar.build_summary_expr(tab_id oid) RETURNS TEXT AS $$/*
Given a table, return an SQL expression that will build a summary for each row of the table.
Args:
tab_id: The OID of the table being summarized.
*/
SELECT format(
'msar.format_data(%I)::text',
msar.get_column_name(tab_id, msar.get_default_summary_column(tab_id))
);
$$ LANGUAGE SQL STABLE RETURNS NULL ON NULL INPUT;


CREATE OR REPLACE FUNCTION msar.build_summary_cte_expr_for_table(tab_id oid) RETURNS TEXT AS $$/*
Build an SQL text expression defining a sequence of CTEs that give summaries for linked records.
This summary amounts to just the first string-like column value for that linked record.
Args:
tab_oid: The table for whose fkey values' linked records we'll get summaries.
tab_id: The table for whose fkey values' linked records we'll get summaries.
*/
WITH fkey_map_cte AS (SELECT * FROM msar.get_fkey_map_cte(tab_id))
SELECT ', ' || string_agg(
format(
$c$summary_cte_%1$s AS (
SELECT
msar.format_data(%2$I) AS key,
msar.format_data(%3$I)::text AS summary
%3$s AS summary
FROM %4$I.%5$I
)$c$,
conkey,
msar.get_column_name(target_oid, confkey),
msar.get_column_name(target_oid, msar.get_default_summary_column(target_oid)),
msar.build_summary_expr(target_oid),
msar.get_relation_schema_name(target_oid),
msar.get_relation_name(target_oid)
), ', '
Expand Down

0 comments on commit 6a7e991

Please sign in to comment.