cartodb-postgresql/scripts-available/CDB_Conf.sql

67 lines
1.9 KiB
MySQL
Raw Normal View History

2015-08-19 17:20:06 +08:00
----------------------------------
-- CONF MANAGEMENT FUNCTIONS
--
-- Meant to be used by superadmin user.
-- Functions needing reading configuration should use SECURITY DEFINER.
----------------------------------
2015-08-14 16:12:10 +08:00
-- This will trigger NOTICE if CDB_CONF already exists
2015-08-14 02:43:17 +08:00
DO LANGUAGE 'plpgsql' $$
BEGIN
2015-08-20 14:44:59 +08:00
CREATE TABLE IF NOT EXISTS cartodb.CDB_CONF ( KEY TEXT PRIMARY KEY, VALUE JSON NOT NULL );
2015-08-14 02:43:17 +08:00
END
$$;
CREATE OR REPLACE
2015-08-20 14:44:59 +08:00
FUNCTION cartodb.CDB_Conf_SetConf(key TEXT, value JSON)
2015-08-14 02:43:17 +08:00
RETURNS void AS $$
BEGIN
2015-08-20 14:44:59 +08:00
PERFORM cartodb.CDB_Conf_RemoveConf(key);
EXECUTE 'INSERT INTO cartodb.CDB_CONF (KEY, VALUE) VALUES ($1, $2);' USING key, value;
2015-08-14 02:43:17 +08:00
END
$$ LANGUAGE PLPGSQL VOLATILE;
CREATE OR REPLACE
2015-08-20 14:44:59 +08:00
FUNCTION cartodb.CDB_Conf_RemoveConf(key text)
2015-08-14 02:43:17 +08:00
RETURNS void AS $$
BEGIN
2015-08-20 14:44:59 +08:00
PERFORM cartodb._CDB_Conf_Cache('remove', key);
EXECUTE 'DELETE FROM cartodb.CDB_CONF WHERE KEY = $1;' USING key;
2015-08-14 02:43:17 +08:00
END
$$ LANGUAGE PLPGSQL VOLATILE;
CREATE OR REPLACE
2015-08-20 14:44:59 +08:00
FUNCTION cartodb.CDB_Conf_GetConf(key text)
RETURNS JSON AS $$
2015-08-14 02:43:17 +08:00
DECLARE
2015-08-20 14:44:59 +08:00
value JSON;
2015-08-14 02:43:17 +08:00
BEGIN
2015-08-20 14:44:59 +08:00
EXECUTE 'select cartodb._CDB_Conf_Cache(''get'', $1);' INTO value USING key;
RETURN value;
2015-08-14 02:43:17 +08:00
END
2015-08-19 17:20:06 +08:00
$$ LANGUAGE PLPGSQL STABLE;
2015-08-18 16:44:10 +08:00
-- Single cache function allowing SD private dict usage
CREATE OR REPLACE
2015-08-20 14:44:59 +08:00
FUNCTION cartodb._CDB_Conf_Cache(operation text, key text)
RETURNS JSON AS
2015-08-18 16:44:10 +08:00
$$
if 'conf' not in SD:
2015-08-20 16:25:31 +08:00
SD['conf'] = {}
2015-08-18 16:44:10 +08:00
if operation == 'remove':
2015-08-20 14:44:59 +08:00
if key in SD['conf']:
del(SD['conf'][key])
2015-08-18 16:44:10 +08:00
elif operation == 'get':
2015-08-20 14:44:59 +08:00
if key not in SD['conf'] or SD['conf'][key] == None:
2015-08-18 16:44:10 +08:00
value = None
2015-08-20 14:44:59 +08:00
# Execute returns string, not json :(
response = plpy.execute("SELECT value FROM cartodb.CDB_CONF WHERE KEY = '%s'" % key);
2015-08-18 16:44:10 +08:00
if len(response) > 0:
2015-08-20 14:44:59 +08:00
value = response[0]['value']
SD['conf'][key] = value
return SD['conf'][key]
2015-08-18 16:44:10 +08:00
else:
raise Exception('Unknown operation: %s' % operation)
$$ LANGUAGE 'plpythonu' VOLATILE;