File: create_or_replace_to_create.pl

package info (click to toggle)
postgis 3.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 70,052 kB
  • sloc: ansic: 162,204; sql: 93,950; xml: 53,121; cpp: 12,646; perl: 5,658; sh: 5,369; makefile: 3,434; python: 1,205; yacc: 447; lex: 151; pascal: 58
file content (121 lines) | stat: -rw-r--r-- 2,603 bytes parent folder | download | duplicates (3)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env perl

#
# PostGIS - Spatial Types for PostgreSQL
# http://postgis.net
#
# Copyright (C) 2022 Regina Obe <lr@pcorp.us>
#
# This is free software; you can redistribute and/or modify it under
# the terms of the GNU General Public Licence. See the COPYING file.
#

#
# This script produces an .sql file containing
# CREATE OR REPLACE calls for each function
# in postgis.sql
#
# In addition, the transaction contains
# a check for Major postgis_lib_version()
# to match the one contained in lwpostgis.sql
#
# This never happens by just running make install
# as MODULE_FILENAME contains SO_MAJOR under
# all architectures.
#
#

eval "exec perl -w $0 $@"
  if (0);

use strict;
use warnings;

#
# Commandline argument handling
#
($#ARGV == 0)
  ||die
"Usage: perl create_or_replace_to_create.pl <create_script.sql> [<schema>]\nCreates a new SQL script that has all CREATE OR REPLACE converted to CREATE so it is suitable for CREATE EXTENSION only.\n"
  if ( @ARGV < 1 || @ARGV > 3 );

my $sql_file = $ARGV[0];

die "Unable to open input SQL file $sql_file\n"
  if ( !-f $sql_file );

#
# Go through the SQL file and strip out objects that cannot be
# applied to an existing, loaded database: types and operators
# and operator classes that have already been defined.
#
my $comment = '';
open( INPUT, $sql_file ) || die "Couldn't open file: $sql_file\n";
while(<INPUT>)
{

    if (/^create or replace function/i)
    {
        my $def .= $_;
        my $endfunc = 0;
		$def =~ s/CREATE OR REPLACE/CREATE/;
		#print "-- entering create or replace loop\n";
        while(<INPUT>)
        {
            $def .= $_;
            $endfunc = 1 if /^\s*(\$\$\s*)?LANGUAGE /i;

            last if ( $endfunc && /\;/ );
        }

        print $def;
		#print "-- exiting create or replace loop\n";
    }
	elsif (/^do *language .*\$\$/i)
    {
        print;
        while(<INPUT>)
        {
            print;
            last if /\$\$/;
        }
    }

    # Always output create ore replace view (see ticket #1097)
    elsif (/^create or replace view\s+(\S+)\s*/i)
    {
		my $def = $_;
        $def = $_;
		$def =~ s/CREATE OR REPLACE/CREATE/;
        while(<INPUT>)
        {
            $def .= $_;
            last if /\;\s*$/;
        }
		print $def;
    }

    # Always output create ore replace rule
    elsif (/^create or replace rule\s+(\S+)\s*/i)
    {
		my $def = $_;
		$def = $_;
		$def =~ s/CREATE OR REPLACE/CREATE/;
        while(<INPUT>)
        {
            $def .= $_;
            last if /\;\s*$/;
        }
		print $def;
    }
	else {
		print;
	}
}

close(INPUT);

1;

__END__