move all mysql- and Pg-specific code to DBD driver callbacks
[DBIx-DBSchema.git] / DBSchema / DBD / mysql.pm
index f3804dd..a99dcf4 100644 (file)
@@ -4,12 +4,13 @@ use strict;
 use vars qw($VERSION @ISA %typemap);
 use DBIx::DBSchema::DBD;
 
-$VERSION = '0.03';
+$VERSION = '0.06';
 @ISA = qw(DBIx::DBSchema::DBD);
 
 %typemap = (
   'TIMESTAMP'      => 'DATETIME',
   'SERIAL'         => 'INTEGER',
+  'BIGSERIAL'      => 'BIGINT',
   'BOOL'           => 'TINYINT',
   'LONG VARBINARY' => 'LONGBLOB',
 );
@@ -34,10 +35,12 @@ This module implements a MySQL-native driver for DBIx::DBSchema.
 
 sub columns {
   my($proto, $dbh, $table ) = @_;
+  my $oldkhv=$dbh->{FetchHashKeyName};
+  $dbh->{FetchHashKeyName}="NAME";
   my $sth = $dbh->prepare("SHOW COLUMNS FROM $table") or die $dbh->errstr;
   $sth->execute or die $sth->errstr;
-  map {
-    $_->{'Type'} =~ /^(\w+)\(?([\d\,]+)?\)?( unsigned)?$/
+  my @r = map {
+    $_->{'Type'} =~ /^(\w+)\(?([^)]+)?\)?( \d+)?$/
       or die "Illegal type: ". $_->{'Type'}. "\n";
     my($type, $length) = ($1, $2);
     [
@@ -49,6 +52,8 @@ sub columns {
       $_->{'Extra'}
     ]
   } @{ $sth->fetchall_arrayref( {} ) };
+  $dbh->{FetchHashKeyName}=$oldkhv;
+  @r;
 }
 
 #sub primary_key {
@@ -83,6 +88,8 @@ sub index {
 
 sub _show_index {
   my($proto, $dbh, $table ) = @_;
+  my $oldkhv=$dbh->{FetchHashKeyName};
+  $dbh->{FetchHashKeyName}="NAME";
   my $sth = $dbh->prepare("SHOW INDEX FROM $table")
     or die $dbh->errstr;
   $sth->execute or die $sth->errstr;
@@ -98,10 +105,31 @@ sub _show_index {
       push @{ $unique{ $row->{'Key_name'} } }, $row->{'Column_name'};
     }
   }
+  $dbh->{FetchHashKeyName}=$oldkhv;
 
   ( $pkey, \%unique, \%index );
 }
 
+sub column_callback {
+  my( $proto, $dbh, $table, $column_obj ) = @_;
+
+  my $hashref = { 'explicit_null' => 1, };
+
+  $hashref->{'effective_local'} = 'AUTO_INCREMENT'
+    if $column_obj->type =~ /^(\w*)SERIAL$/i;
+
+  if ( $column_obj->default =~ /^(NOW)\(\)$/i
+       && $column_obj->type =~ /^(TIMESTAMP|DATETIME)$/i ) {
+
+    $hashref->{'effective_default'} = 'CURRENT_TIMESTAMP';
+    $hashref->{'effective_type'} = 'TIMESTAMP';
+
+  }
+
+  $hashref;
+
+}
+
 =head1 AUTHOR
 
 Ivan Kohler <ivan-dbix-dbschema@420.am>
@@ -110,6 +138,7 @@ Ivan Kohler <ivan-dbix-dbschema@420.am>
 
 Copyright (c) 2000 Ivan Kohler
 Copyright (c) 2000 Mail Abuse Prevention System LLC
+Copyright (c) 2007 Freeside Internet Services, Inc.
 All rights reserved.
 This program is free software; you can redistribute it and/or modify it under
 the same terms as Perl itself.