-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_objs.pl
executable file
·326 lines (251 loc) · 8.76 KB
/
mysql_objs.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env perl
# Output MySQL objects to the file system for archiving
use strict;
use DBD::mysql;
use File::Path qw (make_path); # to make output directories
use Getopt::Std qw (getopts);
use IO::File;
my(%opts);
getopts('h:s:u:p:o:d',\%opts);
my($host, $port, $schema, $user, $password, $output_dir, $debug);
my($debug_mode) = $opts{'d'};
print STDERR "DEBUG MODE\n" if ($debug_mode);
# schema, user, and password do not have defaults
$schema = $opts{'s'};
$user = $opts{'u'};
$password = $opts{'p'};
if (! ($schema and $user and $password)) {
print STDERR qq{
Usage ${0} [-h host[:port]] -s schema -u username -p password [-o output_dir] [-d]
Host and port default to "localhost:3306"
Output directory will default the current directory
All object files will be written to output_dir/schema/<object>
Pass -d to activate debugging mode, with messages to STDERR. The db
connection will be made, but no directories or files will be output
};
exit(0);
}
# host and port parsing
$host = 'localhost';
$port = '3306';
if ($opts{'h'} =~ /^([^:]+):?(\d+)?$/) {
$host = $1 or 'localhost';
$port = $2 or '3306';
printf STDERR ("Host: %s Port: %s\n",$host,$port) if ($debug_mode);
} else {
printf STDERR ("Using default Host: %s Port: %s\n",$host,$port) if ($debug_mode);
}
# output dir
if ($output_dir = $opts{'o'}) {
chop($output_dir) if $output_dir =~ m{/$}; # no trailing slash, please
$output_dir ||= '.';
if ($debug_mode) {
print STDERR "Would output to ${output_dir}\n";
} else {
make_path($output_dir);
}
}
# db connection
my($info_dsn) = sprintf("DBI:mysql:database=%s;host=%s;port=%s",'information_schema', $host, $port);
my($mysql_dbh, $info_dbh);
eval {
$info_dbh = DBI->connect($info_dsn, $user, $password);
};
if ($@) {
print STDERR "Could not connect to the `information_schema` meta database";
die "Aborting"
}
# keys are the object types to pull out
#
# keys are:
# [db_handle, object name selection query, placeholder for prepared statement]
#
my(%objects) = ('TABLE' => [$info_dbh,
q{SELECT table_name
FROM information_schema.tables
WHERE table_schema = ?
ORDER BY table_name},
""],
'VIEW' => [$info_dbh,
q{SELECT table_name
FROM information_schema.views
WHERE table_schema = ?
ORDER BY table_name},
""],
'PROCEDURE' => [$info_dbh,
q{SELECT routine_name
FROM information_schema.routines
WHERE routine_schema = ?
AND routine_type = 'PROCEDURE'
ORDER BY routine_name},
""],
'FUNCTION' => [$info_dbh,
q{SELECT routine_name
FROM information_schema.routines
WHERE routine_schema = ?
AND routine_type = 'FUNCTION'
ORDER BY routine_name},
""]);
my($obj);
foreach (keys %objects) {
print STDERR " Processing $_\n" if ($debug_mode);
if (defined ($obj = $objects{$_})) {
$obj->[2] = $obj->[0]->prepare($obj->[1]);
if ('TABLE' eq $_) {
$obj->[2]->bind_param(1, $schema);
&process_tables($obj, $schema, $output_dir, $debug_mode);
} elsif ('VIEW' eq $_) {
$obj->[2]->bind_param(1, $schema);
&process_views($obj, $schema, $output_dir, $debug_mode);
} elsif ('PROCEDURE' eq $_) {
$obj->[2]->bind_param(1, $schema);
&process_procedures($obj, $schema, $output_dir, $debug_mode);
} elsif ('FUNCTION' eq $_) {
$obj->[2]->bind_param(1, $schema);
&process_functions($obj, $schema, $output_dir, $debug_mode);
}
}
}
### Output handlers
#
# TABLE
#
sub process_tables {
my($obj, $schema, $output_dir, $debug_mode) = @_;
my($name_query) = $obj->[2];
$name_query->execute();
my($show_sql) = q{SHOW CREATE TABLE %s.%s}; # built up in the while() loop
my($table_name, $show_query, $dir, $fh);
while (my $name = $name_query->fetchrow_arrayref()) {
$table_name = $name->[0];
if ($debug_mode) {
printf STDERR (" Found TABLE : %s.%s\n", $schema, $table_name);
} else {
if (! $dir) {
$dir = sprintf("%s/%s/%s", $output_dir, $schema, 'TABLE');
make_path($dir);
}
$fh = new IO::File("${dir}/${table_name}.sql","w");
printf $fh ("# TABLE : %s.%s\n", $schema, $table_name);
printf $fh ("DELIMITER %s\n", q{$$});
printf $fh ("USE %s %s\n\n", $schema, q{$$});
# Cannot use a prepared statement here, since the table
# name will be escaped (quoted) as a string.
#
$show_query = $obj->[0]->prepare(sprintf($show_sql,$schema,$table_name));
$show_query->execute();
while (my $output = $show_query->fetchrow_arrayref()) {
print $fh $output->[1],"\n";
}
printf $fh ("%s\n\nDELIMITER %s\n", q{$$}, q{;});
$fh->close();
}
}
}
#
# VIEW
#
sub process_views {
my($obj, $schema, $output_dir, $debug_mode) = @_;
my($name_query) = $obj->[2];
$name_query->execute();
my($show_sql) = q{SHOW CREATE VIEW %s.%s}; # built up in the while() loop
my($view_name, $show_query, $dir, $fh);
while (my $name = $name_query->fetchrow_arrayref()) {
$view_name = $name->[0];
if ($debug_mode) {
printf STDERR (" Found VIEW : %s.%s\n", $schema, $view_name);
} else {
if (! $dir) {
$dir = sprintf("%s/%s/%s", $output_dir, $schema, 'VIEW');
make_path($dir);
}
$fh = new IO::File("${dir}/${view_name}.sql","w");
printf $fh ("# VIEW : %s.%s\n", $schema, $view_name);
printf $fh ("DELIMITER %s\n", q{$$});
printf $fh ("USE %s %s\n\n", $schema, q{$$});
# Cannot use a prepared statement here, since the view
# name will be escaped (quoted) as a string.
#
$show_query = $obj->[0]->prepare(sprintf($show_sql,$schema,$view_name));
$show_query->execute();
while (my $output = $show_query->fetchrow_arrayref()) {
print $fh $output->[1],"\n";
}
printf $fh ("%s\n\nDELIMITER %s\n", q{$$}, q{;});
$fh->close();
}
}
}
#
# PROCEDURE
#
sub process_procedures {
my($obj, $schema, $output_dir, $debug_mode) = @_;
my($name_query) = $obj->[2];
$name_query->execute();
my($show_sql) = q{SHOW CREATE PROCEDURE %s.%s}; # built up in the while() loop
my($procedure_name, $show_query, $dir, $fh);
while (my $name = $name_query->fetchrow_arrayref()) {
$procedure_name = $name->[0];
if ($debug_mode) {
printf STDERR (" Found PROCEDURE : %s.%s\n", $schema, $procedure_name);
} else {
if (! $dir) {
$dir = sprintf("%s/%s/%s", $output_dir, $schema, 'PROCEDURE');
make_path($dir);
}
$fh = new IO::File("${dir}/${procedure_name}.sql","w");
printf $fh ("# PROCEDURE : %s.%s\n", $schema, $procedure_name);
printf $fh ("DELIMITER %s\n", q{$$});
printf $fh ("USE %s %s\n", $schema, q{$$});
printf $fh ("DROP PROCEDURE IF EXISTS %s.%s %s\n\n", $schema, $procedure_name, q{$$});
# Cannot use a prepared statement here, since the procedure
# name will be escaped (quoted) as a string.
#
$show_query = $obj->[0]->prepare(sprintf($show_sql,$schema,$procedure_name));
$show_query->execute();
while (my $output = $show_query->fetchrow_arrayref()) {
print $fh $output->[2],"\n";
}
printf $fh ("%s\n\nDELIMITER %s\n", q{$$}, q{;});
$fh->close();
}
}
}
#
# FUNCTION
#
sub process_functions {
my($obj, $schema, $output_dir, $debug_mode) = @_;
my($name_query) = $obj->[2];
$name_query->execute();
my($show_sql) = q{SHOW CREATE FUNCTION %s.%s}; # built up in the while() loop
my($function_name, $show_query, $dir, $fh);
while (my $name = $name_query->fetchrow_arrayref()) {
$function_name = $name->[0];
if ($debug_mode) {
printf STDERR (" Found FUNCTION : %s.%s\n", $schema, $function_name);
} else {
if (! $dir) {
$dir = sprintf("%s/%s/%s", $output_dir, $schema, 'FUNCTION');
make_path($dir);
}
$fh = new IO::File("${dir}/${function_name}.sql","w");
printf $fh ("# FUNCTION : %s.%s\n", $schema, $function_name);
printf $fh ("DELIMITER %s\n", q{$$});
printf $fh ("USE %s %s\n", $schema, q{$$});
printf $fh ("DROP FUNCTION IF EXISTS %s.%s %s\n\n", $schema, $function_name, q{$$});
# Cannot use a prepared statement here, since the function
# name will be escaped (quoted) as a string.
#
$show_query = $obj->[0]->prepare(sprintf($show_sql,$schema,$function_name));
$show_query->execute();
while (my $output = $show_query->fetchrow_arrayref()) {
print $fh $output->[2],"\n";
}
printf $fh ("%s\n\nDELIMITER %s\n", q{$$}, q{;});
$fh->close();
}
}
}