Merge pull request #25 from CartoDB/add-config-table-and-funcs

Config table and functions for server extension
This commit is contained in:
Rafa de la Torre 2015-11-12 17:04:26 +01:00
commit 05d75c82ee
6 changed files with 67 additions and 43 deletions

View File

@ -0,0 +1,21 @@
-- Create a conf table
SELECT cdb_geocoder_server._config_set('cds', '{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}');
_config_set
-------------
(1 row)
-- Test key retrieval
SELECT cdb_geocoder_server._config_get('cds');
_config_get
----------------------------------------------------------------------------
{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}
(1 row)
-- Test returns NULL if key doesn't exist
SELECT cdb_geocoder_server._config_get('no existe');
_config_get
-------------
(1 row)

View File

@ -1,15 +0,0 @@
-- Create a conf table
CREATE TABLE conf (key TEXT PRIMARY KEY, values_json TEXT NOT NULL);
INSERT INTO conf VALUES ('cds', '{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}');
-- Test key retrieval
SELECT cdb_geocoder_server._get_conf('cds');
_get_conf
----------------------------------------------------------------------------
{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}
(1 row)
-- Test no key exception
SELECT cdb_geocoder_server._get_conf('no existe');
ERROR: Missing key 'no existe' in conf table
-- Drop conf table
DROP TABLE conf;

View File

@ -0,0 +1,38 @@
--
-- 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;

View File

@ -1,16 +0,0 @@
-- Get values_json for provided key from conf table
CREATE OR REPLACE FUNCTION cdb_geocoder_server._get_conf(_key TEXT)
RETURNS text
AS $$
DECLARE
rec RECORD;
BEGIN
SELECT INTO rec values_json FROM conf WHERE conf.key = _key;
IF NOT FOUND THEN
RAISE EXCEPTION 'Missing key ''%'' in conf table', _key;
END IF;
RETURN rec.values_json;
END
$$ LANGUAGE plpgsql;

View File

@ -0,0 +1,8 @@
-- Create a conf table
SELECT cdb_geocoder_server._config_set('cds', '{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}');
-- Test key retrieval
SELECT cdb_geocoder_server._config_get('cds');
-- Test returns NULL if key doesn't exist
SELECT cdb_geocoder_server._config_get('no existe');

View File

@ -1,12 +0,0 @@
-- Create a conf table
CREATE TABLE conf (key TEXT PRIMARY KEY, values_json TEXT NOT NULL);
INSERT INTO conf VALUES ('cds', '{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}');
-- Test key retrieval
SELECT cdb_geocoder_server._get_conf('cds');
-- Test no key exception
SELECT cdb_geocoder_server._get_conf('no existe');
-- Drop conf table
DROP TABLE conf;