2016-12-13 23:27:38 +08:00
|
|
|
--
|
2016-12-14 17:27:43 +08:00
|
|
|
-- Exception-safe private DataServices API function
|
2016-12-13 23:27:38 +08:00
|
|
|
--
|
|
|
|
|
2016-12-14 17:27:43 +08:00
|
|
|
CREATE OR REPLACE FUNCTION <%= DATASERVICES_CLIENT_SCHEMA %>._<%= name %>_exception_safe (<%= params_with_type_and_default.join(' ,') %>)
|
2016-12-13 23:27:38 +08:00
|
|
|
RETURNS <%= return_type %> AS $$
|
|
|
|
DECLARE
|
|
|
|
<% if not multi_row %>ret <%= return_type %>;<% end %>
|
|
|
|
username text;
|
|
|
|
orgname text;
|
2016-12-14 17:33:50 +08:00
|
|
|
_returned_sqlstate TEXT;
|
|
|
|
_message_text TEXT;
|
|
|
|
_pg_exception_context TEXT;
|
2016-12-13 23:27:38 +08:00
|
|
|
BEGIN
|
|
|
|
IF session_user = 'publicuser' OR session_user ~ 'cartodb_publicuser_*' THEN
|
|
|
|
RAISE EXCEPTION 'The api_key must be provided';
|
|
|
|
END IF;
|
|
|
|
SELECT u, o INTO username, orgname FROM <%= DATASERVICES_CLIENT_SCHEMA %>._cdb_entity_config() AS (u text, o text);
|
|
|
|
-- JSON value stored "" is taken as literal
|
|
|
|
IF username IS NULL OR username = '' OR username = '""' THEN
|
|
|
|
RAISE EXCEPTION 'Username is a mandatory argument, check it out';
|
|
|
|
END IF;
|
2016-12-13 23:38:30 +08:00
|
|
|
|
|
|
|
<% if multi_row %>
|
2016-12-13 23:51:11 +08:00
|
|
|
BEGIN
|
|
|
|
RETURN QUERY
|
|
|
|
SELECT * FROM <%= DATASERVICES_CLIENT_SCHEMA %>._<%= name %>(<%= ['username', 'orgname'].concat(params).join(', ') %>);
|
|
|
|
EXCEPTION
|
|
|
|
WHEN OTHERS THEN
|
2016-12-14 17:35:44 +08:00
|
|
|
GET STACKED DIAGNOSTICS _returned_sqlstate = RETURNED_SQLSTATE,
|
|
|
|
_message_text = MESSAGE_TEXT,
|
|
|
|
_pg_exception_context = PG_EXCEPTION_CONTEXT;
|
|
|
|
RAISE WARNING USING ERRCODE = _returned_sqlstate, MESSAGE = _message_text, DETAIL = _pg_exception_context;
|
2016-12-13 23:51:11 +08:00
|
|
|
END;
|
2016-12-13 23:38:30 +08:00
|
|
|
<% elsif multi_field %>
|
2016-12-14 00:00:55 +08:00
|
|
|
BEGIN
|
|
|
|
SELECT * FROM <%= DATASERVICES_CLIENT_SCHEMA %>._<%= name %>(<%= ['username', 'orgname'].concat(params).join(', ') %>) INTO ret;
|
|
|
|
RETURN ret;
|
|
|
|
EXCEPTION
|
|
|
|
WHEN OTHERS THEN
|
2016-12-14 02:19:03 +08:00
|
|
|
GET STACKED DIAGNOSTICS _returned_sqlstate = RETURNED_SQLSTATE,
|
|
|
|
_message_text = MESSAGE_TEXT,
|
|
|
|
_pg_exception_context = PG_EXCEPTION_CONTEXT;
|
|
|
|
RAISE WARNING USING ERRCODE = _returned_sqlstate, MESSAGE = _message_text, DETAIL = _pg_exception_context;
|
2016-12-14 00:00:55 +08:00
|
|
|
RETURN ret;
|
|
|
|
END;
|
2016-12-13 23:38:30 +08:00
|
|
|
<% else %>
|
|
|
|
BEGIN
|
2016-12-13 23:27:38 +08:00
|
|
|
SELECT <%= DATASERVICES_CLIENT_SCHEMA %>._<%= name %>(<%= ['username', 'orgname'].concat(params).join(', ') %>) INTO ret;
|
|
|
|
RETURN ret;
|
2016-12-13 23:38:30 +08:00
|
|
|
EXCEPTION
|
|
|
|
WHEN OTHERS THEN
|
2016-12-14 17:35:44 +08:00
|
|
|
GET STACKED DIAGNOSTICS _returned_sqlstate = RETURNED_SQLSTATE,
|
|
|
|
_message_text = MESSAGE_TEXT,
|
|
|
|
_pg_exception_context = PG_EXCEPTION_CONTEXT;
|
|
|
|
RAISE WARNING USING ERRCODE = _returned_sqlstate, MESSAGE = _message_text, DETAIL = _pg_exception_context;
|
2016-12-13 23:38:30 +08:00
|
|
|
RETURN ret;
|
|
|
|
END;
|
|
|
|
<% end %>
|
2016-12-13 23:27:38 +08:00
|
|
|
END;
|
|
|
|
$$ LANGUAGE 'plpgsql' SECURITY DEFINER;
|