Add a new helper function _CDB_Table_Exists

This commit is contained in:
Rafa de la Torre 2018-07-03 13:00:24 +02:00
parent 2e68626165
commit 55a77b0ef0
4 changed files with 38 additions and 1 deletions

View File

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

View File

@ -158,3 +158,21 @@ BEGIN
RETURN left(string, (i - 1)); RETURN left(string, (i - 1));
END; END;
$$ LANGUAGE 'plpgsql' IMMUTABLE PARALLEL SAFE; $$ 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 FUNCTION cartodb._CDB_Table_Exists(table_name_with_optional_schema TEXT)
RETURNS bool
AS $$
BEGIN
IF EXISTS(SELECT * FROM pg_class WHERE table_name_with_optional_schema::regclass::oid = oid AND relkind = 'r') THEN
RETURN true;
ELSE
RETURN false;
END IF;
EXCEPTION
WHEN invalid_schema_name OR undefined_table THEN
RETURN false;
END;
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL SAFE;

View File

@ -126,3 +126,13 @@ SELECT * FROM cartodb._CDB_Octet_Truncate('piraña', 6);
-- Test _CDB_Octet_Truncate UTF8 case -- Test _CDB_Octet_Truncate UTF8 case
SELECT * FROM cartodb._CDB_Octet_Truncate('piraña', 7); 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ñ pirañ
piraña piraña
CREATE TABLE
f
f
t
t
f
f
DROP TABLE