--- /dev/null
+#!/usr/bin/perl -w\r
+\r
+#\r
+# PostGIS - Spatial Types for PostgreSQL\r
+# http://postgis.net\r
+#\r
+# Copyright (C) 2016 Regina Obe <lr@pcorp.us>\r
+#\r
+# This is free software; you can redistribute and/or modify it under\r
+# the terms of the GNU General Public Licence. See the COPYING file.\r
+#\r
+\r
+#\r
+# This script produces an .sql file containing\r
+# ALTER FUNCTION SET PATH calls for each function\r
+# in postgis.sql\r
+\r
+# Having postgis functions force the search path \r
+# to just where postgis is installed is needed\r
+# so that functions that call other functions during \r
+# database restore, materialized view creation, foreign table calls\r
+# will always be able to find the companion functions\r
+#\r
+\r
+eval "exec perl -w $0 $@"\r
+ if (0);\r
+\r
+use strict;\r
+use warnings;\r
+\r
+#\r
+# Commandline argument handling\r
+#\r
+($#ARGV == 0) ||\r
+die "Usage: perl postgis_proc_set_path.pl <postgis.sql> <version_from> [<schema>]\nCreates a new SQL script \r
+to set search path for all functions in input script file.\n"\r
+ if ( @ARGV < 1 || @ARGV > 3 );\r
+\r
+my $sql_file = $ARGV[0];\r
+my $module = 'postgis';\r
+my $soname = '';\r
+my $version_to = "";\r
+my $version_to_num = 0;\r
+my $version_from = $ARGV[1];\r
+my $version_from_num = 0;\r
+my $schema = "";\r
+$schema = $ARGV[2] if @ARGV > 2;\r
+\r
+die "Unable to open input SQL file $sql_file\n"\r
+ if ( ! -f $sql_file );\r
+\r
+## Header of do \r
+print 'DO language plpgsql $$';\r
+my $dofunc_start = <<"EOF";\r
+DECLARE param_postgis_schema text;\r
+BEGIN\r
+-- check if PostGIS is already installed\r
+param_postgis_schema = (SELECT n.nspname from pg_extension e join pg_namespace n on e.extnamespace = n.oid WHERE extname = 'postgis');\r
+\r
+-- if in middle install, it will be the current_schema or what was there already\r
+param_postgis_schema = COALESCE(param_postgis_schema, current_schema());\r
+\r
+IF param_postgis_schema != current_schema() THEN\r
+ EXECUTE 'set search_path TO ' || quote_ident(param_postgis_schema);\r
+END IF;\r
+\r
+-- PostGIS set search path of functions\r
+EOF\r
+print $dofunc_start;\r
+\r
+#\r
+# Search the SQL file for the target version number (the \r
+# version we are upgrading *to*.\r
+#\r
+open( INPUT, $sql_file ) || die "Couldn't open file: $sql_file\n";\r
+\r
+\r
+print qq{\r
+--\r
+-- ALTER FUNCTION script\r
+--\r
+\r
+};\r
+\r
+#print "BEGIN;\n";\r
+print "SET search_path TO $schema;\n" if $schema;\r
+\r
+#\r
+# Go through the SQL file and find all functions\r
+# for each create an ALTER FUNCTION statement\r
+# to set the search_path to schema postgis is installed in\r
+open( INPUT, $sql_file ) || die "Couldn't open file: $sql_file\n";\r
+while(<INPUT>)\r
+{\r
+\r
+ if ( /^create or replace function([^\)]+)([\)]{0,1})/i )\r
+ {\r
+ my $funchead = $1; #contains function header except the end )\r
+ my $endhead = 0;\r
+ my $endfunchead = $2;\r
+ if ($2 eq ')') ## reached end of header\r
+ {\r
+ $endhead = 1;\r
+ }\r
+ \r
+ #raster folks decided to break their func head in multiple lines \r
+ # so we need to do this crazy thing\r
+ if ($endhead != 1)\r
+ {\r
+ while(<INPUT>)\r
+ {\r
+ #look for expressions with no ( and optionally ending in )\r
+ if ( /^([^\)]*)([\)]{0,1})/i )\r
+ {\r
+ $funchead .= $1;\r
+ $endfunchead = $2;\r
+ if ($2 eq ')') ## reached end of header\r
+ {\r
+ $endhead = 1;\r
+ \r
+ }\r
+ }\r
+ last if ( $endhead );\r
+ \r
+ }\r
+ }\r
+ #strip quoted , trips up the default strip\r
+ $funchead =~ s/(',')+//ig;\r
+ #strip off default args from the function header\r
+ $funchead =~ s/(default\s+[A-Za-z\.\+\-0-9\'\[\]\:\s]*)//ig;\r
+ \r
+\r
+ print "EXECUTE 'ALTER FUNCTION $funchead $endfunchead SET search_path=' || quote_ident(param_postgis_schema) || ';';\n";\r
+ }\r
+\r
+}\r
+\r
+close( INPUT );\r
+\r
+## End of DO\r
+print 'END;';\r
+print '$$';\r
+\r
+__END__\r
+ \r