line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SQL::Loader::MySQL; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
85525
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
62
|
|
4
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
58
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
10
|
use base qw( SQL::Loader ); |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
1311
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
SQL::Loader::MySQL |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head3 In Perl script |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use SQL::Loader::MySQL; |
19
|
|
|
|
|
|
|
SQL::Loader::MySQL->new( |
20
|
|
|
|
|
|
|
url => $schema_url, |
21
|
|
|
|
|
|
|
dbname => $dbname, |
22
|
|
|
|
|
|
|
dbuser => $dbuser, |
23
|
|
|
|
|
|
|
dbpass => $dbpass, |
24
|
|
|
|
|
|
|
print_http_headers => , |
25
|
|
|
|
|
|
|
quiet => |
26
|
|
|
|
|
|
|
)->run; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
The database specified by $dbname must exist. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head3 In URL specified |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$table_name |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
... |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Purpose |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
... |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
| $order |
43
|
|
|
|
|
|
|
| $column_name |
44
|
|
|
|
|
|
|
| $column_type |
45
|
|
|
|
|
|
|
| $additional_info |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
| |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
For further details see README file. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 DESCRIPTION |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Screen scrape a database schema from a twiki ( or compatible ) web page and create it in a MySQL database. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 INPUT PARAMETERS |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
$schema_url - the url to scrape db schema from - required |
58
|
|
|
|
|
|
|
$dbname - name of database to use - required |
59
|
|
|
|
|
|
|
$dbuser - database username - required |
60
|
|
|
|
|
|
|
$dbpass - database password - required |
61
|
|
|
|
|
|
|
$print_http_headers ( boolean ) - Test $url server response only by requesting headers ( does not rebuild db ) |
62
|
|
|
|
|
|
|
$quiet ( boolean ) - do not print any informational messages |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 SEE ALSO |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
L |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 INHERITANCE |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
L |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 METHODS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 create |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
create a mysql table |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=cut |
81
|
|
|
|
|
|
|
sub create_table { |
82
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
83
|
0
|
|
|
|
|
|
my ( $name, $cols ) = @_; |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
my $dbname = $self->dbname(); |
86
|
0
|
|
|
|
|
|
my $dbuser = $self->dbuser(); |
87
|
0
|
|
|
|
|
|
my $dbpass = $self->dbpass(); |
88
|
0
|
|
|
|
|
|
my $quiet = $self->quiet(); |
89
|
0
|
|
|
|
|
|
my $dbh = $self->dbh(); |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
my $rcount = 0; |
92
|
0
|
|
|
|
|
|
my $notype = 0; |
93
|
0
|
|
|
|
|
|
my $q; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
foreach my $rr (@{$cols}) { |
|
0
|
|
|
|
|
|
|
96
|
0
|
0
|
0
|
|
|
|
if ($rr->[2] =~ /^\s*$/ || $rr->[2] =~ / \;/) { # cols with no type set yet are skipped - |
97
|
|
|
|
|
|
|
# considered 'non-production' tables |
98
|
0
|
|
|
|
|
|
$notype = 1; |
99
|
0
|
|
|
|
|
|
next; |
100
|
|
|
|
|
|
|
} |
101
|
0
|
0
|
|
|
|
|
if ( $rcount == 0 ) { |
102
|
0
|
|
|
|
|
|
$q = "DROP TABLE IF EXISTS ".$name->[0].";"; |
103
|
0
|
0
|
|
|
|
|
$dbh->do( $q ) || die $dbh->errstr; |
104
|
0
|
|
|
|
|
|
$q = "CREATE TABLE ".$name->[0]." ("; |
105
|
|
|
|
|
|
|
} |
106
|
0
|
|
|
|
|
|
$q .= $rr->[1] . " " . uc( $rr->[2] ); |
107
|
|
|
|
|
|
|
# set primary keys via 2 possible flags indicating to do so: |
108
|
0
|
0
|
|
|
|
|
if ( $rr->[1] eq 'id' ) { |
|
|
0
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# 1: a field name 'id' is automatically made PK A_I |
110
|
0
|
|
|
|
|
|
$q .= " PRIMARY KEY AUTO_INCREMENT"; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
elsif ( $rr->[3] =~ /PK/ ) { |
113
|
|
|
|
|
|
|
# 2: if description of a field contains the case sensitive letters 'PK' it will be made a primary key |
114
|
0
|
|
|
|
|
|
$q .= " PRIMARY KEY"; |
115
|
|
|
|
|
|
|
} |
116
|
0
|
0
|
|
|
|
|
$q .= ", " unless $rcount == scalar(@{$cols} - 1); |
|
0
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
$rcount++; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
$q .= ");"; |
121
|
|
|
|
|
|
|
|
122
|
0
|
0
|
|
|
|
|
unless ($notype) { |
123
|
0
|
0
|
|
|
|
|
print "RUNNING QUERY: $q\n" unless $quiet; |
124
|
0
|
0
|
|
|
|
|
$dbh->do( $q ) || die $dbh->errstr; |
125
|
0
|
0
|
|
|
|
|
unless ( $quiet ) { |
126
|
0
|
|
|
|
|
|
print "\n"; |
127
|
0
|
|
|
|
|
|
print '*' x 100, "\n\n"; |
128
|
0
|
|
|
|
|
|
print "OK", "\n\n"; |
129
|
0
|
|
|
|
|
|
print '*' x 100, "\n\n"; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
return 1; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 connect_string |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
return dbh connect string. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=cut |
141
|
|
|
|
|
|
|
sub connect_string { |
142
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
143
|
0
|
|
|
|
|
|
my $dbname = $self->dbname(); |
144
|
0
|
|
|
|
|
|
my $dbuser = $self->dbuser(); |
145
|
0
|
|
|
|
|
|
my $dbpass = $self->dbpass(); |
146
|
0
|
|
|
|
|
|
return ("dbi:mysql:$dbname","$dbuser","$dbpass"); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
1; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
__END__ |