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

Possible fix for "ERROR: array must have even number of elements" #2

Merged
merged 2 commits into from Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions ddl/func_recursively_delete.sql
@@ -1,4 +1,6 @@
CREATE OR REPLACE FUNCTION recursively_delete(
DROP FUNCTION IF EXISTS recursively_delete;

CREATE FUNCTION recursively_delete(
ARG_table REGCLASS ,
ARG_in ANYELEMENT ,
ARG_for_realz BOOL DEFAULT FALSE
Expand Down Expand Up @@ -61,13 +63,13 @@ BEGIN
IF array_ndims(ARG_in) = 1 THEN
VAR_in := format('(%s)', (SELECT string_agg(format('%L', ael), ', ') FROM unnest(ARG_in) ael));
ELSE
VAR_in := string_agg(format('(%s)', (SELECT string_agg(format('%L', ael), ', ') FROM jsonb_array_elements_text(ael) ael)), ', ') FROM jsonb_array_elements(to_jsonb(ARG_in)) ael;
VAR_in := string_agg(format('(%s)', (SELECT string_agg(format('%L', ael), ', ') FROM jsonb_array_elements_text(ael) ael)), ', ') FROM jsonb_array_elements(array_to_json(ARG_in)::JSONB) ael;
END IF;
WHEN 'integer[]' THEN
IF array_ndims(ARG_in) = 1 THEN
VAR_in := format('(%s)', array_to_string(ARG_in, ', '));
ELSE
VAR_in := string_agg(format('(%s)', (SELECT string_agg(ael, ', ') FROM jsonb_array_elements_text(ael) ael)), ', ') FROM jsonb_array_elements(to_jsonb(ARG_in)) ael;
VAR_in := string_agg(format('(%s)', (SELECT string_agg(ael, ', ') FROM jsonb_array_elements_text(ael) ael)), ', ') FROM jsonb_array_elements(array_to_json(ARG_in)::JSONB) ael;
END IF;
ELSE
RAISE 'ARG_in "%" for %-column primary key is of an unexpected type: %', ARG_in, array_length(VAR_pk_col_names, 1), pg_typeof(ARG_in);
Expand Down Expand Up @@ -187,7 +189,7 @@ BEGIN
EXIT;
END IF;

VAR_del_results := jsonb_set(VAR_del_results, ARRAY[VAR_del_result_rec.queue_i], to_jsonb(VAR_del_result_rec.n_del));
VAR_del_results := jsonb_set(VAR_del_results, ARRAY[VAR_del_result_rec.queue_i], VAR_del_result_rec.n_del::TEXT::JSONB);
END LOOP;

IF NOT ARG_for_realz THEN
Expand Down
12 changes: 7 additions & 5 deletions ddl/func_recursively_delete_.sql
@@ -1,4 +1,6 @@
CREATE OR REPLACE FUNCTION _recursively_delete(
DROP FUNCTION IF EXISTS _recursively_delete;

CREATE FUNCTION _recursively_delete(
ARG_table REGCLASS ,
ARG_pk_col_names TEXT[] ,
_ARG_depth INT DEFAULT 0 ,
Expand All @@ -21,7 +23,7 @@ BEGIN
IF _ARG_depth = 0 THEN
_ARG_path := _ARG_path || ARRAY['ROOT'];

VAR_ctab_pk_col_names := to_json(ARG_pk_col_names);
VAR_ctab_pk_col_names := array_to_json(ARG_pk_col_names)::JSONB;

-- Not really a statement of truth, but a convenient thing to pretend. For the initial
-- "bootstrap" CTE auxiliary statement, this HACK takes care of equating the user's ARG_in with
Expand Down Expand Up @@ -50,7 +52,7 @@ BEGIN
'depth' , _ARG_depth,
'i' , VAR_flat_graph_i,
'i_up' , _ARG_flat_graph_i_up,
'path' , to_jsonb(_ARG_path),
'path' , array_to_json(_ARG_path)::JSONB,
'ptab_uk_col_names', _ARG_fk_con->'ptab_uk_col_names'
);

Expand All @@ -72,7 +74,7 @@ BEGIN
-- we'll call "deppers").
FOR VAR_i IN VAR_path_pos_of_oid .. array_length(_ARG_path, 1) LOOP
FOR VAR_flat_graph_node IN SELECT jsonb_array_elements(_ARG_flat_graph) LOOP
IF VAR_flat_graph_node->'path' = to_jsonb(_ARG_path[1:VAR_i]) THEN
IF VAR_flat_graph_node->'path' = array_to_json(_ARG_path[1:VAR_i])::JSONB THEN
VAR_circ_dep := VAR_circ_dep || VAR_flat_graph_node;

EXIT;
Expand All @@ -94,7 +96,7 @@ BEGIN
NULL,
--
_ARG_depth + 1,
to_jsonb(VAR_fk_con_rec),
row_to_json(VAR_fk_con_rec)::JSONB,
VAR_flat_graph_i,
_ARG_path || VAR_fk_con_rec.oid::TEXT,
--
Expand Down