Fully qualify function calls
This commit is contained in:
20
Makefile
20
Makefile
@@ -123,24 +123,15 @@ REGRESS = test_setup $(REGRESS_LEGACY)
|
|||||||
|
|
||||||
PG_CONFIG = pg_config
|
PG_CONFIG = pg_config
|
||||||
PGXS := $(shell $(PG_CONFIG) --pgxs)
|
PGXS := $(shell $(PG_CONFIG) --pgxs)
|
||||||
PG_PARALLEL := $(shell $(PG_CONFIG) --version | ($(AWK) '{$$2*=1000; if ($$2 >= 9600) print 1; else print 0;}' 2> /dev/null || echo 0))
|
|
||||||
include $(PGXS)
|
include $(PGXS)
|
||||||
|
|
||||||
$(EXTENSION)--$(EXTVERSION).sql: $(CDBSCRIPTS) cartodb_version.sql Makefile
|
$(EXTENSION)--$(EXTVERSION).sql: $(CDBSCRIPTS) cartodb_version.sql Makefile
|
||||||
echo '\echo Use "CREATE EXTENSION $(EXTENSION)" to load this file. \quit' > $@
|
echo '\echo Use "CREATE EXTENSION $(EXTENSION)" to load this file. \quit' > $@
|
||||||
cat $(CDBSCRIPTS) | \
|
cat $(CDBSCRIPTS) | \
|
||||||
$(SED) -e 's/public\./cartodb./g' \
|
$(SED) -e 's/@extschema@/cartodb/g' \
|
||||||
-e 's/:DATABASE_USERNAME/cdb_org_admin/g' \
|
-e "s/@postgisschema@/public/g" >> $@
|
||||||
-e "s/''public''/''cartodb''/g" >> $@
|
|
||||||
echo "GRANT USAGE ON SCHEMA cartodb TO public;" >> $@
|
echo "GRANT USAGE ON SCHEMA cartodb TO public;" >> $@
|
||||||
cat cartodb_version.sql >> $@
|
cat cartodb_version.sql >> $@
|
||||||
ifeq ($(PG_PARALLEL), 0)
|
|
||||||
# Remove PARALLEL in aggregates and functions
|
|
||||||
$(eval TMPFILE := $(shell mktemp /tmp/$(basename $0).XXXXXXXX))
|
|
||||||
$(SED) -e 's/PARALLEL \= [A-Z]*,/''/g' \
|
|
||||||
-e 's/PARALLEL [A-Z]*/''/g' $@ > $(TMPFILE)
|
|
||||||
mv $(TMPFILE) $@
|
|
||||||
endif
|
|
||||||
|
|
||||||
$(EXTENSION)--unpackaged--$(EXTVERSION).sql: $(EXTENSION)--$(EXTVERSION).sql util/create_from_unpackaged.sh Makefile
|
$(EXTENSION)--unpackaged--$(EXTVERSION).sql: $(EXTENSION)--$(EXTVERSION).sql util/create_from_unpackaged.sh Makefile
|
||||||
./util/create_from_unpackaged.sh $(EXTVERSION)
|
./util/create_from_unpackaged.sh $(EXTVERSION)
|
||||||
@@ -153,12 +144,9 @@ $(EXTENSION)--$(EXTVERSION)--$(EXTVERSION)next.sql: $(EXTENSION)--$(EXTVERSION).
|
|||||||
|
|
||||||
$(EXTENSION).control: $(EXTENSION).control.in Makefile
|
$(EXTENSION).control: $(EXTENSION).control.in Makefile
|
||||||
$(SED) -e 's/@@VERSION@@/$(EXTVERSION)/' $< > $@
|
$(SED) -e 's/@@VERSION@@/$(EXTVERSION)/' $< > $@
|
||||||
ifeq ($(PG_PARALLEL), 0)
|
|
||||||
echo -e "\033[0;31mExtension created without PARALLEL support\033[0m"
|
|
||||||
endif
|
|
||||||
|
|
||||||
cartodb_version.sql: cartodb_version.sql.in Makefile $(GITDIR)/index
|
cartodb_version.sql: cartodb_version.sql.in Makefile $(GITDIR)/index
|
||||||
$(SED) -e 's/@@VERSION@@/$(EXTVERSION)/' $< > $@
|
$(SED) -e 's/@@VERSION@@/$(EXTVERSION)/' -e 's/@extschema@/cartodb/g' -e "s/@postgisschema@/public/g" $< > $@
|
||||||
|
|
||||||
# Needed for consistent `echo` results with backslashes
|
# Needed for consistent `echo` results with backslashes
|
||||||
SHELL = bash
|
SHELL = bash
|
||||||
@@ -175,7 +163,7 @@ legacy_regress: $(REGRESS_OLD) Makefile
|
|||||||
echo '\t' >> $${of}; \
|
echo '\t' >> $${of}; \
|
||||||
echo '\set QUIET off' >> $${of}; \
|
echo '\set QUIET off' >> $${of}; \
|
||||||
cat $${f} | \
|
cat $${f} | \
|
||||||
$(SED) -e 's/public\./cartodb./g' >> $${of}; \
|
$(SED) -e 's/@@VERSION@@/$(EXTVERSION)/' -e 's/@extschema@/cartodb/g' -e "s/@postgisschema@/public/g" >> $${of}; \
|
||||||
exp=expected/test/$${tn}.out; \
|
exp=expected/test/$${tn}.out; \
|
||||||
echo '\set ECHO none' > $${exp}; \
|
echo '\set ECHO none' > $${exp}; \
|
||||||
cat test/$${tn}_expect >> $${exp}; \
|
cat test/$${tn}_expect >> $${exp}; \
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
DO $$ BEGIN IF EXISTS (SELECT * FROM pg_proc p, pg_namespace n WHERE p.proname = 'cdb_transformtowebmercator' AND p.pronamespace = n.oid AND n.nspname = 'public') THEN RAISE EXCEPTION 'Use CREATE EXTENSION cartodb FROM unpackaged'; END IF; END; $$ LANGUAGE 'plpgsql'; -- forbid duplicated extension
|
DO $$ BEGIN IF EXISTS (SELECT * FROM pg_proc p, pg_namespace n WHERE p.proname = 'cdb_transformtowebmercator' AND p.pronamespace = n.oid AND n.nspname = 'public') THEN RAISE EXCEPTION 'Use CREATE EXTENSION cartodb FROM unpackaged'; END IF; END; $$ LANGUAGE 'plpgsql'; -- forbid duplicated extension
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION cartodb.CDB_version()
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_version()
|
||||||
RETURNS text AS $$
|
RETURNS text AS $$
|
||||||
SELECT '@@VERSION@@'::text;
|
SELECT '@@VERSION@@'::text;
|
||||||
$$ language 'sql' IMMUTABLE STRICT;
|
$$ language 'sql' IMMUTABLE STRICT;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
-- Table to register analysis nodes from https://github.com/cartodb/camshaft
|
-- Table to register analysis nodes from https://github.com/cartodb/camshaft
|
||||||
CREATE TABLE IF NOT EXISTS
|
CREATE TABLE IF NOT EXISTS
|
||||||
cartodb.cdb_analysis_catalog (
|
@extschema@.cdb_analysis_catalog (
|
||||||
-- md5 hex hash
|
-- md5 hex hash
|
||||||
node_id char(40) CONSTRAINT cdb_analysis_catalog_pkey PRIMARY KEY,
|
node_id char(40) CONSTRAINT cdb_analysis_catalog_pkey PRIMARY KEY,
|
||||||
-- being json allows to do queries like analysis_def->>'type' = 'buffer'
|
-- being json allows to do queries like analysis_def->>'type' = 'buffer'
|
||||||
@@ -34,7 +34,7 @@ cartodb.cdb_analysis_catalog (
|
|||||||
-- This can only be called from an SQL script executed by CREATE EXTENSION
|
-- This can only be called from an SQL script executed by CREATE EXTENSION
|
||||||
DO LANGUAGE 'plpgsql' $$
|
DO LANGUAGE 'plpgsql' $$
|
||||||
BEGIN
|
BEGIN
|
||||||
PERFORM pg_catalog.pg_extension_config_dump('cartodb.cdb_analysis_catalog', '');
|
PERFORM pg_catalog.pg_extension_config_dump('@extschema@.cdb_analysis_catalog', '');
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
@@ -45,7 +45,7 @@ $$;
|
|||||||
DO $$
|
DO $$
|
||||||
BEGIN
|
BEGIN
|
||||||
BEGIN
|
BEGIN
|
||||||
ALTER TABLE cartodb.cdb_analysis_catalog ADD COLUMN last_modified_by uuid;
|
ALTER TABLE @extschema@.cdb_analysis_catalog ADD COLUMN last_modified_by uuid;
|
||||||
EXCEPTION
|
EXCEPTION
|
||||||
WHEN duplicate_column THEN END;
|
WHEN duplicate_column THEN END;
|
||||||
END;
|
END;
|
||||||
@@ -54,7 +54,7 @@ $$;
|
|||||||
DO $$
|
DO $$
|
||||||
BEGIN
|
BEGIN
|
||||||
BEGIN
|
BEGIN
|
||||||
ALTER TABLE cartodb.cdb_analysis_catalog ADD COLUMN last_error_message text;
|
ALTER TABLE @extschema@.cdb_analysis_catalog ADD COLUMN last_error_message text;
|
||||||
EXCEPTION
|
EXCEPTION
|
||||||
WHEN duplicate_column THEN END;
|
WHEN duplicate_column THEN END;
|
||||||
END;
|
END;
|
||||||
@@ -63,7 +63,7 @@ $$;
|
|||||||
DO $$
|
DO $$
|
||||||
BEGIN
|
BEGIN
|
||||||
BEGIN
|
BEGIN
|
||||||
ALTER TABLE cartodb.cdb_analysis_catalog ADD COLUMN cache_tables regclass[] NOT NULL DEFAULT '{}';
|
ALTER TABLE @extschema@.cdb_analysis_catalog ADD COLUMN cache_tables regclass[] NOT NULL DEFAULT '{}';
|
||||||
EXCEPTION
|
EXCEPTION
|
||||||
WHEN duplicate_column THEN END;
|
WHEN duplicate_column THEN END;
|
||||||
END;
|
END;
|
||||||
@@ -72,7 +72,7 @@ $$;
|
|||||||
DO $$
|
DO $$
|
||||||
BEGIN
|
BEGIN
|
||||||
BEGIN
|
BEGIN
|
||||||
ALTER TABLE cartodb.cdb_analysis_catalog ADD COLUMN username text;
|
ALTER TABLE @extschema@.cdb_analysis_catalog ADD COLUMN username text;
|
||||||
EXCEPTION
|
EXCEPTION
|
||||||
WHEN duplicate_column THEN END;
|
WHEN duplicate_column THEN END;
|
||||||
END;
|
END;
|
||||||
@@ -84,12 +84,12 @@ DO LANGUAGE 'plpgsql' $$
|
|||||||
DECLARE
|
DECLARE
|
||||||
column_index int;
|
column_index int;
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT ordinal_position FROM information_schema.columns WHERE table_name='cdb_analysis_catalog' AND table_schema='cartodb' AND column_name='username' INTO column_index;
|
SELECT ordinal_position FROM information_schema.columns WHERE table_name='cdb_analysis_catalog' AND table_schema='@extschema@' AND column_name='username' INTO column_index;
|
||||||
IF column_index = 1 OR column_index = 10 THEN
|
IF column_index = 1 OR column_index = 10 THEN
|
||||||
ALTER TABLE cartodb.cdb_analysis_catalog ADD COLUMN username_final text;
|
ALTER TABLE @extschema@.cdb_analysis_catalog ADD COLUMN username_final text;
|
||||||
UPDATE cartodb.cdb_analysis_catalog SET username_final = username;
|
UPDATE @extschema@.cdb_analysis_catalog SET username_final = username;
|
||||||
ALTER TABLE cartodb.cdb_analysis_catalog DROP COLUMN username;
|
ALTER TABLE @extschema@.cdb_analysis_catalog DROP COLUMN username;
|
||||||
ALTER TABLE cartodb.cdb_analysis_catalog RENAME COLUMN username_final TO username;
|
ALTER TABLE @extschema@.cdb_analysis_catalog RENAME COLUMN username_final TO username;
|
||||||
END IF;
|
END IF;
|
||||||
END;
|
END;
|
||||||
$$;
|
$$;
|
||||||
|
|||||||
@@ -1,24 +1,24 @@
|
|||||||
-- Read configuration parameter analysis_quota_factor, making it
|
-- Read configuration parameter analysis_quota_factor, making it
|
||||||
-- accessible to regular users (which don't have access to cdb_conf)
|
-- accessible to regular users (which don't have access to cdb_conf)
|
||||||
CREATE OR REPLACE FUNCTION _CDB_GetConfAnalysisQuotaFactor()
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_GetConfAnalysisQuotaFactor()
|
||||||
RETURNS float8 AS
|
RETURNS float8 AS
|
||||||
$$
|
$$
|
||||||
BEGIN
|
BEGIN
|
||||||
RETURN CDB_Conf_GetConf('analysis_quota_factor')::text::float8;
|
RETURN @extschema@.CDB_Conf_GetConf('analysis_quota_factor')::text::float8;
|
||||||
END;
|
END;
|
||||||
$$
|
$$
|
||||||
LANGUAGE 'plpgsql' STABLE PARALLEL SAFE SECURITY DEFINER;
|
LANGUAGE 'plpgsql' STABLE PARALLEL SAFE SECURITY DEFINER;
|
||||||
|
|
||||||
|
|
||||||
-- Get the factor (fraction of the quota) for Camshaft cached analysis tables
|
-- Get the factor (fraction of the quota) for Camshaft cached analysis tables
|
||||||
CREATE OR REPLACE FUNCTION _CDB_AnalysisQuotaFactor()
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_AnalysisQuotaFactor()
|
||||||
RETURNS float8 AS
|
RETURNS float8 AS
|
||||||
$$
|
$$
|
||||||
DECLARE
|
DECLARE
|
||||||
factor float8;
|
factor float8;
|
||||||
BEGIN
|
BEGIN
|
||||||
-- We use a floating point cdb_conf parameter
|
-- We use a floating point cdb_conf parameter
|
||||||
factor := _CDB_GetConfAnalysisQuotaFactor();
|
factor := @extschema@._CDB_GetConfAnalysisQuotaFactor();
|
||||||
-- With a default value
|
-- With a default value
|
||||||
IF factor IS NULL THEN
|
IF factor IS NULL THEN
|
||||||
factor := 2;
|
factor := 2;
|
||||||
@@ -33,7 +33,7 @@ LANGUAGE 'plpgsql' STABLE PARALLEL SAFE;
|
|||||||
-- The name of an analysis table is passed; this, in addition to the
|
-- The name of an analysis table is passed; this, in addition to the
|
||||||
-- db role that executes this function is used to determined which
|
-- db role that executes this function is used to determined which
|
||||||
-- analysis tables will be considered.
|
-- analysis tables will be considered.
|
||||||
CREATE OR REPLACE FUNCTION CDB_CheckAnalysisQuota(table_name TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_CheckAnalysisQuota(table_name TEXT)
|
||||||
RETURNS void AS
|
RETURNS void AS
|
||||||
$$
|
$$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -54,7 +54,7 @@ BEGIN
|
|||||||
|
|
||||||
SELECT current_schema() INTO schema_name;
|
SELECT current_schema() INTO schema_name;
|
||||||
EXECUTE FORMAT('SELECT %I._CDB_UserQuotaInBytes();', schema_name) INTO nominal_quota;
|
EXECUTE FORMAT('SELECT %I._CDB_UserQuotaInBytes();', schema_name) INTO nominal_quota;
|
||||||
IF nominal_quota*_CDB_AnalysisQuotaFactor() < _CDB_AnalysisDataSize(schema_name) THEN
|
IF nominal_quota * @extschema@._CDB_AnalysisQuotaFactor() < @extschema@._CDB_AnalysisDataSize(schema_name) THEN
|
||||||
-- The limit is defined by a factor applied to the total space quota for the user
|
-- The limit is defined by a factor applied to the total space quota for the user
|
||||||
RAISE EXCEPTION 'Analysis cache space limits exceeded';
|
RAISE EXCEPTION 'Analysis cache space limits exceeded';
|
||||||
END IF;
|
END IF;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
-- This function returns TRUE if a given table name corresponds to a Camshaft cached analysis table
|
-- This function returns TRUE if a given table name corresponds to a Camshaft cached analysis table
|
||||||
-- Scope: private.
|
-- Scope: private.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_IsAnalysisTableName(table_name TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_IsAnalysisTableName(table_name TEXT)
|
||||||
RETURNS BOOLEAN
|
RETURNS BOOLEAN
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -15,10 +15,10 @@ $$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
|||||||
-- that may contain user tables are returned.
|
-- that may contain user tables are returned.
|
||||||
-- For each table, the regclass, schema name and table name are returned.
|
-- For each table, the regclass, schema name and table name are returned.
|
||||||
-- Scope: private.
|
-- Scope: private.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_AnalysisTablesInSchema(schema_name text DEFAULT NULL)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_AnalysisTablesInSchema(schema_name text DEFAULT NULL)
|
||||||
RETURNS TABLE(table_regclass REGCLASS, schema_name TEXT, table_name TEXT)
|
RETURNS TABLE(table_regclass REGCLASS, schema_name TEXT, table_name TEXT)
|
||||||
AS $$
|
AS $$
|
||||||
SELECT * FROM _CDB_UserTablesInSchema(schema_name) WHERE _CDB_IsAnalysisTableName(table_name);
|
SELECT * FROM @extschema@._CDB_UserTablesInSchema(schema_name) WHERE @extschema@._CDB_IsAnalysisTableName(table_name);
|
||||||
$$ LANGUAGE 'sql' STABLE PARALLEL SAFE;
|
$$ LANGUAGE 'sql' STABLE PARALLEL SAFE;
|
||||||
|
|
||||||
-- This function returns a relation user tables excluding analysis tables
|
-- This function returns a relation user tables excluding analysis tables
|
||||||
@@ -26,24 +26,24 @@ $$ LANGUAGE 'sql' STABLE PARALLEL SAFE;
|
|||||||
-- that may contain user tables are returned.
|
-- that may contain user tables are returned.
|
||||||
-- For each table, the regclass, schema name and table name are returned.
|
-- For each table, the regclass, schema name and table name are returned.
|
||||||
-- Scope: private.
|
-- Scope: private.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_NonAnalysisTablesInSchema(schema_name text DEFAULT NULL)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_NonAnalysisTablesInSchema(schema_name text DEFAULT NULL)
|
||||||
RETURNS TABLE(table_regclass REGCLASS, schema_name TEXT, table_name TEXT)
|
RETURNS TABLE(table_regclass REGCLASS, schema_name TEXT, table_name TEXT)
|
||||||
AS $$
|
AS $$
|
||||||
SELECT * FROM _CDB_UserTablesInSchema(schema_name) WHERE Not _CDB_IsAnalysisTableName(table_name);
|
SELECT * FROM @extschema@._CDB_UserTablesInSchema(schema_name) WHERE Not @extschema@._CDB_IsAnalysisTableName(table_name);
|
||||||
$$ LANGUAGE 'sql' STABLE PARALLEL SAFE;
|
$$ LANGUAGE 'sql' STABLE PARALLEL SAFE;
|
||||||
|
|
||||||
-- Total spaced used up by Camshaft cached analysis tables in the given schema.
|
-- Total spaced used up by Camshaft cached analysis tables in the given schema.
|
||||||
-- Scope: private.
|
-- Scope: private.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_AnalysisDataSize(schema_name TEXT DEFAULT NULL)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_AnalysisDataSize(schema_name TEXT DEFAULT NULL)
|
||||||
RETURNS bigint AS
|
RETURNS bigint AS
|
||||||
$$
|
$$
|
||||||
DECLARE
|
DECLARE
|
||||||
total_size bigint;
|
total_size bigint;
|
||||||
BEGIN
|
BEGIN
|
||||||
WITH analysis_tables AS (
|
WITH analysis_tables AS (
|
||||||
SELECT t.schema_name, t.table_name FROM _CDB_AnalysisTablesInSchema(schema_name) t
|
SELECT t.schema_name, t.table_name FROM @extschema@._CDB_AnalysisTablesInSchema(schema_name) t
|
||||||
)
|
)
|
||||||
SELECT COALESCE(INT8(SUM(_CDB_total_relation_size(analysis_tables.schema_name, analysis_tables.table_name))))::int8
|
SELECT COALESCE(INT8(SUM(@extschema@._CDB_total_relation_size(analysis_tables.schema_name, analysis_tables.table_name))))::int8
|
||||||
INTO total_size FROM analysis_tables;
|
INTO total_size FROM analysis_tables;
|
||||||
IF total_size IS NOT NULL THEN
|
IF total_size IS NOT NULL THEN
|
||||||
RETURN total_size;
|
RETURN total_size;
|
||||||
|
|||||||
@@ -9,13 +9,13 @@
|
|||||||
|
|
||||||
-- 1) Required checks before running cartodbfication
|
-- 1) Required checks before running cartodbfication
|
||||||
-- Either will pass silenty or raise an exception
|
-- Either will pass silenty or raise an exception
|
||||||
CREATE OR REPLACE FUNCTION _CDB_check_prerequisites(schema_name TEXT, reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_check_prerequisites(schema_name TEXT, reloid REGCLASS)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
sql TEXT;
|
sql TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
IF cartodb.schema_exists(schema_name) = false THEN
|
IF @extschema@.schema_exists(schema_name) = false THEN
|
||||||
RAISE EXCEPTION 'Invalid schema name "%"', schema_name;
|
RAISE EXCEPTION 'Invalid schema name "%"', schema_name;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ END;
|
|||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
-- Drop cartodb triggers (might prevent changing columns)
|
-- Drop cartodb triggers (might prevent changing columns)
|
||||||
CREATE OR REPLACE FUNCTION _CDB_drop_triggers(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_drop_triggers(reloid REGCLASS)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -53,7 +53,7 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
|
|
||||||
|
|
||||||
-- Cartodb_id creation & validation or renaming if invalid
|
-- Cartodb_id creation & validation or renaming if invalid
|
||||||
CREATE OR REPLACE FUNCTION _CDB_create_cartodb_id_column(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_create_cartodb_id_column(reloid REGCLASS)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -200,7 +200,7 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
|
|
||||||
-- Create all triggers
|
-- Create all triggers
|
||||||
-- NOTE: drop/create has the side-effect of re-enabling disabled triggers
|
-- NOTE: drop/create has the side-effect of re-enabling disabled triggers
|
||||||
CREATE OR REPLACE FUNCTION _CDB_create_triggers(schema_name TEXT, reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_create_triggers(schema_name TEXT, reloid REGCLASS)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -209,28 +209,28 @@ BEGIN
|
|||||||
-- "track_updates"
|
-- "track_updates"
|
||||||
sql := 'CREATE trigger track_updates AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON '
|
sql := 'CREATE trigger track_updates AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON '
|
||||||
|| reloid::text
|
|| reloid::text
|
||||||
|| ' FOR EACH STATEMENT EXECUTE PROCEDURE public.cdb_tablemetadata_trigger()';
|
|| ' FOR EACH STATEMENT EXECUTE PROCEDURE @extschema@.cdb_tablemetadata_trigger()';
|
||||||
EXECUTE sql;
|
EXECUTE sql;
|
||||||
|
|
||||||
-- "update_the_geom_webmercator"
|
-- "update_the_geom_webmercator"
|
||||||
-- TODO: why _before_ and not after ?
|
-- TODO: why _before_ and not after ?
|
||||||
sql := 'CREATE trigger update_the_geom_webmercator_trigger BEFORE INSERT OR UPDATE OF the_geom ON '
|
sql := 'CREATE trigger update_the_geom_webmercator_trigger BEFORE INSERT OR UPDATE OF the_geom ON '
|
||||||
|| reloid::text
|
|| reloid::text
|
||||||
|| ' FOR EACH ROW EXECUTE PROCEDURE public._CDB_update_the_geom_webmercator()';
|
|| ' FOR EACH ROW EXECUTE PROCEDURE @extschema@._CDB_update_the_geom_webmercator()';
|
||||||
EXECUTE sql;
|
EXECUTE sql;
|
||||||
|
|
||||||
-- "test_quota" and "test_quota_per_row"
|
-- "test_quota" and "test_quota_per_row"
|
||||||
|
|
||||||
sql := 'CREATE TRIGGER test_quota BEFORE UPDATE OR INSERT ON '
|
sql := 'CREATE TRIGGER test_quota BEFORE UPDATE OR INSERT ON '
|
||||||
|| reloid::text
|
|| reloid::text
|
||||||
|| ' EXECUTE PROCEDURE public.CDB_CheckQuota(0.1, ''-1'', '''
|
|| ' EXECUTE PROCEDURE @extschema@.CDB_CheckQuota(0.1, ''-1'', '''
|
||||||
|| schema_name::text
|
|| schema_name::text
|
||||||
|| ''')';
|
|| ''')';
|
||||||
EXECUTE sql;
|
EXECUTE sql;
|
||||||
|
|
||||||
sql := 'CREATE TRIGGER test_quota_per_row BEFORE UPDATE OR INSERT ON '
|
sql := 'CREATE TRIGGER test_quota_per_row BEFORE UPDATE OR INSERT ON '
|
||||||
|| reloid::text
|
|| reloid::text
|
||||||
|| ' FOR EACH ROW EXECUTE PROCEDURE public.CDB_CheckQuota(0.001, ''-1'', '''
|
|| ' FOR EACH ROW EXECUTE PROCEDURE @extschema@.CDB_CheckQuota(0.001, ''-1'', '''
|
||||||
|| schema_name::text
|
|| schema_name::text
|
||||||
|| ''')';
|
|| ''')';
|
||||||
EXECUTE sql;
|
EXECUTE sql;
|
||||||
@@ -239,7 +239,7 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
|
|
||||||
-- 8.b) Create all raster triggers
|
-- 8.b) Create all raster triggers
|
||||||
-- NOTE: drop/create has the side-effect of re-enabling disabled triggers
|
-- NOTE: drop/create has the side-effect of re-enabling disabled triggers
|
||||||
CREATE OR REPLACE FUNCTION _CDB_create_raster_triggers(schema_name TEXT, reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_create_raster_triggers(schema_name TEXT, reloid REGCLASS)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -248,21 +248,21 @@ BEGIN
|
|||||||
-- "track_updates"
|
-- "track_updates"
|
||||||
sql := 'CREATE trigger track_updates AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON '
|
sql := 'CREATE trigger track_updates AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON '
|
||||||
|| reloid::text
|
|| reloid::text
|
||||||
|| ' FOR EACH STATEMENT EXECUTE PROCEDURE public.cdb_tablemetadata_trigger()';
|
|| ' FOR EACH STATEMENT EXECUTE PROCEDURE @extschema@.cdb_tablemetadata_trigger()';
|
||||||
EXECUTE sql;
|
EXECUTE sql;
|
||||||
|
|
||||||
-- "test_quota" and "test_quota_per_row"
|
-- "test_quota" and "test_quota_per_row"
|
||||||
|
|
||||||
sql := 'CREATE TRIGGER test_quota BEFORE UPDATE OR INSERT ON '
|
sql := 'CREATE TRIGGER test_quota BEFORE UPDATE OR INSERT ON '
|
||||||
|| reloid::text
|
|| reloid::text
|
||||||
|| ' EXECUTE PROCEDURE public.CDB_CheckQuota(1, ''-1'', '''
|
|| ' EXECUTE PROCEDURE @extschema@.CDB_CheckQuota(1, ''-1'', '''
|
||||||
|| schema_name::text
|
|| schema_name::text
|
||||||
|| ''')';
|
|| ''')';
|
||||||
EXECUTE sql;
|
EXECUTE sql;
|
||||||
|
|
||||||
sql := 'CREATE TRIGGER test_quota_per_row BEFORE UPDATE OR INSERT ON '
|
sql := 'CREATE TRIGGER test_quota_per_row BEFORE UPDATE OR INSERT ON '
|
||||||
|| reloid::text
|
|| reloid::text
|
||||||
|| ' FOR EACH ROW EXECUTE PROCEDURE public.CDB_CheckQuota(0.001, ''-1'', '''
|
|| ' FOR EACH ROW EXECUTE PROCEDURE @extschema@.CDB_CheckQuota(0.001, ''-1'', '''
|
||||||
|| schema_name::text
|
|| schema_name::text
|
||||||
|| ''')';
|
|| ''')';
|
||||||
EXECUTE sql;
|
EXECUTE sql;
|
||||||
@@ -272,11 +272,11 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
|
|
||||||
|
|
||||||
-- Update the_geom_webmercator
|
-- Update the_geom_webmercator
|
||||||
CREATE OR REPLACE FUNCTION _CDB_update_the_geom_webmercator()
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_update_the_geom_webmercator()
|
||||||
RETURNS trigger
|
RETURNS trigger
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
NEW.the_geom_webmercator := public.CDB_TransformToWebmercator(NEW.the_geom);
|
NEW.the_geom_webmercator := @extschema@.CDB_TransformToWebmercator(NEW.the_geom);
|
||||||
RETURN NEW;
|
RETURN NEW;
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|
||||||
@@ -284,7 +284,7 @@ $$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|
|||||||
--- Trigger to update the updated_at column. No longer added by default
|
--- Trigger to update the updated_at column. No longer added by default
|
||||||
--- but kept here for compatibility with old tables which still have this behavior
|
--- but kept here for compatibility with old tables which still have this behavior
|
||||||
--- and have it added
|
--- and have it added
|
||||||
CREATE OR REPLACE FUNCTION _CDB_update_updated_at()
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_update_updated_at()
|
||||||
RETURNS TRIGGER AS $$
|
RETURNS TRIGGER AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
NEW.updated_at := now();
|
NEW.updated_at := now();
|
||||||
@@ -293,7 +293,7 @@ END;
|
|||||||
$$ LANGUAGE plpgsql VOLATILE;
|
$$ LANGUAGE plpgsql VOLATILE;
|
||||||
|
|
||||||
-- Auxiliary function
|
-- Auxiliary function
|
||||||
CREATE OR REPLACE FUNCTION cartodb._CDB_is_raster_table(schema_name TEXT, reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_is_raster_table(schema_name TEXT, reloid REGCLASS)
|
||||||
RETURNS BOOLEAN
|
RETURNS BOOLEAN
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -301,7 +301,7 @@ DECLARE
|
|||||||
is_raster BOOLEAN;
|
is_raster BOOLEAN;
|
||||||
rel_name TEXT;
|
rel_name TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
IF cartodb.schema_exists(schema_name) = FALSE THEN
|
IF @extschema@.schema_exists(schema_name) = FALSE THEN
|
||||||
RAISE EXCEPTION 'Invalid schema name "%"', schema_name;
|
RAISE EXCEPTION 'Invalid schema name "%"', schema_name;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
@@ -331,11 +331,11 @@ $$ LANGUAGE PLPGSQL STABLE PARALLEL UNSAFE;
|
|||||||
-- Ensure a table is a "cartodb" table (See https://github.com/CartoDB/cartodb/wiki/CartoDB-user-table)
|
-- Ensure a table is a "cartodb" table (See https://github.com/CartoDB/cartodb/wiki/CartoDB-user-table)
|
||||||
|
|
||||||
DROP FUNCTION IF EXISTS CDB_CartodbfyTable(reloid REGCLASS);
|
DROP FUNCTION IF EXISTS CDB_CartodbfyTable(reloid REGCLASS);
|
||||||
CREATE OR REPLACE FUNCTION CDB_CartodbfyTable(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_CartodbfyTable(reloid REGCLASS)
|
||||||
RETURNS REGCLASS
|
RETURNS REGCLASS
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
RETURN cartodb.CDB_CartodbfyTable('public', reloid);
|
RETURN @extschema@.CDB_CartodbfyTable('public', reloid);
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE PLPGSQL;
|
$$ LANGUAGE PLPGSQL;
|
||||||
|
|
||||||
@@ -388,7 +388,7 @@ $$ LANGUAGE PLPGSQL;
|
|||||||
-- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
-- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
||||||
|
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Columns(OUT pkey TEXT, OUT geomcol TEXT, OUT mercgeomcol TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Columns(OUT pkey TEXT, OUT geomcol TEXT, OUT mercgeomcol TEXT)
|
||||||
RETURNS record
|
RETURNS record
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -401,7 +401,7 @@ END;
|
|||||||
$$ LANGUAGE 'plpgsql' IMMUTABLE PARALLEL SAFE;
|
$$ LANGUAGE 'plpgsql' IMMUTABLE PARALLEL SAFE;
|
||||||
|
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Error(message TEXT, funcname TEXT DEFAULT '_CDB_Error')
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Error(message TEXT, funcname TEXT DEFAULT '_CDB_Error')
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -412,7 +412,7 @@ END;
|
|||||||
$$ LANGUAGE 'plpgsql' VOLATILE PARALLEL SAFE;
|
$$ LANGUAGE 'plpgsql' VOLATILE PARALLEL SAFE;
|
||||||
|
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION _CDB_SQL(sql TEXT, funcname TEXT DEFAULT '_CDB_SQL')
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_SQL(sql TEXT, funcname TEXT DEFAULT '_CDB_SQL')
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -432,7 +432,7 @@ $$ LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
|||||||
-- aware. Find a unique relation name in the given schema, starting from the
|
-- aware. Find a unique relation name in the given schema, starting from the
|
||||||
-- template given. If the template is already unique, just return it;
|
-- template given. If the template is already unique, just return it;
|
||||||
-- otherwise, append an increasing integer until you find a unique variant.
|
-- otherwise, append an increasing integer until you find a unique variant.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Unique_Relation_Name(schemaname TEXT, relationname TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Unique_Relation_Name(schemaname TEXT, relationname TEXT)
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -451,7 +451,7 @@ $$ LANGUAGE 'plpgsql' VOLATILE PARALLEL SAFE;
|
|||||||
-- aware. Find a unique column name in the given relation, starting from the
|
-- aware. Find a unique column name in the given relation, starting from the
|
||||||
-- column name given. If the column name is already unique, just return it;
|
-- column name given. If the column name is already unique, just return it;
|
||||||
-- otherwise, append an increasing integer until you find a unique variant.
|
-- otherwise, append an increasing integer until you find a unique variant.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Unique_Column_Name(reloid REGCLASS, columnname TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Unique_Column_Name(reloid REGCLASS, columnname TEXT)
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -471,7 +471,7 @@ $$ LANGUAGE 'plpgsql' VOLATILE PARALLEL SAFE;
|
|||||||
-- we can no-op on the table copy and just ensure that the
|
-- we can no-op on the table copy and just ensure that the
|
||||||
-- indexes and triggers are in place
|
-- indexes and triggers are in place
|
||||||
DROP FUNCTION IF EXISTS _CDB_Has_Usable_Primary_ID(reloid REGCLASS);
|
DROP FUNCTION IF EXISTS _CDB_Has_Usable_Primary_ID(reloid REGCLASS);
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Has_Usable_Primary_ID(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Has_Usable_Primary_ID(reloid REGCLASS)
|
||||||
RETURNS BOOLEAN
|
RETURNS BOOLEAN
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -485,7 +485,7 @@ BEGIN
|
|||||||
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', 'entered function';
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', 'entered function';
|
||||||
|
|
||||||
-- Read in the names of the CartoDB columns
|
-- Read in the names of the CartoDB columns
|
||||||
const := _CDB_Columns();
|
const := @extschema@._CDB_Columns();
|
||||||
|
|
||||||
-- Do we already have a properly named column?
|
-- Do we already have a properly named column?
|
||||||
SELECT a.attname, i.indisprimary, i.indisunique, a.attnotnull, a.atttypid
|
SELECT a.attname, i.indisprimary, i.indisunique, a.attnotnull, a.atttypid
|
||||||
@@ -537,8 +537,8 @@ BEGIN
|
|||||||
|
|
||||||
-- Clean up test constraint
|
-- Clean up test constraint
|
||||||
IF useable_key THEN
|
IF useable_key THEN
|
||||||
PERFORM _CDB_SQL(Format('ALTER TABLE %s DROP CONSTRAINT %s_pk', reloid::text, const.pkey));
|
PERFORM @extschema@._CDB_SQL(Format('ALTER TABLE %s DROP CONSTRAINT %s_pk', reloid::text, const.pkey));
|
||||||
PERFORM _CDB_SQL(Format('ALTER TABLE %s DROP CONSTRAINT %s_integer', reloid::text, const.pkey));
|
PERFORM @extschema@._CDB_SQL(Format('ALTER TABLE %s DROP CONSTRAINT %s_integer', reloid::text, const.pkey));
|
||||||
|
|
||||||
-- Move non-valid column out of the way
|
-- Move non-valid column out of the way
|
||||||
ELSE
|
ELSE
|
||||||
@@ -546,7 +546,7 @@ BEGIN
|
|||||||
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %',
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %',
|
||||||
Format('found non-valid ''%s''', const.pkey);
|
Format('found non-valid ''%s''', const.pkey);
|
||||||
|
|
||||||
PERFORM _CDB_Error(sql, Format('_CDB_Has_Usable_Primary_ID: Error: invalid cartodb_id, %s', const.pkey));
|
PERFORM @extschema@._CDB_Error(sql, Format('_CDB_Has_Usable_Primary_ID: Error: invalid cartodb_id, %s', const.pkey));
|
||||||
|
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
@@ -569,7 +569,7 @@ BEGIN
|
|||||||
|
|
||||||
-- Yes! Ok, rename it.
|
-- Yes! Ok, rename it.
|
||||||
IF FOUND THEN
|
IF FOUND THEN
|
||||||
PERFORM _CDB_SQL(Format('ALTER TABLE %s RENAME COLUMN %s TO %s', reloid::text, rec.attname, const.pkey),'_CDB_Has_Usable_Primary_ID');
|
PERFORM @extschema@._CDB_SQL(Format('ALTER TABLE %s RENAME COLUMN %s TO %s', reloid::text, rec.attname, const.pkey),'_CDB_Has_Usable_Primary_ID');
|
||||||
RETURN true;
|
RETURN true;
|
||||||
ELSE
|
ELSE
|
||||||
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %',
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %',
|
||||||
@@ -586,7 +586,7 @@ END;
|
|||||||
$$ LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Has_Usable_PK_Sequence(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Has_Usable_PK_Sequence(reloid REGCLASS)
|
||||||
RETURNS BOOLEAN
|
RETURNS BOOLEAN
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -595,7 +595,7 @@ DECLARE
|
|||||||
has_sequence BOOLEAN = false;
|
has_sequence BOOLEAN = false;
|
||||||
BEGIN
|
BEGIN
|
||||||
|
|
||||||
const := _CDB_Columns();
|
const := @extschema@._CDB_Columns();
|
||||||
|
|
||||||
SELECT pg_get_serial_sequence(reloid::text, const.pkey)
|
SELECT pg_get_serial_sequence(reloid::text, const.pkey)
|
||||||
INTO STRICT seq;
|
INTO STRICT seq;
|
||||||
@@ -607,19 +607,19 @@ $$ LANGUAGE 'plpgsql' STABLE PARALLEL SAFE;
|
|||||||
|
|
||||||
-- Return a set of columns that can be candidates to be the_geom[webmercator]
|
-- Return a set of columns that can be candidates to be the_geom[webmercator]
|
||||||
-- with some extra information to analyze them.
|
-- with some extra information to analyze them.
|
||||||
CREATE OR REPLACE FUNCTION _cdb_geom_candidate_columns(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._cdb_geom_candidate_columns(reloid REGCLASS)
|
||||||
RETURNS TABLE (attname name, srid integer, typname name, desired_attname text, desired_srid integer)
|
RETURNS TABLE (attname name, srid integer, typname name, desired_attname text, desired_srid integer)
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
const RECORD;
|
const RECORD;
|
||||||
BEGIN
|
BEGIN
|
||||||
|
|
||||||
const := _CDB_Columns();
|
const := @extschema@._CDB_Columns();
|
||||||
|
|
||||||
RETURN QUERY
|
RETURN QUERY
|
||||||
SELECT
|
SELECT
|
||||||
a.attname,
|
a.attname,
|
||||||
CASE WHEN t.typname = 'geometry' THEN postgis_typmod_srid(a.atttypmod) ELSE NULL END AS srid,
|
CASE WHEN t.typname = 'geometry' THEN @postgisschema@.postgis_typmod_srid(a.atttypmod) ELSE NULL END AS srid,
|
||||||
t.typname,
|
t.typname,
|
||||||
f.desired_attname, f.desired_srid
|
f.desired_attname, f.desired_srid
|
||||||
FROM pg_class c
|
FROM pg_class c
|
||||||
@@ -629,15 +629,16 @@ BEGIN
|
|||||||
WHERE c.oid = reloid
|
WHERE c.oid = reloid
|
||||||
AND a.attnum > 0
|
AND a.attnum > 0
|
||||||
AND NOT a.attisdropped
|
AND NOT a.attisdropped
|
||||||
AND postgis_typmod_srid(a.atttypmod) IN (4326, 3857, 0)
|
AND @postgisschema@.postgis_typmod_srid(a.atttypmod) IN (4326, 3857, 0)
|
||||||
ORDER BY t.oid ASC;
|
ORDER BY t.oid ASC;
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE 'plpgsql' STABLE PARALLEL SAFE;
|
$$ LANGUAGE 'plpgsql' STABLE PARALLEL SAFE;
|
||||||
|
|
||||||
DO $$
|
DO $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
SET search_path TO @extschema@;
|
||||||
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = '_cdb_has_usable_geom_record') THEN
|
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = '_cdb_has_usable_geom_record') THEN
|
||||||
CREATE TYPE _cdb_has_usable_geom_record
|
CREATE TYPE @extschema@._cdb_has_usable_geom_record
|
||||||
AS (has_usable_geoms boolean,
|
AS (has_usable_geoms boolean,
|
||||||
text_geom_column boolean,
|
text_geom_column boolean,
|
||||||
text_geom_column_name text,
|
text_geom_column_name text,
|
||||||
@@ -650,8 +651,8 @@ BEGIN
|
|||||||
END$$;
|
END$$;
|
||||||
|
|
||||||
DROP FUNCTION IF EXISTS _CDB_Has_Usable_Geom(REGCLASS);
|
DROP FUNCTION IF EXISTS _CDB_Has_Usable_Geom(REGCLASS);
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Has_Usable_Geom(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Has_Usable_Geom(reloid REGCLASS)
|
||||||
RETURNS _cdb_has_usable_geom_record
|
RETURNS @extschema@._cdb_has_usable_geom_record
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
r1 RECORD;
|
r1 RECORD;
|
||||||
@@ -679,11 +680,11 @@ BEGIN
|
|||||||
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %', 'entered function';
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %', 'entered function';
|
||||||
|
|
||||||
-- Read in the names of the CartoDB columns
|
-- Read in the names of the CartoDB columns
|
||||||
const := _CDB_Columns();
|
const := @extschema@._CDB_Columns();
|
||||||
|
|
||||||
-- Do we have a column we can use?
|
-- Do we have a column we can use?
|
||||||
FOR r1 IN
|
FOR r1 IN
|
||||||
SELECT * FROM _cdb_geom_candidate_columns(reloid)
|
SELECT * FROM @extschema@._cdb_geom_candidate_columns(reloid)
|
||||||
LOOP
|
LOOP
|
||||||
|
|
||||||
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %', Format('checking column ''%s''', r1.attname);
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %', Format('checking column ''%s''', r1.attname);
|
||||||
@@ -697,7 +698,7 @@ BEGIN
|
|||||||
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %', Format('column ''%s'' is a text column', r1.attname);
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %', Format('column ''%s'' is a text column', r1.attname);
|
||||||
|
|
||||||
BEGIN
|
BEGIN
|
||||||
sql := Format('SELECT Max(ST_SRID(%I::geometry)) AS srid FROM %I', r1.attname, reloid::text);
|
sql := Format('SELECT Max(@postgisschema@.ST_SRID(%I::@postgisschema@.geometry)) AS srid FROM %I', r1.attname, reloid::text);
|
||||||
EXECUTE sql INTO srid;
|
EXECUTE sql INTO srid;
|
||||||
-- This gets skipped if EXCEPTION happens
|
-- This gets skipped if EXCEPTION happens
|
||||||
-- Let the table writer know we need to convert from text
|
-- Let the table writer know we need to convert from text
|
||||||
@@ -714,9 +715,9 @@ BEGIN
|
|||||||
WHEN others THEN
|
WHEN others THEN
|
||||||
IF SQLERRM = 'parse error - invalid geometry' THEN
|
IF SQLERRM = 'parse error - invalid geometry' THEN
|
||||||
text_geom_column := false;
|
text_geom_column := false;
|
||||||
str := cartodb._CDB_Unique_Column_Identifier(NULL, r1.attname, NULL, reloid);
|
str := @extschema@._CDB_Unique_Column_Identifier(NULL, r1.attname, NULL, reloid);
|
||||||
sql := Format('ALTER TABLE %s RENAME COLUMN %s TO %I', reloid::text, r1.attname, str);
|
sql := Format('ALTER TABLE %s RENAME COLUMN %s TO %I', reloid::text, r1.attname, str);
|
||||||
PERFORM _CDB_SQL(sql,'_CDB_Has_Usable_Geom');
|
PERFORM @extschema@._CDB_SQL(sql,'_CDB_Has_Usable_Geom');
|
||||||
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %',
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %',
|
||||||
Format('Text column %s is not convertible to geometry, renamed to %s', r1.attname, str);
|
Format('Text column %s is not convertible to geometry, renamed to %s', r1.attname, str);
|
||||||
ELSE
|
ELSE
|
||||||
@@ -726,9 +727,9 @@ BEGIN
|
|||||||
|
|
||||||
-- Just change its name so we can write a new column into that name.
|
-- Just change its name so we can write a new column into that name.
|
||||||
ELSE
|
ELSE
|
||||||
str := cartodb._CDB_Unique_Column_Identifier(NULL, r1.attname, NULL, reloid);
|
str := @extschema@._CDB_Unique_Column_Identifier(NULL, r1.attname, NULL, reloid);
|
||||||
sql := Format('ALTER TABLE %s RENAME COLUMN %s TO %I', reloid::text, r1.attname, str);
|
sql := Format('ALTER TABLE %s RENAME COLUMN %s TO %I', reloid::text, r1.attname, str);
|
||||||
PERFORM _CDB_SQL(sql,'_CDB_Has_Usable_Geom');
|
PERFORM @extschema@._CDB_SQL(sql,'_CDB_Has_Usable_Geom');
|
||||||
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %',
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %',
|
||||||
Format('%s is the wrong type, renamed to %s', r1.attname, str);
|
Format('%s is the wrong type, renamed to %s', r1.attname, str);
|
||||||
END IF;
|
END IF;
|
||||||
@@ -784,7 +785,7 @@ $$ LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
|||||||
-- a "good" one, and the same for the geometry columns. If all the required
|
-- a "good" one, and the same for the geometry columns. If all the required
|
||||||
-- columns are in place already, it no-ops and just renames the table to
|
-- columns are in place already, it no-ops and just renames the table to
|
||||||
-- the destination if necessary.
|
-- the destination if necessary.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Rewrite_Table(reloid REGCLASS, destschema TEXT DEFAULT NULL)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Rewrite_Table(reloid REGCLASS, destschema TEXT DEFAULT NULL)
|
||||||
RETURNS BOOLEAN
|
RETURNS BOOLEAN
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -820,7 +821,7 @@ BEGIN
|
|||||||
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): %', 'entered function';
|
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): %', 'entered function';
|
||||||
|
|
||||||
-- Read CartoDB standard column names in
|
-- Read CartoDB standard column names in
|
||||||
const := _CDB_Columns();
|
const := @extschema@._CDB_Columns();
|
||||||
|
|
||||||
-- Save the raw schema/table names for later
|
-- Save the raw schema/table names for later
|
||||||
SELECT n.nspname, c.relname, c.relname
|
SELECT n.nspname, c.relname, c.relname
|
||||||
@@ -836,7 +837,7 @@ BEGIN
|
|||||||
-- See if there is a primary key column we need to carry along to the
|
-- See if there is a primary key column we need to carry along to the
|
||||||
-- new table. If this is true, it implies there is an indexed
|
-- new table. If this is true, it implies there is an indexed
|
||||||
-- primary key of integer type named (by default) cartodb_id
|
-- primary key of integer type named (by default) cartodb_id
|
||||||
SELECT _CDB_Has_Usable_Primary_ID(reloid)
|
SELECT @extschema@._CDB_Has_Usable_Primary_ID(reloid)
|
||||||
INTO STRICT has_usable_primary_key;
|
INTO STRICT has_usable_primary_key;
|
||||||
|
|
||||||
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): has_usable_primary_key %', has_usable_primary_key;
|
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): has_usable_primary_key %', has_usable_primary_key;
|
||||||
@@ -854,23 +855,23 @@ BEGIN
|
|||||||
-- transformation of the table, we can just ensure proper
|
-- transformation of the table, we can just ensure proper
|
||||||
-- indexes are in place and apply a rename
|
-- indexes are in place and apply a rename
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM _CDB_Has_Usable_Geom(reloid)
|
FROM @extschema@._CDB_Has_Usable_Geom(reloid)
|
||||||
INTO STRICT gc;
|
INTO STRICT gc;
|
||||||
|
|
||||||
-- If geom is the wrong name, just rename it.
|
-- If geom is the wrong name, just rename it.
|
||||||
IF gc.has_geom AND gc.has_geom_name != const.geomcol THEN
|
IF gc.has_geom AND gc.has_geom_name != const.geomcol THEN
|
||||||
sql := Format('ALTER TABLE %s DROP COLUMN IF EXISTS %I', reloid::text, const.geomcol);
|
sql := Format('ALTER TABLE %s DROP COLUMN IF EXISTS %I', reloid::text, const.geomcol);
|
||||||
PERFORM _CDB_SQL(sql,'_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(sql,'_CDB_Rewrite_Table');
|
||||||
sql := Format('ALTER TABLE %s RENAME COLUMN %I TO %I', reloid::text, gc.has_geom_name, const.geomcol);
|
sql := Format('ALTER TABLE %s RENAME COLUMN %I TO %I', reloid::text, gc.has_geom_name, const.geomcol);
|
||||||
PERFORM _CDB_SQL(sql,'_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(sql,'_CDB_Rewrite_Table');
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- If mercgeom is the wrong name, just rename it.
|
-- If mercgeom is the wrong name, just rename it.
|
||||||
IF gc.has_mercgeom AND gc.has_mercgeom_name != const.mercgeomcol THEN
|
IF gc.has_mercgeom AND gc.has_mercgeom_name != const.mercgeomcol THEN
|
||||||
sql := Format('ALTER TABLE %s DROP COLUMN IF EXISTS %I', reloid::text, const.mercgeomcol);
|
sql := Format('ALTER TABLE %s DROP COLUMN IF EXISTS %I', reloid::text, const.mercgeomcol);
|
||||||
PERFORM _CDB_SQL(sql,'_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(sql,'_CDB_Rewrite_Table');
|
||||||
sql := Format('ALTER TABLE %s RENAME COLUMN %I TO %I', reloid::text, gc.has_mercgeom_name, const.mercgeomcol);
|
sql := Format('ALTER TABLE %s RENAME COLUMN %I TO %I', reloid::text, gc.has_mercgeom_name, const.mercgeomcol);
|
||||||
PERFORM _CDB_SQL(sql,'_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(sql,'_CDB_Rewrite_Table');
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
|
|
||||||
@@ -885,7 +886,7 @@ BEGIN
|
|||||||
IF destschema != relschema THEN
|
IF destschema != relschema THEN
|
||||||
|
|
||||||
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): perfect table needs to be moved to schema (%)', destschema;
|
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): perfect table needs to be moved to schema (%)', destschema;
|
||||||
PERFORM _CDB_SQL(Format('ALTER TABLE %s SET SCHEMA %I', reloid::text, destschema), '_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(Format('ALTER TABLE %s SET SCHEMA %I', reloid::text, destschema), '_CDB_Rewrite_Table');
|
||||||
|
|
||||||
ELSE
|
ELSE
|
||||||
|
|
||||||
@@ -911,21 +912,21 @@ BEGIN
|
|||||||
INTO relseq;
|
INTO relseq;
|
||||||
-- If it's the name we want, then rename it
|
-- If it's the name we want, then rename it
|
||||||
IF relseq IS NOT NULL AND relseq = Format('%I.%I', destschema, destseq) THEN
|
IF relseq IS NOT NULL AND relseq = Format('%I.%I', destschema, destseq) THEN
|
||||||
PERFORM _CDB_SQL(Format('ALTER SEQUENCE %s RENAME TO %I', relseq, Format('tmp_%s', destseq)), '_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(Format('ALTER SEQUENCE %s RENAME TO %I', relseq, Format('tmp_%s', destseq)), '_CDB_Rewrite_Table');
|
||||||
END IF;
|
END IF;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- Put the primary key sequence in the right schema
|
-- Put the primary key sequence in the right schema
|
||||||
-- If the new table is not moving, better ensure the sequence name
|
-- If the new table is not moving, better ensure the sequence name
|
||||||
-- is unique
|
-- is unique
|
||||||
destseq := cartodb._CDB_Unique_Identifier(NULL, relname, '_' || const.pkey || '_seq', destschema);
|
destseq := @extschema@._CDB_Unique_Identifier(NULL, relname, '_' || const.pkey || '_seq', destschema);
|
||||||
destseq := Format('%I.%I', destschema, destseq);
|
destseq := Format('%I.%I', destschema, destseq);
|
||||||
PERFORM _CDB_SQL(Format('CREATE SEQUENCE %s', destseq), '_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(Format('CREATE SEQUENCE %s', destseq), '_CDB_Rewrite_Table');
|
||||||
|
|
||||||
-- Temporary table name if we are re-writing in place
|
-- Temporary table name if we are re-writing in place
|
||||||
-- Note copyname is already escaped and safe to use as identifier
|
-- Note copyname is already escaped and safe to use as identifier
|
||||||
IF destschema = relschema THEN
|
IF destschema = relschema THEN
|
||||||
copyname := Format('%I.%I', destschema, cartodb._CDB_Unique_Identifier(NULL, destname, NULL), destschema);
|
copyname := Format('%I.%I', destschema, @extschema@._CDB_Unique_Identifier(NULL, destname, NULL), destschema);
|
||||||
ELSE
|
ELSE
|
||||||
copyname := Format('%I.%I', destschema, destname);
|
copyname := Format('%I.%I', destschema, destname);
|
||||||
END IF;
|
END IF;
|
||||||
@@ -966,11 +967,11 @@ BEGIN
|
|||||||
ORDER BY a.attnum
|
ORDER BY a.attnum
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
)
|
)
|
||||||
SELECT ', ST_Transform('
|
SELECT ', @postgisschema@.ST_Transform('
|
||||||
|| t.missing_srid_start || t.attname || '::geometry' || t.missing_srid_end
|
|| t.missing_srid_start || t.attname || '::geometry' || t.missing_srid_end
|
||||||
|| ',4326)::Geometry(GEOMETRY,4326) AS '
|
|| ',4326)::Geometry(GEOMETRY,4326) AS '
|
||||||
|| const.geomcol
|
|| const.geomcol
|
||||||
|| ', cartodb.CDB_TransformToWebmercator('
|
|| ', @extschema@.CDB_TransformToWebmercator('
|
||||||
|| t.missing_srid_start || t.attname || '::geometry' || t.missing_srid_end
|
|| t.missing_srid_start || t.attname || '::geometry' || t.missing_srid_end
|
||||||
|| ')::Geometry(GEOMETRY,3857) AS '
|
|| ')::Geometry(GEOMETRY,3857) AS '
|
||||||
|| const.mercgeomcol,
|
|| const.mercgeomcol,
|
||||||
@@ -1009,7 +1010,7 @@ BEGIN
|
|||||||
-- column and read the first SRID off it it, if there is a row
|
-- column and read the first SRID off it it, if there is a row
|
||||||
-- to read.
|
-- to read.
|
||||||
IF FOUND THEN
|
IF FOUND THEN
|
||||||
EXECUTE Format('SELECT ST_SRID(%s) AS srid FROM %s LIMIT 1', rec.attname, reloid::text)
|
EXECUTE Format('SELECT @postgisschema@.ST_SRID(%s) AS srid FROM %s LIMIT 1', rec.attname, reloid::text)
|
||||||
INTO geom_srid;
|
INTO geom_srid;
|
||||||
ELSE
|
ELSE
|
||||||
geom_srid := 0;
|
geom_srid := 0;
|
||||||
@@ -1025,7 +1026,7 @@ BEGIN
|
|||||||
SELECT
|
SELECT
|
||||||
a.attname,
|
a.attname,
|
||||||
postgis_typmod_type(a.atttypmod) AS geomtype,
|
postgis_typmod_type(a.atttypmod) AS geomtype,
|
||||||
CASE WHEN postgis_typmod_srid(a.atttypmod) = 0 AND srid.srid = 0 THEN 'ST_SetSRID(' ELSE '' END AS missing_srid_start,
|
CASE WHEN postgis_typmod_srid(a.atttypmod) = 0 AND srid.srid = 0 THEN '@postgisschema@.ST_SetSRID(' ELSE '' END AS missing_srid_start,
|
||||||
CASE WHEN postgis_typmod_srid(a.atttypmod) = 0 AND srid.srid = 0 THEN ',4326)' ELSE '' END AS missing_srid_end
|
CASE WHEN postgis_typmod_srid(a.atttypmod) = 0 AND srid.srid = 0 THEN ',4326)' ELSE '' END AS missing_srid_end
|
||||||
FROM pg_class c
|
FROM pg_class c
|
||||||
JOIN pg_attribute a ON a.attrelid = c.oid
|
JOIN pg_attribute a ON a.attrelid = c.oid
|
||||||
@@ -1038,11 +1039,11 @@ BEGIN
|
|||||||
ORDER BY a.attnum
|
ORDER BY a.attnum
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
)
|
)
|
||||||
SELECT ', ST_Transform('
|
SELECT ', @postgisschema@.ST_Transform('
|
||||||
|| t.missing_srid_start || t.attname || t.missing_srid_end
|
|| t.missing_srid_start || t.attname || t.missing_srid_end
|
||||||
|| ',4326)::Geometry(GEOMETRY,4326) AS '
|
|| ',4326)::Geometry(GEOMETRY,4326) AS '
|
||||||
|| const.geomcol
|
|| const.geomcol
|
||||||
|| ', cartodb.CDB_TransformToWebmercator('
|
|| ', @extschema@.CDB_TransformToWebmercator('
|
||||||
|| t.missing_srid_start || t.attname || t.missing_srid_end
|
|| t.missing_srid_start || t.attname || t.missing_srid_end
|
||||||
|| ')::Geometry(GEOMETRY,3857) AS '
|
|| ')::Geometry(GEOMETRY,3857) AS '
|
||||||
|| const.mercgeomcol,
|
|| const.mercgeomcol,
|
||||||
@@ -1096,7 +1097,7 @@ BEGIN
|
|||||||
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): %', sql;
|
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): %', sql;
|
||||||
|
|
||||||
-- Run it!
|
-- Run it!
|
||||||
PERFORM _CDB_SQL(sql, '_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(sql, '_CDB_Rewrite_Table');
|
||||||
|
|
||||||
-- Set up the primary key sequence
|
-- Set up the primary key sequence
|
||||||
-- If we copied the primary key from the original data, we need
|
-- If we copied the primary key from the original data, we need
|
||||||
@@ -1106,38 +1107,38 @@ BEGIN
|
|||||||
INTO destseqmax;
|
INTO destseqmax;
|
||||||
|
|
||||||
IF destseqmax IS NOT NULL THEN
|
IF destseqmax IS NOT NULL THEN
|
||||||
PERFORM _CDB_SQL(Format('SELECT setval(''%s'', %s)', destseq, destseqmax), '_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(Format('SELECT setval(''%s'', %s)', destseq, destseqmax), '_CDB_Rewrite_Table');
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- Make the primary key use the sequence as its default value
|
-- Make the primary key use the sequence as its default value
|
||||||
sql := Format('ALTER TABLE %s ALTER COLUMN %s SET DEFAULT nextval(''%s'')',
|
sql := Format('ALTER TABLE %s ALTER COLUMN %s SET DEFAULT nextval(''%s'')',
|
||||||
copyname, const.pkey, destseq);
|
copyname, const.pkey, destseq);
|
||||||
PERFORM _CDB_SQL(sql, '_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(sql, '_CDB_Rewrite_Table');
|
||||||
|
|
||||||
-- Make the sequence owned by the table, so when the table drops,
|
-- Make the sequence owned by the table, so when the table drops,
|
||||||
-- the sequence does too
|
-- the sequence does too
|
||||||
sql := Format('ALTER SEQUENCE %s OWNED BY %s.%s', destseq, copyname, const.pkey);
|
sql := Format('ALTER SEQUENCE %s OWNED BY %s.%s', destseq, copyname, const.pkey);
|
||||||
PERFORM _CDB_SQL(sql,'_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(sql,'_CDB_Rewrite_Table');
|
||||||
|
|
||||||
|
|
||||||
-- We just made a copy, so we can drop the original now
|
-- We just made a copy, so we can drop the original now
|
||||||
sql := Format('DROP TABLE %s', reloid::text);
|
sql := Format('DROP TABLE %s', reloid::text);
|
||||||
PERFORM _CDB_SQL(sql, '_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(sql, '_CDB_Rewrite_Table');
|
||||||
|
|
||||||
-- If the table is being created by a SECURITY DEFINER function
|
-- If the table is being created by a SECURITY DEFINER function
|
||||||
-- make sure the user is set back to the user who is connected
|
-- make sure the user is set back to the user who is connected
|
||||||
IF current_user != session_user THEN
|
IF current_user != session_user THEN
|
||||||
sql := Format('ALTER TABLE IF EXISTS %s OWNER TO %s', copyname, session_user);
|
sql := Format('ALTER TABLE IF EXISTS %s OWNER TO %s', copyname, session_user);
|
||||||
PERFORM _CDB_SQL(sql, '_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(sql, '_CDB_Rewrite_Table');
|
||||||
sql := Format('ALTER SEQUENCE IF EXISTS %s OWNER TO %s', destseq, session_user);
|
sql := Format('ALTER SEQUENCE IF EXISTS %s OWNER TO %s', destseq, session_user);
|
||||||
PERFORM _CDB_SQL(sql, '_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(sql, '_CDB_Rewrite_Table');
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- If we used a temporary destination table
|
-- If we used a temporary destination table
|
||||||
-- we can now rename it into place
|
-- we can now rename it into place
|
||||||
IF destschema = relschema THEN
|
IF destschema = relschema THEN
|
||||||
sql := Format('ALTER TABLE %s RENAME TO %I', copyname, destname);
|
sql := Format('ALTER TABLE %s RENAME TO %I', copyname, destname);
|
||||||
PERFORM _CDB_SQL(sql, '_CDB_Rewrite_Table');
|
PERFORM @extschema@._CDB_SQL(sql, '_CDB_Rewrite_Table');
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
RETURN true;
|
RETURN true;
|
||||||
@@ -1149,7 +1150,7 @@ $$ LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
|||||||
-- Assumes the table already has the right metadata columns
|
-- Assumes the table already has the right metadata columns
|
||||||
-- (primary key and two geometry columns) and adds primary key
|
-- (primary key and two geometry columns) and adds primary key
|
||||||
-- and geometry indexes if necessary.
|
-- and geometry indexes if necessary.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Add_Indexes(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Add_Indexes(reloid REGCLASS)
|
||||||
RETURNS BOOLEAN
|
RETURNS BOOLEAN
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -1163,7 +1164,7 @@ BEGIN
|
|||||||
RAISE DEBUG 'CDB(_CDB_Add_Indexes): %', 'entered function';
|
RAISE DEBUG 'CDB(_CDB_Add_Indexes): %', 'entered function';
|
||||||
|
|
||||||
-- Read CartoDB standard column names in
|
-- Read CartoDB standard column names in
|
||||||
const := _CDB_Columns();
|
const := @extschema@._CDB_Columns();
|
||||||
|
|
||||||
-- Extract just the relname to use for the index names
|
-- Extract just the relname to use for the index names
|
||||||
SELECT c.relname
|
SELECT c.relname
|
||||||
@@ -1189,7 +1190,7 @@ BEGIN
|
|||||||
IF FOUND THEN
|
IF FOUND THEN
|
||||||
RAISE DEBUG 'CDB(_CDB_Add_Indexes): dropping unwanted primary key ''%''', rec.pkey;
|
RAISE DEBUG 'CDB(_CDB_Add_Indexes): dropping unwanted primary key ''%''', rec.pkey;
|
||||||
sql := Format('ALTER TABLE %s DROP CONSTRAINT IF EXISTS %s', reloid::text, rec.pkey);
|
sql := Format('ALTER TABLE %s DROP CONSTRAINT IF EXISTS %s', reloid::text, rec.pkey);
|
||||||
PERFORM _CDB_SQL(sql, '_CDB_Add_Indexes');
|
PERFORM @extschema@._CDB_SQL(sql, '_CDB_Add_Indexes');
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
|
|
||||||
@@ -1210,7 +1211,7 @@ BEGIN
|
|||||||
-- No primary key? Add one.
|
-- No primary key? Add one.
|
||||||
IF NOT FOUND THEN
|
IF NOT FOUND THEN
|
||||||
sql := Format('ALTER TABLE %s ADD PRIMARY KEY (%s)', reloid::text, const.pkey);
|
sql := Format('ALTER TABLE %s ADD PRIMARY KEY (%s)', reloid::text, const.pkey);
|
||||||
PERFORM _CDB_SQL(sql, '_CDB_Add_Indexes');
|
PERFORM @extschema@._CDB_SQL(sql, '_CDB_Add_Indexes');
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- Add geometry indexes to all "special geometry columns" that
|
-- Add geometry indexes to all "special geometry columns" that
|
||||||
@@ -1239,7 +1240,7 @@ BEGIN
|
|||||||
AND am.amname != 'gist'
|
AND am.amname != 'gist'
|
||||||
LOOP
|
LOOP
|
||||||
sql := Format('CREATE INDEX ON %s USING GIST (%s)', reloid::text, rec.attname);
|
sql := Format('CREATE INDEX ON %s USING GIST (%s)', reloid::text, rec.attname);
|
||||||
PERFORM _CDB_SQL(sql, '_CDB_Add_Indexes');
|
PERFORM @extschema@._CDB_SQL(sql, '_CDB_Add_Indexes');
|
||||||
END LOOP;
|
END LOOP;
|
||||||
|
|
||||||
RETURN true;
|
RETURN true;
|
||||||
@@ -1247,8 +1248,8 @@ BEGIN
|
|||||||
END;
|
END;
|
||||||
$$ LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
DROP FUNCTION IF EXISTS CDB_CartodbfyTable(destschema TEXT, reloid REGCLASS);
|
DROP FUNCTION IF EXISTS @extschema@.CDB_CartodbfyTable(destschema TEXT, reloid REGCLASS);
|
||||||
CREATE OR REPLACE FUNCTION CDB_CartodbfyTable(destschema TEXT, reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_CartodbfyTable(destschema TEXT, reloid REGCLASS)
|
||||||
RETURNS REGCLASS
|
RETURNS REGCLASS
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -1270,7 +1271,7 @@ BEGIN
|
|||||||
FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid
|
FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid
|
||||||
WHERE c.oid = reloid;
|
WHERE c.oid = reloid;
|
||||||
|
|
||||||
PERFORM cartodb._CDB_check_prerequisites(destschema, reloid);
|
PERFORM @extschema@._CDB_check_prerequisites(destschema, reloid);
|
||||||
|
|
||||||
-- Check destination schema exists
|
-- Check destination schema exists
|
||||||
-- Throws an exception of there is no matching schema
|
-- Throws an exception of there is no matching schema
|
||||||
@@ -1287,29 +1288,29 @@ BEGIN
|
|||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- Drop triggers first
|
-- Drop triggers first
|
||||||
PERFORM _CDB_drop_triggers(reloid);
|
PERFORM @extschema@._CDB_drop_triggers(reloid);
|
||||||
|
|
||||||
-- Rasters only get a cartodb_id and a limited selection of triggers
|
-- Rasters only get a cartodb_id and a limited selection of triggers
|
||||||
-- underlying assumption is that they are already formed up correctly
|
-- underlying assumption is that they are already formed up correctly
|
||||||
SELECT cartodb._CDB_is_raster_table(destschema, reloid) INTO is_raster;
|
SELECT @extschema@._CDB_is_raster_table(destschema, reloid) INTO is_raster;
|
||||||
IF is_raster THEN
|
IF is_raster THEN
|
||||||
|
|
||||||
PERFORM cartodb._CDB_create_cartodb_id_column(reloid);
|
PERFORM @extschema@._CDB_create_cartodb_id_column(reloid);
|
||||||
PERFORM cartodb._CDB_create_raster_triggers(destschema, reloid);
|
PERFORM @extschema@._CDB_create_raster_triggers(destschema, reloid);
|
||||||
|
|
||||||
ELSE
|
ELSE
|
||||||
|
|
||||||
-- Rewrite (or rename) the table to the new location
|
-- Rewrite (or rename) the table to the new location
|
||||||
PERFORM _CDB_Rewrite_Table(reloid, destschema);
|
PERFORM @extschema@._CDB_Rewrite_Table(reloid, destschema);
|
||||||
|
|
||||||
-- The old regclass might not be valid anymore if we re-wrote the table...
|
-- The old regclass might not be valid anymore if we re-wrote the table...
|
||||||
destoid := (destschema || '.' || destname)::regclass;
|
destoid := (destschema || '.' || destname)::regclass;
|
||||||
|
|
||||||
-- Add indexes to the destination table, as necessary
|
-- Add indexes to the destination table, as necessary
|
||||||
PERFORM _CDB_Add_Indexes(destoid);
|
PERFORM @extschema@._CDB_Add_Indexes(destoid);
|
||||||
|
|
||||||
-- Add triggers to the destination table, as necessary
|
-- Add triggers to the destination table, as necessary
|
||||||
PERFORM _CDB_create_triggers(destschema, destoid);
|
PERFORM @extschema@._CDB_create_triggers(destschema, destoid);
|
||||||
|
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
-- Function returning the column names of a table
|
-- Function returning the column names of a table
|
||||||
CREATE OR REPLACE FUNCTION CDB_ColumnNames(REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_ColumnNames(REGCLASS)
|
||||||
RETURNS SETOF information_schema.sql_identifier
|
RETURNS SETOF information_schema.sql_identifier
|
||||||
AS $$
|
AS $$
|
||||||
SELECT
|
SELECT
|
||||||
@@ -13,4 +13,4 @@ $$ LANGUAGE SQL STABLE PARALLEL SAFE;
|
|||||||
|
|
||||||
-- This is to migrate from pre-0.2.0 version
|
-- This is to migrate from pre-0.2.0 version
|
||||||
-- See http://github.com/CartoDB/cartodb-postgresql/issues/36
|
-- See http://github.com/CartoDB/cartodb-postgresql/issues/36
|
||||||
GRANT EXECUTE ON FUNCTION CDB_ColumnNames(REGCLASS) TO PUBLIC;
|
GRANT EXECUTE ON FUNCTION @extschema@.CDB_ColumnNames(REGCLASS) TO PUBLIC;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
-- Function returning the type of a column
|
-- Function returning the type of a column
|
||||||
CREATE OR REPLACE FUNCTION CDB_ColumnType(REGCLASS, TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_ColumnType(REGCLASS, TEXT)
|
||||||
RETURNS information_schema.character_data
|
RETURNS information_schema.character_data
|
||||||
AS $$
|
AS $$
|
||||||
SELECT
|
SELECT
|
||||||
@@ -13,4 +13,4 @@ $$ LANGUAGE SQL STABLE PARALLEL SAFE;
|
|||||||
|
|
||||||
-- This is to migrate from pre-0.2.0 version
|
-- This is to migrate from pre-0.2.0 version
|
||||||
-- See http://github.com/CartoDB/cartodb-postgresql/issues/36
|
-- See http://github.com/CartoDB/cartodb-postgresql/issues/36
|
||||||
GRANT EXECUTE ON FUNCTION CDB_ColumnType(REGCLASS, TEXT) TO public;
|
GRANT EXECUTE ON FUNCTION @extschema@.CDB_ColumnType(REGCLASS, TEXT) TO public;
|
||||||
|
|||||||
@@ -5,44 +5,44 @@
|
|||||||
-- Functions needing reading configuration should use SECURITY DEFINER.
|
-- Functions needing reading configuration should use SECURITY DEFINER.
|
||||||
----------------------------------
|
----------------------------------
|
||||||
|
|
||||||
-- This will trigger NOTICE if cartodb.CDB_CONF already exists
|
-- This will trigger NOTICE if @extschema@.CDB_CONF already exists
|
||||||
DO LANGUAGE 'plpgsql' $$
|
DO LANGUAGE 'plpgsql' $$
|
||||||
BEGIN
|
BEGIN
|
||||||
CREATE TABLE IF NOT EXISTS cartodb.CDB_CONF ( KEY TEXT PRIMARY KEY, VALUE JSON NOT NULL );
|
CREATE TABLE IF NOT EXISTS @extschema@.CDB_CONF ( KEY TEXT PRIMARY KEY, VALUE JSON NOT NULL );
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
-- This can only be called from an SQL script executed by CREATE EXTENSION
|
-- This can only be called from an SQL script executed by CREATE EXTENSION
|
||||||
DO LANGUAGE 'plpgsql' $$
|
DO LANGUAGE 'plpgsql' $$
|
||||||
BEGIN
|
BEGIN
|
||||||
PERFORM pg_catalog.pg_extension_config_dump('cartodb.CDB_CONF', '');
|
PERFORM pg_catalog.pg_extension_config_dump('@extschema@.CDB_CONF', '');
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Conf_SetConf(key text, value JSON)
|
FUNCTION @extschema@.CDB_Conf_SetConf(key text, value JSON)
|
||||||
RETURNS void AS $$
|
RETURNS void AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
PERFORM cartodb.CDB_Conf_RemoveConf(key);
|
PERFORM @extschema@.CDB_Conf_RemoveConf(key);
|
||||||
EXECUTE 'INSERT INTO cartodb.CDB_CONF (KEY, VALUE) VALUES ($1, $2);' USING key, value;
|
EXECUTE 'INSERT INTO @extschema@.CDB_CONF (KEY, VALUE) VALUES ($1, $2);' USING key, value;
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Conf_RemoveConf(key text)
|
FUNCTION @extschema@.CDB_Conf_RemoveConf(key text)
|
||||||
RETURNS void AS $$
|
RETURNS void AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
EXECUTE 'DELETE FROM cartodb.CDB_CONF WHERE KEY = $1;' USING key;
|
EXECUTE 'DELETE FROM @extschema@.CDB_CONF WHERE KEY = $1;' USING key;
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Conf_GetConf(key text)
|
FUNCTION @extschema@.CDB_Conf_GetConf(key text)
|
||||||
RETURNS JSON AS $$
|
RETURNS JSON AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
value JSON;
|
value JSON;
|
||||||
BEGIN
|
BEGIN
|
||||||
EXECUTE 'SELECT VALUE FROM cartodb.CDB_CONF WHERE KEY = $1;' INTO value USING key;
|
EXECUTE 'SELECT VALUE FROM @extschema@.CDB_CONF WHERE KEY = $1;' INTO value USING key;
|
||||||
RETURN value;
|
RETURN value;
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL STABLE PARALLEL SAFE;
|
$$ LANGUAGE PLPGSQL STABLE PARALLEL SAFE;
|
||||||
|
|||||||
@@ -3,12 +3,12 @@
|
|||||||
-- Introduced again to make sure that updates do not leave dangling functions
|
-- Introduced again to make sure that updates do not leave dangling functions
|
||||||
--
|
--
|
||||||
|
|
||||||
DROP FUNCTION IF EXISTS cartodb.cdb_handle_create_table();
|
DROP FUNCTION IF EXISTS @extschema@.cdb_handle_create_table();
|
||||||
DROP FUNCTION IF EXISTS cartodb.cdb_handle_drop_table();
|
DROP FUNCTION IF EXISTS @extschema@.cdb_handle_drop_table();
|
||||||
DROP FUNCTION IF EXISTS cartodb.cdb_handle_alter_column();
|
DROP FUNCTION IF EXISTS @extschema@.cdb_handle_alter_column();
|
||||||
DROP FUNCTION IF EXISTS cartodb.cdb_handle_drop_column();
|
DROP FUNCTION IF EXISTS @extschema@.cdb_handle_drop_column();
|
||||||
DROP FUNCTION IF EXISTS cartodb.cdb_handle_add_column();
|
DROP FUNCTION IF EXISTS @extschema@.cdb_handle_add_column();
|
||||||
DROP FUNCTION IF EXISTS cartodb.cdb_disable_ddl_hooks();
|
DROP FUNCTION IF EXISTS @extschema@.cdb_disable_ddl_hooks();
|
||||||
DROP FUNCTION IF EXISTS cartodb.cdb_enable_ddl_hooks();
|
DROP FUNCTION IF EXISTS @extschema@.cdb_enable_ddl_hooks();
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
-- Convert timestamp to double precision
|
-- Convert timestamp to double precision
|
||||||
--
|
--
|
||||||
CREATE OR REPLACE FUNCTION CDB_DateToNumber(input timestamp)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_DateToNumber(input timestamp)
|
||||||
RETURNS double precision AS $$
|
RETURNS double precision AS $$
|
||||||
DECLARE output double precision;
|
DECLARE output double precision;
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -16,7 +16,7 @@ LANGUAGE 'plpgsql' IMMUTABLE STRICT PARALLEL UNSAFE;
|
|||||||
|
|
||||||
-- Convert timestamp with time zone to double precision
|
-- Convert timestamp with time zone to double precision
|
||||||
--
|
--
|
||||||
CREATE OR REPLACE FUNCTION CDB_DateToNumber(input timestamp with time zone)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_DateToNumber(input timestamp with time zone)
|
||||||
RETURNS double precision AS $$
|
RETURNS double precision AS $$
|
||||||
DECLARE output double precision;
|
DECLARE output double precision;
|
||||||
BEGIN
|
BEGIN
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
-- Find thousand and decimal digits separators
|
-- Find thousand and decimal digits separators
|
||||||
CREATE OR REPLACE FUNCTION CDB_DigitSeparator (rel REGCLASS, fld TEXT, OUT t CHAR, OUT d CHAR)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_DigitSeparator (rel REGCLASS, fld TEXT, OUT t CHAR, OUT d CHAR)
|
||||||
as $$
|
as $$
|
||||||
DECLARE
|
DECLARE
|
||||||
sql TEXT;
|
sql TEXT;
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
-- 1. width_bucket/histograms: http://tapoueh.org/blog/2014/02/21-PostgreSQL-histogram
|
-- 1. width_bucket/histograms: http://tapoueh.org/blog/2014/02/21-PostgreSQL-histogram
|
||||||
-- 2. R implementation: https://github.com/cran/agrmt
|
-- 2. R implementation: https://github.com/cran/agrmt
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION CDB_DistType ( in_array NUMERIC[] ) RETURNS text as $$
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_DistType ( in_array NUMERIC[] ) RETURNS text as $$
|
||||||
DECLARE
|
DECLARE
|
||||||
element_count INT4;
|
element_count INT4;
|
||||||
minv numeric;
|
minv numeric;
|
||||||
@@ -60,7 +60,7 @@ BEGIN
|
|||||||
i := i + 1;
|
i := i + 1;
|
||||||
END LOOP;
|
END LOOP;
|
||||||
|
|
||||||
signature = _CDB_DistTypeClassify(ajus);
|
signature = @extschema@._CDB_DistTypeClassify(ajus);
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
RETURN signature;
|
RETURN signature;
|
||||||
@@ -69,7 +69,7 @@ $$ language plpgsql IMMUTABLE STRICT PARALLEL SAFE;
|
|||||||
|
|
||||||
-- Classify data into AJUSFL
|
-- Classify data into AJUSFL
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION _CDB_DistTypeClassify ( in_array INT[] ) RETURNS text as $$
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_DistTypeClassify ( in_array INT[] ) RETURNS text as $$
|
||||||
DECLARE
|
DECLARE
|
||||||
element_count INT4;
|
element_count INT4;
|
||||||
maxv numeric;
|
maxv numeric;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
--
|
--
|
||||||
--
|
--
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION CDB_DistinctMeasure ( in_array text[], threshold numeric DEFAULT null ) RETURNS numeric as $$
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_DistinctMeasure ( in_array text[], threshold numeric DEFAULT null ) RETURNS numeric as $$
|
||||||
DECLARE
|
DECLARE
|
||||||
element_count INT4;
|
element_count INT4;
|
||||||
maxval numeric;
|
maxval numeric;
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
--
|
--
|
||||||
--
|
--
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION CDB_EqualIntervalBins ( in_array anyarray, breaks INT ) RETURNS anyarray as $$
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_EqualIntervalBins ( in_array anyarray, breaks INT ) RETURNS anyarray as $$
|
||||||
WITH stats AS (
|
WITH stats AS (
|
||||||
SELECT min(e), (max(e)-min(e))/breaks AS del
|
SELECT min(e), (max(e)-min(e))/breaks AS del
|
||||||
FROM (SELECT unnest(in_array) e) AS p)
|
FROM (SELECT unnest(in_array) e) AS p)
|
||||||
@@ -21,4 +21,4 @@ SELECT array_agg(bins)
|
|||||||
FROM stats) q;
|
FROM stats) q;
|
||||||
$$ LANGUAGE SQL IMMUTABLE PARALLEL SAFE;
|
$$ LANGUAGE SQL IMMUTABLE PARALLEL SAFE;
|
||||||
|
|
||||||
DROP FUNCTION IF EXISTS CDB_EqualIntervalBins( numeric[], integer);
|
DROP FUNCTION IF EXISTS @extschema@.CDB_EqualIntervalBins( numeric[], integer);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
-- Internal function to generate stats for a table if they don't exist
|
-- Internal function to generate stats for a table if they don't exist
|
||||||
CREATE OR REPLACE FUNCTION _CDB_GenerateStats(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_GenerateStats(reloid REGCLASS)
|
||||||
RETURNS VOID
|
RETURNS VOID
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -15,14 +15,14 @@ END
|
|||||||
$$ LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL UNSAFE SECURITY DEFINER;
|
$$ LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL UNSAFE SECURITY DEFINER;
|
||||||
|
|
||||||
-- Return a row count estimate of the result of a query using statistics
|
-- Return a row count estimate of the result of a query using statistics
|
||||||
CREATE OR REPLACE FUNCTION CDB_EstimateRowCount(query text)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_EstimateRowCount(query text)
|
||||||
RETURNS Numeric
|
RETURNS Numeric
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
plan JSON;
|
plan JSON;
|
||||||
BEGIN
|
BEGIN
|
||||||
-- Make sure statistics exist for all the tables of the query
|
-- Make sure statistics exist for all the tables of the query
|
||||||
PERFORM _CDB_GenerateStats(tabname) FROM unnest(CDB_QueryTablesText(query)) AS tabname;
|
PERFORM @extschema@._CDB_GenerateStats(tabname) FROM unnest(@extschema@.CDB_QueryTablesText(query)) AS tabname;
|
||||||
|
|
||||||
-- Use the query planner to obtain an estimate of the number of result rows
|
-- Use the query planner to obtain an estimate of the number of result rows
|
||||||
EXECUTE 'EXPLAIN (FORMAT JSON) ' || query INTO STRICT plan;
|
EXECUTE 'EXPLAIN (FORMAT JSON) ' || query INTO STRICT plan;
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
SELECT pg_catalog.pg_extension_config_dump('cartodb.cdb_tablemetadata','');
|
SELECT pg_catalog.pg_extension_config_dump('@extschema@.cdb_tablemetadata','');
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
CREATE OR REPLACE FUNCTION cartodb.cdb_extension_reload() RETURNS void
|
CREATE OR REPLACE FUNCTION @extschema@.cdb_extension_reload() RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
ver TEXT;
|
ver TEXT;
|
||||||
sql TEXT;
|
sql TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
ver := split_part(cartodb.cdb_version(), ' ', 1);
|
ver := split_part(@extschema@.cdb_version(), ' ', 1);
|
||||||
sql := 'ALTER EXTENSION cartodb UPDATE TO ''' || ver || 'next''';
|
sql := 'ALTER EXTENSION cartodb UPDATE TO ''' || ver || 'next''';
|
||||||
EXECUTE sql;
|
EXECUTE sql;
|
||||||
sql := 'ALTER EXTENSION cartodb UPDATE TO ''' || ver || '''';
|
sql := 'ALTER EXTENSION cartodb UPDATE TO ''' || ver || '''';
|
||||||
@@ -12,7 +12,7 @@ BEGIN
|
|||||||
END;
|
END;
|
||||||
$$ language 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
$$ language 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION cartodb.schema_exists(schema_name text)
|
CREATE OR REPLACE FUNCTION @extschema@.schema_exists(schema_name text)
|
||||||
RETURNS boolean AS
|
RETURNS boolean AS
|
||||||
$$
|
$$
|
||||||
SELECT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = schema_name::text);
|
SELECT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = schema_name::text);
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
-- All the FDW settings are read from the `cdb_conf.fdws` entry json file.
|
-- All the FDW settings are read from the `cdb_conf.fdws` entry json file.
|
||||||
---------------------------
|
---------------------------
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION cartodb._CDB_Setup_FDW(fdw_name text, config json)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Setup_FDW(fdw_name text, config json)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -58,12 +58,12 @@ BEGIN
|
|||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- Give the organization role usage permisions over the schema
|
-- Give the organization role usage permisions over the schema
|
||||||
SELECT cartodb.CDB_Organization_Member_Group_Role_Member_Name() INTO org_role;
|
SELECT @extschema@.CDB_Organization_Member_Group_Role_Member_Name() INTO org_role;
|
||||||
EXECUTE FORMAT ('GRANT USAGE ON SCHEMA %I TO %I', fdw_name, org_role);
|
EXECUTE FORMAT ('GRANT USAGE ON SCHEMA %I TO %I', fdw_name, org_role);
|
||||||
|
|
||||||
-- Bring here the remote cdb_tablemetadata
|
-- Bring here the remote cdb_tablemetadata
|
||||||
IF NOT EXISTS ( SELECT * FROM PG_CLASS WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname=fdw_name) and relname='cdb_tablemetadata') THEN
|
IF NOT EXISTS ( SELECT * FROM PG_CLASS WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname=fdw_name) and relname='cdb_tablemetadata') THEN
|
||||||
EXECUTE FORMAT ('CREATE FOREIGN TABLE %I.cdb_tablemetadata (tabname text, updated_at timestamp with time zone) SERVER %I OPTIONS (table_name ''cdb_tablemetadata_text'', schema_name ''public'', updatable ''false'')', fdw_name, fdw_name);
|
EXECUTE FORMAT ('CREATE FOREIGN TABLE %I.cdb_tablemetadata (tabname text, updated_at timestamp with time zone) SERVER %I OPTIONS (table_name ''cdb_tablemetadata_text'', schema_name ''@extschema@'', updatable ''false'')', fdw_name, fdw_name);
|
||||||
END IF;
|
END IF;
|
||||||
EXECUTE FORMAT ('GRANT SELECT ON %I.cdb_tablemetadata TO %I', fdw_name, org_role);
|
EXECUTE FORMAT ('GRANT SELECT ON %I.cdb_tablemetadata TO %I', fdw_name, org_role);
|
||||||
|
|
||||||
@@ -71,37 +71,37 @@ END
|
|||||||
$$
|
$$
|
||||||
LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION cartodb._CDB_Setup_FDWS()
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Setup_FDWS()
|
||||||
RETURNS VOID AS
|
RETURNS VOID AS
|
||||||
$$
|
$$
|
||||||
DECLARE
|
DECLARE
|
||||||
row record;
|
row record;
|
||||||
BEGIN
|
BEGIN
|
||||||
FOR row IN SELECT p.key, p.value from lateral json_each(cartodb.CDB_Conf_GetConf('fdws')) p LOOP
|
FOR row IN SELECT p.key, p.value from lateral json_each(@extschema@.CDB_Conf_GetConf('fdws')) p LOOP
|
||||||
EXECUTE 'SELECT cartodb._CDB_Setup_FDW($1, $2)' USING row.key, row.value;
|
EXECUTE 'SELECT @extschema@._CDB_Setup_FDW($1, $2)' USING row.key, row.value;
|
||||||
END LOOP;
|
END LOOP;
|
||||||
END
|
END
|
||||||
$$
|
$$
|
||||||
LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION cartodb._CDB_Setup_FDW(fdw_name text)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Setup_FDW(fdw_name text)
|
||||||
RETURNS void AS
|
RETURNS void AS
|
||||||
$BODY$
|
$BODY$
|
||||||
DECLARE
|
DECLARE
|
||||||
config json;
|
config json;
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT p.value FROM LATERAL json_each(cartodb.CDB_Conf_GetConf('fdws')) p WHERE p.key = fdw_name INTO config;
|
SELECT p.value FROM LATERAL json_each(@extschema@.CDB_Conf_GetConf('fdws')) p WHERE p.key = fdw_name INTO config;
|
||||||
EXECUTE 'SELECT cartodb._CDB_Setup_FDW($1, $2)' USING fdw_name, config;
|
EXECUTE 'SELECT @extschema@._CDB_Setup_FDW($1, $2)' USING fdw_name, config;
|
||||||
END
|
END
|
||||||
$BODY$
|
$BODY$
|
||||||
LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|
LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION cartodb.CDB_Add_Remote_Table(source text, table_name text)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_Add_Remote_Table(source text, table_name text)
|
||||||
RETURNS void AS
|
RETURNS void AS
|
||||||
$$
|
$$
|
||||||
BEGIN
|
BEGIN
|
||||||
PERFORM cartodb._CDB_Setup_FDW(source);
|
PERFORM @extschema@._CDB_Setup_FDW(source);
|
||||||
EXECUTE FORMAT ('IMPORT FOREIGN SCHEMA %I LIMIT TO (%I) FROM SERVER %I INTO %I;', source, table_name, source, source);
|
EXECUTE FORMAT ('IMPORT FOREIGN SCHEMA %I LIMIT TO (%I) FROM SERVER %I INTO %I;', source, table_name, source, source);
|
||||||
--- Grant SELECT to publicuser
|
--- Grant SELECT to publicuser
|
||||||
EXECUTE FORMAT ('GRANT SELECT ON %I.%I TO publicuser;', source, table_name);
|
EXECUTE FORMAT ('GRANT SELECT ON %I.%I TO publicuser;', source, table_name);
|
||||||
@@ -109,7 +109,7 @@ END
|
|||||||
$$
|
$$
|
||||||
LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|
LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION cartodb.CDB_Get_Foreign_Updated_At(foreign_table regclass)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_Get_Foreign_Updated_At(foreign_table regclass)
|
||||||
RETURNS timestamp with time zone AS
|
RETURNS timestamp with time zone AS
|
||||||
$$
|
$$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -132,7 +132,7 @@ $$
|
|||||||
LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|
LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION cartodb._cdb_dbname_of_foreign_table(reloid oid)
|
CREATE OR REPLACE FUNCTION @extschema@._cdb_dbname_of_foreign_table(reloid oid)
|
||||||
RETURNS TEXT AS $$
|
RETURNS TEXT AS $$
|
||||||
SELECT option_value FROM pg_options_to_table((
|
SELECT option_value FROM pg_options_to_table((
|
||||||
|
|
||||||
@@ -149,18 +149,18 @@ $$ LANGUAGE SQL VOLATILE PARALLEL UNSAFE;
|
|||||||
-- It is aware of foreign tables
|
-- It is aware of foreign tables
|
||||||
-- It assumes the local (schema_name, table_name) map to the remote ones with the same name
|
-- It assumes the local (schema_name, table_name) map to the remote ones with the same name
|
||||||
-- Note: dbname is never quoted whereas schema and table names are when needed.
|
-- Note: dbname is never quoted whereas schema and table names are when needed.
|
||||||
CREATE OR REPLACE FUNCTION cartodb.CDB_QueryTables_Updated_At(query text)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_QueryTables_Updated_At(query text)
|
||||||
RETURNS TABLE(dbname text, schema_name text, table_name text, updated_at timestamptz)
|
RETURNS TABLE(dbname text, schema_name text, table_name text, updated_at timestamptz)
|
||||||
AS $$
|
AS $$
|
||||||
WITH query_tables AS (
|
WITH query_tables AS (
|
||||||
SELECT unnest(CDB_QueryTablesText(query)) schema_table_name
|
SELECT unnest(@extschema@.CDB_QueryTablesText(query)) schema_table_name
|
||||||
), query_tables_oid AS (
|
), query_tables_oid AS (
|
||||||
SELECT schema_table_name, schema_table_name::regclass::oid AS reloid
|
SELECT schema_table_name, schema_table_name::regclass::oid AS reloid
|
||||||
FROM query_tables
|
FROM query_tables
|
||||||
),
|
),
|
||||||
fqtn AS (
|
fqtn AS (
|
||||||
SELECT
|
SELECT
|
||||||
(CASE WHEN c.relkind = 'f' THEN cartodb._cdb_dbname_of_foreign_table(query_tables_oid.reloid)
|
(CASE WHEN c.relkind = 'f' THEN @extschema@._cdb_dbname_of_foreign_table(query_tables_oid.reloid)
|
||||||
ELSE current_database()
|
ELSE current_database()
|
||||||
END)::text AS dbname,
|
END)::text AS dbname,
|
||||||
quote_ident(n.nspname::text) schema_name,
|
quote_ident(n.nspname::text) schema_name,
|
||||||
@@ -172,8 +172,8 @@ AS $$
|
|||||||
WHERE c.oid = query_tables_oid.reloid
|
WHERE c.oid = query_tables_oid.reloid
|
||||||
)
|
)
|
||||||
SELECT fqtn.dbname, fqtn.schema_name, fqtn.table_name,
|
SELECT fqtn.dbname, fqtn.schema_name, fqtn.table_name,
|
||||||
(CASE WHEN relkind = 'f' THEN cartodb.CDB_Get_Foreign_Updated_At(reloid)
|
(CASE WHEN relkind = 'f' THEN @extschema@.CDB_Get_Foreign_Updated_At(reloid)
|
||||||
ELSE (SELECT md.updated_at FROM CDB_TableMetadata md WHERE md.tabname = reloid)
|
ELSE (SELECT md.updated_at FROM @extschema@.CDB_TableMetadata md WHERE md.tabname = reloid)
|
||||||
END) AS updated_at
|
END) AS updated_at
|
||||||
FROM fqtn;
|
FROM fqtn;
|
||||||
$$ LANGUAGE SQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE SQL VOLATILE PARALLEL UNSAFE;
|
||||||
@@ -182,7 +182,7 @@ $$ LANGUAGE SQL VOLATILE PARALLEL UNSAFE;
|
|||||||
-- Return the last updated time of a set of tables
|
-- Return the last updated time of a set of tables
|
||||||
-- It is aware of foreign tables
|
-- It is aware of foreign tables
|
||||||
-- It assumes the local (schema_name, table_name) map to the remote ones with the same name
|
-- It assumes the local (schema_name, table_name) map to the remote ones with the same name
|
||||||
CREATE OR REPLACE FUNCTION cartodb.CDB_Last_Updated_Time(tables text[])
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_Last_Updated_Time(tables text[])
|
||||||
RETURNS timestamptz AS $$
|
RETURNS timestamptz AS $$
|
||||||
WITH t AS (
|
WITH t AS (
|
||||||
SELECT unnest(tables) AS schema_table_name
|
SELECT unnest(tables) AS schema_table_name
|
||||||
@@ -190,8 +190,8 @@ RETURNS timestamptz AS $$
|
|||||||
SELECT (t.schema_table_name)::regclass::oid as reloid FROM t
|
SELECT (t.schema_table_name)::regclass::oid as reloid FROM t
|
||||||
), t_updated_at AS (
|
), t_updated_at AS (
|
||||||
SELECT
|
SELECT
|
||||||
(CASE WHEN relkind = 'f' THEN cartodb.CDB_Get_Foreign_Updated_At(reloid)
|
(CASE WHEN relkind = 'f' THEN @extschema@.CDB_Get_Foreign_Updated_At(reloid)
|
||||||
ELSE (SELECT md.updated_at FROM CDB_TableMetadata md WHERE md.tabname = reloid)
|
ELSE (SELECT md.updated_at FROM @extschema@.CDB_TableMetadata md WHERE md.tabname = reloid)
|
||||||
END) AS updated_at
|
END) AS updated_at
|
||||||
FROM t_oid
|
FROM t_oid
|
||||||
LEFT JOIN pg_catalog.pg_class c ON c.oid = reloid
|
LEFT JOIN pg_catalog.pg_class c ON c.oid = reloid
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
-- Enqueues a job to run Ghost tables linking process for the provided username
|
-- Enqueues a job to run Ghost tables linking process for the provided username
|
||||||
CREATE OR REPLACE FUNCTION cartodb._CDB_LinkGhostTables(username text, db_name text, event_name text)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_LinkGhostTables(username text, db_name text, event_name text)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
if not username:
|
if not username:
|
||||||
@@ -11,7 +11,7 @@ AS $$
|
|||||||
else:
|
else:
|
||||||
json = GD['json']
|
json = GD['json']
|
||||||
|
|
||||||
tis_config = plpy.execute("select cartodb.CDB_Conf_GetConf('invalidation_service');")[0]['cdb_conf_getconf']
|
tis_config = plpy.execute("select @extschema@.CDB_Conf_GetConf('invalidation_service');")[0]['cdb_conf_getconf']
|
||||||
if not tis_config:
|
if not tis_config:
|
||||||
plpy.warning('Invalidation service configuration not found. Skipping Ghost Tables linking.')
|
plpy.warning('Invalidation service configuration not found. Skipping Ghost Tables linking.')
|
||||||
return
|
return
|
||||||
@@ -50,74 +50,74 @@ AS $$
|
|||||||
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
-- Enqueues a job to run Ghost tables linking process for the current user
|
-- Enqueues a job to run Ghost tables linking process for the current user
|
||||||
CREATE OR REPLACE FUNCTION cartodb.CDB_LinkGhostTables(event_name text DEFAULT 'USER')
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_LinkGhostTables(event_name text DEFAULT 'USER')
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
username TEXT;
|
username TEXT;
|
||||||
db_name TEXT;
|
db_name TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
EXECUTE 'SELECT cartodb.CDB_Username();' INTO username;
|
EXECUTE 'SELECT @extschema@.CDB_Username();' INTO username;
|
||||||
EXECUTE 'SELECT current_database();' INTO db_name;
|
EXECUTE 'SELECT current_database();' INTO db_name;
|
||||||
|
|
||||||
PERFORM cartodb._CDB_LinkGhostTables(username, db_name, event_name);
|
PERFORM @extschema@._CDB_LinkGhostTables(username, db_name, event_name);
|
||||||
RAISE NOTICE '_CDB_LinkGhostTables() called with username=%, event_name=%', username, event_name;
|
RAISE NOTICE '_CDB_LinkGhostTables() called with username=%, event_name=%', username, event_name;
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
||||||
|
|
||||||
-- Trigger function to call CDB_LinkGhostTables()
|
-- Trigger function to call CDB_LinkGhostTables()
|
||||||
CREATE OR REPLACE FUNCTION cartodb._CDB_LinkGhostTablesTrigger()
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_LinkGhostTablesTrigger()
|
||||||
RETURNS trigger
|
RETURNS trigger
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
ddl_tag TEXT;
|
ddl_tag TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
EXECUTE 'DELETE FROM cartodb.cdb_ddl_execution WHERE txid = txid_current() RETURNING tag;' INTO ddl_tag;
|
EXECUTE 'DELETE FROM @extschema@.cdb_ddl_execution WHERE txid = txid_current() RETURNING tag;' INTO ddl_tag;
|
||||||
PERFORM cartodb.CDB_LinkGhostTables(ddl_tag);
|
PERFORM @extschema@.CDB_LinkGhostTables(ddl_tag);
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
||||||
|
|
||||||
-- Event trigger to save the current transaction in cartodb.cdb_ddl_execution
|
-- Event trigger to save the current transaction in @extschema@.cdb_ddl_execution
|
||||||
CREATE OR REPLACE FUNCTION cartodb.CDB_SaveDDLTransaction()
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_SaveDDLTransaction()
|
||||||
RETURNS event_trigger
|
RETURNS event_trigger
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
INSERT INTO cartodb.cdb_ddl_execution VALUES (txid_current(), tg_tag) ON CONFLICT (txid) DO NOTHING;
|
INSERT INTO @extschema@.cdb_ddl_execution VALUES (txid_current(), tg_tag) ON CONFLICT (txid) DO NOTHING;
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
||||||
|
|
||||||
-- Creates the trigger on DDL events to link ghost tables
|
-- Creates the trigger on DDL events to link ghost tables
|
||||||
CREATE OR REPLACE FUNCTION cartodb.CDB_EnableGhostTablesTrigger()
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_EnableGhostTablesTrigger()
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
DROP EVENT TRIGGER IF EXISTS link_ghost_tables;
|
DROP EVENT TRIGGER IF EXISTS link_ghost_tables;
|
||||||
DROP TRIGGER IF EXISTS check_ddl_update ON cartodb.cdb_ddl_execution;
|
DROP TRIGGER IF EXISTS check_ddl_update ON @extschema@.cdb_ddl_execution;
|
||||||
|
|
||||||
-- Table to store the transaction id from DDL events to avoid multiple executions
|
-- Table to store the transaction id from DDL events to avoid multiple executions
|
||||||
CREATE TABLE IF NOT EXISTS cartodb.cdb_ddl_execution(txid integer PRIMARY KEY, tag text);
|
CREATE TABLE IF NOT EXISTS @extschema@.cdb_ddl_execution(txid integer PRIMARY KEY, tag text);
|
||||||
|
|
||||||
CREATE CONSTRAINT TRIGGER check_ddl_update
|
CREATE CONSTRAINT TRIGGER check_ddl_update
|
||||||
AFTER INSERT ON cartodb.cdb_ddl_execution
|
AFTER INSERT ON @extschema@.cdb_ddl_execution
|
||||||
INITIALLY DEFERRED
|
INITIALLY DEFERRED
|
||||||
FOR EACH ROW
|
FOR EACH ROW
|
||||||
EXECUTE PROCEDURE cartodb._CDB_LinkGhostTablesTrigger();
|
EXECUTE PROCEDURE @extschema@._CDB_LinkGhostTablesTrigger();
|
||||||
|
|
||||||
CREATE EVENT TRIGGER link_ghost_tables
|
CREATE EVENT TRIGGER link_ghost_tables
|
||||||
ON ddl_command_end
|
ON ddl_command_end
|
||||||
WHEN TAG IN ('CREATE TABLE', 'SELECT INTO', 'DROP TABLE', 'ALTER TABLE', 'CREATE TRIGGER', 'DROP TRIGGER', 'CREATE VIEW', 'DROP VIEW', 'ALTER VIEW')
|
WHEN TAG IN ('CREATE TABLE', 'SELECT INTO', 'DROP TABLE', 'ALTER TABLE', 'CREATE TRIGGER', 'DROP TRIGGER', 'CREATE VIEW', 'DROP VIEW', 'ALTER VIEW')
|
||||||
EXECUTE PROCEDURE cartodb.CDB_SaveDDLTransaction();
|
EXECUTE PROCEDURE @extschema@.CDB_SaveDDLTransaction();
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
-- Drops the trigger on DDL events to link ghost tables
|
-- Drops the trigger on DDL events to link ghost tables
|
||||||
CREATE OR REPLACE FUNCTION cartodb.CDB_DisableGhostTablesTrigger()
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_DisableGhostTablesTrigger()
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
DROP EVENT TRIGGER IF EXISTS link_ghost_tables;
|
DROP EVENT TRIGGER IF EXISTS link_ghost_tables;
|
||||||
DROP TRIGGER IF EXISTS check_ddl_update ON cartodb.cdb_ddl_execution;
|
DROP TRIGGER IF EXISTS check_ddl_update ON @extschema@.cdb_ddl_execution;
|
||||||
DROP TABLE IF EXISTS cartodb.cdb_ddl_execution;
|
DROP TABLE IF EXISTS @extschema@.cdb_ddl_execution;
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|
||||||
|
|||||||
@@ -1,23 +1,23 @@
|
|||||||
-- Great circle point-to-point routes, based on:
|
-- Great circle point-to-point routes, based on:
|
||||||
-- http://blog.cartodb.com/jets-and-datelines/
|
-- http://blog.cartodb.com/jets-and-datelines/
|
||||||
--
|
--
|
||||||
CREATE OR REPLACE FUNCTION CDB_GreatCircle(start_point geometry, end_point geometry, max_segment_length NUMERIC DEFAULT 100000)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_GreatCircle(start_point @postgisschema@.geometry, end_point @postgisschema@.geometry, max_segment_length NUMERIC DEFAULT 100000)
|
||||||
RETURNS geometry AS $$
|
RETURNS @postgisschema@.geometry AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
line geometry;
|
line @postgisschema@.geometry;
|
||||||
BEGIN
|
BEGIN
|
||||||
line = ST_Segmentize(
|
line = @postgisschema@.ST_Segmentize(
|
||||||
ST_Makeline(
|
@postgisschema@.ST_Makeline(
|
||||||
start_point,
|
start_point,
|
||||||
end_point
|
end_point
|
||||||
)::geography,
|
)::geography,
|
||||||
max_segment_length
|
max_segment_length
|
||||||
)::geometry;
|
)::geometry;
|
||||||
|
|
||||||
IF ST_XMax(line) - ST_XMin(line) > 180 THEN
|
IF @postgisschema@.ST_XMax(line) - @postgisschema@.ST_XMin(line) > 180 THEN
|
||||||
line = ST_Difference(
|
line = @postgisschema@.ST_Difference(
|
||||||
ST_ShiftLongitude(line),
|
@postgisschema@.ST_ShiftLongitude(line),
|
||||||
ST_Buffer(ST_GeomFromText('LINESTRING(180 90, 180 -90)', 4326), 0.00001)
|
@postgisschema@.ST_Buffer(@postgisschema@.ST_GeomFromText('LINESTRING(180 90, 180 -90)', 4326), 0.00001)
|
||||||
);
|
);
|
||||||
END IF;
|
END IF;
|
||||||
RETURN line;
|
RETURN line;
|
||||||
|
|||||||
@@ -6,14 +6,14 @@
|
|||||||
|
|
||||||
-- Creates a new group
|
-- Creates a new group
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Group_CreateGroup(group_name text)
|
FUNCTION @extschema@.CDB_Group_CreateGroup(group_name text)
|
||||||
RETURNS VOID AS $$
|
RETURNS VOID AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
group_role TEXT;
|
group_role TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
group_role := cartodb._CDB_Group_GroupRole(group_name);
|
group_role := @extschema@._CDB_Group_GroupRole(group_name);
|
||||||
EXECUTE format('CREATE ROLE %I NOLOGIN;', group_role);
|
EXECUTE format('CREATE ROLE %I NOLOGIN;', group_role);
|
||||||
PERFORM cartodb._CDB_Group_CreateGroup_API(group_name, group_role);
|
PERFORM @extschema@._CDB_Group_CreateGroup_API(group_name, group_role);
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
@@ -23,72 +23,72 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
-- Not even the role creator can drop the role and the objects it owns.
|
-- Not even the role creator can drop the role and the objects it owns.
|
||||||
-- All group owned objects by the group are permissions.
|
-- All group owned objects by the group are permissions.
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Group_DropGroup(group_name text)
|
FUNCTION @extschema@.CDB_Group_DropGroup(group_name text)
|
||||||
RETURNS VOID AS $$
|
RETURNS VOID AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
group_role TEXT;
|
group_role TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
group_role := cartodb._CDB_Group_GroupRole(group_name);
|
group_role := @extschema@._CDB_Group_GroupRole(group_name);
|
||||||
EXECUTE format('DROP OWNED BY %I', group_role);
|
EXECUTE format('DROP OWNED BY %I', group_role);
|
||||||
EXECUTE format('DROP ROLE IF EXISTS %I', group_role);
|
EXECUTE format('DROP ROLE IF EXISTS %I', group_role);
|
||||||
PERFORM cartodb._CDB_Group_DropGroup_API(group_name);
|
PERFORM @extschema@._CDB_Group_DropGroup_API(group_name);
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
-- Renames a group
|
-- Renames a group
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Group_RenameGroup(old_group_name text, new_group_name text)
|
FUNCTION @extschema@.CDB_Group_RenameGroup(old_group_name text, new_group_name text)
|
||||||
RETURNS VOID AS $$
|
RETURNS VOID AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
old_group_role TEXT;
|
old_group_role TEXT;
|
||||||
new_group_role TEXT;
|
new_group_role TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
old_group_role = cartodb._CDB_Group_GroupRole(old_group_name);
|
old_group_role = @extschema@._CDB_Group_GroupRole(old_group_name);
|
||||||
new_group_role = cartodb._CDB_Group_GroupRole(new_group_name);
|
new_group_role = @extschema@._CDB_Group_GroupRole(new_group_name);
|
||||||
EXECUTE format('ALTER ROLE %I RENAME TO %I', old_group_role, new_group_role);
|
EXECUTE format('ALTER ROLE %I RENAME TO %I', old_group_role, new_group_role);
|
||||||
PERFORM cartodb._CDB_Group_RenameGroup_API(old_group_name, new_group_name, new_group_role);
|
PERFORM @extschema@._CDB_Group_RenameGroup_API(old_group_name, new_group_name, new_group_role);
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
-- Adds users to a group
|
-- Adds users to a group
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Group_AddUsers(group_name text, usernames text[])
|
FUNCTION @extschema@.CDB_Group_AddUsers(group_name text, usernames text[])
|
||||||
RETURNS VOID AS $$
|
RETURNS VOID AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
group_role TEXT;
|
group_role TEXT;
|
||||||
user_role TEXT;
|
user_role TEXT;
|
||||||
username TEXT;
|
username TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
group_role := cartodb._CDB_Group_GroupRole(group_name);
|
group_role := @extschema@._CDB_Group_GroupRole(group_name);
|
||||||
foreach username in array usernames
|
foreach username in array usernames
|
||||||
loop
|
loop
|
||||||
user_role := cartodb._CDB_User_RoleFromUsername(username);
|
user_role := @extschema@._CDB_User_RoleFromUsername(username);
|
||||||
IF(group_role IS NULL OR user_role IS NULL)
|
IF(group_role IS NULL OR user_role IS NULL)
|
||||||
THEN
|
THEN
|
||||||
RAISE EXCEPTION 'Group role (%) and user role (%) must be already existing', group_role, user_role;
|
RAISE EXCEPTION 'Group role (%) and user role (%) must be already existing', group_role, user_role;
|
||||||
END IF;
|
END IF;
|
||||||
EXECUTE format('GRANT %I TO %I', group_role, user_role);
|
EXECUTE format('GRANT %I TO %I', group_role, user_role);
|
||||||
end loop;
|
end loop;
|
||||||
PERFORM cartodb._CDB_Group_AddUsers_API(group_name, usernames);
|
PERFORM @extschema@._CDB_Group_AddUsers_API(group_name, usernames);
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
-- Removes users from a group
|
-- Removes users from a group
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Group_RemoveUsers(group_name text, usernames text[])
|
FUNCTION @extschema@.CDB_Group_RemoveUsers(group_name text, usernames text[])
|
||||||
RETURNS VOID AS $$
|
RETURNS VOID AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
group_role TEXT;
|
group_role TEXT;
|
||||||
user_role TEXT;
|
user_role TEXT;
|
||||||
username TEXT;
|
username TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
group_role := cartodb._CDB_Group_GroupRole(group_name);
|
group_role := @extschema@._CDB_Group_GroupRole(group_name);
|
||||||
foreach username in array usernames
|
foreach username in array usernames
|
||||||
loop
|
loop
|
||||||
user_role := cartodb._CDB_User_RoleFromUsername(username);
|
user_role := @extschema@._CDB_User_RoleFromUsername(username);
|
||||||
EXECUTE format('REVOKE %I FROM %I', group_role, user_role);
|
EXECUTE format('REVOKE %I FROM %I', group_role, user_role);
|
||||||
end loop;
|
end loop;
|
||||||
PERFORM cartodb._CDB_Group_RemoveUsers_API(group_name, usernames);
|
PERFORM @extschema@._CDB_Group_RemoveUsers_API(group_name, usernames);
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
@@ -100,67 +100,67 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
|
|
||||||
-- Grants table read permission to a group
|
-- Grants table read permission to a group
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Group_Table_GrantRead(group_name text, username text, table_name text)
|
FUNCTION @extschema@.CDB_Group_Table_GrantRead(group_name text, username text, table_name text)
|
||||||
RETURNS VOID AS $$
|
RETURNS VOID AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
group_role TEXT;
|
group_role TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
PERFORM cartodb._CDB_Group_Table_GrantRead(group_name, username, table_name, true);
|
PERFORM @extschema@._CDB_Group_Table_GrantRead(group_name, username, table_name, true);
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_Table_GrantRead(group_name text, username text, table_name text, sync boolean)
|
FUNCTION @extschema@._CDB_Group_Table_GrantRead(group_name text, username text, table_name text, sync boolean)
|
||||||
RETURNS VOID AS $$
|
RETURNS VOID AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
group_role TEXT;
|
group_role TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
group_role := cartodb._CDB_Group_GroupRole(group_name);
|
group_role := @extschema@._CDB_Group_GroupRole(group_name);
|
||||||
EXECUTE format('GRANT USAGE ON SCHEMA %I TO %I', username, group_role);
|
EXECUTE format('GRANT USAGE ON SCHEMA %I TO %I', username, group_role);
|
||||||
EXECUTE format('GRANT SELECT ON TABLE %I.%I TO %I', username, table_name, group_role );
|
EXECUTE format('GRANT SELECT ON TABLE %I.%I TO %I', username, table_name, group_role );
|
||||||
IF(sync) THEN
|
IF(sync) THEN
|
||||||
PERFORM cartodb._CDB_Group_Table_GrantPermission_API(group_name, username, table_name, 'r');
|
PERFORM @extschema@._CDB_Group_Table_GrantPermission_API(group_name, username, table_name, 'r');
|
||||||
END IF;
|
END IF;
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
-- Grants table write permission to a group
|
-- Grants table write permission to a group
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Group_Table_GrantReadWrite(group_name text, username text, table_name text)
|
FUNCTION @extschema@.CDB_Group_Table_GrantReadWrite(group_name text, username text, table_name text)
|
||||||
RETURNS VOID AS $$
|
RETURNS VOID AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
group_role TEXT;
|
group_role TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
PERFORM cartodb._CDB_Group_Table_GrantReadWrite(group_name, username, table_name, true);
|
PERFORM @extschema@._CDB_Group_Table_GrantReadWrite(group_name, username, table_name, true);
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_Table_GrantReadWrite(group_name text, username text, table_name text, sync boolean)
|
FUNCTION @extschema@._CDB_Group_Table_GrantReadWrite(group_name text, username text, table_name text, sync boolean)
|
||||||
RETURNS VOID AS $$
|
RETURNS VOID AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
group_role TEXT;
|
group_role TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
group_role := cartodb._CDB_Group_GroupRole(group_name);
|
group_role := @extschema@._CDB_Group_GroupRole(group_name);
|
||||||
EXECUTE format('GRANT USAGE ON SCHEMA %I TO %I', username, group_role);
|
EXECUTE format('GRANT USAGE ON SCHEMA %I TO %I', username, group_role);
|
||||||
EXECUTE format('GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE %I.%I TO %I', username, table_name, group_role);
|
EXECUTE format('GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE %I.%I TO %I', username, table_name, group_role);
|
||||||
PERFORM cartodb._CDB_Group_TableSequences_Permission(group_name, username, table_name, true);
|
PERFORM @extschema@._CDB_Group_TableSequences_Permission(group_name, username, table_name, true);
|
||||||
IF(sync) THEN
|
IF(sync) THEN
|
||||||
PERFORM cartodb._CDB_Group_Table_GrantPermission_API(group_name, username, table_name, 'w');
|
PERFORM @extschema@._CDB_Group_Table_GrantPermission_API(group_name, username, table_name, 'w');
|
||||||
END IF;
|
END IF;
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
-- Granting and revoking permissions on sequences
|
-- Granting and revoking permissions on sequences
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_TableSequences_Permission(group_name text, username text, table_name text, do_grant bool)
|
FUNCTION @extschema@._CDB_Group_TableSequences_Permission(group_name text, username text, table_name text, do_grant bool)
|
||||||
RETURNS VOID AS $$
|
RETURNS VOID AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
column_name TEXT;
|
column_name TEXT;
|
||||||
sequence_name TEXT;
|
sequence_name TEXT;
|
||||||
group_role TEXT;
|
group_role TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
group_role := cartodb._CDB_Group_GroupRole(group_name);
|
group_role := @extschema@._CDB_Group_GroupRole(group_name);
|
||||||
FOR column_name IN EXECUTE 'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_CATALOG = current_database() AND TABLE_SCHEMA = $1 AND TABLE_NAME = $2 AND COLUMN_DEFAULT LIKE ''nextval%''' USING username, table_name
|
FOR column_name IN EXECUTE 'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_CATALOG = current_database() AND TABLE_SCHEMA = $1 AND TABLE_NAME = $2 AND COLUMN_DEFAULT LIKE ''nextval%''' USING username, table_name
|
||||||
LOOP
|
LOOP
|
||||||
EXECUTE format('SELECT PG_GET_SERIAL_SEQUENCE(''%I.%I'', ''%I'')', username, table_name, column_name) INTO sequence_name;
|
EXECUTE format('SELECT PG_GET_SERIAL_SEQUENCE(''%I.%I'', ''%I'')', username, table_name, column_name) INTO sequence_name;
|
||||||
@@ -179,26 +179,26 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
|
|
||||||
-- Revokes all permissions on a table from a group
|
-- Revokes all permissions on a table from a group
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Group_Table_RevokeAll(group_name text, username text, table_name text)
|
FUNCTION @extschema@.CDB_Group_Table_RevokeAll(group_name text, username text, table_name text)
|
||||||
RETURNS VOID AS $$
|
RETURNS VOID AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
group_role TEXT;
|
group_role TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
PERFORM cartodb._CDB_Group_Table_RevokeAll(group_name, username, table_name, true);
|
PERFORM @extschema@._CDB_Group_Table_RevokeAll(group_name, username, table_name, true);
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_Table_RevokeAll(group_name text, username text, table_name text, sync boolean)
|
FUNCTION @extschema@._CDB_Group_Table_RevokeAll(group_name text, username text, table_name text, sync boolean)
|
||||||
RETURNS VOID AS $$
|
RETURNS VOID AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
group_role TEXT;
|
group_role TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
group_role := cartodb._CDB_Group_GroupRole(group_name);
|
group_role := @extschema@._CDB_Group_GroupRole(group_name);
|
||||||
EXECUTE format('REVOKE ALL ON TABLE %I.%I FROM %I', username, table_name, group_role);
|
EXECUTE format('REVOKE ALL ON TABLE %I.%I FROM %I', username, table_name, group_role);
|
||||||
PERFORM cartodb._CDB_Group_TableSequences_Permission(group_name, username, table_name, false);
|
PERFORM @extschema@._CDB_Group_TableSequences_Permission(group_name, username, table_name, false);
|
||||||
IF(sync) THEN
|
IF(sync) THEN
|
||||||
PERFORM cartodb._CDB_Group_Table_RevokeAllPermission_API(group_name, username, table_name);
|
PERFORM @extschema@._CDB_Group_Table_RevokeAllPermission_API(group_name, username, table_name);
|
||||||
END IF;
|
END IF;
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
@@ -208,14 +208,14 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
-----------------------
|
-----------------------
|
||||||
-- Given a group name returns a role. group_name must be a valid PostgreSQL idenfifier. See http://www.postgresql.org/docs/9.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
|
-- Given a group name returns a role. group_name must be a valid PostgreSQL idenfifier. See http://www.postgresql.org/docs/9.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_GroupRole(group_name text)
|
FUNCTION @extschema@._CDB_Group_GroupRole(group_name text)
|
||||||
RETURNS TEXT AS $$
|
RETURNS TEXT AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
group_role TEXT;
|
group_role TEXT;
|
||||||
prefix TEXT;
|
prefix TEXT;
|
||||||
max_length constant INTEGER := 63;
|
max_length constant INTEGER := 63;
|
||||||
BEGIN
|
BEGIN
|
||||||
prefix = format('%s_g_', cartodb._CDB_Group_ShortDatabaseName());
|
prefix = format('%s_g_', @extschema@._CDB_Group_ShortDatabaseName());
|
||||||
group_role := format('%s%s', prefix, group_name);
|
group_role := format('%s%s', prefix, group_name);
|
||||||
IF LENGTH(group_role) > max_length
|
IF LENGTH(group_role) > max_length
|
||||||
THEN
|
THEN
|
||||||
@@ -227,7 +227,7 @@ $$ LANGUAGE PLPGSQL STABLE PARALLEL SAFE;
|
|||||||
|
|
||||||
-- Returns the first owner of the schema matching username. Organization user schemas must have one only owner.
|
-- Returns the first owner of the schema matching username. Organization user schemas must have one only owner.
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_User_RoleFromUsername(username text)
|
FUNCTION @extschema@._CDB_User_RoleFromUsername(username text)
|
||||||
RETURNS TEXT AS $$
|
RETURNS TEXT AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
user_role TEXT;
|
user_role TEXT;
|
||||||
@@ -241,7 +241,7 @@ $$ LANGUAGE PLPGSQL STABLE PARALLEL SAFE;
|
|||||||
|
|
||||||
-- Database names are too long, we need a shorter version for composing role names
|
-- Database names are too long, we need a shorter version for composing role names
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_ShortDatabaseName()
|
FUNCTION @extschema@._CDB_Group_ShortDatabaseName()
|
||||||
RETURNS TEXT AS $$
|
RETURNS TEXT AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
short_database_name TEXT;
|
short_database_name TEXT;
|
||||||
|
|||||||
@@ -2,30 +2,30 @@
|
|||||||
-- GROUP METADATA API FUNCTIONS
|
-- GROUP METADATA API FUNCTIONS
|
||||||
--
|
--
|
||||||
-- Meant to be used by CDB_Group_* functions to sync data with the editor.
|
-- Meant to be used by CDB_Group_* functions to sync data with the editor.
|
||||||
-- Requires configuration parameter. Example: SELECT cartodb.CDB_Conf_SetConf('groups_api', '{ "host": "127.0.0.1", "port": 3000, "timeout": 10, "username": "extension", "password": "elephant" }');
|
-- Requires configuration parameter. Example: SELECT @extschema@.CDB_Conf_SetConf('groups_api', '{ "host": "127.0.0.1", "port": 3000, "timeout": 10, "username": "extension", "password": "elephant" }');
|
||||||
----------------------------------
|
----------------------------------
|
||||||
|
|
||||||
-- TODO: delete this development cleanup before final merge
|
-- TODO: delete this development cleanup before final merge
|
||||||
DROP FUNCTION IF EXISTS cartodb.CDB_Group_AddMember(group_name text, username text);
|
DROP FUNCTION IF EXISTS @extschema@.CDB_Group_AddMember(group_name text, username text);
|
||||||
DROP FUNCTION IF EXISTS cartodb.CDB_Group_RemoveMember(group_name text, username text);
|
DROP FUNCTION IF EXISTS @extschema@.CDB_Group_RemoveMember(group_name text, username text);
|
||||||
DROP FUNCTION IF EXISTS cartodb._CDB_Group_AddMember_API(group_name text, username text);
|
DROP FUNCTION IF EXISTS @extschema@._CDB_Group_AddMember_API(group_name text, username text);
|
||||||
DROP FUNCTION IF EXISTS cartodb._CDB_Group_RemoveMember_API(group_name text, username text);
|
DROP FUNCTION IF EXISTS @extschema@._CDB_Group_RemoveMember_API(group_name text, username text);
|
||||||
|
|
||||||
-- Sends the create group request
|
-- Sends the create group request
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_CreateGroup_API(group_name text, group_role text)
|
FUNCTION @extschema@._CDB_Group_CreateGroup_API(group_name text, group_role text)
|
||||||
RETURNS VOID AS
|
RETURNS VOID AS
|
||||||
$$
|
$$
|
||||||
import string
|
import string
|
||||||
|
|
||||||
url = '/api/v1/databases/{0}/groups'
|
url = '/api/v1/databases/{0}/groups'
|
||||||
body = '{ "name": "%s", "database_role": "%s" }' % (group_name, group_role)
|
body = '{ "name": "%s", "database_role": "%s" }' % (group_name, group_role)
|
||||||
query = "select cartodb._CDB_Group_API_Request('POST', '%s', '%s', '{200, 409}') as response_status" % (url, body)
|
query = "select @extschema@._CDB_Group_API_Request('POST', '%s', '%s', '{200, 409}') as response_status" % (url, body)
|
||||||
plpy.execute(query)
|
plpy.execute(query)
|
||||||
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_DropGroup_API(group_name text)
|
FUNCTION @extschema@._CDB_Group_DropGroup_API(group_name text)
|
||||||
RETURNS VOID AS
|
RETURNS VOID AS
|
||||||
$$
|
$$
|
||||||
import string
|
import string
|
||||||
@@ -33,12 +33,12 @@ $$
|
|||||||
|
|
||||||
url = '/api/v1/databases/{0}/groups/%s' % (urllib.pathname2url(group_name))
|
url = '/api/v1/databases/{0}/groups/%s' % (urllib.pathname2url(group_name))
|
||||||
|
|
||||||
query = "select cartodb._CDB_Group_API_Request('DELETE', '%s', '', '{204, 404}') as response_status" % url
|
query = "select @extschema@._CDB_Group_API_Request('DELETE', '%s', '', '{204, 404}') as response_status" % url
|
||||||
plpy.execute(query)
|
plpy.execute(query)
|
||||||
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_RenameGroup_API(old_group_name text, new_group_name text, new_group_role text)
|
FUNCTION @extschema@._CDB_Group_RenameGroup_API(old_group_name text, new_group_name text, new_group_role text)
|
||||||
RETURNS VOID AS
|
RETURNS VOID AS
|
||||||
$$
|
$$
|
||||||
import string
|
import string
|
||||||
@@ -46,12 +46,12 @@ $$
|
|||||||
|
|
||||||
url = '/api/v1/databases/{0}/groups/%s' % (urllib.pathname2url(old_group_name))
|
url = '/api/v1/databases/{0}/groups/%s' % (urllib.pathname2url(old_group_name))
|
||||||
body = '{ "name": "%s", "database_role": "%s" }' % (new_group_name, new_group_role)
|
body = '{ "name": "%s", "database_role": "%s" }' % (new_group_name, new_group_role)
|
||||||
query = "select cartodb._CDB_Group_API_Request('PUT', '%s', '%s', '{200, 409}') as response_status" % (url, body)
|
query = "select @extschema@._CDB_Group_API_Request('PUT', '%s', '%s', '{200, 409}') as response_status" % (url, body)
|
||||||
plpy.execute(query)
|
plpy.execute(query)
|
||||||
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_AddUsers_API(group_name text, usernames text[])
|
FUNCTION @extschema@._CDB_Group_AddUsers_API(group_name text, usernames text[])
|
||||||
RETURNS VOID AS
|
RETURNS VOID AS
|
||||||
$$
|
$$
|
||||||
import string
|
import string
|
||||||
@@ -59,12 +59,12 @@ $$
|
|||||||
|
|
||||||
url = '/api/v1/databases/{0}/groups/%s/users' % (urllib.pathname2url(group_name))
|
url = '/api/v1/databases/{0}/groups/%s/users' % (urllib.pathname2url(group_name))
|
||||||
body = "{ \"users\": [\"%s\"] }" % "\",\"".join(usernames)
|
body = "{ \"users\": [\"%s\"] }" % "\",\"".join(usernames)
|
||||||
query = "select cartodb._CDB_Group_API_Request('POST', '%s', '%s', '{200, 409}') as response_status" % (url, body)
|
query = "select @extschema@._CDB_Group_API_Request('POST', '%s', '%s', '{200, 409}') as response_status" % (url, body)
|
||||||
plpy.execute(query)
|
plpy.execute(query)
|
||||||
$$ LANGUAGE 'plpythonu' VOLATILE SECURITY DEFINER;
|
$$ LANGUAGE 'plpythonu' VOLATILE SECURITY DEFINER;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_RemoveUsers_API(group_name text, usernames text[])
|
FUNCTION @extschema@._CDB_Group_RemoveUsers_API(group_name text, usernames text[])
|
||||||
RETURNS VOID AS
|
RETURNS VOID AS
|
||||||
$$
|
$$
|
||||||
import string
|
import string
|
||||||
@@ -72,20 +72,20 @@ $$
|
|||||||
|
|
||||||
url = '/api/v1/databases/{0}/groups/%s/users' % (urllib.pathname2url(group_name))
|
url = '/api/v1/databases/{0}/groups/%s/users' % (urllib.pathname2url(group_name))
|
||||||
body = "{ \"users\": [\"%s\"] }" % "\",\"".join(usernames)
|
body = "{ \"users\": [\"%s\"] }" % "\",\"".join(usernames)
|
||||||
query = "select cartodb._CDB_Group_API_Request('DELETE', '%s', '%s', '{200, 404}') as response_status" % (url, body)
|
query = "select @extschema@._CDB_Group_API_Request('DELETE', '%s', '%s', '{200, 404}') as response_status" % (url, body)
|
||||||
plpy.execute(query)
|
plpy.execute(query)
|
||||||
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
||||||
|
|
||||||
DO LANGUAGE 'plpgsql' $$
|
DO LANGUAGE 'plpgsql' $$
|
||||||
BEGIN
|
BEGIN
|
||||||
-- Needed for dropping type
|
-- Needed for dropping type
|
||||||
DROP FUNCTION IF EXISTS cartodb._CDB_Group_API_Conf();
|
DROP FUNCTION IF EXISTS @extschema@._CDB_Group_API_Conf();
|
||||||
DROP TYPE IF EXISTS _CDB_Group_API_Params;
|
DROP TYPE IF EXISTS @extschema@._CDB_Group_API_Params;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_Table_GrantPermission_API(group_name text, username text, table_name text, access text)
|
FUNCTION @extschema@._CDB_Group_Table_GrantPermission_API(group_name text, username text, table_name text, access text)
|
||||||
RETURNS VOID AS
|
RETURNS VOID AS
|
||||||
$$
|
$$
|
||||||
import string
|
import string
|
||||||
@@ -93,39 +93,39 @@ $$
|
|||||||
|
|
||||||
url = '/api/v1/databases/{0}/groups/%s/permission/%s/tables/%s' % (urllib.pathname2url(group_name), username, table_name)
|
url = '/api/v1/databases/{0}/groups/%s/permission/%s/tables/%s' % (urllib.pathname2url(group_name), username, table_name)
|
||||||
body = '{ "access": "%s" }' % access
|
body = '{ "access": "%s" }' % access
|
||||||
query = "select cartodb._CDB_Group_API_Request('PUT', '%s', '%s', '{200, 409}') as response_status" % (url, body)
|
query = "select @extschema@._CDB_Group_API_Request('PUT', '%s', '%s', '{200, 409}') as response_status" % (url, body)
|
||||||
plpy.execute(query)
|
plpy.execute(query)
|
||||||
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
||||||
|
|
||||||
DO LANGUAGE 'plpgsql' $$
|
DO LANGUAGE 'plpgsql' $$
|
||||||
BEGIN
|
BEGIN
|
||||||
-- Needed for dropping type
|
-- Needed for dropping type
|
||||||
DROP FUNCTION IF EXISTS cartodb._CDB_Group_API_Conf();
|
DROP FUNCTION IF EXISTS @extschema@._CDB_Group_API_Conf();
|
||||||
DROP TYPE IF EXISTS _CDB_Group_API_Params;
|
DROP TYPE IF EXISTS @extschema@._CDB_Group_API_Params;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_Table_RevokeAllPermission_API(group_name text, username text, table_name text)
|
FUNCTION @extschema@._CDB_Group_Table_RevokeAllPermission_API(group_name text, username text, table_name text)
|
||||||
RETURNS VOID AS
|
RETURNS VOID AS
|
||||||
$$
|
$$
|
||||||
import string
|
import string
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
url = '/api/v1/databases/{0}/groups/%s/permission/%s/tables/%s' % (urllib.pathname2url(group_name), username, table_name)
|
url = '/api/v1/databases/{0}/groups/%s/permission/%s/tables/%s' % (urllib.pathname2url(group_name), username, table_name)
|
||||||
query = "select cartodb._CDB_Group_API_Request('DELETE', '%s', '', '{200, 404}') as response_status" % url
|
query = "select @extschema@._CDB_Group_API_Request('DELETE', '%s', '', '{200, 404}') as response_status" % url
|
||||||
plpy.execute(query)
|
plpy.execute(query)
|
||||||
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
||||||
|
|
||||||
DO LANGUAGE 'plpgsql' $$
|
DO LANGUAGE 'plpgsql' $$
|
||||||
BEGIN
|
BEGIN
|
||||||
-- Needed for dropping type
|
-- Needed for dropping type
|
||||||
DROP FUNCTION IF EXISTS cartodb._CDB_Group_API_Conf();
|
DROP FUNCTION IF EXISTS @extschema@._CDB_Group_API_Conf();
|
||||||
DROP TYPE IF EXISTS _CDB_Group_API_Params;
|
DROP TYPE IF EXISTS @extschema@._CDB_Group_API_Params;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE TYPE _CDB_Group_API_Params AS (
|
CREATE TYPE @extschema@._CDB_Group_API_Params AS (
|
||||||
host text,
|
host text,
|
||||||
port int,
|
port int,
|
||||||
timeout int,
|
timeout int,
|
||||||
@@ -135,21 +135,21 @@ CREATE TYPE _CDB_Group_API_Params AS (
|
|||||||
-- This must be explicitally extracted because "composite types are currently not supported".
|
-- This must be explicitally extracted because "composite types are currently not supported".
|
||||||
-- See http://www.postgresql.org/docs/9.3/static/plpython-database.html.
|
-- See http://www.postgresql.org/docs/9.3/static/plpython-database.html.
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_API_Conf()
|
FUNCTION @extschema@._CDB_Group_API_Conf()
|
||||||
RETURNS _CDB_Group_API_Params AS
|
RETURNS @extschema@._CDB_Group_API_Params AS
|
||||||
$$
|
$$
|
||||||
conf = plpy.execute("SELECT cartodb.CDB_Conf_GetConf('groups_api') conf")[0]['conf']
|
conf = plpy.execute("SELECT @extschema@.CDB_Conf_GetConf('groups_api') conf")[0]['conf']
|
||||||
if conf is None:
|
if conf is None:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
import json
|
import json
|
||||||
params = json.loads(conf)
|
params = json.loads(conf)
|
||||||
auth = 'Basic %s' % plpy.execute("SELECT cartodb._CDB_Group_API_Auth('%s', '%s') as auth" % (params['username'], params['password']))[0]['auth']
|
auth = 'Basic %s' % plpy.execute("SELECT @extschema@._CDB_Group_API_Auth('%s', '%s') as auth" % (params['username'], params['password']))[0]['auth']
|
||||||
return { "host": params['host'], "port": params['port'], 'timeout': params['timeout'], 'auth': auth }
|
return { "host": params['host'], "port": params['port'], 'timeout': params['timeout'], 'auth': auth }
|
||||||
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_API_Auth(username text, password text)
|
FUNCTION @extschema@._CDB_Group_API_Auth(username text, password text)
|
||||||
RETURNS TEXT AS
|
RETURNS TEXT AS
|
||||||
$$
|
$$
|
||||||
import base64
|
import base64
|
||||||
@@ -158,12 +158,12 @@ $$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE;
|
|||||||
|
|
||||||
-- url must contain a '%s' placeholder that will be replaced by current_database, for security reasons.
|
-- url must contain a '%s' placeholder that will be replaced by current_database, for security reasons.
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Group_API_Request(method text, url text, body text, valid_return_codes int[])
|
FUNCTION @extschema@._CDB_Group_API_Request(method text, url text, body text, valid_return_codes int[])
|
||||||
RETURNS int AS
|
RETURNS int AS
|
||||||
$$
|
$$
|
||||||
import httplib
|
import httplib
|
||||||
|
|
||||||
params = plpy.execute("select c.host, c.port, c.timeout, c.auth from cartodb._CDB_Group_API_Conf() c;")[0]
|
params = plpy.execute("select c.host, c.port, c.timeout, c.auth from @extschema@._CDB_Group_API_Conf() c;")[0]
|
||||||
if params['host'] is None:
|
if params['host'] is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -192,4 +192,4 @@ $$
|
|||||||
|
|
||||||
return None
|
return None
|
||||||
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE;
|
||||||
revoke all on function cartodb._CDB_Group_API_Request(text, text, text, int[]) from public;
|
revoke all on function @extschema@._CDB_Group_API_Request(text, text, text, int[]) from public;
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
--
|
--
|
||||||
--
|
--
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION CDB_HeadsTailsBins ( in_array NUMERIC[], breaks INT) RETURNS NUMERIC[] as $$
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_HeadsTailsBins ( in_array NUMERIC[], breaks INT) RETURNS NUMERIC[] as $$
|
||||||
DECLARE
|
DECLARE
|
||||||
element_count INT4;
|
element_count INT4;
|
||||||
arr_mean numeric;
|
arr_mean numeric;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
-- UTF8 safe and length aware. Find a unique identifier with a given prefix
|
-- UTF8 safe and length aware. Find a unique identifier with a given prefix
|
||||||
-- and/or suffix and withing a schema. If a schema is not specified, the identifier
|
-- and/or suffix and withing a schema. If a schema is not specified, the identifier
|
||||||
-- is guaranteed to be unique for all schemas.
|
-- is guaranteed to be unique for all schemas.
|
||||||
CREATE OR REPLACE FUNCTION cartodb._CDB_Unique_Identifier(prefix TEXT, relname TEXT, suffix TEXT, schema TEXT DEFAULT NULL)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Unique_Identifier(prefix TEXT, relname TEXT, suffix TEXT, schema TEXT DEFAULT NULL)
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -24,10 +24,10 @@ BEGIN
|
|||||||
usedspace := usedspace + coalesce(octet_length(prefix), 0);
|
usedspace := usedspace + coalesce(octet_length(prefix), 0);
|
||||||
usedspace := usedspace + coalesce(octet_length(suffix), 0);
|
usedspace := usedspace + coalesce(octet_length(suffix), 0);
|
||||||
|
|
||||||
candrelname := _CDB_Octet_Truncate(relname, maxlen - usedspace);
|
candrelname := @extschema@._CDB_Octet_Truncate(relname, maxlen - usedspace);
|
||||||
|
|
||||||
IF candrelname = '' THEN
|
IF candrelname = '' THEN
|
||||||
PERFORM _CDB_Error('prefixes are to long to generate a valid identifier', '_CDB_Unique_Identifier');
|
PERFORM @extschema@._CDB_Error('prefixes are to long to generate a valid identifier', '_CDB_Unique_Identifier');
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
ident := coalesce(prefix, '') || candrelname || coalesce(suffix, '');
|
ident := coalesce(prefix, '') || candrelname || coalesce(suffix, '');
|
||||||
@@ -59,14 +59,14 @@ BEGIN
|
|||||||
i := i + 1;
|
i := i + 1;
|
||||||
END LOOP;
|
END LOOP;
|
||||||
|
|
||||||
PERFORM _CDB_Error('looping too far', '_CDB_Unique_Identifier');
|
PERFORM @extschema@._CDB_Error('looping too far', '_CDB_Unique_Identifier');
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
|
|
||||||
-- UTF8 safe and length aware. Find a unique identifier for a column with a given prefix
|
-- UTF8 safe and length aware. Find a unique identifier for a column with a given prefix
|
||||||
-- and/or suffix based on colname and within a relation specified via reloid.
|
-- and/or suffix based on colname and within a relation specified via reloid.
|
||||||
CREATE OR REPLACE FUNCTION cartodb._CDB_Unique_Column_Identifier(prefix TEXT, colname TEXT, suffix TEXT, reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Unique_Column_Identifier(prefix TEXT, colname TEXT, suffix TEXT, reloid REGCLASS)
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -85,10 +85,10 @@ BEGIN
|
|||||||
usedspace := usedspace + coalesce(octet_length(prefix), 0);
|
usedspace := usedspace + coalesce(octet_length(prefix), 0);
|
||||||
usedspace := usedspace + coalesce(octet_length(suffix), 0);
|
usedspace := usedspace + coalesce(octet_length(suffix), 0);
|
||||||
|
|
||||||
candcolname := _CDB_Octet_Truncate(colname, maxlen - usedspace);
|
candcolname := @extschema@._CDB_Octet_Truncate(colname, maxlen - usedspace);
|
||||||
|
|
||||||
IF candcolname = '' THEN
|
IF candcolname = '' THEN
|
||||||
PERFORM _CDB_Error('prefixes are to long to generate a valid identifier', '_CDB_Unique_Column_Identifier');
|
PERFORM @extschema@._CDB_Error('prefixes are to long to generate a valid identifier', '_CDB_Unique_Column_Identifier');
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
ident := coalesce(prefix, '') || candcolname || coalesce(suffix, '');
|
ident := coalesce(prefix, '') || candcolname || coalesce(suffix, '');
|
||||||
@@ -114,14 +114,14 @@ BEGIN
|
|||||||
i := i + 1;
|
i := i + 1;
|
||||||
END LOOP;
|
END LOOP;
|
||||||
|
|
||||||
PERFORM _CDB_Error('looping too far', '_CDB_Unique_Column_Identifier');
|
PERFORM @extschema@._CDB_Error('looping too far', '_CDB_Unique_Column_Identifier');
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE 'plpgsql' VOLATILE PARALLEL SAFE;
|
$$ LANGUAGE 'plpgsql' VOLATILE PARALLEL SAFE;
|
||||||
|
|
||||||
|
|
||||||
-- Truncates a given string to a max_octets octets taking care
|
-- Truncates a given string to a max_octets octets taking care
|
||||||
-- not to leave characters in half. UTF8 safe.
|
-- not to leave characters in half. UTF8 safe.
|
||||||
CREATE OR REPLACE FUNCTION cartodb._CDB_Octet_Truncate(string TEXT, max_octets INTEGER)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Octet_Truncate(string TEXT, max_octets INTEGER)
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -162,7 +162,7 @@ $$ LANGUAGE 'plpgsql' IMMUTABLE PARALLEL SAFE;
|
|||||||
|
|
||||||
-- Checks if a given text representing a qualified or unqualified table name (relation)
|
-- 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.
|
-- 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)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Table_Exists(table_name_with_optional_schema TEXT)
|
||||||
RETURNS bool
|
RETURNS bool
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
-- Return an Hexagon with given center and side (or maximal radius)
|
-- Return an Hexagon with given center and side (or maximal radius)
|
||||||
CREATE OR REPLACE FUNCTION CDB_MakeHexagon(center GEOMETRY, radius FLOAT8)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_MakeHexagon(center GEOMETRY, radius FLOAT8)
|
||||||
RETURNS GEOMETRY
|
RETURNS GEOMETRY
|
||||||
AS $$
|
AS $$
|
||||||
SELECT ST_MakePolygon(ST_MakeLine(geom))
|
SELECT @postgisschema@.ST_MakePolygon(@postgisschema@.ST_MakeLine(geom))
|
||||||
FROM
|
FROM
|
||||||
(
|
(
|
||||||
SELECT (ST_DumpPoints(ST_ExteriorRing(ST_Buffer($1, $2, 3)))).*
|
SELECT (@postgisschema@.ST_DumpPoints(@postgisschema@.ST_ExteriorRing(@postgisschema@.ST_Buffer($1, $2, 3)))).*
|
||||||
) as points
|
) as points
|
||||||
WHERE path[1] % 2 != 0
|
WHERE path[1] % 2 != 0
|
||||||
$$ LANGUAGE 'sql' IMMUTABLE STRICT PARALLEL SAFE;
|
$$ LANGUAGE 'sql' IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
|
||||||
-- In older versions of the extension, CDB_HexagonGrid had a different signature
|
-- In older versions of the extension, CDB_HexagonGrid had a different signature
|
||||||
DROP FUNCTION IF EXISTS cartodb.CDB_HexagonGrid(GEOMETRY, FLOAT8, GEOMETRY);
|
DROP FUNCTION IF EXISTS @extschema@.CDB_HexagonGrid(GEOMETRY, FLOAT8, GEOMETRY);
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Fill given extent with an hexagonal coverage
|
-- Fill given extent with an hexagonal coverage
|
||||||
@@ -35,7 +35,7 @@ DROP FUNCTION IF EXISTS cartodb.CDB_HexagonGrid(GEOMETRY, FLOAT8, GEOMETRY);
|
|||||||
-- and exception will occur.
|
-- and exception will occur.
|
||||||
----
|
----
|
||||||
-- DROP FUNCTION IF EXISTS CDB_HexagonGrid(ext GEOMETRY, side FLOAT8);
|
-- DROP FUNCTION IF EXISTS CDB_HexagonGrid(ext GEOMETRY, side FLOAT8);
|
||||||
CREATE OR REPLACE FUNCTION CDB_HexagonGrid(ext GEOMETRY, side FLOAT8, origin GEOMETRY DEFAULT NULL, maxcells INTEGER DEFAULT 512*512)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_HexagonGrid(ext GEOMETRY, side FLOAT8, origin GEOMETRY DEFAULT NULL, maxcells INTEGER DEFAULT 512*512)
|
||||||
RETURNS SETOF GEOMETRY
|
RETURNS SETOF GEOMETRY
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -80,11 +80,11 @@ BEGIN
|
|||||||
yoff := 0;
|
yoff := 0;
|
||||||
|
|
||||||
IF origin IS NOT NULL THEN
|
IF origin IS NOT NULL THEN
|
||||||
IF ST_SRID(origin) != srid THEN
|
IF @postgisschema@.ST_SRID(origin) != srid THEN
|
||||||
RAISE EXCEPTION 'SRID mismatch between extent (%) and origin (%)', srid, ST_SRID(origin);
|
RAISE EXCEPTION 'SRID mismatch between extent (%) and origin (%)', srid, ST_SRID(origin);
|
||||||
END IF;
|
END IF;
|
||||||
xoff := ST_X(origin);
|
xoff := @postgisschema@.ST_X(origin);
|
||||||
yoff := ST_Y(origin);
|
yoff := @postgisschema@.ST_Y(origin);
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
RAISE DEBUG 'X offset: %', xoff;
|
RAISE DEBUG 'X offset: %', xoff;
|
||||||
@@ -96,19 +96,19 @@ BEGIN
|
|||||||
RAISE DEBUG 'Y grid size: %', ygrd;
|
RAISE DEBUG 'Y grid size: %', ygrd;
|
||||||
|
|
||||||
-- Tweak horizontal start on hstep*2 grid from origin
|
-- Tweak horizontal start on hstep*2 grid from origin
|
||||||
hskip := ceil((ST_XMin(ext)-xoff)/hstep);
|
hskip := ceil((@postgisschema@.ST_XMin(ext)-xoff)/hstep);
|
||||||
RAISE DEBUG 'hskip: %', hskip;
|
RAISE DEBUG 'hskip: %', hskip;
|
||||||
hstart := xoff + hskip*hstep;
|
hstart := xoff + hskip*hstep;
|
||||||
RAISE DEBUG 'hstart: %', hstart;
|
RAISE DEBUG 'hstart: %', hstart;
|
||||||
|
|
||||||
-- Tweak vertical start on hstep grid from origin
|
-- Tweak vertical start on hstep grid from origin
|
||||||
vstart := yoff + ceil((ST_Ymin(ext)-yoff)/vstep)*vstep;
|
vstart := yoff + ceil((@postgisschema@.ST_Ymin(ext)-yoff)/vstep)*vstep;
|
||||||
RAISE DEBUG 'vstart: %', vstart;
|
RAISE DEBUG 'vstart: %', vstart;
|
||||||
|
|
||||||
hend := ST_XMax(ext);
|
hend := @postgisschema@.ST_XMax(ext);
|
||||||
vend := ST_YMax(ext);
|
vend := @postgisschema@.ST_YMax(ext);
|
||||||
|
|
||||||
IF vstart - (vstep/2.0) < ST_YMin(ext) THEN
|
IF vstart - (vstep/2.0) < @postgisschema@.ST_YMin(ext) THEN
|
||||||
vstartary := ARRAY[ vstart + (vstep/2.0), vstart ];
|
vstartary := ARRAY[ vstart + (vstep/2.0), vstart ];
|
||||||
ELSE
|
ELSE
|
||||||
vstartary := ARRAY[ vstart - (vstep/2.0), vstart ];
|
vstartary := ARRAY[ vstart - (vstep/2.0), vstart ];
|
||||||
@@ -125,21 +125,21 @@ BEGIN
|
|||||||
RAISE DEBUG 'vstartary: % : %', vstartary[1], vstartary[2];
|
RAISE DEBUG 'vstartary: % : %', vstartary[1], vstartary[2];
|
||||||
RAISE DEBUG 'vstartidx: %', vstartidx;
|
RAISE DEBUG 'vstartidx: %', vstartidx;
|
||||||
|
|
||||||
c := ST_SetSRID(ST_MakePoint(hstart, vstartary[vstartidx+1]), srid);
|
c := @postgisschema@.ST_SetSRID(@postgisschema@.ST_MakePoint(hstart, vstartary[vstartidx+1]), srid);
|
||||||
h := ST_SnapToGrid(CDB_MakeHexagon(c, side), xoff, yoff, xgrd, ygrd);
|
h := @postgisschema@.ST_SnapToGrid(@extschema@.CDB_MakeHexagon(c, side), xoff, yoff, xgrd, ygrd);
|
||||||
vstartidx := (vstartidx + 1) % 2;
|
vstartidx := (vstartidx + 1) % 2;
|
||||||
WHILE ST_X(c) < hend LOOP -- over X
|
WHILE @postgisschema@.ST_X(c) < hend LOOP -- over X
|
||||||
--RAISE DEBUG 'X loop starts, center point: %', ST_AsText(c);
|
--RAISE DEBUG 'X loop starts, center point: %', ST_AsText(c);
|
||||||
WHILE ST_Y(c) < vend LOOP -- over Y
|
WHILE @postgisschema@.ST_Y(c) < vend LOOP -- over Y
|
||||||
--RAISE DEBUG 'Center: %', ST_AsText(c);
|
--RAISE DEBUG 'Center: %', ST_AsText(c);
|
||||||
--h := ST_SnapToGrid(CDB_MakeHexagon(c, side), xoff, yoff, xgrd, ygrd);
|
--h := ST_SnapToGrid(CDB_MakeHexagon(c, side), xoff, yoff, xgrd, ygrd);
|
||||||
RETURN NEXT h;
|
RETURN NEXT h;
|
||||||
h := ST_SnapToGrid(ST_Translate(h, 0, vstep), xoff, yoff, xgrd, ygrd);
|
h := @postgisschema@.ST_SnapToGrid(ST_Translate(h, 0, vstep), xoff, yoff, xgrd, ygrd);
|
||||||
c := ST_Translate(c, 0, vstep); -- TODO: drop ?
|
c := @postgisschema@.ST_Translate(c, 0, vstep); -- TODO: drop ?
|
||||||
END LOOP;
|
END LOOP;
|
||||||
-- TODO: translate h direcly ...
|
-- TODO: translate h direcly ...
|
||||||
c := ST_SetSRID(ST_MakePoint(ST_X(c)+hstep, vstartary[vstartidx+1]), srid);
|
c := @postgisschema@.ST_SetSRID(@postgisschema@.ST_MakePoint(ST_X(c)+hstep, vstartary[vstartidx+1]), srid);
|
||||||
h := ST_SnapToGrid(CDB_MakeHexagon(c, side), xoff, yoff, xgrd, ygrd);
|
h := @postgisschema@.ST_SnapToGrid(@extschema@.CDB_MakeHexagon(c, side), xoff, yoff, xgrd, ygrd);
|
||||||
vstartidx := (vstartidx + 1) % 2;
|
vstartidx := (vstartidx + 1) % 2;
|
||||||
END LOOP;
|
END LOOP;
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
--
|
--
|
||||||
--
|
--
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION CDB_JenksBins(in_array NUMERIC[], breaks INT, iterations INT DEFAULT 0, invert BOOLEAN DEFAULT FALSE)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_JenksBins(in_array NUMERIC[], breaks INT, iterations INT DEFAULT 0, invert BOOLEAN DEFAULT FALSE)
|
||||||
RETURNS NUMERIC[] as
|
RETURNS NUMERIC[] as
|
||||||
$$
|
$$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -95,7 +95,7 @@ BEGIN
|
|||||||
IF i > breaks THEN EXIT; END IF;
|
IF i > breaks THEN EXIT; END IF;
|
||||||
END LOOP;
|
END LOOP;
|
||||||
|
|
||||||
best_result = CDB_JenksBinsIteration(in_matrix, breaks, classes, invert, sdam, shuffles);
|
best_result = @extschema@.CDB_JenksBinsIteration(in_matrix, breaks, classes, invert, sdam, shuffles);
|
||||||
|
|
||||||
--set the seed so we can ensure the same results
|
--set the seed so we can ensure the same results
|
||||||
SELECT setseed(0.4567) INTO seedtarget;
|
SELECT setseed(0.4567) INTO seedtarget;
|
||||||
@@ -126,7 +126,7 @@ BEGIN
|
|||||||
IF i > breaks THEN EXIT; END IF;
|
IF i > breaks THEN EXIT; END IF;
|
||||||
END LOOP;
|
END LOOP;
|
||||||
|
|
||||||
curr_result = CDB_JenksBinsIteration(in_matrix, breaks, classes, invert, sdam, shuffles);
|
curr_result = @extschema@.CDB_JenksBinsIteration(in_matrix, breaks, classes, invert, sdam, shuffles);
|
||||||
|
|
||||||
IF curr_result[1] > best_result[1] THEN
|
IF curr_result[1] > best_result[1] THEN
|
||||||
best_result = curr_result;
|
best_result = curr_result;
|
||||||
@@ -146,9 +146,9 @@ $$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL RESTRICTED;
|
|||||||
-- Returns an array with:
|
-- Returns an array with:
|
||||||
-- - First element: gvf
|
-- - First element: gvf
|
||||||
-- - Second to 2+n: Category limits
|
-- - Second to 2+n: Category limits
|
||||||
DROP FUNCTION IF EXISTS CDB_JenksBinsIteration ( in_matrix NUMERIC[], breaks INT, classes INT[], invert BOOLEAN, element_count INT4, arr_mean NUMERIC, max_search INT); -- Old signature
|
DROP FUNCTION IF EXISTS @extschema@.CDB_JenksBinsIteration ( in_matrix NUMERIC[], breaks INT, classes INT[], invert BOOLEAN, element_count INT4, arr_mean NUMERIC, max_search INT); -- Old signature
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION CDB_JenksBinsIteration ( in_matrix NUMERIC[], breaks INT, classes INT[], invert BOOLEAN, sdam NUMERIC, max_search INT DEFAULT 50) RETURNS NUMERIC[] as $$
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_JenksBinsIteration ( in_matrix NUMERIC[], breaks INT, classes INT[], invert BOOLEAN, sdam NUMERIC, max_search INT DEFAULT 50) RETURNS NUMERIC[] as $$
|
||||||
DECLARE
|
DECLARE
|
||||||
i INT;
|
i INT;
|
||||||
iterations INT = 0;
|
iterations INT = 0;
|
||||||
|
|||||||
@@ -7,13 +7,11 @@
|
|||||||
--
|
--
|
||||||
--
|
--
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION CDB_LatLng (lat NUMERIC, lng NUMERIC) RETURNS geometry as $$
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_LatLng (lat NUMERIC, lng NUMERIC) RETURNS @postgisschema@.geometry as $$
|
||||||
-- this function is silly
|
SELECT @postgisschema@.ST_SetSRID(@postgisschema@.ST_MakePoint(lng,lat), 4326);
|
||||||
SELECT ST_SetSRID(ST_MakePoint(lng,lat),4326);
|
|
||||||
$$ language SQL IMMUTABLE PARALLEL SAFE;
|
$$ language SQL IMMUTABLE PARALLEL SAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION CDB_LatLng (lat FLOAT8, lng FLOAT8) RETURNS geometry as $$
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_LatLng (lat FLOAT8, lng FLOAT8) RETURNS @postgisschema@.geometry as $$
|
||||||
-- this function is silly
|
SELECT @postgisschema@.ST_SetSRID(@postgisschema@.ST_MakePoint(lng,lat), 4326);
|
||||||
SELECT ST_SetSRID(ST_MakePoint(lng,lat),4326);
|
|
||||||
$$ language SQL IMMUTABLE PARALLEL SAFE;
|
$$ language SQL IMMUTABLE PARALLEL SAFE;
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
-- Mode
|
-- Mode
|
||||||
-- https://wiki.postgresql.org/wiki/Aggregate_Mode
|
-- https://wiki.postgresql.org/wiki/Aggregate_Mode
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION cartodb._CDB_Math_final_mode(anyarray)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Math_final_mode(anyarray)
|
||||||
RETURNS anyelement AS
|
RETURNS anyelement AS
|
||||||
$BODY$
|
$BODY$
|
||||||
SELECT a
|
SELECT a
|
||||||
@@ -15,12 +15,12 @@ $BODY$
|
|||||||
$BODY$
|
$BODY$
|
||||||
LANGUAGE 'sql' IMMUTABLE PARALLEL SAFE;
|
LANGUAGE 'sql' IMMUTABLE PARALLEL SAFE;
|
||||||
|
|
||||||
DROP AGGREGATE IF EXISTS cartodb.CDB_Math_Mode(anyelement);
|
DROP AGGREGATE IF EXISTS @extschema@.CDB_Math_Mode(anyelement);
|
||||||
|
|
||||||
CREATE AGGREGATE cartodb.CDB_Math_Mode(anyelement) (
|
CREATE AGGREGATE @extschema@.CDB_Math_Mode(anyelement) (
|
||||||
SFUNC=array_append,
|
SFUNC=array_append,
|
||||||
STYPE=anyarray,
|
STYPE=anyarray,
|
||||||
FINALFUNC=_CDB_Math_final_mode,
|
FINALFUNC=@extschema@._CDB_Math_final_mode,
|
||||||
PARALLEL = SAFE,
|
PARALLEL = SAFE,
|
||||||
INITCOND='{}'
|
INITCOND='{}'
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Organization_Member_Group_Role_Member_Name()
|
FUNCTION @extschema@.CDB_Organization_Member_Group_Role_Member_Name()
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
SELECT 'cdb_org_member'::text || '_' || md5(current_database());
|
SELECT 'cdb_org_member'::text || '_' || md5(current_database());
|
||||||
@@ -10,7 +10,7 @@ DO LANGUAGE 'plpgsql' $$
|
|||||||
DECLARE
|
DECLARE
|
||||||
cdb_org_member_role_name TEXT;
|
cdb_org_member_role_name TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
cdb_org_member_role_name := cartodb.CDB_Organization_Member_Group_Role_Member_Name();
|
cdb_org_member_role_name := @extschema@.CDB_Organization_Member_Group_Role_Member_Name();
|
||||||
IF NOT EXISTS ( SELECT * FROM pg_roles WHERE rolname= cdb_org_member_role_name )
|
IF NOT EXISTS ( SELECT * FROM pg_roles WHERE rolname= cdb_org_member_role_name )
|
||||||
THEN
|
THEN
|
||||||
EXECUTE 'CREATE ROLE "' || cdb_org_member_role_name || '" NOLOGIN;';
|
EXECUTE 'CREATE ROLE "' || cdb_org_member_role_name || '" NOLOGIN;';
|
||||||
@@ -19,11 +19,11 @@ END
|
|||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Organization_Create_Member(role_name text)
|
FUNCTION @extschema@.CDB_Organization_Create_Member(role_name text)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
EXECUTE 'GRANT "' || cartodb.CDB_Organization_Member_Group_Role_Member_Name() || '" TO "' || role_name || '"';
|
EXECUTE 'GRANT "' || @extschema@.CDB_Organization_Member_Group_Role_Member_Name() || '" TO "' || role_name || '"';
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
@@ -31,7 +31,7 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
-- Administrator
|
-- Administrator
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Organization_Admin_Role_Name()
|
FUNCTION @extschema@._CDB_Organization_Admin_Role_Name()
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
SELECT current_database() || '_a'::text;
|
SELECT current_database() || '_a'::text;
|
||||||
@@ -43,7 +43,7 @@ DO LANGUAGE 'plpgsql' $$
|
|||||||
DECLARE
|
DECLARE
|
||||||
cdb_org_admin_role_name TEXT;
|
cdb_org_admin_role_name TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
cdb_org_admin_role_name := cartodb._CDB_Organization_Admin_Role_Name();
|
cdb_org_admin_role_name := @extschema@._CDB_Organization_Admin_Role_Name();
|
||||||
IF NOT EXISTS ( SELECT * FROM pg_roles WHERE rolname= cdb_org_admin_role_name )
|
IF NOT EXISTS ( SELECT * FROM pg_roles WHERE rolname= cdb_org_admin_role_name )
|
||||||
THEN
|
THEN
|
||||||
EXECUTE format('CREATE ROLE %I CREATEROLE NOLOGIN;', cdb_org_admin_role_name);
|
EXECUTE format('CREATE ROLE %I CREATEROLE NOLOGIN;', cdb_org_admin_role_name);
|
||||||
@@ -52,15 +52,15 @@ END
|
|||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Organization_AddAdmin(username text)
|
FUNCTION @extschema@.CDB_Organization_AddAdmin(username text)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
cdb_user_role TEXT;
|
cdb_user_role TEXT;
|
||||||
cdb_admin_role TEXT;
|
cdb_admin_role TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
cdb_admin_role := cartodb._CDB_Organization_Admin_Role_Name();
|
cdb_admin_role := @extschema@._CDB_Organization_Admin_Role_Name();
|
||||||
cdb_user_role := cartodb._CDB_User_RoleFromUsername(username);
|
cdb_user_role := @extschema@._CDB_User_RoleFromUsername(username);
|
||||||
EXECUTE format('GRANT %I TO %I WITH ADMIN OPTION', cdb_admin_role, cdb_user_role);
|
EXECUTE format('GRANT %I TO %I WITH ADMIN OPTION', cdb_admin_role, cdb_user_role);
|
||||||
-- CREATEROLE is not inherited, and is needed for user creation
|
-- CREATEROLE is not inherited, and is needed for user creation
|
||||||
EXECUTE format('ALTER ROLE %I CREATEROLE', cdb_user_role);
|
EXECUTE format('ALTER ROLE %I CREATEROLE', cdb_user_role);
|
||||||
@@ -68,15 +68,15 @@ END
|
|||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Organization_RemoveAdmin(username text)
|
FUNCTION @extschema@.CDB_Organization_RemoveAdmin(username text)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
cdb_user_role TEXT;
|
cdb_user_role TEXT;
|
||||||
cdb_admin_role TEXT;
|
cdb_admin_role TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
cdb_admin_role := cartodb._CDB_Organization_Admin_Role_Name();
|
cdb_admin_role := @extschema@._CDB_Organization_Admin_Role_Name();
|
||||||
cdb_user_role := cartodb._CDB_User_RoleFromUsername(username);
|
cdb_user_role := @extschema@._CDB_User_RoleFromUsername(username);
|
||||||
EXECUTE format('ALTER ROLE %I NOCREATEROLE', cdb_user_role);
|
EXECUTE format('ALTER ROLE %I NOCREATEROLE', cdb_user_role);
|
||||||
EXECUTE format('REVOKE %I FROM %I', cdb_admin_role, cdb_user_role);
|
EXECUTE format('REVOKE %I FROM %I', cdb_admin_role, cdb_user_role);
|
||||||
END
|
END
|
||||||
@@ -86,7 +86,7 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
-- Sharing tables
|
-- Sharing tables
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Organization_Add_Table_Read_Permission(from_schema text, table_name text, to_role_name text)
|
FUNCTION @extschema@.CDB_Organization_Add_Table_Read_Permission(from_schema text, table_name text, to_role_name text)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -96,16 +96,16 @@ END
|
|||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Organization_Add_Table_Organization_Read_Permission(from_schema text, table_name text)
|
FUNCTION @extschema@.CDB_Organization_Add_Table_Organization_Read_Permission(from_schema text, table_name text)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
EXECUTE 'SELECT cartodb.CDB_Organization_Add_Table_Read_Permission(''' || from_schema || ''', ''' || table_name || ''', ''' || cartodb.CDB_Organization_Member_Group_Role_Member_Name() || ''');';
|
EXECUTE 'SELECT @extschema@.CDB_Organization_Add_Table_Read_Permission(''' || from_schema || ''', ''' || table_name || ''', ''' || @extschema@.CDB_Organization_Member_Group_Role_Member_Name() || ''');';
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb._CDB_Organization_Get_Table_Sequences(from_schema text, table_name text)
|
FUNCTION @extschema@._CDB_Organization_Get_Table_Sequences(from_schema text, table_name text)
|
||||||
RETURNS SETOF TEXT
|
RETURNS SETOF TEXT
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -124,7 +124,7 @@ END
|
|||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Organization_Add_Table_Read_Write_Permission(from_schema text, table_name text, to_role_name text)
|
FUNCTION @extschema@.CDB_Organization_Add_Table_Read_Write_Permission(from_schema text, table_name text, to_role_name text)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -133,24 +133,24 @@ BEGIN
|
|||||||
EXECUTE 'GRANT USAGE ON SCHEMA "' || from_schema || '" TO "' || to_role_name || '"';
|
EXECUTE 'GRANT USAGE ON SCHEMA "' || from_schema || '" TO "' || to_role_name || '"';
|
||||||
EXECUTE 'GRANT SELECT, INSERT, UPDATE, DELETE ON "' || from_schema || '"."' || table_name || '" TO "' || to_role_name || '"';
|
EXECUTE 'GRANT SELECT, INSERT, UPDATE, DELETE ON "' || from_schema || '"."' || table_name || '" TO "' || to_role_name || '"';
|
||||||
|
|
||||||
FOR sequence_name IN SELECT * FROM cartodb._CDB_Organization_Get_Table_Sequences(from_schema, table_name) LOOP
|
FOR sequence_name IN SELECT * FROM @extschema@._CDB_Organization_Get_Table_Sequences(from_schema, table_name) LOOP
|
||||||
EXECUTE 'GRANT USAGE, SELECT ON SEQUENCE ' || sequence_name || ' TO "' || to_role_name || '"';
|
EXECUTE 'GRANT USAGE, SELECT ON SEQUENCE ' || sequence_name || ' TO "' || to_role_name || '"';
|
||||||
END LOOP;
|
END LOOP;
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Organization_Add_Table_Organization_Read_Write_Permission(from_schema text, table_name text)
|
FUNCTION @extschema@.CDB_Organization_Add_Table_Organization_Read_Write_Permission(from_schema text, table_name text)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
EXECUTE 'SELECT cartodb.CDB_Organization_Add_Table_Read_Write_Permission(''' || from_schema || ''', ''' || table_name || ''', ''' || cartodb.CDB_Organization_Member_Group_Role_Member_Name() || ''');';
|
EXECUTE 'SELECT @extschema@.CDB_Organization_Add_Table_Read_Write_Permission(''' || from_schema || ''', ''' || table_name || ''', ''' || @extschema@.CDB_Organization_Member_Group_Role_Member_Name() || ''');';
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Organization_Remove_Access_Permission(from_schema text, table_name text, to_role_name text)
|
FUNCTION @extschema@.CDB_Organization_Remove_Access_Permission(from_schema text, table_name text, to_role_name text)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -162,10 +162,10 @@ END
|
|||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE
|
CREATE OR REPLACE
|
||||||
FUNCTION cartodb.CDB_Organization_Remove_Organization_Access_Permission(from_schema text, table_name text)
|
FUNCTION @extschema@.CDB_Organization_Remove_Organization_Access_Permission(from_schema text, table_name text)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
EXECUTE 'SELECT cartodb.CDB_Organization_Remove_Access_Permission(''' || from_schema || ''', ''' || table_name || ''', ''' || cartodb.CDB_Organization_Member_Group_Role_Member_Name() || ''');';
|
EXECUTE 'SELECT @extschema@.CDB_Organization_Remove_Access_Permission(''' || from_schema || ''', ''' || table_name || ''', ''' || @extschema@.CDB_Organization_Member_Group_Role_Member_Name() || ''');';
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
-- Scope: public
|
-- Scope: public
|
||||||
-- Parameters:
|
-- Parameters:
|
||||||
-- reloid: oid of the table.
|
-- reloid: oid of the table.
|
||||||
CREATE OR REPLACE FUNCTION CDB_DropOverviews(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_DropOverviews(reloid REGCLASS)
|
||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -10,9 +10,9 @@ DECLARE
|
|||||||
schema_name TEXT;
|
schema_name TEXT;
|
||||||
table_name TEXT;
|
table_name TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT * FROM _cdb_split_table_name(reloid) INTO schema_name, table_name;
|
SELECT * FROM @extschema@._cdb_split_table_name(reloid) INTO schema_name, table_name;
|
||||||
FOR row IN
|
FOR row IN
|
||||||
SELECT * FROM CDB_Overviews(reloid)
|
SELECT * FROM @extschema@.CDB_Overviews(reloid)
|
||||||
LOOP
|
LOOP
|
||||||
EXECUTE Format('DROP TABLE %s;', row.overview_table);
|
EXECUTE Format('DROP TABLE %s;', row.overview_table);
|
||||||
RAISE NOTICE 'Dropped overview for level %: %', row.z, row.overview_table;
|
RAISE NOTICE 'Dropped overview for level %: %', row.z, row.overview_table;
|
||||||
@@ -29,20 +29,20 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
-- Return relation of overviews for the table with
|
-- Return relation of overviews for the table with
|
||||||
-- the base table oid,
|
-- the base table oid,
|
||||||
-- z level of the overview and overview table oid, ordered by z.
|
-- z level of the overview and overview table oid, ordered by z.
|
||||||
CREATE OR REPLACE FUNCTION CDB_Overviews(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_Overviews(reloid REGCLASS)
|
||||||
RETURNS TABLE(base_table REGCLASS, z integer, overview_table REGCLASS)
|
RETURNS TABLE(base_table REGCLASS, z integer, overview_table REGCLASS)
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
schema_name TEXT;
|
schema_name TEXT;
|
||||||
base_table_name TEXT;
|
base_table_name TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT * FROM _cdb_split_table_name(reloid) INTO schema_name, base_table_name;
|
SELECT * FROM @extschema@._cdb_split_table_name(reloid) INTO schema_name, base_table_name;
|
||||||
RETURN QUERY SELECT
|
RETURN QUERY SELECT
|
||||||
reloid AS base_table,
|
reloid AS base_table,
|
||||||
_CDB_OverviewTableZ(table_name) AS z,
|
@extschema@._CDB_OverviewTableZ(table_name) AS z,
|
||||||
table_regclass AS overview_table
|
table_regclass AS overview_table
|
||||||
FROM _CDB_UserTablesInSchema(schema_name)
|
FROM @extschema@._CDB_UserTablesInSchema(schema_name)
|
||||||
WHERE _CDB_IsOverviewTableOf((SELECT relname FROM pg_class WHERE oid=reloid), table_name)
|
WHERE @extschema@._CDB_IsOverviewTableOf((SELECT relname FROM pg_class WHERE oid=reloid), table_name)
|
||||||
ORDER BY z;
|
ORDER BY z;
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL STABLE PARALLEL RESTRICTED;
|
$$ LANGUAGE PLPGSQL STABLE PARALLEL RESTRICTED;
|
||||||
@@ -56,18 +56,18 @@ $$ LANGUAGE PLPGSQL STABLE PARALLEL RESTRICTED;
|
|||||||
-- z level of the overview and overview table oid, ordered by z.
|
-- z level of the overview and overview table oid, ordered by z.
|
||||||
-- Note: CDB_Overviews can be applied to the result of CDB_QueryTablesText
|
-- Note: CDB_Overviews can be applied to the result of CDB_QueryTablesText
|
||||||
-- to obtain the overviews applicable to a query.
|
-- to obtain the overviews applicable to a query.
|
||||||
CREATE OR REPLACE FUNCTION CDB_Overviews(tables regclass[])
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_Overviews(tables regclass[])
|
||||||
RETURNS TABLE(base_table REGCLASS, z integer, overview_table REGCLASS)
|
RETURNS TABLE(base_table REGCLASS, z integer, overview_table REGCLASS)
|
||||||
AS $$
|
AS $$
|
||||||
SELECT
|
SELECT
|
||||||
base_table::regclass AS base_table,
|
base_table::regclass AS base_table,
|
||||||
_CDB_OverviewTableZ(table_name) AS z,
|
@extschema@._CDB_OverviewTableZ(table_name) AS z,
|
||||||
table_regclass AS overview_table
|
table_regclass AS overview_table
|
||||||
FROM
|
FROM
|
||||||
_CDB_UserTablesInSchema(), unnest(tables) base_table
|
@extschema@._CDB_UserTablesInSchema(), unnest(tables) base_table
|
||||||
WHERE
|
WHERE
|
||||||
schema_name = _cdb_schema_name(base_table)
|
schema_name = _cdb_schema_name(base_table)
|
||||||
AND _CDB_IsOverviewTableOf((SELECT relname FROM pg_class WHERE oid=base_table), table_name)
|
AND @extschema@._CDB_IsOverviewTableOf((SELECT relname FROM pg_class WHERE oid=base_table), table_name)
|
||||||
ORDER BY base_table, z;
|
ORDER BY base_table, z;
|
||||||
$$ LANGUAGE SQL STABLE PARALLEL SAFE;
|
$$ LANGUAGE SQL STABLE PARALLEL SAFE;
|
||||||
|
|
||||||
@@ -76,11 +76,11 @@ $$ LANGUAGE SQL STABLE PARALLEL SAFE;
|
|||||||
-- Parameters
|
-- Parameters
|
||||||
-- reloid: oid of the input table.
|
-- reloid: oid of the input table.
|
||||||
-- Return value A box2d extent in 3857.
|
-- Return value A box2d extent in 3857.
|
||||||
CREATE OR REPLACE FUNCTION _cdb_estimated_extent(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._cdb_estimated_extent(reloid REGCLASS)
|
||||||
RETURNS box2d
|
RETURNS @postgisschema@.box2d
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
ext box2d;
|
ext @postgisschema@.box2d;
|
||||||
ext_query text;
|
ext_query text;
|
||||||
table_id record;
|
table_id record;
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -89,7 +89,7 @@ AS $$
|
|||||||
FROM pg_class c JOIN pg_namespace n on n.oid = c.relnamespace WHERE c.oid = reloid::oid;
|
FROM pg_class c JOIN pg_namespace n on n.oid = c.relnamespace WHERE c.oid = reloid::oid;
|
||||||
|
|
||||||
ext_query = format(
|
ext_query = format(
|
||||||
'SELECT ST_EstimatedExtent(''%1$s'', ''%2$s'', ''%3$s'');',
|
'SELECT @postgisschema@.ST_EstimatedExtent(''%1$s'', ''%2$s'', ''%3$s'');',
|
||||||
table_id.schema_name, table_id.table_name, 'the_geom_webmercator'
|
table_id.schema_name, table_id.table_name, 'the_geom_webmercator'
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -99,7 +99,7 @@ AS $$
|
|||||||
EXECUTE format('ANALYZE %1$s', reloid);
|
EXECUTE format('ANALYZE %1$s', reloid);
|
||||||
|
|
||||||
-- We check the geometry type in case the error is due to empty geometries
|
-- We check the geometry type in case the error is due to empty geometries
|
||||||
IF _CDB_GeometryTypes(reloid) IS NULL THEN
|
IF @extschema@._CDB_GeometryTypes(reloid) IS NULL THEN
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
@@ -116,7 +116,7 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
-- reloid: oid of the input table. It must be a cartodbfy'ed table.
|
-- reloid: oid of the input table. It must be a cartodbfy'ed table.
|
||||||
-- nz: number of zoom levels to consider from z0 upward.
|
-- nz: number of zoom levels to consider from z0 upward.
|
||||||
-- Return value: feature density (num_features / webmercator_squared_meters).
|
-- Return value: feature density (num_features / webmercator_squared_meters).
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Feature_Density(reloid REGCLASS, nz integer)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Feature_Density(reloid REGCLASS, nz integer)
|
||||||
RETURNS FLOAT8
|
RETURNS FLOAT8
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -135,7 +135,7 @@ AS $$
|
|||||||
-- the area of tiles at level Z: c*c*power(2, -2*z)
|
-- the area of tiles at level Z: c*c*power(2, -2*z)
|
||||||
-- with c = CDB_XYZ_Resolution(-8) (earth circumference)
|
-- with c = CDB_XYZ_Resolution(-8) (earth circumference)
|
||||||
min_features = '500';
|
min_features = '500';
|
||||||
SELECT CDB_XYZ_Resolution(-8) INTO c;
|
SELECT @extschema@.CDB_XYZ_Resolution(-8) INTO c;
|
||||||
|
|
||||||
-- We first compute a set of *seed* tiles, of the minimum Z level, z0, such that
|
-- We first compute a set of *seed* tiles, of the minimum Z level, z0, such that
|
||||||
-- they cover the extent of the table and we have at least n of them in each
|
-- they cover the extent of the table and we have at least n of them in each
|
||||||
@@ -146,27 +146,27 @@ AS $$
|
|||||||
-- considered tiles.
|
-- considered tiles.
|
||||||
EXECUTE Format('
|
EXECUTE Format('
|
||||||
WITH RECURSIVE t(x, y, z, e) AS (
|
WITH RECURSIVE t(x, y, z, e) AS (
|
||||||
WITH ext AS (SELECT _cdb_estimated_extent(%6$s) as g),
|
WITH ext AS (SELECT @extschema@._cdb_estimated_extent(%6$s) as g),
|
||||||
base AS (
|
base AS (
|
||||||
SELECT
|
SELECT
|
||||||
least(
|
least(
|
||||||
-floor(log(2, (greatest(ST_XMax(ext.g)-ST_XMin(ext.g), ST_YMax(ext.g)-ST_YMin(ext.g))/(%4$s*%5$s))::numeric)),
|
-floor(log(2, (greatest(@postgisschema@.ST_XMax(ext.g)-@postgisschema@.ST_XMin(ext.g), @postgisschema@.ST_YMax(ext.g)-@postgisschema@.ST_YMin(ext.g))/(%4$s*%5$s))::numeric)),
|
||||||
_CDB_MaxOverviewLevel()+1
|
@extschema@._CDB_MaxOverviewLevel()+1
|
||||||
)::integer z
|
)::integer z
|
||||||
FROM ext
|
FROM ext
|
||||||
),
|
),
|
||||||
lim AS (
|
lim AS (
|
||||||
SELECT
|
SELECT
|
||||||
FLOOR((ST_XMin(ext.g)+CDB_XYZ_Resolution(0)*128)/(CDB_XYZ_Resolution(base.z)*256))::integer x0,
|
FLOOR((@postgisschema@.ST_XMin(ext.g)+@extschema@.CDB_XYZ_Resolution(0)*128)/(@extschema@.CDB_XYZ_Resolution(base.z)*256))::integer x0,
|
||||||
FLOOR((ST_XMax(ext.g)+CDB_XYZ_Resolution(0)*128)/(CDB_XYZ_Resolution(base.z)*256))::integer x1,
|
FLOOR((@postgisschema@.ST_XMax(ext.g)+@extschema@.CDB_XYZ_Resolution(0)*128)/(@extschema@.CDB_XYZ_Resolution(base.z)*256))::integer x1,
|
||||||
FLOOR((CDB_XYZ_Resolution(0)*128-ST_YMin(ext.g))/(CDB_XYZ_Resolution(base.z)*256))::integer y1,
|
FLOOR((@extschema@.CDB_XYZ_Resolution(0)*128-@postgisschema@.ST_YMin(ext.g))/(@extschema@.CDB_XYZ_Resolution(base.z)*256))::integer y1,
|
||||||
FLOOR((CDB_XYZ_Resolution(0)*128-ST_YMax(ext.g))/(CDB_XYZ_Resolution(base.z)*256))::integer y0
|
FLOOR((@extschema@.CDB_XYZ_Resolution(0)*128-@postgisschema@.ST_YMax(ext.g))/(@extschema@.CDB_XYZ_Resolution(base.z)*256))::integer y0
|
||||||
FROM ext, base
|
FROM ext, base
|
||||||
),
|
),
|
||||||
seed AS (
|
seed AS (
|
||||||
SELECT xt, yt, base.z, (
|
SELECT xt, yt, base.z, (
|
||||||
SELECT count(*) FROM %1$s
|
SELECT count(*) FROM %1$s
|
||||||
WHERE the_geom_webmercator && CDB_XYZ_Extent(xt, yt, base.z)
|
WHERE the_geom_webmercator && @extschema@.CDB_XYZ_Extent(xt, yt, base.z)
|
||||||
) e
|
) e
|
||||||
FROM base, lim, generate_series(lim.x0, lim.x1) xt, generate_series(lim.y0, lim.y1) yt
|
FROM base, lim, generate_series(lim.x0, lim.x1) xt, generate_series(lim.y0, lim.y1) yt
|
||||||
)
|
)
|
||||||
@@ -174,12 +174,12 @@ AS $$
|
|||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT x*2 + xx, y*2 + yy, t.z+1, (
|
SELECT x*2 + xx, y*2 + yy, t.z+1, (
|
||||||
SELECT count(*) FROM %1$s
|
SELECT count(*) FROM %1$s
|
||||||
WHERE the_geom_webmercator && CDB_XYZ_Extent(t.x*2 + c.xx, t.y*2 + c.yy, t.z+1)
|
WHERE the_geom_webmercator && @extschema@.CDB_XYZ_Extent(t.x*2 + c.xx, t.y*2 + c.yy, t.z+1)
|
||||||
)
|
)
|
||||||
FROM t, base, (VALUES (0, 0), (0, 1), (1, 1), (1, 0)) AS c(xx, yy)
|
FROM t, base, (VALUES (0, 0), (0, 1), (1, 1), (1, 0)) AS c(xx, yy)
|
||||||
WHERE t.e > %2$s AND t.z < least(base.z + %3$s, _CDB_MaxZoomLevel())
|
WHERE t.e > %2$s AND t.z < least(base.z + %3$s, _CDB_MaxZoomLevel())
|
||||||
)
|
)
|
||||||
SELECT MAX(e/ST_Area(CDB_XYZ_Extent(x,y,z))) FROM t where e > 0;
|
SELECT MAX(e/@postgisschema@.ST_Area(@extschema@.CDB_XYZ_Extent(x,y,z))) FROM t where e > 0;
|
||||||
', reloid::text, min_features, nz, n, c, reloid::oid)
|
', reloid::text, min_features, nz, n, c, reloid::oid)
|
||||||
INTO fd;
|
INTO fd;
|
||||||
RETURN fd;
|
RETURN fd;
|
||||||
@@ -193,7 +193,7 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
-- Parameters:
|
-- Parameters:
|
||||||
-- reloid: oid of the input table. It must be a cartodbfy'ed table.
|
-- reloid: oid of the input table. It must be a cartodbfy'ed table.
|
||||||
-- Return value: Z level as an integer
|
-- Return value: Z level as an integer
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Feature_Density_Ref_Z_Strategy(reloid REGCLASS, tolerance_px FLOAT8 DEFAULT NULL)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Feature_Density_Ref_Z_Strategy(reloid REGCLASS, tolerance_px FLOAT8 DEFAULT NULL)
|
||||||
RETURNS INTEGER
|
RETURNS INTEGER
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -210,15 +210,15 @@ AS $$
|
|||||||
|
|
||||||
-- Compute fd as an estimation of the (maximum) number
|
-- Compute fd as an estimation of the (maximum) number
|
||||||
-- of features per unit of tile area (in webmercator squared meters)
|
-- of features per unit of tile area (in webmercator squared meters)
|
||||||
SELECT _CDB_Feature_Density(reloid, nz) INTO fd;
|
SELECT @extschema@._CDB_Feature_Density(reloid, nz) INTO fd;
|
||||||
-- lim maximum number of (desiderable) features per tile
|
-- lim maximum number of (desiderable) features per tile
|
||||||
-- we have c = 2*Pi*R = CDB_XYZ_Resolution(-8) (earth circumference)
|
-- we have c = 2*Pi*R = CDB_XYZ_Resolution(-8) (earth circumference)
|
||||||
-- ta(z): tile area = power(c*power(2,-z), 2) = c*c*power(2,-2*z)
|
-- ta(z): tile area = power(c*power(2,-z), 2) = c*c*power(2,-2*z)
|
||||||
-- => fd*ta(z) is the average number of features per tile at level z
|
-- => fd*ta(z) is the average number of features per tile at level z
|
||||||
-- find minimum z so that fd*ta(z) <= lim
|
-- find minimum z so that fd*ta(z) <= lim
|
||||||
-- compute a rough 'feature density' value
|
-- compute a rough 'feature density' value
|
||||||
SELECT CDB_XYZ_Resolution(-8) INTO c;
|
SELECT @extschema@.CDB_XYZ_Resolution(-8) INTO c;
|
||||||
RETURN least(_CDB_MaxOverviewLevel()+1, ceil(log(2.0, (c*c*fd/lim)::numeric)/2));
|
RETURN least(@extschema@._CDB_MaxOverviewLevel()+1, ceil(log(2.0, (c*c*fd/lim)::numeric)/2));
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
@@ -231,7 +231,7 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
-- overview_z Z level of the overview to be named, must be smaller than ref_z
|
-- overview_z Z level of the overview to be named, must be smaller than ref_z
|
||||||
-- Return value: the name to be used for the overview. The name is always
|
-- Return value: the name to be used for the overview. The name is always
|
||||||
-- unqualified (does not include a schema name).
|
-- unqualified (does not include a schema name).
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Overview_Name(ref REGCLASS, ref_z INTEGER, overview_z INTEGER)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Overview_Name(ref REGCLASS, ref_z INTEGER, overview_z INTEGER)
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -240,9 +240,9 @@ AS $$
|
|||||||
suffix TEXT;
|
suffix TEXT;
|
||||||
is_overview BOOLEAN;
|
is_overview BOOLEAN;
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT * FROM _cdb_split_table_name(ref) INTO schema_name, base;
|
SELECT * FROM @extschema@._cdb_split_table_name(ref) INTO schema_name, base;
|
||||||
SELECT _CDB_OverviewBaseTableName(base) INTO base;
|
SELECT @extschema@._CDB_OverviewBaseTableName(base) INTO base;
|
||||||
RETURN _CDB_OverviewTableName(base, overview_z);
|
RETURN @extschema@._CDB_OverviewTableName(base, overview_z);
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
$$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
||||||
|
|
||||||
@@ -254,7 +254,7 @@ $$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
|||||||
-- ref_z Z level assigned to the original table
|
-- ref_z Z level assigned to the original table
|
||||||
-- overview_z Z level of the overview to be generated, must be smaller than ref_z
|
-- overview_z Z level of the overview to be generated, must be smaller than ref_z
|
||||||
-- Return value: Name of the generated overview table
|
-- Return value: Name of the generated overview table
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Sampling_Reduce_Strategy(reloid REGCLASS, ref_z INTEGER, overview_z INTEGER, tolerance_px FLOAT8 DEFAULT NULL, has_overview_created BOOLEAN DEFAULT FALSE)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Sampling_Reduce_Strategy(reloid REGCLASS, ref_z INTEGER, overview_z INTEGER, tolerance_px FLOAT8 DEFAULT NULL, has_overview_created BOOLEAN DEFAULT FALSE)
|
||||||
RETURNS REGCLASS
|
RETURNS REGCLASS
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -268,11 +268,11 @@ AS $$
|
|||||||
overview_table_name TEXT;
|
overview_table_name TEXT;
|
||||||
creation_clause TEXT;
|
creation_clause TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
overview_rel := _CDB_Overview_Name(reloid, ref_z, overview_z);
|
overview_rel := @extschema@._CDB_Overview_Name(reloid, ref_z, overview_z);
|
||||||
-- TODO: compute fraction from tolerance_px if not NULL
|
-- TODO: compute fraction from tolerance_px if not NULL
|
||||||
fraction := power(2, 2*(overview_z - ref_z));
|
fraction := power(2, 2*(overview_z - ref_z));
|
||||||
|
|
||||||
SELECT * FROM _cdb_split_table_name(reloid) INTO schema_name, table_name;
|
SELECT * FROM @extschema@._cdb_split_table_name(reloid) INTO schema_name, table_name;
|
||||||
|
|
||||||
overview_table_name := Format('%I.%I', schema_name, overview_rel);
|
overview_table_name := Format('%I.%I', schema_name, overview_rel);
|
||||||
IF has_overview_created THEN
|
IF has_overview_created THEN
|
||||||
@@ -299,7 +299,7 @@ AS $$
|
|||||||
%1$s SELECT * FROM %2$s
|
%1$s SELECT * FROM %2$s
|
||||||
WHERE ctid = ANY (
|
WHERE ctid = ANY (
|
||||||
ARRAY[
|
ARRAY[
|
||||||
(SELECT CDB_RandomTids(''%2$s'', %3$s))
|
(SELECT @extschema@.CDB_RandomTids(''%2$s'', %3$s))
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
', creation_clause, reloid, num_samples);
|
', creation_clause, reloid, num_samples);
|
||||||
@@ -320,7 +320,7 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
-- overview table to match those of the dataset. It will only perform any change
|
-- overview table to match those of the dataset. It will only perform any change
|
||||||
-- if the overview table belgons to the same scheme as the dataset and it
|
-- if the overview table belgons to the same scheme as the dataset and it
|
||||||
-- matches the scheme naming for overview tables.
|
-- matches the scheme naming for overview tables.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Register_Overview(dataset REGCLASS, overview_table REGCLASS, overview_z INTEGER)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Register_Overview(dataset REGCLASS, overview_table REGCLASS, overview_z INTEGER)
|
||||||
RETURNS VOID
|
RETURNS VOID
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -334,10 +334,10 @@ AS $$
|
|||||||
-- This function will only register a table as an overview table if it matches
|
-- This function will only register a table as an overview table if it matches
|
||||||
-- the overviews naming scheme for the dataset and z level and the table belongs
|
-- the overviews naming scheme for the dataset and z level and the table belongs
|
||||||
-- to the same scheme as the the dataset
|
-- to the same scheme as the the dataset
|
||||||
SELECT * FROM _cdb_split_table_name(dataset) INTO dataset_scheme, dataset_name;
|
SELECT * FROM @extschema@._cdb_split_table_name(dataset) INTO dataset_scheme, dataset_name;
|
||||||
SELECT * FROM _cdb_split_table_name(overview_table) INTO overview_scheme, overview_name;
|
SELECT * FROM @extschema@._cdb_split_table_name(overview_table) INTO overview_scheme, overview_name;
|
||||||
IF dataset_scheme = overview_scheme AND
|
IF dataset_scheme = overview_scheme AND
|
||||||
overview_name = _CDB_OverviewTableName(dataset_name, overview_z) THEN
|
overview_name = @extschema@._CDB_OverviewTableName(dataset_name, overview_z) THEN
|
||||||
|
|
||||||
-- preserve the owner of the base table
|
-- preserve the owner of the base table
|
||||||
SELECT u.usename
|
SELECT u.usename
|
||||||
@@ -356,7 +356,7 @@ AS $$
|
|||||||
WHERE c_from.oid = dataset
|
WHERE c_from.oid = dataset
|
||||||
AND c_to.oid = overview_table;
|
AND c_to.oid = overview_table;
|
||||||
|
|
||||||
PERFORM _CDB_Add_Indexes(overview_table);
|
PERFORM @extschema@._CDB_Add_Indexes(overview_table);
|
||||||
|
|
||||||
-- TODO: If metadata about existing overviews is to be stored
|
-- TODO: If metadata about existing overviews is to be stored
|
||||||
-- it should be done here (CDB_Overviews would consume such metadata)
|
-- it should be done here (CDB_Overviews would consume such metadata)
|
||||||
@@ -371,10 +371,10 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
|||||||
-- Parameters
|
-- Parameters
|
||||||
-- reloid: oid of the input table. It must be a cartodbfy'ed table.
|
-- reloid: oid of the input table. It must be a cartodbfy'ed table.
|
||||||
-- Return value: set of attribute names
|
-- Return value: set of attribute names
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Aggregable_Attributes(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Aggregable_Attributes(reloid REGCLASS)
|
||||||
RETURNS SETOF information_schema.sql_identifier
|
RETURNS SETOF information_schema.sql_identifier
|
||||||
AS $$
|
AS $$
|
||||||
SELECT c FROM CDB_ColumnNames(reloid) c, _CDB_Columns() cdb
|
SELECT c FROM @extschema@.CDB_ColumnNames(reloid) c, @extschema@._CDB_Columns() cdb
|
||||||
WHERE c NOT IN (
|
WHERE c NOT IN (
|
||||||
cdb.pkey, cdb.geomcol, cdb.mercgeomcol
|
cdb.pkey, cdb.geomcol, cdb.mercgeomcol
|
||||||
)
|
)
|
||||||
@@ -386,14 +386,14 @@ $$ LANGUAGE SQL STABLE PARALLEL SAFE;
|
|||||||
-- Parameters
|
-- Parameters
|
||||||
-- reloid: oid of the input table. It must be a cartodbfy'ed table.
|
-- reloid: oid of the input table. It must be a cartodbfy'ed table.
|
||||||
-- Return value: SQL subexpression as text
|
-- Return value: SQL subexpression as text
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Aggregable_Attributes_Expression(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Aggregable_Attributes_Expression(reloid REGCLASS)
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
attr_list TEXT;
|
attr_list TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT string_agg(s.c, ',') FROM (
|
SELECT string_agg(s.c, ',') FROM (
|
||||||
SELECT * FROM _CDB_Aggregable_Attributes(reloid) c
|
SELECT * FROM @extschema@._CDB_Aggregable_Attributes(reloid) c
|
||||||
) AS s INTO attr_list;
|
) AS s INTO attr_list;
|
||||||
|
|
||||||
RETURN attr_list;
|
RETURN attr_list;
|
||||||
@@ -401,7 +401,7 @@ END
|
|||||||
$$ LANGUAGE PLPGSQL STABLE PARALLEL SAFE;
|
$$ LANGUAGE PLPGSQL STABLE PARALLEL SAFE;
|
||||||
|
|
||||||
-- Check if a column of a table is of an unlimited-length text type
|
-- Check if a column of a table is of an unlimited-length text type
|
||||||
CREATE OR REPLACE FUNCTION _cdb_unlimited_text_column(reloid REGCLASS, col_name TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@._cdb_unlimited_text_column(reloid REGCLASS, col_name TEXT)
|
||||||
RETURNS BOOLEAN
|
RETURNS BOOLEAN
|
||||||
AS $$
|
AS $$
|
||||||
SELECT EXISTS (
|
SELECT EXISTS (
|
||||||
@@ -416,7 +416,7 @@ AS $$
|
|||||||
);
|
);
|
||||||
$$ LANGUAGE SQL STABLE PARALLEL SAFE;
|
$$ LANGUAGE SQL STABLE PARALLEL SAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION _cdb_categorical_column(reloid REGCLASS, col_name TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@._cdb_categorical_column(reloid REGCLASS, col_name TEXT)
|
||||||
RETURNS BOOLEAN
|
RETURNS BOOLEAN
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -445,7 +445,7 @@ BEGIN
|
|||||||
END;
|
END;
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL RESTRICTED;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL RESTRICTED;
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION _cdb_mode_of_array(anyarray)
|
CREATE OR REPLACE FUNCTION @extschema@._cdb_mode_of_array(anyarray)
|
||||||
RETURNS anyelement AS
|
RETURNS anyelement AS
|
||||||
$$
|
$$
|
||||||
SELECT a
|
SELECT a
|
||||||
@@ -456,11 +456,11 @@ $$
|
|||||||
$$
|
$$
|
||||||
LANGUAGE SQL IMMUTABLE PARALLEL SAFE;
|
LANGUAGE SQL IMMUTABLE PARALLEL SAFE;
|
||||||
|
|
||||||
DROP AGGREGATE IF EXISTS _cdb_mode(anyelement);
|
DROP AGGREGATE IF EXISTS @extschema@._cdb_mode(anyelement);
|
||||||
CREATE AGGREGATE _cdb_mode(anyelement) (
|
CREATE AGGREGATE @extschema@._cdb_mode(anyelement) (
|
||||||
SFUNC=array_append,
|
SFUNC=array_append,
|
||||||
STYPE=anyarray,
|
STYPE=anyarray,
|
||||||
FINALFUNC=_cdb_mode_of_array,
|
FINALFUNC=@extschema@._cdb_mode_of_array,
|
||||||
PARALLEL = SAFE,
|
PARALLEL = SAFE,
|
||||||
INITCOND='{}'
|
INITCOND='{}'
|
||||||
);
|
);
|
||||||
@@ -473,7 +473,7 @@ CREATE AGGREGATE _cdb_mode(anyelement) (
|
|||||||
-- table_alias: (optional) table qualifier for the column to be aggregated
|
-- table_alias: (optional) table qualifier for the column to be aggregated
|
||||||
-- Return SQL subexpression as text with aggregated attribute aliased
|
-- Return SQL subexpression as text with aggregated attribute aliased
|
||||||
-- with its original name.
|
-- with its original name.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Attribute_Aggregation_Expression(reloid REGCLASS, column_name TEXT, table_alias TEXT DEFAULT '')
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Attribute_Aggregation_Expression(reloid REGCLASS, column_name TEXT, table_alias TEXT DEFAULT '')
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -490,10 +490,10 @@ BEGIN
|
|||||||
qualified_column := Format('%I', column_name);
|
qualified_column := Format('%I', column_name);
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
column_type := CDB_ColumnType(reloid, column_name);
|
column_type := @extschema@.CDB_ColumnType(reloid, column_name);
|
||||||
|
|
||||||
SELECT EXISTS (
|
SELECT EXISTS (
|
||||||
SELECT * FROM CDB_ColumnNames(reloid) as colname WHERE colname = '_feature_count'
|
SELECT * FROM @extschema@.CDB_ColumnNames(reloid) as colname WHERE colname = '_feature_count'
|
||||||
) INTO has_counter_column;
|
) INTO has_counter_column;
|
||||||
IF has_counter_column THEN
|
IF has_counter_column THEN
|
||||||
feature_count := '_feature_count';
|
feature_count := '_feature_count';
|
||||||
@@ -503,24 +503,24 @@ BEGIN
|
|||||||
total_feature_count := 'count(*)';
|
total_feature_count := 'count(*)';
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
base_table := _CDB_OverviewBaseTable(reloid);
|
base_table := @extschema@._CDB_OverviewBaseTable(reloid);
|
||||||
|
|
||||||
CASE column_type
|
CASE column_type
|
||||||
WHEN 'double precision', 'real', 'integer', 'bigint', 'numeric' THEN
|
WHEN 'double precision', 'real', 'integer', 'bigint', 'numeric' THEN
|
||||||
IF column_name = '_feature_count' THEN
|
IF column_name = '_feature_count' THEN
|
||||||
RETURN 'SUM(_feature_count)';
|
RETURN 'SUM(_feature_count)';
|
||||||
ELSE
|
ELSE
|
||||||
IF column_type = 'integer' AND _cdb_categorical_column(base_table, column_name) THEN
|
IF column_type = 'integer' AND @extschema@._cdb_categorical_column(base_table, column_name) THEN
|
||||||
RETURN Format('CDB_Math_Mode(%s)::', qualified_column) || column_type;
|
RETURN Format('CDB_Math_Mode(%s)::', qualified_column) || column_type;
|
||||||
ELSE
|
ELSE
|
||||||
RETURN Format('SUM(%s*%s)/%s::' || column_type, qualified_column, feature_count, total_feature_count);
|
RETURN Format('SUM(%s*%s)/%s::' || column_type, qualified_column, feature_count, total_feature_count);
|
||||||
END IF;
|
END IF;
|
||||||
END IF;
|
END IF;
|
||||||
WHEN 'text', 'character varying', 'character' THEN
|
WHEN 'text', 'character varying', 'character' THEN
|
||||||
IF _cdb_categorical_column(base_table, column_name) THEN
|
IF @extschema@._cdb_categorical_column(base_table, column_name) THEN
|
||||||
RETURN Format('_cdb_mode(%s)::', qualified_column) || column_type;
|
RETURN Format('_cdb_mode(%s)::', qualified_column) || column_type;
|
||||||
ELSE
|
ELSE
|
||||||
IF _cdb_unlimited_text_column(base_table, column_name) THEN
|
IF @extschema@._cdb_unlimited_text_column(base_table, column_name) THEN
|
||||||
-- TODO: this should not be applied to columns containing largish text;
|
-- TODO: this should not be applied to columns containing largish text;
|
||||||
-- it is intended only to short names/identifiers
|
-- it is intended only to short names/identifiers
|
||||||
RETURN 'CASE WHEN count(distinct ' || qualified_column || ') = 1 THEN MIN(' || qualified_column || ') WHEN ' || total_feature_count || ' < 5 THEN string_agg(distinct ' || qualified_column || ','' / '') ELSE ''*'' END::' || column_type;
|
RETURN 'CASE WHEN count(distinct ' || qualified_column || ') = 1 THEN MIN(' || qualified_column || ') WHEN ' || total_feature_count || ' < 5 THEN string_agg(distinct ' || qualified_column || ','' / '') ELSE ''*'' END::' || column_type;
|
||||||
@@ -542,15 +542,15 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL RESTRICTED;
|
|||||||
-- reloid: oid of the input table. It must be a cartodbfy'ed table.
|
-- reloid: oid of the input table. It must be a cartodbfy'ed table.
|
||||||
-- table_alias: (optional) table qualifier for the columns to be aggregated
|
-- table_alias: (optional) table qualifier for the columns to be aggregated
|
||||||
-- Return value: SQL subexpression as text
|
-- Return value: SQL subexpression as text
|
||||||
CREATE OR REPLACE FUNCTION _CDB_Aggregated_Attributes_Expression(reloid REGCLASS, table_alias TEXT DEFAULT '')
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_Aggregated_Attributes_Expression(reloid REGCLASS, table_alias TEXT DEFAULT '')
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
attr_list TEXT;
|
attr_list TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT string_agg(_CDB_Attribute_Aggregation_Expression(reloid, s.c, table_alias) || Format(' AS %s', s.c), ',')
|
SELECT string_agg(@extschema@._CDB_Attribute_Aggregation_Expression(reloid, s.c, table_alias) || Format(' AS %s', s.c), ',')
|
||||||
FROM (
|
FROM (
|
||||||
SELECT * FROM _CDB_Aggregable_Attributes(reloid) c
|
SELECT * FROM @extschema@._CDB_Aggregable_Attributes(reloid) c
|
||||||
) AS s INTO attr_list;
|
) AS s INTO attr_list;
|
||||||
|
|
||||||
RETURN attr_list;
|
RETURN attr_list;
|
||||||
@@ -562,14 +562,14 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL RESTRICTED;
|
|||||||
-- Parameters
|
-- Parameters
|
||||||
-- reloid: oid of the input table. It must be a cartodbfy'ed table.
|
-- reloid: oid of the input table. It must be a cartodbfy'ed table.
|
||||||
-- Return value: array of geometry type names
|
-- Return value: array of geometry type names
|
||||||
CREATE OR REPLACE FUNCTION _CDB_GeometryTypes(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_GeometryTypes(reloid REGCLASS)
|
||||||
RETURNS TEXT[]
|
RETURNS TEXT[]
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
gtypes TEXT[];
|
gtypes TEXT[];
|
||||||
BEGIN
|
BEGIN
|
||||||
EXECUTE Format('
|
EXECUTE Format('
|
||||||
SELECT array_agg(DISTINCT ST_GeometryType(the_geom)) FROM (
|
SELECT array_agg(DISTINCT @postgisschema@.ST_GeometryType(the_geom)) FROM (
|
||||||
SELECT the_geom FROM %s
|
SELECT the_geom FROM %s
|
||||||
WHERE (the_geom is not null) LIMIT 10
|
WHERE (the_geom is not null) LIMIT 10
|
||||||
) as geom_types
|
) as geom_types
|
||||||
@@ -589,7 +589,7 @@ $$ LANGUAGE PLPGSQL STABLE PARALLEL SAFE;
|
|||||||
-- ref_z Z level assigned to the original table
|
-- ref_z Z level assigned to the original table
|
||||||
-- overview_z Z level of the overview to be generated, must be smaller than ref_z
|
-- overview_z Z level of the overview to be generated, must be smaller than ref_z
|
||||||
-- Return value: Name of the generated overview table
|
-- Return value: Name of the generated overview table
|
||||||
CREATE OR REPLACE FUNCTION _CDB_GridCluster_Reduce_Strategy(reloid REGCLASS, ref_z INTEGER, overview_z INTEGER, grid_px FLOAT8 DEFAULT NULL, has_overview_created BOOLEAN DEFAULT FALSE)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_GridCluster_Reduce_Strategy(reloid REGCLASS, ref_z INTEGER, overview_z INTEGER, grid_px FLOAT8 DEFAULT NULL, has_overview_created BOOLEAN DEFAULT FALSE)
|
||||||
RETURNS REGCLASS
|
RETURNS REGCLASS
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -613,7 +613,7 @@ AS $$
|
|||||||
overview_table_name TEXT;
|
overview_table_name TEXT;
|
||||||
creation_clause TEXT;
|
creation_clause TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT _CDB_GeometryTypes(reloid) INTO gtypes;
|
SELECT @extschema@._CDB_GeometryTypes(reloid) INTO gtypes;
|
||||||
IF gtypes IS NULL OR array_upper(gtypes, 1) <> 1 OR gtypes[1] <> 'ST_Point' THEN
|
IF gtypes IS NULL OR array_upper(gtypes, 1) <> 1 OR gtypes[1] <> 'ST_Point' THEN
|
||||||
-- This strategy only supports datasets with point geomety
|
-- This strategy only supports datasets with point geomety
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
@@ -621,22 +621,22 @@ AS $$
|
|||||||
|
|
||||||
--TODO: check applicability: geometry type, minimum number of points...
|
--TODO: check applicability: geometry type, minimum number of points...
|
||||||
|
|
||||||
overview_rel := _CDB_Overview_Name(reloid, ref_z, overview_z);
|
overview_rel := @extschema@._CDB_Overview_Name(reloid, ref_z, overview_z);
|
||||||
|
|
||||||
-- Grid size in pixels at Z level overview_z
|
-- Grid size in pixels at Z level overview_z
|
||||||
IF grid_px IS NULL THEN
|
IF grid_px IS NULL THEN
|
||||||
grid_px := 1.0;
|
grid_px := 1.0;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
SELECT * FROM _cdb_split_table_name(reloid) INTO schema_name, table_name;
|
SELECT * FROM @extschema@._cdb_split_table_name(reloid) INTO schema_name, table_name;
|
||||||
|
|
||||||
-- pixel_m: size of a pixel in webmercator units (meters)
|
-- pixel_m: size of a pixel in webmercator units (meters)
|
||||||
SELECT CDB_XYZ_Resolution(overview_z) INTO pixel_m;
|
SELECT @extschema@.CDB_XYZ_Resolution(overview_z) INTO pixel_m;
|
||||||
-- grid size in meters
|
-- grid size in meters
|
||||||
grid_m = grid_px * pixel_m;
|
grid_m = grid_px * pixel_m;
|
||||||
|
|
||||||
attributes := _CDB_Aggregable_Attributes_Expression(reloid);
|
attributes := @extschema@._CDB_Aggregable_Attributes_Expression(reloid);
|
||||||
aggr_attributes := _CDB_Aggregated_Attributes_Expression(reloid);
|
aggr_attributes := @extschema@._CDB_Aggregated_Attributes_Expression(reloid);
|
||||||
IF attributes <> '' THEN
|
IF attributes <> '' THEN
|
||||||
attributes := ', ' || attributes;
|
attributes := ', ' || attributes;
|
||||||
END IF;
|
END IF;
|
||||||
@@ -666,12 +666,12 @@ AS $$
|
|||||||
CASE c
|
CASE c
|
||||||
WHEN 'cartodb_id' THEN 'cartodb_id'
|
WHEN 'cartodb_id' THEN 'cartodb_id'
|
||||||
WHEN 'the_geom' THEN
|
WHEN 'the_geom' THEN
|
||||||
Format('ST_Transform(%s, 4326) AS the_geom', point_geom)
|
Format('@postgisschema@.ST_Transform(%s, 4326) AS the_geom', point_geom)
|
||||||
WHEN 'the_geom_webmercator' THEN
|
WHEN 'the_geom_webmercator' THEN
|
||||||
Format('%s AS the_geom_webmercator', point_geom)
|
Format('%s AS the_geom_webmercator', point_geom)
|
||||||
ELSE c
|
ELSE c
|
||||||
END AS column
|
END AS column
|
||||||
FROM CDB_ColumnNames(reloid) c
|
FROM @extschema@.CDB_ColumnNames(reloid) c
|
||||||
)
|
)
|
||||||
SELECT string_agg(s.column, ',') FROM (
|
SELECT string_agg(s.column, ',') FROM (
|
||||||
SELECT * FROM cols
|
SELECT * FROM cols
|
||||||
@@ -701,8 +701,8 @@ AS $$
|
|||||||
SELECT
|
SELECT
|
||||||
%5$s
|
%5$s
|
||||||
count(*) AS n,
|
count(*) AS n,
|
||||||
Floor(ST_X(f.the_geom_webmercator)/%2$s)::int AS gx,
|
Floor(@postgisschema@.ST_X(f.the_geom_webmercator)/%2$s)::int AS gx,
|
||||||
Floor(ST_Y(f.the_geom_webmercator)/%2$s)::int AS gy,
|
Floor(@postgisschema@.ST_Y(f.the_geom_webmercator)/%2$s)::int AS gy,
|
||||||
MIN(cartodb_id) AS cartodb_id
|
MIN(cartodb_id) AS cartodb_id
|
||||||
FROM %1$s f
|
FROM %1$s f
|
||||||
WHERE f.the_geom_webmercator IS NOT NULL
|
WHERE f.the_geom_webmercator IS NOT NULL
|
||||||
@@ -716,7 +716,7 @@ AS $$
|
|||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
-- This strategy places the aggregation of each cluster at the centroid of the cluster members.
|
-- This strategy places the aggregation of each cluster at the centroid of the cluster members.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_GridClusterCentroid_Reduce_Strategy(reloid REGCLASS, ref_z INTEGER, overview_z INTEGER, grid_px FLOAT8 DEFAULT NULL, has_overview_created BOOLEAN DEFAULT FALSE)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_GridClusterCentroid_Reduce_Strategy(reloid REGCLASS, ref_z INTEGER, overview_z INTEGER, grid_px FLOAT8 DEFAULT NULL, has_overview_created BOOLEAN DEFAULT FALSE)
|
||||||
RETURNS REGCLASS
|
RETURNS REGCLASS
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -740,7 +740,7 @@ AS $$
|
|||||||
overview_table_name TEXT;
|
overview_table_name TEXT;
|
||||||
creation_clause TEXT;
|
creation_clause TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT _CDB_GeometryTypes(reloid) INTO gtypes;
|
SELECT @extschema@._CDB_GeometryTypes(reloid) INTO gtypes;
|
||||||
IF gtypes IS NULL OR array_upper(gtypes, 1) <> 1 OR gtypes[1] <> 'ST_Point' THEN
|
IF gtypes IS NULL OR array_upper(gtypes, 1) <> 1 OR gtypes[1] <> 'ST_Point' THEN
|
||||||
-- This strategy only supports datasets with point geomety
|
-- This strategy only supports datasets with point geomety
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
@@ -748,22 +748,22 @@ AS $$
|
|||||||
|
|
||||||
--TODO: check applicability: geometry type, minimum number of points...
|
--TODO: check applicability: geometry type, minimum number of points...
|
||||||
|
|
||||||
overview_rel := _CDB_Overview_Name(reloid, ref_z, overview_z);
|
overview_rel := @extschema@._CDB_Overview_Name(reloid, ref_z, overview_z);
|
||||||
|
|
||||||
-- Grid size in pixels at Z level overview_z
|
-- Grid size in pixels at Z level overview_z
|
||||||
IF grid_px IS NULL THEN
|
IF grid_px IS NULL THEN
|
||||||
grid_px := 1.0;
|
grid_px := 1.0;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
SELECT * FROM _cdb_split_table_name(reloid) INTO schema_name, table_name;
|
SELECT * FROM @extschema@._cdb_split_table_name(reloid) INTO schema_name, table_name;
|
||||||
|
|
||||||
-- pixel_m: size of a pixel in webmercator units (meters)
|
-- pixel_m: size of a pixel in webmercator units (meters)
|
||||||
SELECT CDB_XYZ_Resolution(overview_z) INTO pixel_m;
|
SELECT @extschema@.CDB_XYZ_Resolution(overview_z) INTO pixel_m;
|
||||||
-- grid size in meters
|
-- grid size in meters
|
||||||
grid_m = grid_px * pixel_m;
|
grid_m = grid_px * pixel_m;
|
||||||
|
|
||||||
attributes := _CDB_Aggregable_Attributes_Expression(reloid);
|
attributes := @extschema@._CDB_Aggregable_Attributes_Expression(reloid);
|
||||||
aggr_attributes := _CDB_Aggregated_Attributes_Expression(reloid);
|
aggr_attributes := @extschema@._CDB_Aggregated_Attributes_Expression(reloid);
|
||||||
IF attributes <> '' THEN
|
IF attributes <> '' THEN
|
||||||
attributes := ', ' || attributes;
|
attributes := ', ' || attributes;
|
||||||
END IF;
|
END IF;
|
||||||
@@ -785,7 +785,7 @@ AS $$
|
|||||||
offset_y := Format('%2$s/2 - MOD((%1$s)::numeric, (%2$s)::numeric)::float8', cell_y, pixel_m);
|
offset_y := Format('%2$s/2 - MOD((%1$s)::numeric, (%2$s)::numeric)::float8', cell_y, pixel_m);
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
point_geom := Format('ST_SetSRID(ST_MakePoint(%1$s + %3$s, %2$s + %4$s), 3857)', cell_x, cell_y, offset_x, offset_y);
|
point_geom := Format('@postgisschema@.ST_SetSRID(@postgisschema@.ST_MakePoint(%1$s + %3$s, %2$s + %4$s), 3857)', cell_x, cell_y, offset_x, offset_y);
|
||||||
|
|
||||||
-- compute the resulting columns in the same order as in the base table
|
-- compute the resulting columns in the same order as in the base table
|
||||||
WITH cols AS (
|
WITH cols AS (
|
||||||
@@ -793,9 +793,9 @@ AS $$
|
|||||||
CASE c
|
CASE c
|
||||||
WHEN 'cartodb_id' THEN 'cartodb_id'
|
WHEN 'cartodb_id' THEN 'cartodb_id'
|
||||||
WHEN 'the_geom' THEN
|
WHEN 'the_geom' THEN
|
||||||
'ST_Transform(ST_SetSRID(ST_MakePoint(_sum_of_x/n, _sum_of_y/n), 3857), 4326) AS the_geom'
|
'@postgisschema@.ST_Transform(@postgisschema@.ST_SetSRID(@postgisschema@.ST_MakePoint(_sum_of_x/n, _sum_of_y/n), 3857), 4326) AS the_geom'
|
||||||
WHEN 'the_geom_webmercator' THEN
|
WHEN 'the_geom_webmercator' THEN
|
||||||
'ST_SetSRID(ST_MakePoint(_sum_of_x/n, _sum_of_y/n), 3857) AS the_geom_webmercator'
|
'@postgisschema@.ST_SetSRID(@postgisschema@.ST_MakePoint(_sum_of_x/n, _sum_of_y/n), 3857) AS the_geom_webmercator'
|
||||||
ELSE c
|
ELSE c
|
||||||
END AS column
|
END AS column
|
||||||
FROM CDB_ColumnNames(reloid) c
|
FROM CDB_ColumnNames(reloid) c
|
||||||
@@ -828,10 +828,10 @@ AS $$
|
|||||||
SELECT
|
SELECT
|
||||||
%5$s
|
%5$s
|
||||||
count(*) AS n,
|
count(*) AS n,
|
||||||
SUM(ST_X(f.the_geom_webmercator)) AS _sum_of_x,
|
SUM(@postgisschema@.ST_X(f.the_geom_webmercator)) AS _sum_of_x,
|
||||||
SUM(ST_Y(f.the_geom_webmercator)) AS _sum_of_y,
|
SUM(@postgisschema@.ST_Y(f.the_geom_webmercator)) AS _sum_of_y,
|
||||||
Floor(ST_Y(f.the_geom_webmercator)/%2$s)::int AS gy,
|
Floor(@postgisschema@.ST_Y(f.the_geom_webmercator)/%2$s)::int AS gy,
|
||||||
Floor(ST_X(f.the_geom_webmercator)/%2$s)::int AS gx,
|
Floor(@postgisschema@.ST_X(f.the_geom_webmercator)/%2$s)::int AS gx,
|
||||||
MIN(cartodb_id) AS cartodb_id
|
MIN(cartodb_id) AS cartodb_id
|
||||||
FROM %1$s f
|
FROM %1$s f
|
||||||
GROUP BY gx, gy
|
GROUP BY gx, gy
|
||||||
@@ -844,7 +844,7 @@ AS $$
|
|||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
-- This strategy places the aggregation of each cluster at the position of one of the cluster members.
|
-- This strategy places the aggregation of each cluster at the position of one of the cluster members.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_GridClusterSample_Reduce_Strategy(reloid REGCLASS, ref_z INTEGER, overview_z INTEGER, grid_px FLOAT8 DEFAULT NULL, has_overview_created BOOLEAN DEFAULT FALSE)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_GridClusterSample_Reduce_Strategy(reloid REGCLASS, ref_z INTEGER, overview_z INTEGER, grid_px FLOAT8 DEFAULT NULL, has_overview_created BOOLEAN DEFAULT FALSE)
|
||||||
RETURNS REGCLASS
|
RETURNS REGCLASS
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -868,7 +868,7 @@ AS $$
|
|||||||
overview_table_name TEXT;
|
overview_table_name TEXT;
|
||||||
creation_clause TEXT;
|
creation_clause TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT _CDB_GeometryTypes(reloid) INTO gtypes;
|
SELECT @extschema@._CDB_GeometryTypes(reloid) INTO gtypes;
|
||||||
IF gtypes IS NULL OR array_upper(gtypes, 1) <> 1 OR gtypes[1] <> 'ST_Point' THEN
|
IF gtypes IS NULL OR array_upper(gtypes, 1) <> 1 OR gtypes[1] <> 'ST_Point' THEN
|
||||||
-- This strategy only supports datasets with point geomety
|
-- This strategy only supports datasets with point geomety
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
@@ -876,22 +876,22 @@ AS $$
|
|||||||
|
|
||||||
--TODO: check applicability: geometry type, minimum number of points...
|
--TODO: check applicability: geometry type, minimum number of points...
|
||||||
|
|
||||||
overview_rel := _CDB_Overview_Name(reloid, ref_z, overview_z);
|
overview_rel := @extschema@._CDB_Overview_Name(reloid, ref_z, overview_z);
|
||||||
|
|
||||||
-- Grid size in pixels at Z level overview_z
|
-- Grid size in pixels at Z level overview_z
|
||||||
IF grid_px IS NULL THEN
|
IF grid_px IS NULL THEN
|
||||||
grid_px := 1.0;
|
grid_px := 1.0;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
SELECT * FROM _cdb_split_table_name(reloid) INTO schema_name, table_name;
|
SELECT * FROM @extschema@._cdb_split_table_name(reloid) INTO schema_name, table_name;
|
||||||
|
|
||||||
-- pixel_m: size of a pixel in webmercator units (meters)
|
-- pixel_m: size of a pixel in webmercator units (meters)
|
||||||
SELECT CDB_XYZ_Resolution(overview_z) INTO pixel_m;
|
SELECT @extschema@.CDB_XYZ_Resolution(overview_z) INTO pixel_m;
|
||||||
-- grid size in meters
|
-- grid size in meters
|
||||||
grid_m = grid_px * pixel_m;
|
grid_m = grid_px * pixel_m;
|
||||||
|
|
||||||
attributes := _CDB_Aggregable_Attributes_Expression(reloid);
|
attributes := @extschema@._CDB_Aggregable_Attributes_Expression(reloid);
|
||||||
aggr_attributes := _CDB_Aggregated_Attributes_Expression(reloid);
|
aggr_attributes := @extschema@._CDB_Aggregated_Attributes_Expression(reloid);
|
||||||
IF attributes <> '' THEN
|
IF attributes <> '' THEN
|
||||||
attributes := ', ' || attributes;
|
attributes := ', ' || attributes;
|
||||||
END IF;
|
END IF;
|
||||||
@@ -913,7 +913,7 @@ AS $$
|
|||||||
offset_y := Format('%2$s/2 - MOD((%1$s)::numeric, (%2$s)::numeric)::float8', cell_y, pixel_m);
|
offset_y := Format('%2$s/2 - MOD((%1$s)::numeric, (%2$s)::numeric)::float8', cell_y, pixel_m);
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
point_geom := Format('ST_SetSRID(ST_MakePoint(%1$s + %3$s, %2$s + %4$s), 3857)', cell_x, cell_y, offset_x, offset_y);
|
point_geom := Format('@postgisschema@.ST_SetSRID(@postgisschema@.ST_MakePoint(%1$s + %3$s, %2$s + %4$s), 3857)', cell_x, cell_y, offset_x, offset_y);
|
||||||
|
|
||||||
-- compute the resulting columns in the same order as in the base table
|
-- compute the resulting columns in the same order as in the base table
|
||||||
WITH cols AS (
|
WITH cols AS (
|
||||||
@@ -922,7 +922,7 @@ AS $$
|
|||||||
WHEN 'cartodb_id' THEN 'cartodb_id'
|
WHEN 'cartodb_id' THEN 'cartodb_id'
|
||||||
ELSE c
|
ELSE c
|
||||||
END AS column
|
END AS column
|
||||||
FROM CDB_ColumnNames(reloid) c
|
FROM @extschema@.CDB_ColumnNames(reloid) c
|
||||||
)
|
)
|
||||||
SELECT string_agg(s.column, ',') FROM (
|
SELECT string_agg(s.column, ',') FROM (
|
||||||
SELECT * FROM cols
|
SELECT * FROM cols
|
||||||
@@ -952,8 +952,8 @@ AS $$
|
|||||||
SELECT
|
SELECT
|
||||||
%5$s
|
%5$s
|
||||||
count(*) AS n,
|
count(*) AS n,
|
||||||
Floor(ST_X(_f.the_geom_webmercator)/%2$s)::int AS gx,
|
Floor(@postgisschema@.ST_X(_f.the_geom_webmercator)/%2$s)::int AS gx,
|
||||||
Floor(ST_Y(_f.the_geom_webmercator)/%2$s)::int AS gy,
|
Floor(@postgisschema@.ST_Y(_f.the_geom_webmercator)/%2$s)::int AS gy,
|
||||||
MIN(cartodb_id) AS cartodb_id
|
MIN(cartodb_id) AS cartodb_id
|
||||||
FROM %1$s _f
|
FROM %1$s _f
|
||||||
GROUP BY gx, gy
|
GROUP BY gx, gy
|
||||||
@@ -980,7 +980,7 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
-- created by the strategy must have the same columns
|
-- created by the strategy must have the same columns
|
||||||
-- as the base table and in the same order.
|
-- as the base table and in the same order.
|
||||||
-- Return value: Array with the names of the generated overview tables
|
-- Return value: Array with the names of the generated overview tables
|
||||||
CREATE OR REPLACE FUNCTION CDB_CreateOverviews(reloid REGCLASS, refscale_strategy regproc DEFAULT '_CDB_Feature_Density_Ref_Z_Strategy(REGCLASS,FLOAT8)'::regprocedure, reduce_strategy regproc DEFAULT '_CDB_GridCluster_Reduce_Strategy(REGCLASS,INTEGER,INTEGER,FLOAT8,BOOLEAN)'::regprocedure)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_CreateOverviews(reloid REGCLASS, refscale_strategy regproc DEFAULT '@extschema@._CDB_Feature_Density_Ref_Z_Strategy(REGCLASS,FLOAT8)'::regprocedure, reduce_strategy regproc DEFAULT '@extschema@._CDB_GridCluster_Reduce_Strategy(REGCLASS,INTEGER,INTEGER,FLOAT8,BOOLEAN)'::regprocedure)
|
||||||
RETURNS text[]
|
RETURNS text[]
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -988,12 +988,12 @@ DECLARE
|
|||||||
BEGIN
|
BEGIN
|
||||||
-- Use the default tolerance
|
-- Use the default tolerance
|
||||||
tolerance_px := 1.0;
|
tolerance_px := 1.0;
|
||||||
RETURN CDB_CreateOverviewsWithToleranceInPixels(reloid, tolerance_px, refscale_strategy, reduce_strategy);
|
RETURN @extschema@.CDB_CreateOverviewsWithToleranceInPixels(reloid, tolerance_px, refscale_strategy, reduce_strategy);
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
-- Create overviews with additional parameter to define the desired detail/tolerance in pixels
|
-- Create overviews with additional parameter to define the desired detail/tolerance in pixels
|
||||||
CREATE OR REPLACE FUNCTION CDB_CreateOverviewsWithToleranceInPixels(reloid REGCLASS, tolerance_px FLOAT8, refscale_strategy regproc DEFAULT '_CDB_Feature_Density_Ref_Z_Strategy(REGCLASS,FLOAT8)'::regprocedure, reduce_strategy regproc DEFAULT '_CDB_GridCluster_Reduce_Strategy(REGCLASS,INTEGER,INTEGER,FLOAT8,BOOLEAN)'::regprocedure)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_CreateOverviewsWithToleranceInPixels(reloid REGCLASS, tolerance_px FLOAT8, refscale_strategy regproc DEFAULT '@extschema@._CDB_Feature_Density_Ref_Z_Strategy(REGCLASS,FLOAT8)'::regprocedure, reduce_strategy regproc DEFAULT '@extschema@._CDB_GridCluster_Reduce_Strategy(REGCLASS,INTEGER,INTEGER,FLOAT8,BOOLEAN)'::regprocedure)
|
||||||
RETURNS text[]
|
RETURNS text[]
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -1045,7 +1045,7 @@ BEGIN
|
|||||||
|
|
||||||
IF overview_tables IS NOT NULL AND array_length(overview_tables, 1) > 0 THEN
|
IF overview_tables IS NOT NULL AND array_length(overview_tables, 1) > 0 THEN
|
||||||
SELECT EXISTS (
|
SELECT EXISTS (
|
||||||
SELECT * FROM CDB_ColumnNames(reloid) as colname WHERE colname = '_feature_count'
|
SELECT * FROM @extschema@.CDB_ColumnNames(reloid) as colname WHERE colname = '_feature_count'
|
||||||
) INTO has_counter_column;
|
) INTO has_counter_column;
|
||||||
IF NOT has_counter_column THEN
|
IF NOT has_counter_column THEN
|
||||||
EXECUTE Format('
|
EXECUTE Format('
|
||||||
@@ -1061,10 +1061,10 @@ $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
|
|||||||
-- Here are some older signatures of these functions, no longer in use.
|
-- Here are some older signatures of these functions, no longer in use.
|
||||||
-- They must be droped here, after the (new) definition of the function `CDB_CreateOverviews`
|
-- They must be droped here, after the (new) definition of the function `CDB_CreateOverviews`
|
||||||
-- because that function used to contain references to them in the default argument values.
|
-- because that function used to contain references to them in the default argument values.
|
||||||
DROP FUNCTION IF EXISTS _CDB_Feature_Density_Ref_Z_Strategy(REGCLASS);
|
DROP FUNCTION IF EXISTS @extschema@._CDB_Feature_Density_Ref_Z_Strategy(REGCLASS);
|
||||||
DROP FUNCTION IF EXISTS _CDB_GridCluster_Reduce_Strategy(REGCLASS,INTEGER,INTEGER);
|
DROP FUNCTION IF EXISTS @extschema@._CDB_GridCluster_Reduce_Strategy(REGCLASS,INTEGER,INTEGER);
|
||||||
DROP FUNCTION IF EXISTS _CDB_GridCluster_Reduce_Strategy(REGCLASS,INTEGER,INTEGER,FLOAT8);
|
DROP FUNCTION IF EXISTS @extschema@._CDB_GridCluster_Reduce_Strategy(REGCLASS,INTEGER,INTEGER,FLOAT8);
|
||||||
DROP FUNCTION IF EXISTS _CDB_GridClusterCentroid_Reduce_Strategy(REGCLASS, INTEGER, INTEGER, FLOAT8);
|
DROP FUNCTION IF EXISTS @extschema@._CDB_GridClusterCentroid_Reduce_Strategy(REGCLASS, INTEGER, INTEGER, FLOAT8);
|
||||||
DROP FUNCTION IF EXISTS _CDB_GridClusterSample_Reduce_Strategy(REGCLASS, INTEGER, INTEGER, FLOAT8);
|
DROP FUNCTION IF EXISTS @extschema@._CDB_GridClusterSample_Reduce_Strategy(REGCLASS, INTEGER, INTEGER, FLOAT8);
|
||||||
DROP FUNCTION IF EXISTS _CDB_Sampling_Reduce_Strategy(REGCLASS,INTEGER,INTEGER);
|
DROP FUNCTION IF EXISTS @extschema@._CDB_Sampling_Reduce_Strategy(REGCLASS,INTEGER,INTEGER);
|
||||||
DROP FUNCTION IF EXISTS _CDB_Sampling_Reduce_Strategy(REGCLASS,INTEGER,INTEGER,FLOAT8);
|
DROP FUNCTION IF EXISTS @extschema@._CDB_Sampling_Reduce_Strategy(REGCLASS,INTEGER,INTEGER,FLOAT8);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
-- Auxiliary overviews FUNCTIONS
|
-- Auxiliary overviews FUNCTIONS
|
||||||
|
|
||||||
-- Maximum zoom level for which overviews may be created
|
-- Maximum zoom level for which overviews may be created
|
||||||
CREATE OR REPLACE FUNCTION _CDB_MaxOverviewLevel()
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_MaxOverviewLevel()
|
||||||
RETURNS INTEGER
|
RETURNS INTEGER
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -18,7 +18,7 @@ AS $$
|
|||||||
$$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
$$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
||||||
|
|
||||||
-- Maximum zoom level usable with integer coordinates
|
-- Maximum zoom level usable with integer coordinates
|
||||||
CREATE OR REPLACE FUNCTION _CDB_MaxZoomLevel()
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_MaxZoomLevel()
|
||||||
RETURNS INTEGER
|
RETURNS INTEGER
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -31,7 +31,7 @@ $$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
|||||||
-- that may contain user tables are returned.
|
-- that may contain user tables are returned.
|
||||||
-- For each table, the regclass, schema name and table name are returned.
|
-- For each table, the regclass, schema name and table name are returned.
|
||||||
-- Scope: private.
|
-- Scope: private.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_UserTablesInSchema(schema_name text DEFAULT NULL)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_UserTablesInSchema(schema_name text DEFAULT NULL)
|
||||||
RETURNS TABLE(table_regclass REGCLASS, schema_name TEXT, table_name TEXT)
|
RETURNS TABLE(table_regclass REGCLASS, schema_name TEXT, table_name TEXT)
|
||||||
AS $$
|
AS $$
|
||||||
SELECT
|
SELECT
|
||||||
@@ -43,7 +43,7 @@ AS $$
|
|||||||
WHERE c.relkind = 'r'
|
WHERE c.relkind = 'r'
|
||||||
AND c.relname NOT IN ('cdb_tablemetadata', 'cdb_analysis_catalog', 'cdb_conf', 'spatial_ref_sys')
|
AND c.relname NOT IN ('cdb_tablemetadata', 'cdb_analysis_catalog', 'cdb_conf', 'spatial_ref_sys')
|
||||||
AND CASE WHEN schema_name IS NULL
|
AND CASE WHEN schema_name IS NULL
|
||||||
THEN n.nspname NOT IN ('pg_catalog', 'information_schema', 'topology', 'cartodb')
|
THEN n.nspname NOT IN ('pg_catalog', 'information_schema', 'topology', '@extschema@')
|
||||||
ELSE n.nspname = schema_name
|
ELSE n.nspname = schema_name
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE 'sql' STABLE PARALLEL SAFE;
|
$$ LANGUAGE 'sql' STABLE PARALLEL SAFE;
|
||||||
@@ -51,7 +51,7 @@ $$ LANGUAGE 'sql' STABLE PARALLEL SAFE;
|
|||||||
-- Pattern that can be used to detect overview tables and Extract
|
-- Pattern that can be used to detect overview tables and Extract
|
||||||
-- the intended zoom level from the table name.
|
-- the intended zoom level from the table name.
|
||||||
-- Scope: private.
|
-- Scope: private.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_OverviewTableDiscriminator()
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_OverviewTableDiscriminator()
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -63,18 +63,18 @@ $$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
|||||||
|
|
||||||
-- Pattern matched by the overview tables of a given base table name.
|
-- Pattern matched by the overview tables of a given base table name.
|
||||||
-- Scope: private.
|
-- Scope: private.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_OverviewTablePattern(base_table TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_OverviewTablePattern(base_table TEXT)
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
RETURN _CDB_OverviewTableDiscriminator() || base_table;
|
RETURN @extschema@._CDB_OverviewTableDiscriminator() || base_table;
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
$$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
||||||
-- tablename SIMILAR TO _CDB_OverviewTablePattern(base_table)
|
-- tablename SIMILAR TO _CDB_OverviewTablePattern(base_table)
|
||||||
|
|
||||||
-- Name of an overview table, given the base table name and the Z level
|
-- Name of an overview table, given the base table name and the Z level
|
||||||
-- Scope: private.
|
-- Scope: private.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_OverviewTableName(base_table TEXT, z INTEGER)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_OverviewTableName(base_table TEXT, z INTEGER)
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -84,39 +84,39 @@ $$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
|||||||
|
|
||||||
-- Condition to check if a tabla is an overview table of some base table
|
-- Condition to check if a tabla is an overview table of some base table
|
||||||
-- Scope: private.
|
-- Scope: private.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_IsOverviewTableOf(base_table TEXT, otable TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_IsOverviewTableOf(base_table TEXT, otable TEXT)
|
||||||
RETURNS BOOLEAN
|
RETURNS BOOLEAN
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
RETURN otable SIMILAR TO _CDB_OverviewTablePattern(base_table);
|
RETURN otable SIMILAR TO @extschema@._CDB_OverviewTablePattern(base_table);
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
$$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
||||||
|
|
||||||
-- Extract the Z level from an overview table name
|
-- Extract the Z level from an overview table name
|
||||||
-- Scope: private.
|
-- Scope: private.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_OverviewTableZ(otable TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_OverviewTableZ(otable TEXT)
|
||||||
RETURNS INTEGER
|
RETURNS INTEGER
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
RETURN substring(otable from _CDB_OverviewTableDiscriminator())::integer;
|
RETURN substring(otable from @extschema@._CDB_OverviewTableDiscriminator())::integer;
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
$$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
||||||
|
|
||||||
-- Name of the base table corresponding to an overview table
|
-- Name of the base table corresponding to an overview table
|
||||||
-- Scope: private.
|
-- Scope: private.
|
||||||
CREATE OR REPLACE FUNCTION _CDB_OverviewBaseTableName(overview_table TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_OverviewBaseTableName(overview_table TEXT)
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
IF _CDB_OverviewTableZ(overview_table) IS NULL THEN
|
IF @extschema@._CDB_OverviewTableZ(overview_table) IS NULL THEN
|
||||||
RETURN overview_table;
|
RETURN overview_table;
|
||||||
ELSE
|
ELSE
|
||||||
RETURN regexp_replace(overview_table, _CDB_OverviewTableDiscriminator(), '');
|
RETURN regexp_replace(overview_table, @extschema@._CDB_OverviewTableDiscriminator(), '');
|
||||||
END IF;
|
END IF;
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
$$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION _CDB_OverviewBaseTable(overview_table REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_OverviewBaseTable(overview_table REGCLASS)
|
||||||
RETURNS REGCLASS
|
RETURNS REGCLASS
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -125,8 +125,8 @@ AS $$
|
|||||||
base_name TEXT;
|
base_name TEXT;
|
||||||
base_table REGCLASS;
|
base_table REGCLASS;
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT * FROM _cdb_split_table_name(overview_table) INTO schema_name, table_name;
|
SELECT * FROM @extschema@._cdb_split_table_name(overview_table) INTO schema_name, table_name;
|
||||||
base_name := _CDB_OverviewBaseTableName(table_name);
|
base_name := @extschema@._CDB_OverviewBaseTableName(table_name);
|
||||||
IF base_name != table_name THEN
|
IF base_name != table_name THEN
|
||||||
base_table := Format('%I.%I', schema_name, base_name)::regclass;
|
base_table := Format('%I.%I', schema_name, base_name)::regclass;
|
||||||
ELSE
|
ELSE
|
||||||
@@ -142,7 +142,7 @@ $$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
|||||||
-- reloid: oid of the table.
|
-- reloid: oid of the table.
|
||||||
-- Return (schema_name, table_name)
|
-- Return (schema_name, table_name)
|
||||||
-- note that returned names will be quoted if necessary
|
-- note that returned names will be quoted if necessary
|
||||||
CREATE OR REPLACE FUNCTION _cdb_split_table_name(reloid REGCLASS, OUT schema_name TEXT, OUT table_name TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@._cdb_split_table_name(reloid REGCLASS, OUT schema_name TEXT, OUT table_name TEXT)
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT n.nspname, c.relname
|
SELECT n.nspname, c.relname
|
||||||
@@ -158,7 +158,7 @@ $$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
|
|||||||
-- reloid: oid of the table.
|
-- reloid: oid of the table.
|
||||||
-- Return (schema_name, table_name)
|
-- Return (schema_name, table_name)
|
||||||
-- note that returned names will be quoted if necessary
|
-- note that returned names will be quoted if necessary
|
||||||
CREATE OR REPLACE FUNCTION _cdb_schema_name(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@._cdb_schema_name(reloid REGCLASS)
|
||||||
RETURNS TEXT
|
RETURNS TEXT
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
-- @param breaks The number of bins you want to find.
|
-- @param breaks The number of bins you want to find.
|
||||||
--
|
--
|
||||||
--
|
--
|
||||||
CREATE OR REPLACE FUNCTION CDB_QuantileBins(in_array numeric[], breaks int)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_QuantileBins(in_array numeric[], breaks int)
|
||||||
RETURNS numeric[]
|
RETURNS numeric[]
|
||||||
AS $$
|
AS $$
|
||||||
SELECT
|
SELECT
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
-- Regexp curtesy of Hubert Lubaczewski (depesz)
|
-- Regexp curtesy of Hubert Lubaczewski (depesz)
|
||||||
-- Implemented in plpython for performance reasons
|
-- Implemented in plpython for performance reasons
|
||||||
--
|
--
|
||||||
CREATE OR REPLACE FUNCTION CDB_QueryStatements(query text)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_QueryStatements(query text)
|
||||||
RETURNS SETOF TEXT AS $$
|
RETURNS SETOF TEXT AS $$
|
||||||
import re
|
import re
|
||||||
pat = re.compile( r'''((?:[^'"$;]+|"[^"]*"|'[^']*'|(\$[^$]*\$).*?\2)+)''', re.DOTALL )
|
pat = re.compile( r'''((?:[^'"$;]+|"[^"]*"|'[^']*'|(\$[^$]*\$).*?\2)+)''', re.DOTALL )
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
--
|
--
|
||||||
-- Requires PostgreSQL 9.x+
|
-- Requires PostgreSQL 9.x+
|
||||||
--
|
--
|
||||||
CREATE OR REPLACE FUNCTION CDB_QueryTablesText(query text)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_QueryTablesText(query text)
|
||||||
RETURNS text[]
|
RETURNS text[]
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -14,7 +14,7 @@ BEGIN
|
|||||||
|
|
||||||
tables := '{}';
|
tables := '{}';
|
||||||
|
|
||||||
FOR rec IN SELECT CDB_QueryStatements(query) q LOOP
|
FOR rec IN SELECT @extschema@.CDB_QueryStatements(query) q LOOP
|
||||||
BEGIN
|
BEGIN
|
||||||
EXECUTE 'EXPLAIN (FORMAT XML, VERBOSE) ' || rec.q INTO STRICT exp;
|
EXECUTE 'EXPLAIN (FORMAT XML, VERBOSE) ' || rec.q INTO STRICT exp;
|
||||||
EXCEPTION WHEN syntax_error THEN
|
EXCEPTION WHEN syntax_error THEN
|
||||||
@@ -66,10 +66,10 @@ $$ LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL UNSAFE;
|
|||||||
|
|
||||||
-- Keep CDB_QueryTables with same signature for backwards compatibility.
|
-- Keep CDB_QueryTables with same signature for backwards compatibility.
|
||||||
-- It should probably be removed in the future.
|
-- It should probably be removed in the future.
|
||||||
CREATE OR REPLACE FUNCTION CDB_QueryTables(query text)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_QueryTables(query text)
|
||||||
RETURNS name[]
|
RETURNS name[]
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
RETURN CDB_QueryTablesText(query)::name[];
|
RETURN @extschema@.CDB_QueryTablesText(query)::name[];
|
||||||
END
|
END
|
||||||
$$ LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL UNSAFE;
|
$$ LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL UNSAFE;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
CREATE OR REPLACE FUNCTION cartodb._CDB_total_relation_size(_schema_name TEXT, _table_name TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_total_relation_size(_schema_name TEXT, _table_name TEXT)
|
||||||
RETURNS bigint AS
|
RETURNS bigint AS
|
||||||
$$
|
$$
|
||||||
DECLARE relation_size bigint := 0;
|
DECLARE relation_size bigint := 0;
|
||||||
@@ -7,7 +7,7 @@ BEGIN
|
|||||||
SELECT pg_total_relation_size(format('"%s"."%s"', _schema_name, _table_name)) INTO relation_size;
|
SELECT pg_total_relation_size(format('"%s"."%s"', _schema_name, _table_name)) INTO relation_size;
|
||||||
EXCEPTION
|
EXCEPTION
|
||||||
WHEN undefined_table OR OTHERS THEN
|
WHEN undefined_table OR OTHERS THEN
|
||||||
RAISE NOTICE 'cartodb._CDB_total_relation_size(''%'', ''%'') caught error: % (%)', _schema_name, _table_name, SQLERRM, SQLSTATE;
|
RAISE NOTICE '@extschema@._CDB_total_relation_size(''%'', ''%'') caught error: % (%)', _schema_name, _table_name, SQLERRM, SQLSTATE;
|
||||||
END;
|
END;
|
||||||
RETURN relation_size;
|
RETURN relation_size;
|
||||||
END;
|
END;
|
||||||
@@ -15,7 +15,7 @@ $$
|
|||||||
LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
-- Return the estimated size of user data. Used for quota checking.
|
-- Return the estimated size of user data. Used for quota checking.
|
||||||
CREATE OR REPLACE FUNCTION CDB_UserDataSize(schema_name TEXT)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_UserDataSize(schema_name TEXT)
|
||||||
RETURNS bigint AS
|
RETURNS bigint AS
|
||||||
$$
|
$$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -26,20 +26,20 @@ BEGIN
|
|||||||
WHERE o_table_schema = schema_name AND o_table_catalog = current_database()
|
WHERE o_table_schema = schema_name AND o_table_catalog = current_database()
|
||||||
),
|
),
|
||||||
user_tables AS (
|
user_tables AS (
|
||||||
SELECT table_name FROM _CDB_NonAnalysisTablesInSchema(schema_name)
|
SELECT table_name FROM @extschema@._CDB_NonAnalysisTablesInSchema(schema_name)
|
||||||
),
|
),
|
||||||
table_cat AS (
|
table_cat AS (
|
||||||
SELECT
|
SELECT
|
||||||
table_name,
|
table_name,
|
||||||
(
|
(
|
||||||
EXISTS(select * from raster_tables where o_table_name = table_name)
|
EXISTS(select * from raster_tables where o_table_name = table_name)
|
||||||
OR table_name SIMILAR TO _CDB_OverviewTableDiscriminator() || '[\w\d]*'
|
OR table_name SIMILAR TO @extschema@._CDB_OverviewTableDiscriminator() || '[\w\d]*'
|
||||||
) AS is_overview,
|
) AS is_overview,
|
||||||
EXISTS(SELECT * FROM raster_tables WHERE r_table_name = table_name) AS is_raster
|
EXISTS(SELECT * FROM raster_tables WHERE r_table_name = table_name) AS is_raster
|
||||||
FROM user_tables
|
FROM user_tables
|
||||||
),
|
),
|
||||||
sizes AS (
|
sizes AS (
|
||||||
SELECT COALESCE(INT8(SUM(cartodb._CDB_total_relation_size(schema_name, table_name)))) table_size,
|
SELECT COALESCE(INT8(SUM(@extschema@._CDB_total_relation_size(schema_name, table_name)))) table_size,
|
||||||
CASE
|
CASE
|
||||||
WHEN is_overview THEN 0
|
WHEN is_overview THEN 0
|
||||||
WHEN is_raster THEN 1
|
WHEN is_raster THEN 1
|
||||||
@@ -60,15 +60,15 @@ LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
|||||||
|
|
||||||
-- Return the estimated size of user data. Used for quota checking.
|
-- Return the estimated size of user data. Used for quota checking.
|
||||||
-- Implicit schema version for backwards compatibility
|
-- Implicit schema version for backwards compatibility
|
||||||
CREATE OR REPLACE FUNCTION CDB_UserDataSize()
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_UserDataSize()
|
||||||
RETURNS bigint AS
|
RETURNS bigint AS
|
||||||
$$
|
$$
|
||||||
SELECT CDB_UserDataSize('public');
|
SELECT @extschema@.CDB_UserDataSize('public');
|
||||||
$$
|
$$
|
||||||
LANGUAGE 'sql' VOLATILE PARALLEL UNSAFE;
|
LANGUAGE 'sql' VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
-- Triggers cannot have declared arguments: pbfact float8, qmax int8, schema_name text
|
-- Triggers cannot have declared arguments: pbfact float8, qmax int8, schema_name text
|
||||||
CREATE OR REPLACE FUNCTION CDB_CheckQuota()
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_CheckQuota()
|
||||||
RETURNS trigger AS
|
RETURNS trigger AS
|
||||||
$$
|
$$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -80,7 +80,7 @@ DECLARE
|
|||||||
BEGIN
|
BEGIN
|
||||||
IF TG_NARGS = 3 THEN
|
IF TG_NARGS = 3 THEN
|
||||||
schema_name := TG_ARGV[2];
|
schema_name := TG_ARGV[2];
|
||||||
IF cartodb.schema_exists(schema_name) = false THEN
|
IF @extschema@.schema_exists(schema_name) = false THEN
|
||||||
RAISE EXCEPTION 'Invalid schema name "%"', schema_name;
|
RAISE EXCEPTION 'Invalid schema name "%"', schema_name;
|
||||||
END IF;
|
END IF;
|
||||||
ELSE
|
ELSE
|
||||||
@@ -111,7 +111,7 @@ BEGIN
|
|||||||
RETURN NEW;
|
RETURN NEW;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
SELECT public.CDB_UserDataSize(schema_name) INTO quota;
|
SELECT @extschema@.CDB_UserDataSize(schema_name) INTO quota;
|
||||||
IF quota > qmax THEN
|
IF quota > qmax THEN
|
||||||
RAISE EXCEPTION 'Quota exceeded by %KB', (quota-qmax)/1024;
|
RAISE EXCEPTION 'Quota exceeded by %KB', (quota-qmax)/1024;
|
||||||
ELSE RAISE DEBUG 'User quota in bytes: % < % (max allowed)', quota, qmax;
|
ELSE RAISE DEBUG 'User quota in bytes: % < % (max allowed)', quota, qmax;
|
||||||
@@ -124,13 +124,13 @@ $$
|
|||||||
LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE;
|
||||||
|
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION CDB_SetUserQuotaInBytes(schema_name text, bytes int8)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_SetUserQuotaInBytes(schema_name text, bytes int8)
|
||||||
RETURNS int8 AS
|
RETURNS int8 AS
|
||||||
$$
|
$$
|
||||||
DECLARE
|
DECLARE
|
||||||
sql text;
|
sql text;
|
||||||
BEGIN
|
BEGIN
|
||||||
IF cartodb.schema_exists(schema_name::text) = false THEN
|
IF @extschema@.schema_exists(schema_name::text) = false THEN
|
||||||
RAISE EXCEPTION 'Invalid schema name "%"', schema_name::text;
|
RAISE EXCEPTION 'Invalid schema name "%"', schema_name::text;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
@@ -145,11 +145,11 @@ $$
|
|||||||
LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL UNSAFE;
|
LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL UNSAFE;
|
||||||
|
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION CDB_SetUserQuotaInBytes(bytes int8)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_SetUserQuotaInBytes(bytes int8)
|
||||||
RETURNS int8 AS
|
RETURNS int8 AS
|
||||||
$$
|
$$
|
||||||
BEGIN
|
BEGIN
|
||||||
return public.CDB_SetUserQuotaInBytes('public', bytes);
|
return @extschema@.CDB_SetUserQuotaInBytes('public', bytes);
|
||||||
END;
|
END;
|
||||||
$$
|
$$
|
||||||
LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL UNSAFE;
|
LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL UNSAFE;
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
--
|
--
|
||||||
--
|
--
|
||||||
-- }{
|
-- }{
|
||||||
CREATE OR REPLACE FUNCTION CDB_RandomTids(in_table regclass, in_nsamples integer)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_RandomTids(in_table regclass, in_nsamples integer)
|
||||||
RETURNS tid[]
|
RETURNS tid[]
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
-- In older versions of the extension, CDB_RectangleGrid had a different signature
|
-- In older versions of the extension, CDB_RectangleGrid had a different signature
|
||||||
DROP FUNCTION IF EXISTS cartodb.CDB_RectangleGrid(GEOMETRY, FLOAT8, FLOAT8, GEOMETRY);
|
DROP FUNCTION IF EXISTS @extschema@.CDB_RectangleGrid(GEOMETRY, FLOAT8, FLOAT8, GEOMETRY);
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Fill given extent with a rectangular coverage
|
-- Fill given extent with a rectangular coverage
|
||||||
@@ -22,7 +22,7 @@ DROP FUNCTION IF EXISTS cartodb.CDB_RectangleGrid(GEOMETRY, FLOAT8, FLOAT8, GEOM
|
|||||||
-- if the grid requires more cells to cover the extent
|
-- if the grid requires more cells to cover the extent
|
||||||
-- and exception will occur.
|
-- and exception will occur.
|
||||||
--
|
--
|
||||||
CREATE OR REPLACE FUNCTION CDB_RectangleGrid(ext GEOMETRY, width FLOAT8, height FLOAT8, origin GEOMETRY DEFAULT NULL, maxcells INTEGER DEFAULT 512*512)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_RectangleGrid(ext GEOMETRY, width FLOAT8, height FLOAT8, origin GEOMETRY DEFAULT NULL, maxcells INTEGER DEFAULT 512*512)
|
||||||
RETURNS SETOF GEOMETRY
|
RETURNS SETOF GEOMETRY
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -44,17 +44,17 @@ DECLARE
|
|||||||
srid INTEGER;
|
srid INTEGER;
|
||||||
BEGIN
|
BEGIN
|
||||||
|
|
||||||
srid := ST_SRID(ext);
|
srid := @postgisschema@.ST_SRID(ext);
|
||||||
|
|
||||||
xoff := 0;
|
xoff := 0;
|
||||||
yoff := 0;
|
yoff := 0;
|
||||||
|
|
||||||
IF origin IS NOT NULL THEN
|
IF origin IS NOT NULL THEN
|
||||||
IF ST_SRID(origin) != srid THEN
|
IF @postgisschema@.ST_SRID(origin) != srid THEN
|
||||||
RAISE EXCEPTION 'SRID mismatch between extent (%) and origin (%)', srid, ST_SRID(origin);
|
RAISE EXCEPTION 'SRID mismatch between extent (%) and origin (%)', srid, ST_SRID(origin);
|
||||||
END IF;
|
END IF;
|
||||||
xoff := ST_X(origin);
|
xoff := @postgisschema@.ST_X(origin);
|
||||||
yoff := ST_Y(origin);
|
yoff := @postgisschema@.ST_Y(origin);
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
--RAISE DEBUG 'X offset: %', xoff;
|
--RAISE DEBUG 'X offset: %', xoff;
|
||||||
@@ -72,11 +72,11 @@ BEGIN
|
|||||||
vstep := height;
|
vstep := height;
|
||||||
|
|
||||||
-- Tweak horizontal start on hstep grid from origin
|
-- Tweak horizontal start on hstep grid from origin
|
||||||
hstart := xoff + ceil((ST_XMin(ext)-xoff)/hstep)*hstep;
|
hstart := xoff + ceil((@postgisschema@.ST_XMin(ext)-xoff)/hstep)*hstep;
|
||||||
--RAISE DEBUG 'hstart: %', hstart;
|
--RAISE DEBUG 'hstart: %', hstart;
|
||||||
|
|
||||||
-- Tweak vertical start on vstep grid from origin
|
-- Tweak vertical start on vstep grid from origin
|
||||||
vstart := yoff + ceil((ST_Ymin(ext)-yoff)/vstep)*vstep;
|
vstart := yoff + ceil((@postgisschema@.ST_Ymin(ext)-yoff)/vstep)*vstep;
|
||||||
--RAISE DEBUG 'vstart: %', vstart;
|
--RAISE DEBUG 'vstart: %', vstart;
|
||||||
|
|
||||||
hend := ST_XMax(ext);
|
hend := ST_XMax(ext);
|
||||||
@@ -94,10 +94,10 @@ BEGIN
|
|||||||
x := hstart;
|
x := hstart;
|
||||||
WHILE x < hend LOOP -- over X
|
WHILE x < hend LOOP -- over X
|
||||||
y := vstart;
|
y := vstart;
|
||||||
h := ST_MakeEnvelope(x-hw, y-hh, x+hw, y+hh, srid);
|
h := @postgisschema@.ST_MakeEnvelope(x-hw, y-hh, x+hw, y+hh, srid);
|
||||||
WHILE y < vend LOOP -- over Y
|
WHILE y < vend LOOP -- over Y
|
||||||
RETURN NEXT h;
|
RETURN NEXT h;
|
||||||
h := ST_Translate(h, 0, vstep);
|
h := @postgisschema@.ST_Translate(h, 0, vstep);
|
||||||
y := yoff + round(((y + vstep)-yoff)/ygrd)*ygrd; -- round to grid
|
y := yoff + round(((y + vstep)-yoff)/ygrd)*ygrd; -- round to grid
|
||||||
END LOOP;
|
END LOOP;
|
||||||
x := xoff + round(((x + hstep)-xoff)/xgrd)*xgrd; -- round to grid
|
x := xoff + round(((x + hstep)-xoff)/xgrd)*xgrd; -- round to grid
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
---- Make sure 'cartodb' is in database search path
|
---- Make sure '@extschema@' is in database search path
|
||||||
DO
|
DO
|
||||||
$$
|
$$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -8,13 +8,13 @@ BEGIN
|
|||||||
SELECT reset_val INTO var_cur_search_path
|
SELECT reset_val INTO var_cur_search_path
|
||||||
FROM pg_settings WHERE name = 'search_path';
|
FROM pg_settings WHERE name = 'search_path';
|
||||||
|
|
||||||
IF var_cur_search_path LIKE '%cartodb%' THEN
|
IF var_cur_search_path LIKE '%@extschema@%' THEN
|
||||||
RAISE DEBUG '"cartodb" already in database search_path';
|
RAISE DEBUG '"@extschema@" already in database search_path';
|
||||||
ELSE
|
ELSE
|
||||||
var_cur_search_path := var_cur_search_path || ', "cartodb"';
|
var_cur_search_path := var_cur_search_path || ', "@extschema@"';
|
||||||
EXECUTE 'ALTER DATABASE ' || quote_ident(current_database()) ||
|
EXECUTE 'ALTER DATABASE ' || quote_ident(current_database()) ||
|
||||||
' SET search_path = ' || var_cur_search_path;
|
' SET search_path = ' || var_cur_search_path;
|
||||||
RAISE DEBUG '"cartodb" has been added to end of database search_path';
|
RAISE DEBUG '"@extschema@" has been added to end of database search_path';
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- Reset search_path
|
-- Reset search_path
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
--
|
--
|
||||||
|
|
||||||
-- Calculate kurtosis
|
-- Calculate kurtosis
|
||||||
CREATE OR REPLACE FUNCTION CDB_Kurtosis ( in_array NUMERIC[] ) RETURNS NUMERIC as $$
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_Kurtosis ( in_array NUMERIC[] ) RETURNS NUMERIC as $$
|
||||||
DECLARE
|
DECLARE
|
||||||
a numeric;
|
a numeric;
|
||||||
c numeric;
|
c numeric;
|
||||||
@@ -32,7 +32,7 @@ END;
|
|||||||
$$ language plpgsql IMMUTABLE STRICT PARALLEL SAFE;
|
$$ language plpgsql IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
-- Calculate skewness
|
-- Calculate skewness
|
||||||
CREATE OR REPLACE FUNCTION CDB_Skewness ( in_array NUMERIC[] ) RETURNS NUMERIC as $$
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_Skewness ( in_array NUMERIC[] ) RETURNS NUMERIC as $$
|
||||||
DECLARE
|
DECLARE
|
||||||
a numeric;
|
a numeric;
|
||||||
c numeric;
|
c numeric;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
-- Convert string to date
|
-- Convert string to date
|
||||||
--
|
--
|
||||||
DROP FUNCTION IF EXISTS CDB_StringToDate(character varying);
|
DROP FUNCTION IF EXISTS @extschema@.CDB_StringToDate(character varying);
|
||||||
CREATE OR REPLACE FUNCTION CDB_StringToDate(input character varying)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_StringToDate(input character varying)
|
||||||
RETURNS TIMESTAMP AS $$
|
RETURNS TIMESTAMP AS $$
|
||||||
DECLARE output TIMESTAMP;
|
DECLARE output TIMESTAMP;
|
||||||
BEGIN
|
BEGIN
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
-- Function returning indexes for a table
|
-- Function returning indexes for a table
|
||||||
CREATE OR REPLACE FUNCTION CDB_TableIndexes(REGCLASS)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_TableIndexes(REGCLASS)
|
||||||
RETURNS TABLE(index_name name, index_unique bool, index_primary bool, index_keys text array)
|
RETURNS TABLE(index_name name, index_unique bool, index_primary bool, index_keys text array)
|
||||||
AS $$
|
AS $$
|
||||||
|
|
||||||
@@ -24,4 +24,4 @@ $$ LANGUAGE SQL STABLE PARALLEL SAFE;
|
|||||||
|
|
||||||
-- This is to migrate from pre-0.2.0 version
|
-- This is to migrate from pre-0.2.0 version
|
||||||
-- See http://github.com/CartoDB/cartodb-postgresql/issues/36
|
-- See http://github.com/CartoDB/cartodb-postgresql/issues/36
|
||||||
GRANT EXECUTE ON FUNCTION CDB_TableIndexes(REGCLASS) TO public;
|
GRANT EXECUTE ON FUNCTION @extschema@.CDB_TableIndexes(REGCLASS) TO public;
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS
|
CREATE TABLE IF NOT EXISTS
|
||||||
public.CDB_TableMetadata (
|
@extschema@.CDB_TableMetadata (
|
||||||
tabname regclass not null primary key,
|
tabname regclass not null primary key,
|
||||||
updated_at timestamp with time zone not null default now()
|
updated_at timestamp with time zone not null default now()
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE OR REPLACE VIEW public.CDB_TableMetadata_Text AS
|
CREATE OR REPLACE VIEW @extschema@.CDB_TableMetadata_Text AS
|
||||||
SELECT FORMAT('%I.%I', n.nspname::text, c.relname::text) tabname, updated_at
|
SELECT FORMAT('%I.%I', n.nspname::text, c.relname::text) tabname, updated_at
|
||||||
FROM public.CDB_TableMetadata m JOIN pg_catalog.pg_class c ON m.tabname::oid = c.oid
|
FROM @extschema@.CDB_TableMetadata m JOIN pg_catalog.pg_class c ON m.tabname::oid = c.oid
|
||||||
LEFT JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid;
|
LEFT JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid;
|
||||||
|
|
||||||
-- No one can see this
|
-- No one can see this
|
||||||
-- Updates are only possible trough the security definer trigger
|
-- Updates are only possible trough the security definer trigger
|
||||||
-- GRANT SELECT ON public.CDB_TableMetadata TO public;
|
-- GRANT SELECT ON @extschema@.CDB_TableMetadata TO public;
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Trigger logging updated_at in the CDB_TableMetadata
|
-- Trigger logging updated_at in the CDB_TableMetadata
|
||||||
@@ -27,17 +27,17 @@ CREATE OR REPLACE VIEW public.CDB_TableMetadata_Text AS
|
|||||||
--
|
--
|
||||||
-- NOTE: _never_ attach to CDB_TableMetadata ...
|
-- NOTE: _never_ attach to CDB_TableMetadata ...
|
||||||
--
|
--
|
||||||
CREATE OR REPLACE FUNCTION CDB_TableMetadata_Trigger()
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_TableMetadata_Trigger()
|
||||||
RETURNS trigger AS
|
RETURNS trigger AS
|
||||||
$$
|
$$
|
||||||
BEGIN
|
BEGIN
|
||||||
-- Guard against infinite loop
|
-- Guard against infinite loop
|
||||||
IF TG_RELID = 'public.CDB_TableMetadata'::regclass::oid THEN
|
IF TG_RELID = '@extschema@.CDB_TableMetadata'::regclass::oid THEN
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- Cleanup stale entries
|
-- Cleanup stale entries
|
||||||
DELETE FROM public.CDB_TableMetadata
|
DELETE FROM @extschema@.CDB_TableMetadata
|
||||||
WHERE NOT EXISTS (
|
WHERE NOT EXISTS (
|
||||||
SELECT oid FROM pg_class WHERE oid = tabname
|
SELECT oid FROM pg_class WHERE oid = tabname
|
||||||
);
|
);
|
||||||
@@ -45,11 +45,11 @@ BEGIN
|
|||||||
WITH nv as (
|
WITH nv as (
|
||||||
SELECT TG_RELID as tabname, NOW() as t
|
SELECT TG_RELID as tabname, NOW() as t
|
||||||
), updated as (
|
), updated as (
|
||||||
UPDATE public.CDB_TableMetadata x SET updated_at = nv.t
|
UPDATE @extschema@.CDB_TableMetadata x SET updated_at = nv.t
|
||||||
FROM nv WHERE x.tabname = nv.tabname
|
FROM nv WHERE x.tabname = nv.tabname
|
||||||
RETURNING x.tabname
|
RETURNING x.tabname
|
||||||
)
|
)
|
||||||
INSERT INTO public.CDB_TableMetadata SELECT nv.*
|
INSERT INTO @extschema@.CDB_TableMetadata SELECT nv.*
|
||||||
FROM nv LEFT JOIN updated USING(tabname)
|
FROM nv LEFT JOIN updated USING(tabname)
|
||||||
WHERE updated.tabname IS NULL;
|
WHERE updated.tabname IS NULL;
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
|||||||
-- Trigger invalidating varnish whenever CDB_TableMetadata
|
-- Trigger invalidating varnish whenever CDB_TableMetadata
|
||||||
-- record change.
|
-- record change.
|
||||||
--
|
--
|
||||||
CREATE OR REPLACE FUNCTION _CDB_TableMetadata_Updated()
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_TableMetadata_Updated()
|
||||||
RETURNS trigger AS
|
RETURNS trigger AS
|
||||||
$$
|
$$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -93,14 +93,14 @@ BEGIN
|
|||||||
--
|
--
|
||||||
|
|
||||||
-- Call the first varnish invalidation function owned
|
-- Call the first varnish invalidation function owned
|
||||||
-- by a superuser found in cartodb or public schema
|
-- by a superuser found in @extschema@ or public schema
|
||||||
-- (in that order)
|
-- (in that order)
|
||||||
found := false;
|
found := false;
|
||||||
FOR rec IN SELECT u.usesuper, u.usename, n.nspname, p.proname
|
FOR rec IN SELECT u.usesuper, u.usename, n.nspname, p.proname
|
||||||
FROM pg_proc p, pg_namespace n, pg_user u
|
FROM pg_proc p, pg_namespace n, pg_user u
|
||||||
WHERE p.proname = 'cdb_invalidate_varnish'
|
WHERE p.proname = 'cdb_invalidate_varnish'
|
||||||
AND p.pronamespace = n.oid
|
AND p.pronamespace = n.oid
|
||||||
AND n.nspname IN ('public', 'cartodb')
|
AND n.nspname IN ('public', '@extschema@')
|
||||||
AND u.usesysid = p.proowner
|
AND u.usesysid = p.proowner
|
||||||
AND u.usesuper
|
AND u.usesuper
|
||||||
ORDER BY n.nspname
|
ORDER BY n.nspname
|
||||||
@@ -119,26 +119,26 @@ END;
|
|||||||
$$
|
$$
|
||||||
LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
|
||||||
|
|
||||||
DROP TRIGGER IF EXISTS table_modified ON public.CDB_TableMetadata;
|
DROP TRIGGER IF EXISTS table_modified ON @extschema@.CDB_TableMetadata;
|
||||||
-- NOTE: on DELETE we would be unable to convert the table
|
-- NOTE: on DELETE we would be unable to convert the table
|
||||||
-- oid (regclass) to its name
|
-- oid (regclass) to its name
|
||||||
CREATE TRIGGER table_modified AFTER INSERT OR UPDATE
|
CREATE TRIGGER table_modified AFTER INSERT OR UPDATE
|
||||||
ON public.CDB_TableMetadata FOR EACH ROW EXECUTE PROCEDURE
|
ON @extschema@.CDB_TableMetadata FOR EACH ROW EXECUTE PROCEDURE
|
||||||
_CDB_TableMetadata_Updated();
|
_CDB_TableMetadata_Updated();
|
||||||
|
|
||||||
|
|
||||||
-- similar to TOUCH(1) in unix filesystems but for table in cdb_tablemetadata
|
-- similar to TOUCH(1) in unix filesystems but for table in cdb_tablemetadata
|
||||||
CREATE OR REPLACE FUNCTION public.CDB_TableMetadataTouch(tablename regclass)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_TableMetadataTouch(tablename regclass)
|
||||||
RETURNS void AS
|
RETURNS void AS
|
||||||
$$
|
$$
|
||||||
BEGIN
|
BEGIN
|
||||||
WITH upsert AS (
|
WITH upsert AS (
|
||||||
UPDATE public.cdb_tablemetadata
|
UPDATE @extschema@.cdb_tablemetadata
|
||||||
SET updated_at = NOW()
|
SET updated_at = NOW()
|
||||||
WHERE tabname = tablename
|
WHERE tabname = tablename
|
||||||
RETURNING *
|
RETURNING *
|
||||||
)
|
)
|
||||||
INSERT INTO public.cdb_tablemetadata (tabname, updated_at)
|
INSERT INTO @extschema@.cdb_tablemetadata (tabname, updated_at)
|
||||||
SELECT tablename, NOW()
|
SELECT tablename, NOW()
|
||||||
WHERE NOT EXISTS (SELECT * FROM upsert);
|
WHERE NOT EXISTS (SELECT * FROM upsert);
|
||||||
END;
|
END;
|
||||||
|
|||||||
@@ -5,19 +5,19 @@
|
|||||||
-- for web mercator by "clipping" anything outside to the valid
|
-- for web mercator by "clipping" anything outside to the valid
|
||||||
-- range.
|
-- range.
|
||||||
--
|
--
|
||||||
CREATE OR REPLACE FUNCTION CDB_TransformToWebmercator(geom geometry)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_TransformToWebmercator(geom @postgisschema@.geometry)
|
||||||
RETURNS geometry
|
RETURNS @postgisschema@.geometry
|
||||||
AS
|
AS
|
||||||
$$
|
$$
|
||||||
DECLARE
|
DECLARE
|
||||||
valid_extent GEOMETRY;
|
valid_extent @postgisschema@.GEOMETRY;
|
||||||
latlon_input GEOMETRY;
|
latlon_input @postgisschema@.GEOMETRY;
|
||||||
clipped_input GEOMETRY;
|
clipped_input @postgisschema@.GEOMETRY;
|
||||||
to_webmercator GEOMETRY;
|
to_webmercator @postgisschema@.GEOMETRY;
|
||||||
ret GEOMETRY;
|
ret @postgisschema@.GEOMETRY;
|
||||||
BEGIN
|
BEGIN
|
||||||
|
|
||||||
IF ST_Srid(geom) = 3857 THEN
|
IF @postgisschema@.ST_Srid(geom) = 3857 THEN
|
||||||
RETURN geom;
|
RETURN geom;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
@@ -27,18 +27,18 @@ BEGIN
|
|||||||
-- to -85.0511 to 85.0511 but as long as proj
|
-- to -85.0511 to 85.0511 but as long as proj
|
||||||
-- does not complain we are happy
|
-- does not complain we are happy
|
||||||
--
|
--
|
||||||
valid_extent := ST_MakeEnvelope(-180, -89, 180, 89, 4326);
|
valid_extent := @postgisschema@.ST_MakeEnvelope(-180, -89, 180, 89, 4326);
|
||||||
|
|
||||||
-- Then we transform to WGS84 latlon, which is
|
-- Then we transform to WGS84 latlon, which is
|
||||||
-- where we have known coordinates for the clipping
|
-- where we have known coordinates for the clipping
|
||||||
--
|
--
|
||||||
latlon_input := ST_Transform(geom, 4326);
|
latlon_input := @postgisschema@.ST_Transform(geom, 4326);
|
||||||
|
|
||||||
-- Don't bother clipping if the geometry boundary doesn't
|
-- Don't bother clipping if the geometry boundary doesn't
|
||||||
-- go outside the valid extent.
|
-- go outside the valid extent.
|
||||||
IF latlon_input @ valid_extent THEN
|
IF latlon_input @ valid_extent THEN
|
||||||
BEGIN
|
BEGIN
|
||||||
RETURN ST_Transform(latlon_input, 3857);
|
RETURN @postgisschema@.ST_Transform(latlon_input, 3857);
|
||||||
EXCEPTION WHEN OTHERS THEN
|
EXCEPTION WHEN OTHERS THEN
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END;
|
END;
|
||||||
@@ -47,33 +47,33 @@ BEGIN
|
|||||||
-- Since we're going to use ST_Intersection on input
|
-- Since we're going to use ST_Intersection on input
|
||||||
-- we'd better ensure the input is valid
|
-- we'd better ensure the input is valid
|
||||||
-- TODO: only do this if the first ST_Intersection fails ?
|
-- TODO: only do this if the first ST_Intersection fails ?
|
||||||
IF ST_Dimension(geom) != 0 AND
|
IF @postgisschema@.ST_Dimension(geom) != 0 AND
|
||||||
-- See http://trac.osgeo.org/postgis/ticket/1719
|
-- See http://trac.osgeo.org/postgis/ticket/1719
|
||||||
GeometryType(geom) != 'GEOMETRYCOLLECTION'
|
@postgisschema@.GeometryType(geom) != 'GEOMETRYCOLLECTION'
|
||||||
THEN
|
THEN
|
||||||
BEGIN
|
BEGIN
|
||||||
latlon_input := ST_MakeValid(latlon_input);
|
latlon_input := @postgisschema@.ST_MakeValid(latlon_input);
|
||||||
EXCEPTION
|
EXCEPTION
|
||||||
WHEN OTHERS THEN
|
WHEN OTHERS THEN
|
||||||
-- See http://github.com/Vizzuality/cartodb/issues/931
|
-- See http://github.com/Vizzuality/cartodb/issues/931
|
||||||
RAISE WARNING 'Could not clean input geometry: %', SQLERRM;
|
RAISE WARNING 'Could not clean input geometry: %', SQLERRM;
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END;
|
END;
|
||||||
latlon_input := ST_CollectionExtract(latlon_input, ST_Dimension(geom)+1);
|
latlon_input := @postgisschema@.ST_CollectionExtract(latlon_input, ST_Dimension(geom)+1);
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- Then we clip, trying to retain the input type
|
-- Then we clip, trying to retain the input type
|
||||||
-- TODO: catch exceptions here too ?
|
-- TODO: catch exceptions here too ?
|
||||||
clipped_input := ST_Intersection(latlon_input, valid_extent);
|
clipped_input := @postgisschema@.ST_Intersection(latlon_input, valid_extent);
|
||||||
|
|
||||||
-- We transform to web mercator
|
-- We transform to web mercator
|
||||||
to_webmercator := ST_Transform(clipped_input, 3857);
|
to_webmercator := @postgisschema@.ST_Transform(clipped_input, 3857);
|
||||||
|
|
||||||
-- Finally we convert EMPTY to NULL
|
-- Finally we convert EMPTY to NULL
|
||||||
-- See https://github.com/Vizzuality/cartodb/issues/706
|
-- See https://github.com/Vizzuality/cartodb/issues/706
|
||||||
-- And retain "multi" status
|
-- And retain "multi" status
|
||||||
ret := CASE WHEN ST_IsEmpty(to_webmercator) THEN NULL::geometry
|
ret := CASE WHEN @postgisschema@.ST_IsEmpty(to_webmercator) THEN NULL::@postgisschema@.geometry
|
||||||
WHEN GeometryType(geom) LIKE 'MULTI%' THEN ST_Multi(to_webmercator)
|
WHEN @postgisschema@.GeometryType(geom) LIKE 'MULTI%' THEN @postgisschema@.ST_Multi(to_webmercator)
|
||||||
ELSE to_webmercator
|
ELSE to_webmercator
|
||||||
END;
|
END;
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
--
|
--
|
||||||
-- Currently accepted permissions are: 'public', 'private' or 'all'
|
-- Currently accepted permissions are: 'public', 'private' or 'all'
|
||||||
--
|
--
|
||||||
DROP FUNCTION IF EXISTS CDB_UserTables(text);
|
DROP FUNCTION IF EXISTS @extschema@.CDB_UserTables(text);
|
||||||
CREATE OR REPLACE FUNCTION CDB_UserTables(perm text DEFAULT 'all')
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_UserTables(perm text DEFAULT 'all')
|
||||||
RETURNS SETOF name
|
RETURNS SETOF name
|
||||||
AS $$
|
AS $$
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ FROM pg_class c
|
|||||||
JOIN pg_namespace n ON n.oid = c.relnamespace
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||||
WHERE c.relkind = 'r'
|
WHERE c.relkind = 'r'
|
||||||
AND c.relname NOT IN ('cdb_tablemetadata', 'cdb_analysis_catalog', 'cdb_conf', 'spatial_ref_sys')
|
AND c.relname NOT IN ('cdb_tablemetadata', 'cdb_analysis_catalog', 'cdb_conf', 'spatial_ref_sys')
|
||||||
AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'topology', 'cartodb')
|
AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'topology', '@extschema@')
|
||||||
AND CASE WHEN perm = 'public' THEN has_table_privilege('publicuser', c.oid, 'SELECT')
|
AND CASE WHEN perm = 'public' THEN has_table_privilege('publicuser', c.oid, 'SELECT')
|
||||||
WHEN perm = 'private' THEN has_table_privilege(current_user, c.oid, 'SELECT') AND NOT has_table_privilege('publicuser', c.oid, 'SELECT')
|
WHEN perm = 'private' THEN has_table_privilege(current_user, c.oid, 'SELECT') AND NOT has_table_privilege('publicuser', c.oid, 'SELECT')
|
||||||
WHEN perm = 'all' THEN has_table_privilege(current_user, c.oid, 'SELECT') OR has_table_privilege('publicuser', c.oid, 'SELECT')
|
WHEN perm = 'all' THEN has_table_privilege(current_user, c.oid, 'SELECT') OR has_table_privilege('publicuser', c.oid, 'SELECT')
|
||||||
@@ -25,4 +25,4 @@ $$ LANGUAGE 'sql' STABLE PARALLEL SAFE;
|
|||||||
|
|
||||||
-- This is to migrate from pre-0.2.0 version
|
-- This is to migrate from pre-0.2.0 version
|
||||||
-- See http://github.com/CartoDB/cartodb-postgresql/issues/36
|
-- See http://github.com/CartoDB/cartodb-postgresql/issues/36
|
||||||
GRANT EXECUTE ON FUNCTION CDB_UserTables(text) TO public;
|
GRANT EXECUTE ON FUNCTION @extschema@.CDB_UserTables(text) TO public;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
-- Returns the cartodb username of the current PostgreSQL session
|
-- Returns the cartodb username of the current PostgreSQL session
|
||||||
CREATE OR REPLACE FUNCTION cartodb.CDB_Username()
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_Username()
|
||||||
RETURNS text
|
RETURNS text
|
||||||
AS $$
|
AS $$
|
||||||
SELECT cartodb.CDB_Conf_GetConf(CONCAT('api_keys_', session_user))->>'username';
|
SELECT @extschema@.CDB_Conf_GetConf(CONCAT('api_keys_', session_user))->>'username';
|
||||||
$$ LANGUAGE SQL STABLE PARALLEL SAFE SECURITY DEFINER;
|
$$ LANGUAGE SQL STABLE PARALLEL SAFE SECURITY DEFINER;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
-- {
|
-- {
|
||||||
-- Return pixel resolution at the given zoom level
|
-- Return pixel resolution at the given zoom level
|
||||||
-- }{
|
-- }{
|
||||||
CREATE OR REPLACE FUNCTION CDB_XYZ_Resolution(z INTEGER)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_XYZ_Resolution(z INTEGER)
|
||||||
RETURNS FLOAT8
|
RETURNS FLOAT8
|
||||||
AS $$
|
AS $$
|
||||||
-- circumference divided by 256 is z0 resolution, then divide by 2^z
|
-- circumference divided by 256 is z0 resolution, then divide by 2^z
|
||||||
@@ -15,7 +15,7 @@ $$ LANGUAGE SQL IMMUTABLE PARALLEL SAFE STRICT;
|
|||||||
-- SRID of the returned polygon is forceably 3857
|
-- SRID of the returned polygon is forceably 3857
|
||||||
--
|
--
|
||||||
-- }{
|
-- }{
|
||||||
CREATE OR REPLACE FUNCTION CDB_XYZ_Extent(x INTEGER, y INTEGER, z INTEGER)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_XYZ_Extent(x INTEGER, y INTEGER, z INTEGER)
|
||||||
RETURNS GEOMETRY
|
RETURNS GEOMETRY
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
@@ -34,7 +34,7 @@ BEGIN
|
|||||||
-- Size of each tile in pixels (1:1 aspect ratio)
|
-- Size of each tile in pixels (1:1 aspect ratio)
|
||||||
tile_size := 256;
|
tile_size := 256;
|
||||||
|
|
||||||
initial_resolution := CDB_XYZ_Resolution(0);
|
initial_resolution := @extschema@.CDB_XYZ_Resolution(0);
|
||||||
--RAISE DEBUG 'Initial resolution: %', initial_resolution;
|
--RAISE DEBUG 'Initial resolution: %', initial_resolution;
|
||||||
|
|
||||||
origin_shift := (initial_resolution * tile_size) / 2.0;
|
origin_shift := (initial_resolution * tile_size) / 2.0;
|
||||||
@@ -56,7 +56,7 @@ BEGIN
|
|||||||
--RAISE DEBUG 'ymin: %', ymin;
|
--RAISE DEBUG 'ymin: %', ymin;
|
||||||
--RAISE DEBUG 'ymax: %', ymax;
|
--RAISE DEBUG 'ymax: %', ymax;
|
||||||
|
|
||||||
RETURN ST_MakeEnvelope(xmin, ymin, xmax, ymax, 3857);
|
RETURN @postgisschema@.ST_MakeEnvelope(xmin, ymin, xmax, ymax, 3857);
|
||||||
END
|
END
|
||||||
$$ LANGUAGE 'plpgsql' IMMUTABLE STRICT PARALLEL SAFE;
|
$$ LANGUAGE 'plpgsql' IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
-- }
|
-- }
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
-- Maximum supported zoom level
|
-- Maximum supported zoom level
|
||||||
CREATE OR REPLACE FUNCTION _CDB_MaxSupportedZoom()
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_MaxSupportedZoom()
|
||||||
RETURNS int
|
RETURNS int
|
||||||
LANGUAGE SQL
|
LANGUAGE SQL
|
||||||
IMMUTABLE PARALLEL SAFE
|
IMMUTABLE PARALLEL SAFE
|
||||||
@@ -12,7 +12,7 @@ AS $$
|
|||||||
SELECT 29;
|
SELECT 29;
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION cartodb.CDB_ZoomFromScale(scaleDenominator numeric)
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_ZoomFromScale(scaleDenominator numeric)
|
||||||
RETURNS int
|
RETURNS int
|
||||||
LANGUAGE SQL
|
LANGUAGE SQL
|
||||||
IMMUTABLE PARALLEL SAFE
|
IMMUTABLE PARALLEL SAFE
|
||||||
@@ -24,12 +24,12 @@ AS $$
|
|||||||
NULL
|
NULL
|
||||||
WHEN scaleDenominator = 0 THEN
|
WHEN scaleDenominator = 0 THEN
|
||||||
-- Actual zoom level would be infinite
|
-- Actual zoom level would be infinite
|
||||||
_CDB_MaxSupportedZoom()
|
@extschema@._CDB_MaxSupportedZoom()
|
||||||
ELSE
|
ELSE
|
||||||
CAST (
|
CAST (
|
||||||
LEAST(
|
LEAST(
|
||||||
ROUND(LOG(2, 559082264.028/scaleDenominator)),
|
ROUND(LOG(2, 559082264.028/scaleDenominator)),
|
||||||
_CDB_MaxSupportedZoom()
|
@extschema@._CDB_MaxSupportedZoom()
|
||||||
)
|
)
|
||||||
AS INTEGER)
|
AS INTEGER)
|
||||||
END;
|
END;
|
||||||
|
|||||||
@@ -23,9 +23,9 @@ cat ${input} | grep -v 'duplicated extension$' >> ${output}
|
|||||||
|
|
||||||
# Migrate CDB_TableMetadata
|
# Migrate CDB_TableMetadata
|
||||||
cat >> ${output} <<'EOF'
|
cat >> ${output} <<'EOF'
|
||||||
ALTER TABLE cartodb.CDB_TableMetadata DISABLE TRIGGER ALL;
|
ALTER TABLE @extschema@.CDB_TableMetadata DISABLE TRIGGER ALL;
|
||||||
INSERT INTO cartodb.CDB_TableMetadata SELECT * FROM public.CDB_TableMetadata;
|
INSERT INTO @extschema@.CDB_TableMetadata SELECT * FROM public.CDB_TableMetadata;
|
||||||
ALTER TABLE cartodb.CDB_TableMetadata ENABLE TRIGGER ALL;
|
ALTER TABLE @extschema@.CDB_TableMetadata ENABLE TRIGGER ALL;
|
||||||
DROP TABLE public.CDB_TableMetadata;
|
DROP TABLE public.CDB_TableMetadata;
|
||||||
|
|
||||||
-- Set user quota
|
-- Set user quota
|
||||||
@@ -40,7 +40,7 @@ BEGIN
|
|||||||
EXCEPTION WHEN undefined_function THEN
|
EXCEPTION WHEN undefined_function THEN
|
||||||
RAISE EXCEPTION 'Please set user quota before switching to cartodb extension';
|
RAISE EXCEPTION 'Please set user quota before switching to cartodb extension';
|
||||||
END;
|
END;
|
||||||
PERFORM cartodb.CDB_SetUserQuotaInBytes(qmax);
|
PERFORM @extschema@.CDB_SetUserQuotaInBytes(qmax);
|
||||||
DROP FUNCTION public._CDB_UserQuotaInBytes();
|
DROP FUNCTION public._CDB_UserQuotaInBytes();
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE 'plpgsql';
|
$$ LANGUAGE 'plpgsql';
|
||||||
@@ -49,7 +49,7 @@ EOF
|
|||||||
## Cartodbfy tables with a trigger using 'CDB_CheckQuota' or
|
## Cartodbfy tables with a trigger using 'CDB_CheckQuota' or
|
||||||
## 'CDB_TableMetadata_Trigger' from the 'public' schema
|
## 'CDB_TableMetadata_Trigger' from the 'public' schema
|
||||||
#cat >> ${output} <<'EOF'
|
#cat >> ${output} <<'EOF'
|
||||||
#select cartodb.CDB_CartodbfyTable(relname::regclass) from (
|
#select @extschema@.CDB_CartodbfyTable(relname::regclass) from (
|
||||||
# -- names of tables using public.CDB_CheckQuota or
|
# -- names of tables using public.CDB_CheckQuota or
|
||||||
# -- public.CDB_TableMetadata_Trigger in their triggers
|
# -- public.CDB_TableMetadata_Trigger in their triggers
|
||||||
# SELECT distinct c.relname
|
# SELECT distinct c.relname
|
||||||
|
|||||||
Reference in New Issue
Block a user