dataservices-api/server/extension/sql/0.0.1/10_config.sql
Rafa de la Torre b45aba4278 Config table and functions for server extension
Pretty much as done in cartodb and client extensions.
2015-11-12 13:02:51 +01:00

39 lines
1.3 KiB
PL/PgSQL

--
-- This extension has its own table for configurations.
--
-- The table and the function are considered to be private and therefore
-- no permissions are granted for any other user but the creator/geocoder.
CREATE TABLE IF NOT EXISTS cdb_geocoder_server._config ( KEY TEXT PRIMARY KEY, VALUE JSON NOT NULL );
-- Needed to dump config in backups
-- This can only be called from an SQL script executed by CREATE EXTENSION
SELECT pg_catalog.pg_extension_config_dump('cdb_geocoder_server._config', '');
CREATE OR REPLACE FUNCTION cdb_geocoder_server._config_set(key text, value JSON)
RETURNS VOID AS $$
BEGIN
PERFORM cdb_geocoder_server._config_remove(key);
EXECUTE 'INSERT INTO cdb_geocoder_server._config (KEY, VALUE) VALUES ($1, $2);' USING key, value;
END
$$ LANGUAGE PLPGSQL VOLATILE;
CREATE OR REPLACE FUNCTION cdb_geocoder_server._config_remove(key text)
RETURNS VOID AS $$
BEGIN
EXECUTE 'DELETE FROM cdb_geocoder_server._config WHERE KEY = $1;' USING key;
END
$$ LANGUAGE PLPGSQL VOLATILE;
CREATE OR REPLACE FUNCTION cdb_geocoder_server._config_get(key text)
RETURNS JSON AS $$
DECLARE
value JSON;
BEGIN
EXECUTE 'SELECT VALUE FROM cdb_geocoder_server._config WHERE KEY = $1;' INTO value USING key;
RETURN value;
END
$$ LANGUAGE PLPGSQL STABLE;