Merge pull request #332 from CartoDB/cdb-table-exists

Cdb table exists
This commit is contained in:
Rafa de la Torre 2018-07-03 15:45:17 +02:00 committed by GitHub
commit c4980a90f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 1 deletions

View File

@ -1,7 +1,7 @@
# cartodb/Makefile
EXTENSION = cartodb
EXTVERSION = 0.22.2
EXTVERSION = 0.23.0
SED = sed
AWK = awk
@ -88,6 +88,7 @@ UPGRADABLE = \
0.22.0 \
0.22.1 \
0.22.2 \
0.23.0 \
$(EXTVERSION)dev \
$(EXTVERSION)next \
$(END)

View File

@ -1,3 +1,6 @@
0.23.0 (2018-07-03)
* Add a new helper function `_CDB_Table_Exists(table_name_with_optional_schema TEXT)` #332
0.22.2 (2018-05-29)
* Fix: Fix hyphenates usernames in 0.22.1 fix (#331)

View File

@ -158,3 +158,20 @@ BEGIN
RETURN left(string, (i - 1));
END;
$$ LANGUAGE 'plpgsql' IMMUTABLE PARALLEL SAFE;
-- Checks if a given text representing a qualified or unqualified table name (relation)
-- actually exists in the database. It is meant to be used as a guard for other function/queries.
CREATE OR REPLACE FUNCTION cartodb._CDB_Table_Exists(table_name_with_optional_schema TEXT)
RETURNS bool
AS $$
DECLARE
table_exists bool := false;
BEGIN
table_exists := EXISTS(SELECT * FROM pg_class WHERE table_name_with_optional_schema::regclass::oid = oid AND relkind = 'r');
RETURN table_exists;
EXCEPTION
WHEN invalid_schema_name OR undefined_table THEN
RETURN false;
END;
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;

View File

@ -126,3 +126,13 @@ SELECT * FROM cartodb._CDB_Octet_Truncate('piraña', 6);
-- Test _CDB_Octet_Truncate UTF8 case
SELECT * FROM cartodb._CDB_Octet_Truncate('piraña', 7);
-- Test _CDB_Table_Exists
CREATE TABLE public.this_table_exists();
SELECT cartodb._CDB_Table_Exists('this_table_does_not_exist');
SELECT cartodb._CDB_Table_Exists('this_schema_does_not_exist.this_table_does_not_exist');
SELECT cartodb._CDB_Table_Exists('this_table_exists');
SELECT cartodb._CDB_Table_Exists('public.this_table_exists');
SELECT cartodb._CDB_Table_Exists('raster_overviews'); -- view created by postgis
SELECT cartodb._CDB_Table_Exists('public.raster_overviews');
DROP TABLE public.this_table_exists

View File

@ -57,3 +57,11 @@ DROP TABLE
pira
pirañ
piraña
CREATE TABLE
f
f
t
t
f
f
DROP TABLE