File Coverage

blib/lib/DBIx/DBSchema/DBD/mysql.pm
Criterion Covered Total %
statement 12 78 15.3
branch 0 36 0.0
condition 0 15 0.0
subroutine 4 11 36.3
pod 6 6 100.0
total 22 146 15.0


line stmt bran cond sub pod time code
1             package DBIx::DBSchema::DBD::mysql;
2              
3 1     1   1221 use strict;
  1         3  
  1         51  
4 1     1   6 use vars qw($VERSION @ISA %typemap);
  1         2  
  1         168  
5 1     1   7167 use DBIx::DBSchema::DBD;
  1         3  
  1         74  
6              
7             $VERSION = '0.09';
8             @ISA = qw(DBIx::DBSchema::DBD);
9              
10             %typemap = (
11             'TIMESTAMP' => 'DATETIME',
12             'SERIAL' => 'INTEGER',
13             'BIGSERIAL' => 'BIGINT',
14             'BOOL' => 'TINYINT',
15             'LONG VARBINARY' => 'LONGBLOB',
16             'TEXT' => 'LONGTEXT',
17             );
18              
19             =head1 NAME
20              
21             DBIx::DBSchema::DBD::mysql - MySQL native driver for DBIx::DBSchema
22              
23             =head1 SYNOPSIS
24              
25             use DBI;
26             use DBIx::DBSchema;
27              
28             $dbh = DBI->connect('dbi:mysql:database', 'user', 'pass');
29             $schema = new_native DBIx::DBSchema $dbh;
30              
31             =head1 DESCRIPTION
32              
33             This module implements a MySQL-native driver for DBIx::DBSchema.
34              
35             =cut
36 1     1   1211 use Data::Dumper;
  1         13294  
  1         1461  
37              
38             sub columns {
39 0     0 1   my($proto, $dbh, $table ) = @_;
40 0           my $oldkhv=$dbh->{FetchHashKeyName};
41 0           $dbh->{FetchHashKeyName}="NAME";
42 0 0         my $sth = $dbh->prepare("SHOW COLUMNS FROM $table") or die $dbh->errstr;
43 0 0         $sth->execute or die $sth->errstr;
44 0 0         my @r = map {
45             #warn Dumper($_);
46 0           $_->{'Type'} =~ /^(\w+)\(?([^)]+)?\)?( \d+)?$/
47             or die "Illegal type: ". $_->{'Type'}. "\n";
48 0           my($type, $length) = ($1, $2);
49              
50 0           my $default = $_->{'Default'};
51 0 0         if ( defined($default) ) {
52 0 0         $default = \"''" if $default eq '';
53 0 0         $default = \0 if $default eq '0';
54 0 0         $default = \'NOW()' if uc($default) eq 'CURRENT_TIMESTAMP';
55             } else {
56 0           $default = '';
57             }
58              
59             [
60 0 0         $_->{'Field'},
61             $type,
62             ( $_->{'Null'} =~ /^YES$/i ? 'NULL' : '' ),
63             $length,
64             $default,
65             $_->{'Extra'}
66             ]
67 0           } @{ $sth->fetchall_arrayref( {} ) };
68 0           $dbh->{FetchHashKeyName}=$oldkhv;
69 0           @r;
70             }
71              
72             #sub primary_key {
73             # my($proto, $dbh, $table ) = @_;
74             # my $primary_key = '';
75             # my $sth = $dbh->prepare("SHOW INDEX FROM $table")
76             # or die $dbh->errstr;
77             # $sth->execute or die $sth->errstr;
78             # my @pkey = map { $_->{'Column_name'} } grep {
79             # $_->{'Key_name'} eq "PRIMARY"
80             # } @{ $sth->fetchall_arrayref( {} ) };
81             # scalar(@pkey) ? $pkey[0] : '';
82             #}
83              
84             sub primary_key {
85 0     0 1   my($proto, $dbh, $table) = @_;
86 0           my($pkey, $unique_href, $index_href) = $proto->_show_index($dbh, $table);
87 0           $pkey;
88             }
89              
90             sub unique {
91 0     0 1   my($proto, $dbh, $table) = @_;
92 0           my($pkey, $unique_href, $index_href) = $proto->_show_index($dbh, $table);
93 0           $unique_href;
94             }
95              
96             sub index {
97 0     0 1   my($proto, $dbh, $table) = @_;
98 0           my($pkey, $unique_href, $index_href) = $proto->_show_index($dbh, $table);
99 0           $index_href;
100             }
101              
102             sub _show_index {
103 0     0     my($proto, $dbh, $table ) = @_;
104 0           my $oldkhv=$dbh->{FetchHashKeyName};
105 0           $dbh->{FetchHashKeyName}="NAME";
106 0 0         my $sth = $dbh->prepare("SHOW INDEX FROM $table")
107             or die $dbh->errstr;
108 0 0         $sth->execute or die $sth->errstr;
109              
110 0           my $pkey = '';
111 0           my(%index, %unique);
112 0           foreach my $row ( @{ $sth->fetchall_arrayref({}) } ) {
  0            
113 0 0         if ( $row->{'Key_name'} eq 'PRIMARY' ) {
    0          
114 0           $pkey = $row->{'Column_name'};
115             } elsif ( $row->{'Non_unique'} ) { #index
116 0           push @{ $index{ $row->{'Key_name'} } }, $row->{'Column_name'};
  0            
117             } else { #unique
118 0           push @{ $unique{ $row->{'Key_name'} } }, $row->{'Column_name'};
  0            
119             }
120             }
121 0           $dbh->{FetchHashKeyName}=$oldkhv;
122              
123 0           ( $pkey, \%unique, \%index );
124             }
125              
126             sub column_callback {
127 0     0 1   my( $proto, $dbh, $table, $column_obj ) = @_;
128              
129 0           my $hashref = { 'explicit_null' => 1, };
130              
131 0 0         $hashref->{'effective_local'} = 'AUTO_INCREMENT'
132             if $column_obj->type =~ /^(\w*)SERIAL$/i;
133              
134 0 0 0       if ( $column_obj->quoted_default =~ /^(NOW)\(\)$/i
135             && $column_obj->type =~ /^(TIMESTAMP|DATETIME)$/i ) {
136              
137 0           $hashref->{'effective_default'} = 'CURRENT_TIMESTAMP';
138 0           $hashref->{'effective_type'} = 'TIMESTAMP';
139              
140             }
141              
142 0           $hashref;
143              
144             }
145              
146             sub alter_column_callback {
147 0     0 1   my( $proto, $dbh, $table, $old_column, $new_column ) = @_;
148 0           my $old_name = $old_column->name;
149 0           my $new_def = $new_column->line($dbh);
150              
151 0           my $hashref = {};
152              
153 0           my %canonical = (
154             'INTEGER' => 'INT',
155             'SERIAL' => 'INT',
156             'BIGSERIAL' => 'BIGINT',
157             'REAL' => 'DOUBLE', #'FLOAT',
158             'DOUBLE PRECISION' => 'DOUBLE',
159             );
160 0           foreach ($old_column, $new_column) {
161 0 0         $_->type($canonical{uc($_->type)}) if $canonical{uc($_->type)};
162             }
163              
164 0           my %canonical_length = (
165             'INT' => 11,
166             'BIGINT' => 20,
167             'DECIMAL' => '10,0',
168             );
169 0 0 0       $new_column->length( $canonical_length{uc($new_column->type)} )
      0        
170             if $canonical_length{uc($new_column->type)}
171             && ($new_column->length||'') eq '';
172              
173             #change type/length
174 0 0 0       if ( uc($old_column->type) ne uc($new_column->type)
      0        
      0        
175             || ($old_column->length||'') ne ($new_column->length||'')
176             )
177             {
178 0           my $old_def = $old_column->line($dbh);
179 0           $hashref->{'sql_alter_type'} =
180             "CHANGE $old_name $new_def";
181             }
182              
183             #change nullability
184 0 0         if ( $old_column->null ne $new_column->null ) {
185 0           $hashref->{'sql_alter_null'} =
186             "ALTER TABLE $table MODIFY $new_def";
187             }
188              
189 0           $hashref;
190             }
191              
192             =head1 AUTHOR
193              
194             Ivan Kohler
195              
196             =head1 COPYRIGHT
197              
198             Copyright (c) 2000 Ivan Kohler
199             Copyright (c) 2000 Mail Abuse Prevention System LLC
200             Copyright (c) 2007-2013 Freeside Internet Services, Inc.
201             All rights reserved.
202             This program is free software; you can redistribute it and/or modify it under
203             the same terms as Perl itself.
204              
205             =head1 BUGS
206              
207             =head1 SEE ALSO
208              
209             L, L, L, L
210              
211             =cut
212              
213             1;
214