From 8cb9e123b1a625b8bd3e84d8ec9c322a6ee62949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ignacio=20S=C3=A1nchez=20Lara?= Date: Mon, 16 Jul 2018 19:55:04 +0200 Subject: [PATCH] Helper function to convert json arrays to PG arrays --- client/cdb_dataservices_client--0.25.0.sql | 7 ++++++- client/sql/05_utils.sql | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/client/cdb_dataservices_client--0.25.0.sql b/client/cdb_dataservices_client--0.25.0.sql index af58ce2..b69a536 100644 --- a/client/cdb_dataservices_client--0.25.0.sql +++ b/client/cdb_dataservices_client--0.25.0.sql @@ -18,7 +18,12 @@ BEGIN RETURN ROWS; END -$func$ LANGUAGE plpgsql;-- +$func$ LANGUAGE plpgsql; + +-- Taken from https://stackoverflow.com/a/48013356/351721 +CREATE OR REPLACE FUNCTION jsonb_array_casttext(jsonb) RETURNS text[] AS $f$ + SELECT array_agg(x) || ARRAY[]::text[] FROM jsonb_array_elements_text($1) t(x); +$f$ LANGUAGE sql IMMUTABLE;-- -- Geocoder server connection config -- -- The purpose of this function is provide to the PL/Proxy functions diff --git a/client/sql/05_utils.sql b/client/sql/05_utils.sql index d161c6a..31c8445 100644 --- a/client/sql/05_utils.sql +++ b/client/sql/05_utils.sql @@ -12,4 +12,9 @@ BEGIN RETURN ROWS; END -$func$ LANGUAGE plpgsql; \ No newline at end of file +$func$ LANGUAGE plpgsql; + +-- Taken from https://stackoverflow.com/a/48013356/351721 +CREATE OR REPLACE FUNCTION jsonb_array_casttext(jsonb) RETURNS text[] AS $f$ + SELECT array_agg(x) || ARRAY[]::text[] FROM jsonb_array_elements_text($1) t(x); +$f$ LANGUAGE sql IMMUTABLE; \ No newline at end of file