Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
dfjswanson committed Apr 24, 2024
1 parent 179a6d7 commit f610dd1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
29 changes: 26 additions & 3 deletions database/code/meta/svc-service/svc_generate_queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,19 @@ BEGIN

SELECT json_agg(json_build_object('file_name', file_name || '.sql', 'query', query ))
INTO v_source_queries
FROM _sources;
FROM _sources
WHERE query NOT LIKE 'QUERY GENERATION ERROR:%';

INSERT INTO log.actor_log (log_id, message, actor_path, severity, insert_datetime)
SELECT v_imp.log_id, format('Error generating query for source `%s`: %s', source_name, replace(query,'QUERY GENERATION ERROR:','')),'svc_generate_queries', 'E', clock_timestamp()
FROM _sources
WHERE query LIKE 'QUERY GENERATION ERROR:%';

GET DIAGNOSTICS v_count = ROW_COUNT;

IF v_count > 0 THEN
RETURN json_build_object('source', v_source_queries, 'error', 'Error generating source queries');
END IF;

SELECT string_agg(query,E'\n\n' ORDER BY level)
INTO v_all_source_query
Expand All @@ -94,15 +106,26 @@ BEGIN

SELECT json_agg(json_build_object('file_name', file_name || '.sql', 'query', query))
INTO v_output_queries
FROM _outputs;
FROM _outputs
WHERE query NOT LIKE 'QUERY GENERATION ERROR:%';

INSERT INTO log.actor_log (log_id, message, actor_path, severity, insert_datetime)
SELECT v_imp.log_id, format('Error generating query for output `%s`: %s', output_name, replace(query,'QUERY GENERATION ERROR:','')),'svc_generate_queries', 'E', clock_timestamp()
FROM _outputs
WHERE query LIKE 'QUERY GENERATION ERROR:%';

GET DIAGNOSTICS v_count = ROW_COUNT;

IF v_count > 0 THEN
RETURN json_build_object('source', v_source_queries, 'output',v_output_queries, 'error', 'Error generating output queries');
END IF;

SELECT string_agg(query,E'\n\n' ORDER BY level)
INTO v_all_output_query
FROM _outputs;

RETURN json_build_object('source', v_source_queries, 'output',v_output_queries, 'run', E'/*SOURCES*/\n' || v_all_source_query || COALESCE( E'\n/*OUTPUTs*/\n' || v_all_output_query, ''));


END;

$function$;
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ FOR v_cte IN 0 .. v_cte_max LOOP

END LOOP;


RETURN v_sql;


Expand All @@ -109,13 +108,19 @@ AS $function$

DECLARE
v_hub_table_name text = meta.u_get_hub_table_name(in_source_id);
v_sql text;
v_query text;
BEGIN
v_sql := 'DROP TABLE IF EXISTS ' || v_hub_table_name || E';
CREATE TABLE ' || v_hub_table_name || ' AS
' || meta.u_enr_query_generate_query(in_source_id, 'input', 0, '{}'::int[]) || ';
';
RETURN v_sql;

v_query := meta.u_enr_query_generate_query(in_source_id, 'input', 0, '{}'::int[]);
IF v_query LIKE 'QUERY GENERATION ERROR:%' THEN
RETURN v_query;
ELSE
RETURN ( 'DROP TABLE IF EXISTS ' || v_hub_table_name || E';
CREATE TABLE ' || v_hub_table_name || ' AS
' || v_query || ';
');
END IF;

END;

$function$;
2 changes: 1 addition & 1 deletion database/code/meta/u-utility/u_append_object.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- for each key in in_object, replaces if with value form a matching key in in_add_object if it's not null. Runs recursively for jsonb keys
-- for each key in in_object, replaces if with value form a matching key in in_add_object if it's not null. Runs recursively for jsonb keys.
CREATE OR REPLACE FUNCTION meta.u_append_object(in_object jsonb, in_add_object jsonb)
RETURNS jsonb
LANGUAGE plpgsql
Expand Down
20 changes: 20 additions & 0 deletions database/code/meta/u-utility/u_remove_last_array_element.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CREATE OR REPLACE FUNCTION meta.u_remove_last_array_element(
in_ids int[])
RETURNS int[] -- returns array with last element removed
LANGUAGE plpgsql
COST 1
IMMUTABLE PARALLEL SAFE
AS $function$
DECLARE
i int;
v_ret int[] := '{}';
BEGIN

FOR i IN 1..array_upper(in_ids, 1) - 1 LOOP
v_ret := v_ret || in_ids[i];
END LOOP;

RETURN v_ret;
END;

$function$;

0 comments on commit f610dd1

Please sign in to comment.