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);
52 ( $_->{'Null'} =~ /^YES$/i ? 'NULL' : '' ),
57 } @{ $sth->fetchall_arrayref( {} ) };
58 $dbh->{FetchHashKeyName}=$oldkhv;
63 # my($proto, $dbh, $table ) = @_;
64 # my $primary_key = '';
65 # my $sth = $dbh->prepare("SHOW INDEX FROM $table")
66 # or die $dbh->errstr;
67 # $sth->execute or die $sth->errstr;
68 # my @pkey = map { $_->{'Column_name'} } grep {
69 # $_->{'Key_name'} eq "PRIMARY"
70 # } @{ $sth->fetchall_arrayref( {} ) };
71 # scalar(@pkey) ? $pkey[0] : '';
75 my($proto, $dbh, $table) = @_;
76 my($pkey, $unique_href, $index_href) = $proto->_show_index($dbh, $table);
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 $oldkhv=$dbh->{FetchHashKeyName};
95 $dbh->{FetchHashKeyName}="NAME";
96 my $sth = $dbh->prepare("SHOW INDEX FROM $table")
98 $sth->execute or die $sth->errstr;
102 foreach my $row ( @{ $sth->fetchall_arrayref({}) } ) {
103 if ( $row->{'Key_name'} eq 'PRIMARY' ) {
104 $pkey = $row->{'Column_name'};
105 } elsif ( $row->{'Non_unique'} ) { #index
106 push @{ $index{ $row->{'Key_name'} } }, $row->{'Column_name'};
108 push @{ $unique{ $row->{'Key_name'} } }, $row->{'Column_name'};
111 $dbh->{FetchHashKeyName}=$oldkhv;
113 ( $pkey, \%unique, \%index );
116 sub column_callback {
117 my( $proto, $dbh, $table, $column_obj ) = @_;
119 my $hashref = { 'explicit_null' => 1, };
121 $hashref->{'effective_local'} = 'AUTO_INCREMENT'
122 if $column_obj->type =~ /^(\w*)SERIAL$/i;
124 if ( $column_obj->quoted_default =~ /^(NOW)\(\)$/i
125 && $column_obj->type =~ /^(TIMESTAMP|DATETIME)$/i ) {
127 $hashref->{'effective_default'} = 'CURRENT_TIMESTAMP';
128 $hashref->{'effective_type'} = 'TIMESTAMP';
136 sub alter_column_callback {
137 my( $proto, $dbh, $table, $old_column, $new_column ) = @_;
138 my $old_name = $old_column->name;
139 my $new_def = $new_column->line($dbh);
141 # this would have been nice, but it appears to be doing too much...
143 # return {} if $old_column->line($dbh) eq $new_column->line($dbh);
146 # { 'sql_alter_null' =>
147 # "ALTER TABLE $table CHANGE $old_name $new_def",
150 return {} if $old_column->null eq $new_column->null;
151 { 'sql_alter_null' =>
152 "ALTER TABLE $table MODIFY $new_def",
160 Ivan Kohler <ivan-dbix-dbschema@420.am>
164 Copyright (c) 2000 Ivan Kohler
165 Copyright (c) 2000 Mail Abuse Prevention System LLC
166 Copyright (c) 2007-2010 Freeside Internet Services, Inc.
168 This program is free software; you can redistribute it and/or modify it under
169 the same terms as Perl itself.
175 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>