1 package DBIx::DBSchema::DBD::mysql;
4 use vars qw($VERSION @ISA %typemap);
5 use DBIx::DBSchema::DBD;
8 @ISA = qw(DBIx::DBSchema::DBD);
11 'TIMESTAMP' => 'DATETIME',
12 'SERIAL' => 'INTEGER',
13 'BIGSERIAL' => 'BIGINT',
15 'LONG VARBINARY' => 'LONGBLOB',
21 DBIx::DBSchema::DBD::mysql - MySQL native driver for DBIx::DBSchema
28 $dbh = DBI->connect('dbi:mysql:database', 'user', 'pass');
29 $schema = new_native DBIx::DBSchema $dbh;
33 This module implements a MySQL-native driver for DBIx::DBSchema.
39 my($proto, $dbh, $table ) = @_;
40 my $oldkhv=$dbh->{FetchHashKeyName};
41 $dbh->{FetchHashKeyName}="NAME";
42 my $sth = $dbh->prepare("SHOW COLUMNS FROM $table") or die $dbh->errstr;
43 $sth->execute or die $sth->errstr;
46 $_->{'Type'} =~ /^(\w+)\(?([^)]+)?\)?( \d+)?$/
47 or die "Illegal type: ". $_->{'Type'}. "\n";
48 my($type, $length) = ($1, $2);
50 my $default = $_->{'Default'};
51 $default = '' unless defined($default);
52 $default = \0 if $default eq '0';
53 $default = \'NOW()' if uc($default) eq 'CURRENT_TIMESTAMP';
58 ( $_->{'Null'} =~ /^YES$/i ? 'NULL' : '' ),
63 } @{ $sth->fetchall_arrayref( {} ) };
64 $dbh->{FetchHashKeyName}=$oldkhv;
69 # my($proto, $dbh, $table ) = @_;
70 # my $primary_key = '';
71 # my $sth = $dbh->prepare("SHOW INDEX FROM $table")
72 # or die $dbh->errstr;
73 # $sth->execute or die $sth->errstr;
74 # my @pkey = map { $_->{'Column_name'} } grep {
75 # $_->{'Key_name'} eq "PRIMARY"
76 # } @{ $sth->fetchall_arrayref( {} ) };
77 # scalar(@pkey) ? $pkey[0] : '';
81 my($proto, $dbh, $table) = @_;
82 my($pkey, $unique_href, $index_href) = $proto->_show_index($dbh, $table);
87 my($proto, $dbh, $table) = @_;
88 my($pkey, $unique_href, $index_href) = $proto->_show_index($dbh, $table);
93 my($proto, $dbh, $table) = @_;
94 my($pkey, $unique_href, $index_href) = $proto->_show_index($dbh, $table);
99 my($proto, $dbh, $table ) = @_;
100 my $oldkhv=$dbh->{FetchHashKeyName};
101 $dbh->{FetchHashKeyName}="NAME";
102 my $sth = $dbh->prepare("SHOW INDEX FROM $table")
104 $sth->execute or die $sth->errstr;
108 foreach my $row ( @{ $sth->fetchall_arrayref({}) } ) {
109 if ( $row->{'Key_name'} eq 'PRIMARY' ) {
110 $pkey = $row->{'Column_name'};
111 } elsif ( $row->{'Non_unique'} ) { #index
112 push @{ $index{ $row->{'Key_name'} } }, $row->{'Column_name'};
114 push @{ $unique{ $row->{'Key_name'} } }, $row->{'Column_name'};
117 $dbh->{FetchHashKeyName}=$oldkhv;
119 ( $pkey, \%unique, \%index );
122 sub column_callback {
123 my( $proto, $dbh, $table, $column_obj ) = @_;
125 my $hashref = { 'explicit_null' => 1, };
127 $hashref->{'effective_local'} = 'AUTO_INCREMENT'
128 if $column_obj->type =~ /^(\w*)SERIAL$/i;
130 if ( $column_obj->quoted_default =~ /^(NOW)\(\)$/i
131 && $column_obj->type =~ /^(TIMESTAMP|DATETIME)$/i ) {
133 $hashref->{'effective_default'} = 'CURRENT_TIMESTAMP';
134 $hashref->{'effective_type'} = 'TIMESTAMP';
142 sub alter_column_callback {
143 my( $proto, $dbh, $table, $old_column, $new_column ) = @_;
144 my $old_name = $old_column->name;
145 my $new_def = $new_column->line($dbh);
147 # this would have been nice, but it appears to be doing too much...
149 # return {} if $old_column->line($dbh) eq $new_column->line($dbh);
152 # { 'sql_alter_null' =>
153 # "ALTER TABLE $table CHANGE $old_name $new_def",
156 return {} if $old_column->null eq $new_column->null;
157 { 'sql_alter_null' =>
158 "ALTER TABLE $table MODIFY $new_def",
166 Ivan Kohler <ivan-dbix-dbschema@420.am>
170 Copyright (c) 2000 Ivan Kohler
171 Copyright (c) 2000 Mail Abuse Prevention System LLC
172 Copyright (c) 2007-2010 Freeside Internet Services, Inc.
174 This program is free software; you can redistribute it and/or modify it under
175 the same terms as Perl itself.
181 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>