[Bast-commits] r8492 - in branches/DBIx-Class-Schema-Loader/current: lib/DBIx/Class/Schema/Loader/DBI t

caelum at dev.catalyst.perl.org caelum at dev.catalyst.perl.org
Sun Jan 31 22:15:48 GMT 2010


Author: caelum
Date: 2010-01-31 22:15:48 +0000 (Sun, 31 Jan 2010)
New Revision: 8492

Modified:
   branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/DBI/Sybase.pm
   branches/DBIx-Class-Schema-Loader/current/t/12pg_common.t
   branches/DBIx-Class-Schema-Loader/current/t/15sybase_common.t
Log:
better size introspection for Sybase

Modified: branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/DBI/Sybase.pm
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/DBI/Sybase.pm	2010-01-31 15:18:57 UTC (rev 8491)
+++ branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/DBI/Sybase.pm	2010-01-31 22:15:48 UTC (rev 8492)
@@ -229,7 +229,7 @@
     return \@uniqs;
 }
 
-# get the correct data types and defaults
+# get the correct data types, defaults and size
 sub _columns_info_for {
     my $self    = shift;
     my ($table) = @_;
@@ -237,7 +237,8 @@
 
     my $dbh = $self->schema->storage->dbh;
     my $sth = $dbh->prepare(qq{
-SELECT c.name name, t.name type, cm.text deflt
+SELECT c.name name, t.name type, cm.text deflt, c.prec prec, c.scale scale,
+        c.length len
 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
@@ -262,6 +263,19 @@
                 $res->{default_value} = $constant_default;
             }
         }
+
+# XXX we need to handle "binary precision" for FLOAT(X) but I don't know what it means
+        if (my $data_type = $res->{data_type}) {
+            if ($data_type =~ /^(?:text|unitext|image|bigint|int|integer|smallint|tinyint|real|double|double precision|float|date|time|datetime|smalldatetime|money|smallmoney|timestamp|bit)\z/i) {
+                delete $res->{size};
+            }
+            elsif ($data_type =~ /^(?:numeric|decimal)\z/i) {
+                $res->{size} = [ $info->{$col}{prec}, $info->{$col}{scale} ];
+            }
+            elsif ($data_type =~ /^(?:unichar|univarchar)\z/i) {
+                $res->{size} /= 2;
+            }
+        }
     }
 
     return $result;

Modified: branches/DBIx-Class-Schema-Loader/current/t/12pg_common.t
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/t/12pg_common.t	2010-01-31 15:18:57 UTC (rev 8491)
+++ branches/DBIx-Class-Schema-Loader/current/t/12pg_common.t	2010-01-31 22:15:48 UTC (rev 8492)
@@ -98,8 +98,6 @@
                     the_decimal DECIMAL(6, 3)
                 )
             },
-# XXX figure out what to do with DECIMAL(precision, scale) aka
-# NUMERIC(precision, scale)
         ],
         drop  => [ qw/ pg_loader_test1 pg_loader_test2 pg_loader_test3 / ],
         count => 57,
@@ -131,7 +129,8 @@
             like $code, qr/^=head1 NAME\n\n^$class\n\n=head1 DESCRIPTION\n\n^very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long comment\n\n^=cut\n/m,
                 'long table comment is in DESCRIPTION';
 
-            my $rsrc = $schema->resultset('PgLoaderTest3')->result_source;
+            my $rsrc = $schema->resultset($monikers->{pg_loader_test3})
+                ->result_source;
             my @type_columns = grep /^a/, $rsrc->columns;
             my @without_precision = grep !/_with_precision\z/, @type_columns;
             my @with_precision    = grep  /_with_precision\z/, @type_columns;

Modified: branches/DBIx-Class-Schema-Loader/current/t/15sybase_common.t
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/t/15sybase_common.t	2010-01-31 15:18:57 UTC (rev 8491)
+++ branches/DBIx-Class-Schema-Loader/current/t/15sybase_common.t	2010-01-31 22:15:48 UTC (rev 8492)
@@ -3,6 +3,7 @@
 use dbixcsl_common_tests;
 use Test::More;
 use Test::Exception;
+use List::MoreUtils 'apply';
 
 my $dsn      = $ENV{DBICTEST_SYBASE_DSN} || '';
 my $user     = $ENV{DBICTEST_SYBASE_USER} || '';
@@ -24,9 +25,45 @@
                     computed_dt AS getdate()
                 )
             },
+# Test data types, see http://ispirer.com/wiki/sqlways/sybase/data-types
+# XXX handle FLOAT(P) at some point
+            q{
+                CREATE TABLE sybase_loader_test2 (
+                    id INTEGER IDENTITY NOT NULL PRIMARY KEY,
+                    a_text TEXT,
+                    a_unitext UNITEXT,
+                    an_image IMAGE,
+                    a_bigint BIGINT,
+                    an_int INT,
+                    an_integer INTEGER,
+                    a_smallint SMALLINT,
+                    a_tinyint TINYINT,
+                    a_real REAL,
+                    a_double_precision DOUBLE PRECISION,
+                    a_date DATE,
+                    a_time TIME,
+                    a_datetime DATETIME,
+                    a_smalldatetime SMALLDATETIME,
+                    a_money MONEY,
+                    a_smallmoney SMALLMONEY,
+                    a_timestamp timestamp,
+                    a_bit BIT,
+                    a_char_with_precision CHAR(2),
+                    an_nchar_with_precision NCHAR(2),
+                    a_unichar_with_precision UNICHAR(2),
+                    a_varchar_with_precision VARCHAR(2),
+                    an_nvarchar_with_precision NVARCHAR(2),
+                    a_univarchar_with_precision UNIVARCHAR(2),
+                    a_float FLOAT,
+                    a_binary_with_precision BINARY(2),
+                    a_varbinary_with_precision VARBINARY(2),
+                    the_numeric NUMERIC(6,3),
+                    the_decimal DECIMAL(6,3)
+                )
+            },
         ],
-        drop  => [ qw/ sybase_loader_test1 / ],
-        count => 9,
+        drop  => [ qw/ sybase_loader_test1 sybase_loader_test2 / ],
+        count => 38,
         run   => sub {
             my ($schema, $monikers, $classes) = @_;
 
@@ -49,7 +86,6 @@
                 'varchar',
                 'VARCHAR has correct data_type';
 
-
             is $rsrc->column_info('charfield')->{default_value},
                 'foo',
                 'constant DEFAULT is correct';
@@ -76,6 +112,43 @@
             eval { is $$computed_dt_default,
                 'getdate()',
                 'default_value for computed column is correct' };
+
+            $rsrc = $schema->resultset($monikers->{sybase_loader_test2})
+                ->result_source;
+            my @type_columns = grep /^a/, $rsrc->columns;
+            my @without_precision = grep !/_with_precision\z/, @type_columns;
+            my @with_precision    = grep  /_with_precision\z/, @type_columns;
+            my %with_precision;
+            @with_precision{
+                apply { s/_with_precision\z// } @with_precision
+            } = ();
+
+            for my $col (@without_precision) {
+                my ($data_type) = $col =~ /^an?_(.*)/;
+                $data_type =~ s/_/ /g;
+
+                ok((not exists $rsrc->column_info($col)->{size}),
+                    "$data_type " .
+                    (exists $with_precision{$col} ? 'without precision ' : '') .
+                    "has no 'size' column_info")
+                or diag "size is ".$rsrc->column_info($col)->{size}."\n";
+            }
+
+            for my $col (@with_precision) {
+                my ($data_type) = $col =~ /^an?_(.*)_with_precision\z/;
+                ($data_type = uc $data_type) =~ s/_/ /g;
+                my $size = $rsrc->column_info($col)->{size};
+
+                is $size, 2,
+                  "$data_type with precision has a correct 'size' column_info";
+            }
+
+            is_deeply $rsrc->column_info('the_numeric')->{size}, [6,3],
+                'size for NUMERIC(precision, scale) is correct';
+
+            is_deeply $rsrc->column_info('the_decimal')->{size}, [6,3],
+                'size for DECIMAL(precision, scale) is correct';
+
         },
     },
 );




More information about the Bast-commits mailing list