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',
20 DBIx::DBSchema::DBD::mysql - MySQL native driver for DBIx::DBSchema
27 $dbh = DBI->connect('dbi:mysql:database', 'user', 'pass');
28 $schema = new_native DBIx::DBSchema $dbh;
32 This module implements a MySQL-native driver for DBIx::DBSchema.
38 my($proto, $dbh, $table ) = @_;
39 my $oldkhv=$dbh->{FetchHashKeyName};
40 $dbh->{FetchHashKeyName}="NAME";
41 my $sth = $dbh->prepare("SHOW COLUMNS FROM $table") or die $dbh->errstr;
42 $sth->execute or die $sth->errstr;
45 $_->{'Type'} =~ /^(\w+)\(?([^)]+)?\)?( \d+)?$/
46 or die "Illegal type: ". $_->{'Type'}. "\n";
47 my($type, $length) = ($1, $2);
51 ( $_->{'Null'} =~ /^YES$/i ? 'NULL' : '' ),
56 } @{ $sth->fetchall_arrayref( {} ) };
57 $dbh->{FetchHashKeyName}=$oldkhv;
62 # my($proto, $dbh, $table ) = @_;
63 # my $primary_key = '';
64 # my $sth = $dbh->prepare("SHOW INDEX FROM $table")
65 # or die $dbh->errstr;
66 # $sth->execute or die $sth->errstr;
67 # my @pkey = map { $_->{'Column_name'} } grep {
68 # $_->{'Key_name'} eq "PRIMARY"
69 # } @{ $sth->fetchall_arrayref( {} ) };
70 # scalar(@pkey) ? $pkey[0] : '';
74 my($proto, $dbh, $table) = @_;
75 my($pkey, $unique_href, $index_href) = $proto->_show_index($dbh, $table);
80 my($proto, $dbh, $table) = @_;
81 my($pkey, $unique_href, $index_href) = $proto->_show_index($dbh, $table);
86 my($proto, $dbh, $table) = @_;
87 my($pkey, $unique_href, $index_href) = $proto->_show_index($dbh, $table);
92 my($proto, $dbh, $table ) = @_;
93 my $oldkhv=$dbh->{FetchHashKeyName};
94 $dbh->{FetchHashKeyName}="NAME";
95 my $sth = $dbh->prepare("SHOW INDEX FROM $table")
97 $sth->execute or die $sth->errstr;
101 foreach my $row ( @{ $sth->fetchall_arrayref({}) } ) {
102 if ( $row->{'Key_name'} eq 'PRIMARY' ) {
103 $pkey = $row->{'Column_name'};
104 } elsif ( $row->{'Non_unique'} ) { #index
105 push @{ $index{ $row->{'Key_name'} } }, $row->{'Column_name'};
107 push @{ $unique{ $row->{'Key_name'} } }, $row->{'Column_name'};
110 $dbh->{FetchHashKeyName}=$oldkhv;
112 ( $pkey, \%unique, \%index );
115 sub column_callback {
116 my( $proto, $dbh, $table, $column_obj ) = @_;
118 my $hashref = { 'explicit_null' => 1, };
120 $hashref->{'effective_local'} = 'AUTO_INCREMENT'
121 if $column_obj->type =~ /^(\w*)SERIAL$/i;
123 if ( $column_obj->default =~ /^(NOW)\(\)$/i
124 && $column_obj->type =~ /^(TIMESTAMP|DATETIME)$/i ) {
126 $hashref->{'effective_default'} = 'CURRENT_TIMESTAMP';
127 $hashref->{'effective_type'} = 'TIMESTAMP';
135 sub alter_column_callback {
136 my( $proto, $dbh, $table, $old_column, $new_column ) = @_;
137 my $old_name = $old_column->name;
138 my $new_def = $new_column->line($dbh);
140 # this would have been nice, but it appears to be doing too much...
142 # return {} if $old_column->line($dbh) eq $new_column->line($dbh);
145 # { 'sql_alter_null' =>
146 # "ALTER TABLE $table CHANGE $old_name $new_def",
149 return {} if $old_column->null eq $new_column->null;
150 { 'sql_alter_null' =>
151 "ALTER TABLE $table MODIFY $new_def",
159 Ivan Kohler <ivan-dbix-dbschema@420.am>
163 Copyright (c) 2000 Ivan Kohler
164 Copyright (c) 2000 Mail Abuse Prevention System LLC
165 Copyright (c) 2007 Freeside Internet Services, Inc.
167 This program is free software; you can redistribute it and/or modify it under
168 the same terms as Perl itself.
174 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>