From: Rafael Kitover Date: Sun, 31 Jan 2010 15:18:57 +0000 (+0000) Subject: better type mapping and default introspection for Sybase X-Git-Tag: 0.05000~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2fb9a4b3a885791abcff55e29015196d57725b91;p=dbsrgits%2FDBIx-Class-Schema-Loader.git better type mapping and default introspection for Sybase --- diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Sybase.pm b/lib/DBIx/Class/Schema/Loader/DBI/Sybase.pm index c1cef54..e9ecc5d 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Sybase.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Sybase.pm @@ -229,7 +229,7 @@ sub _table_uniq_info { return \@uniqs; } -# set data_type to 'undef' for computed columns +# get the correct data types and defaults sub _columns_info_for { my $self = shift; my ($table) = @_; @@ -237,18 +237,31 @@ sub _columns_info_for { my $dbh = $self->schema->storage->dbh; my $sth = $dbh->prepare(qq{ -SELECT c.name name, c.computedcol computedcol +SELECT c.name name, t.name type, cm.text deflt FROM syscolumns c JOIN sysobjects o ON c.id = o.id +LEFT JOIN systypes t ON c.type = t.type AND c.usertype = t.usertype +LEFT JOIN syscomments cm + ON cm.id = CASE WHEN c.cdefault = 0 THEN c.computedcol ELSE c.cdefault END WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U' }); $sth->execute; local $dbh->{FetchHashKeyName} = 'NAME_lc'; - my $computed_info = $sth->fetchall_hashref('name'); + my $info = $sth->fetchall_hashref('name'); - for my $col (keys %$result) { - $result->{$col}{data_type} = undef - if $computed_info->{$col}{computedcol}; + while (my ($col, $res) = each %$result) { + $res->{data_type} = $info->{$col}{type}; + + if (my $default = $info->{$col}{deflt}) { + if ($default =~ /^AS \s+ (\S+)/ix) { + my $function = $1; + $res->{default_value} = \$function; + } + elsif ($default =~ /^DEFAULT \s+ (\S+)/ix) { + my ($constant_default) = $1 =~ /^['"\[\]]?(.*?)['"\[\]]\z/; + $res->{default_value} = $constant_default; + } + } } return $result; diff --git a/t/15sybase_common.t b/t/15sybase_common.t index 903592f..9b1ab2e 100644 --- a/t/15sybase_common.t +++ b/t/15sybase_common.t @@ -34,32 +34,25 @@ my $tester = dbixcsl_common_tests->new( my $rsrc = $rs->result_source; is $rsrc->column_info('id')->{data_type}, - 'numeric', + 'int', 'INTEGER IDENTITY data_type is correct'; is $rsrc->column_info('id')->{is_auto_increment}, 1, 'INTEGER IDENTITY is_auto_increment => 1'; - { - local $TODO = 'timestamp introspection broken'; - - is $rsrc->column_info('ts')->{data_type}, - 'timestamp', - 'timestamps have the correct data_type'; - } + is $rsrc->column_info('ts')->{data_type}, + 'timestamp', + 'timestamps have the correct data_type'; is $rsrc->column_info('charfield')->{data_type}, 'varchar', 'VARCHAR has correct data_type'; - { - local $TODO = 'constant DEFAULT introspection'; - is $rsrc->column_info('charfield')->{default_value}, - 'foo', - 'constant DEFAULT is correct'; - } + is $rsrc->column_info('charfield')->{default_value}, + 'foo', + 'constant DEFAULT is correct'; is $rsrc->column_info('charfield')->{size}, 10, @@ -72,21 +65,17 @@ my $tester = dbixcsl_common_tests->new( $rsrc->column_info('computed_dt')->{data_type} ; - { - local $TODO = 'default_value for computed columns'; - - my $computed_dt_default = - $rsrc->column_info('computed_dt')->{default_value}; + my $computed_dt_default = + $rsrc->column_info('computed_dt')->{default_value}; - ok ((ref $computed_dt_default eq 'SCALAR'), - 'default_value for computed column is a scalar ref') -# or diag "default_value is: ", $computed_dt_default - ; + ok ((ref $computed_dt_default eq 'SCALAR'), + 'default_value for computed column is a scalar ref') + or diag "default_value is: ", $computed_dt_default + ; - eval { is $$computed_dt_default, - 'getdate()', - 'default_value for computed column is correct' }; - } + eval { is $$computed_dt_default, + 'getdate()', + 'default_value for computed column is correct' }; }, }, );