From ddfdd870445afafbceacb66c1a5f172af19478d6 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Fri, 5 May 2017 08:56:33 -0400 Subject: [PATCH] update execute to rely on UPDATE for templating values in query --- src/pg/sql/26_distance_matrix.sql | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/pg/sql/26_distance_matrix.sql b/src/pg/sql/26_distance_matrix.sql index bf782ca..b0f387d 100644 --- a/src/pg/sql/26_distance_matrix.sql +++ b/src/pg/sql/26_distance_matrix.sql @@ -1,6 +1,7 @@ -- Calculate the distance matrix using underlying road network -- Sample usage: --- select * from cdb_distancematrix("fake_drains", "cvxopt_fake_sources") +-- select * from cdb_distancematrix('fake_drains'::regclass, +-- 'cvxopt_fake_sources'::regclass) CREATE OR REPLACE FUNCTION CDB_DistanceMatrix( origin_table regclass, destination_table regclass, @@ -9,10 +10,9 @@ CREATE OR REPLACE FUNCTION CDB_DistanceMatrix( the_geom geometry(geometry, 4326), length_km numeric, duration_sec numeric) AS $$ -DECLARE - query_string text; BEGIN - query_string := format(' + RETURN QUERY + EXECUTE format(' WITH pairs AS ( SELECT o."cartodb_id" AS origin_id, @@ -28,7 +28,7 @@ BEGIN destination_id, (cdb_route_point_to_point(origin_point, destination_point, - ''%s'')).* + $1)).* FROM pairs) SELECT origin_id::bigint AS origin_id, @@ -36,10 +36,8 @@ BEGIN shape AS the_geom, length::numeric AS length_km, duration::numeric AS duration_sec - FROM results;', origin_table, destination_table, transit_mode); - RAISE NOTICE '%', query_string; - RETURN QUERY - EXECUTE query_string; + FROM results;', origin_table, destination_table) + USING transit_mode; RETURN; END; $$ LANGUAGE plpgsql;