| 1 | package SQL::Loader::MySQL; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use base qw( SQL::Loader ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | 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 => <boolean>, | ||||||||||||||||||||||||||||
| 25 | quiet => <boolean> | ||||||||||||||||||||||||||||
| 26 | )->run; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | The database specified by $dbname must exist. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head3 In URL specified | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | <h2>$table_name</h2> | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | ... | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | <h3>Purpose</h3> | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | ... | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | <table> | ||||||||||||||||||||||||||||
| 41 | <tr> | ||||||||||||||||||||||||||||
| 42 | <td>$order</td> | ||||||||||||||||||||||||||||
| 43 | <td>$column_name</td> | ||||||||||||||||||||||||||||
| 44 | <td>$column_type</td> | ||||||||||||||||||||||||||||
| 45 | <td>$additional_info</td> | ||||||||||||||||||||||||||||
| 46 | </tr> | ||||||||||||||||||||||||||||
| 47 | </table> | ||||||||||||||||||||||||||||
| 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<SQL::Loader> | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 INHERITANCE | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | L<SQL::Loader> | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 METHODS | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head2 create | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | create a mysql table | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub create_table { | ||||||||||||||||||||||||||
|
Subroutine
create_table: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 0 | 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}) { | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ($rr->[2] =~ /^\s*$/ || $rr->[2] =~ / \;/) { # cols with no type set yet are skipped - | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$rr->[2] =~ /^\s*$/ or $rr->[2] =~ / \;/
MC/DC0%0/2
$rr->[2] =~ /^\s*$/ or $rr->[2] =~ / \;/
$rr->[2] =~ /^\s*$/
$rr->[2] =~ / \;/
Truth table0%0/3
A or B
A= $rr->[2] =~ /^\s*$/B= $rr->[2] =~ / \;/
| |||||||||||||||||||||||||||||
| 97 | # considered 'non-production' tables | ||||||||||||||||||||||||||||
| 98 | 0 | $notype = 1; | |||||||||||||||||||||||||||
| 99 | 0 | next; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ( $rcount == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | $q = "DROP TABLE IF EXISTS ".$name->[0].";"; | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | $dbh->do( $q ) || die $dbh->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if ( $rr->[1] eq 'id' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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 | ▶ | $q .= ", " unless $rcount == scalar(@{$cols} - 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | $rcount++; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | $q .= ");"; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | unless ($notype) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | print "RUNNING QUERY: $q\n" unless $quiet; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | $dbh->do( $q ) || die $dbh->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | unless ( $quiet ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub connect_string { | ||||||||||||||||||||||||||
|
Subroutine
connect_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | 0 | 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 | 2 | 1; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||