Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return with false, if there are no results #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions colpivot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ create or replace function colpivot(
out_table varchar, in_query varchar,
key_cols varchar[], class_cols varchar[],
value_e varchar, col_order varchar
) returns void as $$
) returns boolean as $$
declare
in_table varchar;
col varchar;
Expand Down Expand Up @@ -74,6 +74,11 @@ create or replace function colpivot(
query := query || '_key.' || quote_ident(col) || ' ';
i := i + 1;
end loop;
-- empty resultset, bail out
if n_clsc_cols is null then
execute ('drop table ' || in_table);
return false;
end if;
for j in 1..n_clsc_cols loop
query := query || ', ';
col := '';
Expand All @@ -86,6 +91,7 @@ create or replace function colpivot(
ali := '_clsc_' || j::text;
query := query || '(' || replace(value_e, '#', ali) || ')' || ' as ' || quote_ident(col) || ' ';
end loop;
-- from part
query := query || ' from (select distinct ';
i := 0;
foreach col in array key_cols loop
Expand All @@ -103,8 +109,10 @@ create or replace function colpivot(
foreach col in array key_cols loop
if i > 0 then
on_e := on_e || ' and ';
on_e := on_e || '(' || ali || '.' || quote_ident(col) || ' = _key.' || quote_ident(col) || ' OR (' || ali || '.' || quote_ident(col) || ' IS NULL AND _key.' || quote_ident(col) || ' IS NULL)) ';
else
on_e := on_e || ali || '.' || quote_ident(col) || ' = _key.' || quote_ident(col) || ' ';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the first column not get the null comparison check? It's probably obvious but it was so long that I wrote this code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beats me. I would have to test the function with and without checking for null. I'm also in the "fire and forget" mode 😆 But as I use it in the production, I guess it works regardless of maybe useless bloat.
I will check it out, if I have time for it.

end if;
on_e := on_e || ali || '.' || quote_ident(col) || ' = _key.' || quote_ident(col) || ' ';
i := i + 1;
end loop;
for k in 1..n_class_cols loop
Expand All @@ -116,7 +124,7 @@ create or replace function colpivot(
-- raise notice '%', query;
execute ('create temp table ' || quote_ident(out_table) || ' on commit drop as ' || query);
-- cleanup temporary in_table before we return
execute ('drop table ' || in_table)
return;
execute ('drop table ' || in_table);
return true;
end;
$$ language plpgsql volatile;