data-services/geocoder/available-services/sql/services.sql

16 lines
820 B
MySQL
Raw Normal View History

2014-09-20 06:05:53 +08:00
--CREATE TYPE available_services_v1 AS (q TEXT, adm0_a3 TEXT, postal_code_points BOOLEAN, postal_code_polygons BOOLEAN);
2014-09-20 05:51:56 +08:00
2014-09-20 06:08:00 +08:00
--EXAMPLE
-- SELECT (admin0_available_services(Array['United States', 'ESP'])).*
CREATE OR REPLACE FUNCTION admin0_available_services(name text[])
2014-09-20 06:05:53 +08:00
RETURNS SETOF available_services_v1 AS $$
2014-09-20 05:51:56 +08:00
DECLARE
2014-09-20 06:05:53 +08:00
ret available_services_v1%rowtype;
2014-09-20 05:51:56 +08:00
BEGIN
-- FOR ret IN
RETURN QUERY
2014-09-20 06:05:53 +08:00
SELECT d.q, n.adm0_a3, n.postal_code_points, n.postal_code_polygons FROM (SELECT q, lower(regexp_replace(q, '[^a-zA-Z\u00C0-\u00ff]+', '', 'g'))::text x FROM (SELECT unnest(name) q) g) d LEFT OUTER JOIN admin0_synonyms s ON name_ = d.x LEFT OUTER JOIN available_services n ON s.adm0_a3 = n.adm0_a3 GROUP BY d.q, n.adm0_a3, n.postal_code_points, n.postal_code_polygons;
2014-09-20 05:51:56 +08:00
END
$$ LANGUAGE 'plpgsql' SECURITY DEFINER;