-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump_ddl.pl
executable file
·217 lines (182 loc) · 6.75 KB
/
dump_ddl.pl
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use strict;
use lib '/export/home/sybase/clients-mssql/current64/perl';
use DBI;
use Getopt::Std;
sub SubstituteParams($$)
{
my $template = shift;
my $params = shift;
foreach my $s (keys %$params) {
#my $re = qr{/%$s%/};
$template =~ s/%$s%/$$params{$s}/gex;
#if ($template =~ $re) {
# $template =~ $` . $$params{$s} . $';
#}
}
return $template;
}
sub Dump
{
my $dbh = shift;
my $sql = shift;
my $output_dir = shift;
my $template_dir = shift;
my @object_names = @_;
if (@object_names) {
$sql .= " and o.name in ('" . join("', '", @object_names) . "')";
}
# Prepare the statement.
my $sth = $dbh->prepare($sql)
or die "Can't prepare statement: $DBI::errstr";
# Execute the statement.
$sth->execute();
my %ext = (
"P" => "prc",
"PC" => "prc",
"FN" => "udf",
"FS" => "udf",
"TF" => "udf",
"V" => "viw",
"TR" => "trg",
"IF" => "udf"
);
# Fetch and display the result set value.
while (my @row = $sth->fetchrow_array) {
$row[1] =~ s/\s+//;
$row[2] =~ s/^\s*//s;
$row[2] =~ s|/\*\s+\$Id.+?\*/\s*$||si;
$row[2] =~ s/\s*$//s;
my $fn;
if (-e "$template_dir/object-update.$row[1].$ext{$row[1]}") {
$fn = "$template_dir/object-update.$row[1].$ext{$row[1]}";
} else {
$fn = "$template_dir/object-update.$ext{$row[1]}";
}
open TMPL, "<$fn" or die "Can't open template file $fn";
my $x = $/;
undef $/;
my $template = <TMPL>;
$/ = $x;
close TMPL;
my $alter_create = 'ALTER';
$alter_create = 'CREATE' if $template =~ /drop/is;
if ($row[2] =~ /^\s*CREATE\s+(\w+)\s+(?:\[?dbo\]?\.)?\[?([^\]\n\(]+)\]?/si) {
my $a = $1;
my $b = $2;
my $c = $';
if ($a =~ /^proc/i) {
$row[2] = "$alter_create PROCEDURE dbo.$b$c";
} elsif ($a =~ /^func/i) {
$row[2] = "$alter_create FUNCTION dbo.$b$c";
} elsif ($a =~ /^trigger/i) {
$row[2] = "$alter_create TRIGGER dbo.$b$c";
} else {
$row[2] = "$alter_create $a dbo.$b$c";
}
}
my %params = ();
$params{"object_ddl"} = $row[2];
$params{"object_name"} = $row[0];
$template = SubstituteParams($template, \%params);
unless (-d "$output_dir"){
mkdir "$output_dir" or die;
}
open SQL, ">$output_dir$row[0]\.$ext{$row[1]}" or die $!;
$template =~ s/\r\n/\n/gs;
print SQL $template;
close SQL;
}
}
$Getopt::Std::STANDARD_HELP_VERSION = 1;
$Getopt::Std::OUTPUT_HELP_VERSION = 1;
my %opts;
getopts('s:d:u:p:o:t:', \%opts);
my $server = $opts{'s'};
my $database = $opts{'d'};
my $user = $opts{'u'};
my $password = $opts{'p'};
my $output_dir = $opts{'o'} ? $opts{'o'} : '.';
my $template_dir = $opts{'t'};
my @object_names = @ARGV;
if (!$password && $user) {
$password = $user . "_pw";
}
unless ($server || $database || $user || $password || $template_dir) {
print STDERR "usage: dump_ddl -s <server> -d <database> -u <user> -p <password> -t <templates dir> [-o <output directory> <object names>]\n";
exit(1);
}
my %attr = (
PrintError => 0,
RaiseError => 0
);
my $data_source_unix = "dbi:Sybase:$server";
my $data_source_windows = "dbi:ODBC:$server";
if ($output_dir && $output_dir !~ /\/$/) {
$output_dir = "$output_dir/";
}
# Connect to the data source and get a handle for that connection.
my $dbh;
#($dbh = DBI->connect($data_source_windows, $user, $password, \%attr))
# or
($dbh = DBI->connect($data_source_unix, $user, $password, \%attr))
or die "Can't connect to $data_source_unix: $DBI::errstr";
#$dbh->{LongReadLen} = 100000;
$dbh->{PrintError} = 1;
$dbh->do("use $database;");
$dbh->do("set textsize 50000000");
my $sql = "select name, type, definition from sys.objects o, sys.sql_modules m where o.is_ms_shipped = 0 and o.object_id = m.object_id and definition is not null and name not like '%diagram%'";
Dump($dbh, $sql, $output_dir, $template_dir, @object_names);
$sql = qq(
select o.name name, type, 'CREATE ' + case when type='FS' then 'FUNCTION' else 'PROCEDURE' end + ' dbo.' + o.name +
case when p1.definition is not null and type = 'FS' then '(' else '' end +
%params%
case when p1.definition is not null and type = 'FS' then ')' else '' end +
case when ret.definition is not null then char(10) + 'RETURNS ' + ret.definition + ' ' else char(10) end + 'WITH EXECUTE AS CALLER ' + char(10) +
char(10) + 'AS' + char(10) + 'EXTERNAL NAME ' + '[' + a.name + '].[' + assembly_class + '].[' + assembly_method + ']' definition
from sys.objects o
join sys.assembly_modules am on o.object_id = am.object_id
join sys.assemblies a on am.assembly_id = a.assembly_id
left join
(select object_id, p.name + ' [' + t.name +
case when t.name like '%char%' or t.name = 'varbinary'
then '](' + case when p.max_length > 0
then convert(varchar, p.max_length/2)
else 'max' end +
')'
else ']'
end definition, ' ' is_func
from sys.parameters p
join sys.types t on p.system_type_id = t.system_type_id and p.user_type_id = t.user_type_id
where p.name = ''
) ret on ret.object_id = o.object_id
);
my $params = '';
for(my $i = 1; $i <= 20; ++$i) {
$sql = $sql . sprintf (qq(left join (select object_id, p.name + ' [' + t.name +
case when t.name like '%%char%%' or t.name = 'varbinary'
then '](' + case when p.max_length > 0
then convert(varchar, p.max_length/2)
else 'max' end +
')'
else ']'
end +
case when is_output = 1
then ' output'
else ''
end definition
from sys.parameters p
join sys.types t on p.system_type_id = t.system_type_id and p.user_type_id = t.user_type_id
where parameter_id = %d
) p%d on p%d.object_id = o.object_id
), $i, $i, $i);
if ($i == 1) {
$params = $params . sprintf(qq(case when p%d.definition is not null then isnull(ltrim(is_func), char(10)) + p%d.definition else '' end +), $i, $i);
} else {
$params = $params . sprintf(qq(case when p%d.definition is not null then ',' + isnull(is_func, char(10)) + p%d.definition else '' end +), $i, $i);
}
}
$sql =~ s/%params%/$params/s;
$sql = $sql . ' where 1=1 ';
Dump($dbh, $sql, $output_dir, $template_dir, @object_names);
# Disconnect the database from the database handle.
$dbh->disconnect;