-
Notifications
You must be signed in to change notification settings - Fork 111
/
insert_function.sql
31 lines (26 loc) · 1.01 KB
/
insert_function.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
DROP FUNCTION IF EXISTS insert_crowd_mapping_data(text,text,text);
--Assumes only one value being inserted
CREATE OR REPLACE FUNCTION insert_crowd_mapping_data (
_geojson TEXT,
_description TEXT,
_name TEXT)
--Has to return something in order to be used in a "SELECT" statement
RETURNS integer
AS $$
DECLARE
_the_geom GEOMETRY;
--The name of your table in cartoDB
_the_table TEXT := 'crowdmap_basic';
BEGIN
--Convert the GeoJSON to a geometry type for insertion.
_the_geom := ST_SetSRID(ST_GeomFromGeoJSON(_geojson),4326);
--Executes the insert given the supplied geometry, description, and username, while protecting against SQL injection.
EXECUTE ' INSERT INTO '||quote_ident(_the_table)||' (the_geom, description, name)
VALUES ($1, $2, $3)
' USING _the_geom, _description, _name;
RETURN 1;
END;
$$
LANGUAGE plpgsql SECURITY DEFINER ;
--Grant access to the public user
GRANT EXECUTE ON FUNCTION insert_crowd_mapping_data( text, text, text) TO publicuser;