cartodb-postgresql/scripts-available/CDB_Conf.sql

65 lines
1.8 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
CREATE TABLE IF NOT EXISTS cartodb.CDB_CONF ( PARAM TEXT PRIMARY KEY, CONF TEXT NOT NULL );
2015-08-14 02:43:17 +08:00
END
$$;
CREATE OR REPLACE
FUNCTION cartodb.CDB_Conf_SetConf(param text, conf text)
2015-08-14 02:43:17 +08:00
RETURNS void AS $$
BEGIN
PERFORM cartodb.CDB_Conf_RemoveConf(param);
EXECUTE 'INSERT INTO cartodb.CDB_CONF (PARAM, CONF) VALUES ($1, $2);' USING param, conf;
2015-08-14 02:43:17 +08:00
END
$$ LANGUAGE PLPGSQL VOLATILE;
CREATE OR REPLACE
FUNCTION cartodb.CDB_Conf_RemoveConf(param text)
2015-08-14 02:43:17 +08:00
RETURNS void AS $$
BEGIN
2015-08-18 16:44:10 +08:00
PERFORM cartodb._CDB_Conf_Cache('remove', param);
EXECUTE 'DELETE FROM cartodb.CDB_CONF WHERE PARAM = $1;' USING param;
2015-08-14 02:43:17 +08:00
END
$$ LANGUAGE PLPGSQL VOLATILE;
CREATE OR REPLACE
FUNCTION cartodb.CDB_Conf_GetConf(param text)
2015-08-14 02:43:17 +08:00
RETURNS TEXT AS $$
DECLARE
conf TEXT;
2015-08-14 02:43:17 +08:00
BEGIN
2015-08-18 16:44:10 +08:00
EXECUTE 'select cartodb._CDB_Conf_Cache(''get'', $1) as conf;' INTO conf USING param;
RETURN conf;
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
FUNCTION cartodb._CDB_Conf_Cache(operation text, param text)
RETURNS TEXT AS
$$
if 'conf' not in SD:
SD['conf'] = dict()
if operation == 'remove':
SD['conf'][param] = None
elif operation == 'get':
if param not in SD['conf']:
value = None
response = plpy.execute("SELECT conf FROM cartodb.CDB_CONF WHERE PARAM = '%s'" % param);
if len(response) > 0:
value = response[0]['conf']
SD['conf'][param] = value
return SD['conf'][param]
else:
raise Exception('Unknown operation: %s' % operation)
$$ LANGUAGE 'plpythonu' VOLATILE;