From: Rafael Kitover Date: Wed, 31 Mar 2010 21:33:36 +0000 (-0400) Subject: better type info for MySQL X-Git-Tag: 0.06000~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class-Schema-Loader.git;a=commitdiff_plain;h=26334ec166bf25d77c0342c85efebe853303ff41 better type info for MySQL --- diff --git a/Changes b/Changes index 57f9f41..f6df857 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for Perl extension DBIx::Class::Schema::Loader - - t/11mysql_common.t now tests MySQL data types (jhannah) + - better type info for MySQL + - initial MySQL data type tests (jhannah) - don't set result_namespace if it's 'Result' - support for MSSQL databases with case sensitive collation - do not try to detect driver and rebless when used with a custom diff --git a/lib/DBIx/Class/Schema/Loader/DBI.pm b/lib/DBIx/Class/Schema/Loader/DBI.pm index 2eda983..feacaa8 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI.pm @@ -292,11 +292,20 @@ sub _columns_info_for { if ($dbh->can('column_info')) { my %result; eval { - my $sth = $dbh->column_info( undef, $self->db_schema, $table, '%' ); + my $sth = eval { local $SIG{__WARN__} = sub {}; $dbh->column_info( undef, $self->db_schema, $table, '%' ); }; while ( my $info = $sth->fetchrow_hashref() ){ my $column_info = {}; - $column_info->{data_type} = $info->{TYPE_NAME}; - $column_info->{size} = $info->{COLUMN_SIZE} if defined $info->{COLUMN_SIZE}; + $column_info->{data_type} = lc $info->{TYPE_NAME}; + + my $size = $info->{COLUMN_SIZE}; + + if (defined $size && defined $info->{DECIMAL_DIGITS}) { + $column_info->{size} = [$size, $info->{DECIMAL_DIGITS}]; + } + elsif (defined $size) { + $column_info->{size} = $size; + } + $column_info->{is_nullable} = $info->{NULLABLE} ? 1 : 0; $column_info->{default_value} = $info->{COLUMN_DEF} if defined $info->{COLUMN_DEF}; my $col_name = $info->{COLUMN_NAME}; @@ -320,8 +329,17 @@ sub _columns_info_for { my @columns = @{ $self->_is_case_sensitive ? $sth->{NAME} : $sth->{NAME_lc} }; for my $i ( 0 .. $#columns ){ my $column_info = {}; - $column_info->{data_type} = $sth->{TYPE}->[$i]; - $column_info->{size} = $sth->{PRECISION}->[$i] if $sth->{PRECISION}->[$i]; + $column_info->{data_type} = lc $sth->{TYPE}->[$i]; + + my $size = $sth->{PRECISION}[$i]; + + if (defined $size && defined $sth->{SCALE}[$i]) { + $column_info->{size} = [$size, $sth->{SCALE}[$i]]; + } + elsif (defined $size) { + $column_info->{size} = $size; + } + $column_info->{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0; if ($column_info->{data_type} =~ m/^(.*?)\((.*?)\)$/) { @@ -370,3 +388,4 @@ the same terms as Perl itself. =cut 1; +# vim:et sts=4 sw=4 tw=0: diff --git a/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm b/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm index b649ef9..5d9cd2e 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm @@ -115,6 +115,58 @@ sub _table_uniq_info { return \@uniqs; } +sub _columns_info_for { + my $self = shift; + my ($table) = @_; + + my $result = $self->next::method(@_); + + my $dbh = $self->schema->storage->dbh; + + while (my ($col, $info) = each %$result) { + delete $info->{size} + unless $info->{data_type} =~ /^(?: (?:var)?(?:char(?:acter)?|binary) | bit | year)\z/ix; + + if ($info->{data_type} eq 'int') { + $info->{data_type} = 'integer'; + } + elsif ($info->{data_type} eq 'double') { + $info->{data_type} = 'double precision'; + } + + my ($precision, $scale, $column_type) = eval { $dbh->selectrow_array(<<'EOF', {}, lc $table, lc $col) }; +SELECT numeric_precision, numeric_scale, column_type +FROM information_schema.columns +WHERE lower(table_name) = ? AND lower(column_name) = ? +EOF + $column_type = '' if not defined $column_type; + + if ($info->{data_type} eq 'bit' && (not exists $info->{size})) { + $info->{size} = $precision if defined $precision; + } + elsif ($info->{data_type} =~ /^(?:float|double precision|decimal)\z/) { + if (defined $precision && defined $scale) { + if ($precision == 10 && $scale == 0) { + delete $info->{size}; + } + else { + $info->{size} = [$precision,$scale]; + } + } + } + elsif ($info->{data_type} eq 'year') { + if ($column_type =~ /\(2\)/) { + $info->{size} = 2; + } + elsif ($column_type =~ /\(4\)/ || $info->{size} == 4) { + delete $info->{size}; + } + } + } + + return $result; +} + sub _extra_column_info { no warnings 'uninitialized'; my ($self, $table, $col, $info, $dbi_info) = @_; @@ -155,3 +207,4 @@ the same terms as Perl itself. =cut 1; +# vim:et sw=4 sts=4 tw=0: diff --git a/t/11mysql_common.t b/t/11mysql_common.t index 44924eb..34ae23b 100644 --- a/t/11mysql_common.t +++ b/t/11mysql_common.t @@ -1,14 +1,14 @@ use strict; +use Test::More; use lib qw(t/lib); use dbixcsl_common_tests; -use Test::More; my $dsn = $ENV{DBICTEST_MYSQL_DSN} || ''; my $user = $ENV{DBICTEST_MYSQL_USER} || ''; my $password = $ENV{DBICTEST_MYSQL_PASS} || ''; my $test_innodb = $ENV{DBICTEST_MYSQL_INNODB} || 0; -my $skip_rels_msg = 'You need to set the DBICTEST_MYSQL_INNODB environment variable to test relationships'; +my $skip_rels_msg = 'You need to set the DBICTEST_MYSQL_INNODB environment variable to test relationships.'; my $tester = dbixcsl_common_tests->new( vendor => 'Mysql', @@ -18,92 +18,117 @@ my $tester = dbixcsl_common_tests->new( user => $user, password => $password, connect_info_opts=> { on_connect_call => 'set_strict_mode' }, - skip_rels => $test_innodb ? 0 : $skip_rels_msg, + skip_rels => $test_innodb ? 0 : 1, no_inline_rels => 1, no_implicit_rels => 1, data_types => { # http://dev.mysql.com/doc/refman/5.5/en/data-type-overview.html # Numeric Types - 'smallint ' => { data_type => 'SMALLINT', size => 6 }, # Space in key makes column name smallint_ - 'mediumint ' => { data_type => 'MEDIUMINT', size => 9 }, # to avoid MySQL reserved word. - 'int ' => { data_type => 'INT', size => 11 }, - 'integer ' => { data_type => 'INT', size => 11 }, - 'bigint ' => { data_type => 'BIGINT', size => 20 }, - 'serial ' => { data_type => 'BIGINT', size => 20, is_auto_increment => 1, extra => {unsigned => 1} }, - 'float ' => { data_type => 'FLOAT', size => 32 }, - 'double ' => { data_type => 'DOUBLE', size => 64 }, + 'bit' => { data_type => 'bit', size => 1 }, + 'bit(11)' => { data_type => 'bit', size => 11 }, + + 'bool' => { data_type => 'tinyint' }, + 'boolean' => { data_type => 'tinyint' }, + 'tinyint' => { data_type => 'tinyint' }, + 'tinyint unsigned' + => { data_type => 'tinyint', extra => { unsigned => 1 } }, + 'smallint' => { data_type => 'smallint' }, + 'smallint unsigned' + => { data_type => 'smallint', extra => { unsigned => 1 } }, + 'mediumint' => { data_type => 'mediumint' }, + 'mediumint unsigned' + => { data_type => 'mediumint', extra => { unsigned => 1 } }, + 'int' => { data_type => 'integer' }, + 'int unsigned' + => { data_type => 'integer', extra => { unsigned => 1 } }, + 'integer' => { data_type => 'integer' }, + 'integer unsigned' + => { data_type => 'integer', extra => { unsigned => 1 } }, + 'bigint' => { data_type => 'bigint' }, + 'bigint unsigned' + => { data_type => 'bigint', extra => { unsigned => 1 } }, + + 'serial' => { data_type => 'bigint', is_auto_increment => 1, extra => { unsigned => 1 } }, + + 'float' => { data_type => 'float' }, + 'float unsigned' + => { data_type => 'float', extra => { unsigned => 1 } }, + 'double' => { data_type => 'double precision' }, + 'double unsigned' + => { data_type => 'double precision', extra => { unsigned => 1 } }, 'double precision' => - { data_type => 'DOUBLE', size => 64 }, - 'decimal ' => { data_type => 'DECIMAL', size => 10 }, - 'dec ' => { data_type => 'DECIMAL', size => 10 }, - 'fixed ' => { data_type => 'DECIMAL', size => 10 }, + { data_type => 'double precision' }, + 'double precision unsigned' + => { data_type => 'double precision', extra => { unsigned => 1 } }, + + # we skip 'real' because its alias depends on the 'REAL AS FLOAT' setting + + 'float(2)' => { data_type => 'float' }, + 'float(24)' => { data_type => 'float' }, + 'float(25)' => { data_type => 'double precision' }, + + 'float(3,3)' => { data_type => 'float', size => [3,3] }, + 'double(3,3)' => { data_type => 'double precision', size => [3,3] }, + 'double precision(3,3)' + => { data_type => 'double precision', size => [3,3] }, + + 'decimal' => { data_type => 'decimal' }, + 'decimal unsigned' + => { data_type => 'decimal', extra => { unsigned => 1 } }, + 'dec' => { data_type => 'decimal' }, + 'numeric' => { data_type => 'decimal' }, + 'fixed' => { data_type => 'decimal' }, + + 'decimal(3)' => { data_type => 'decimal', size => [3,0] }, + + 'decimal(3,3)' => { data_type => 'decimal', size => [3,3] }, + 'dec(3,3)' => { data_type => 'decimal', size => [3,3] }, + 'numeric(3,3)' => { data_type => 'decimal', size => [3,3] }, + 'fixed(3,3)' => { data_type => 'decimal', size => [3,3] }, + # Date and Time Types - 'date ' => { data_type => 'DATE', size => 10 }, - 'datetime ' => { data_type => 'DATETIME', size => 19 }, - 'timestamp ' => { data_type => 'TIMESTAMP', size => 14, default_value => \"CURRENT_TIMESTAMP" }, - 'time ' => { data_type => 'TIME', size => 8 }, - 'year ' => { data_type => 'YEAR', size => 4 }, + 'date' => { data_type => 'date' }, + 'datetime' => { data_type => 'datetime' }, + 'timestamp DEFAULT current_timestamp' + => { data_type => 'timestamp', default_value => \"CURRENT_TIMESTAMP" }, + 'time' => { data_type => 'time' }, + 'year' => { data_type => 'year' }, + 'year(4)' => { data_type => 'year' }, + 'year(2)' => { data_type => 'year', size => 2 }, + # String Types - 'char ' => { data_type => 'CHAR', size => 1 }, - 'varchar(20)' => { data_type => 'VARCHAR', size => 20 }, - 'binary(1)' => { data_type => 'BINARY', size => 1 }, - 'varbinary(1)' => { data_type => 'VARBINARY', size => 1 }, - 'tinytext ' => { data_type => 'TINYTEXT', size => 255 }, - 'text ' => { data_type => 'TEXT', size => 65535 }, - 'longtext ' => { data_type => 'LONGTEXT', size => 4294967295 }, - 'tinyblob ' => { data_type => 'TINYBLOB', size => 255 }, - 'blob ' => { data_type => 'BLOB', size => 65535 }, - 'mediumblob ' => { data_type => 'MEDIUMBLOB',size => 16777215 }, - 'longblob ' => { data_type => 'LONGBLOB', size => 4294967295 }, - # Hmm... need some t/lib/dbixcsl_common_tests.pm hackery to get these working I think... - # 'enum(1,2,3)' => { data_type => 'ENUM', size => 1 }, - # 'set(1,2,3)' => { data_type => 'SET', size => 1 }, + 'char' => { data_type => 'char', size => 1 }, + 'char(11)' => { data_type => 'char', size => 11 }, + 'varchar(20)' => { data_type => 'varchar', size => 20 }, + 'binary' => { data_type => 'binary', size => 1 }, + 'binary(11)' => { data_type => 'binary', size => 11 }, + 'varbinary(20)'=> { data_type => 'varbinary', size => 20 }, + + 'tinyblob' => { data_type => 'tinyblob' }, + 'tinytext' => { data_type => 'tinytext' }, + 'blob' => { data_type => 'blob' }, + + # text(M) types will map to the appropriate type, length is not stored + 'text' => { data_type => 'text' }, + + 'mediumblob' => { data_type => 'mediumblob' }, + 'mediumtext' => { data_type => 'mediumtext' }, + 'longblob' => { data_type => 'longblob' }, + 'longtext' => { data_type => 'longtext' }, + + "enum('foo', 'bar', 'baz')" + => { data_type => 'enum', extra => { list => [qw/foo bar baz/] } }, + "set('foo', 'bar', 'baz')" + => { data_type => 'set', extra => { list => [qw/foo bar baz/] } }, }, - extra => { - create => [ - qq{ - CREATE TABLE mysql_loader_test1 ( - id INTEGER UNSIGNED NOT NULL PRIMARY KEY, - value ENUM('foo', 'bar', 'baz') - ) - }, - qq{ - CREATE TABLE mysql_loader_test2 ( - id INTEGER UNSIGNED NOT NULL PRIMARY KEY, - somets TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP - ) - }, - ], - drop => [ qw/ mysql_loader_test1 mysql_loader_test2 / ], - count => 5, - run => sub { - my ($schema, $monikers, $classes) = @_; - - my $rs = $schema->resultset($monikers->{mysql_loader_test1}); - my $column_info = $rs->result_source->column_info('id'); - - is($column_info->{extra}->{unsigned}, 1, 'Unsigned MySQL columns'); - - $column_info = $rs->result_source->column_info('value'); - - like($column_info->{data_type}, qr/^enum$/i, 'MySQL ENUM type'); - is_deeply($column_info->{extra}->{list}, [qw/foo bar baz/], - 'MySQL ENUM values'); - - $rs = $schema->resultset($monikers->{mysql_loader_test2}); - $column_info = $rs->result_source->column_info('somets'); - my $default = $column_info->{default_value}; - ok ((ref($default) eq 'SCALAR'), - 'CURRENT_TIMESTAMP default_value is a scalar ref'); - like $$default, qr/^CURRENT_TIMESTAMP\z/i, - 'CURRENT_TIMESTAMP default eq "CURRENT_TIMESTAMP"'; - }, - } ); if( !$dsn || !$user ) { $tester->skip_tests('You need to set the DBICTEST_MYSQL_DSN, _USER, and _PASS environment variables'); } else { + diag $skip_rels_msg if not $test_innodb; $tester->run_tests(); } + +# vim:et sts=4 sw=4 tw=0: diff --git a/t/lib/dbixcsl_common_tests.pm b/t/lib/dbixcsl_common_tests.pm index 1074d35..90184cf 100644 --- a/t/lib/dbixcsl_common_tests.pm +++ b/t/lib/dbixcsl_common_tests.pm @@ -12,6 +12,7 @@ use Digest::MD5; use File::Find 'find'; use Class::Unload (); use Data::Dumper::Concise; +use List::MoreUtils 'apply'; my $DUMP_DIR = './t/_common_dump'; rmtree $DUMP_DIR; @@ -1555,15 +1556,23 @@ sub setup_data_type_tests { my %seen_col_names; while (my ($col_def, $expected_info) = each %$types) { - my $have_size = $col_def =~ /\(/ ? 1 : 0; + (my $type_alias = lc($col_def)) =~ s/\( ([^)]+) \)//xg; + + my $size = $1; + $size = '' unless defined $size; + $size =~ s/\s+//g; + my @size = split /,/, $size; - (my $type_alias = lc($col_def)) =~ s/\([^()]+\)//g; $type_alias =~ s/\s/_/g; $type_alias =~ s/\W//g; - my @size = grep defined($_), $col_def =~ /\( \s* (\d+) \s* ,? \s* (\d+)?/x; + my $col_name = 'col_' . $type_alias; + + if (@size) { + my $size_name = join '_', apply { s/\W//g } @size; - my $col_name = $type_alias . ($have_size ? "_with_size_".(join '_', @size) : ''); + $col_name .= "_with_size_$size_name"; + } $col_name .= "_$seen_col_names{$col_name}" if $seen_col_names{$col_name}++;