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

caelum at dev.catalyst.perl.org caelum at dev.catalyst.perl.org
Sun Nov 22 11:30:23 GMT 2009


Author: caelum
Date: 2009-11-22 11:30:22 +0000 (Sun, 22 Nov 2009)
New Revision: 7929

Modified:
   branches/DBIx-Class-Schema-Loader/current/Changes
   branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm
   branches/DBIx-Class-Schema-Loader/current/t/lib/dbixcsl_mssql_extra_tests.pm
Log:
fix default_value for MSSQL

Modified: branches/DBIx-Class-Schema-Loader/current/Changes
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/Changes	2009-11-22 10:03:33 UTC (rev 7928)
+++ branches/DBIx-Class-Schema-Loader/current/Changes	2009-11-22 11:30:22 UTC (rev 7929)
@@ -1,5 +1,7 @@
 Revision history for Perl extension DBIx::Class::Schema::Loader
 
+        - fix default_value for MSSQL
+
 0.04999_10  2009-10-31 12:28:53
         - patch from Robert Bohne to make _table_uniq_info more correct for
           Oracle

Modified: branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm	2009-11-22 10:03:33 UTC (rev 7928)
+++ branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm	2009-11-22 11:30:22 UTC (rev 7929)
@@ -128,10 +128,27 @@
         $extra_info{is_auto_increment} = 1;
     }
 
+# get default
+    $sth = $dbh->prepare(qq{
+        SELECT COLUMN_DEFAULT
+        FROM INFORMATION_SCHEMA.COLUMNS
+        WHERE TABLE_NAME = '$table' AND COLUMN_NAME = '$column'
+    });
+    $sth->execute;
+    my ($default) = $sth->fetchrow_array;
+
+    if (defined $default) {
+        # strip parens
+        $default =~ s/^\( (.*) \)\z/$1/x;
+
+        # literal or function?
+        $extra_info{default_value} =
+            $default =~ /^' (.*) '\z/x ? $1 : \$default;
+    }
+
     return \%extra_info;
 }
 
-
 =head1 SEE ALSO
 
 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,

Modified: branches/DBIx-Class-Schema-Loader/current/t/lib/dbixcsl_mssql_extra_tests.pm
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/t/lib/dbixcsl_mssql_extra_tests.pm	2009-11-22 10:03:33 UTC (rev 7928)
+++ branches/DBIx-Class-Schema-Loader/current/t/lib/dbixcsl_mssql_extra_tests.pm	2009-11-22 11:30:22 UTC (rev 7929)
@@ -17,9 +17,16 @@
                 dat VARCHAR(8)
             )
         },
+        qq{
+            CREATE TABLE ${vendor}_loader_test2 (
+                id INT IDENTITY NOT NULL PRIMARY KEY,
+                dat VARCHAR(100) DEFAULT 'foo',
+                ts DATETIME DEFAULT getdate()
+            )
+        },
     ],
-    drop   => [ "[${vendor}_loader_test1.dot]" ],
-    count  => 6,
+    drop   => [ "[${vendor}_loader_test1.dot]", "${vendor}_loader_test2"  ],
+    count  => 11,
     run    => sub {
         my ($schema, $monikers, $classes) = @_;
 
@@ -28,16 +35,36 @@
 
         ok((my $rs = eval {
             $schema->resultset("${vendor_titlecased}LoaderTest1Dot") }),
-            'got a resultset');
+            'got a resultset for table with dot in name');
 
         ok((my $from = eval { $rs->result_source->from }),
-            'got an $rsrc->from');
+            'got an $rsrc->from for table with dot in name');
 
-        is ref($from), 'SCALAR', '->table is a scalar ref';
+        is ref($from), 'SCALAR', '->table with dot in name is a scalar ref';
 
         is eval { $$from }, "[${vendor}_loader_test1.dot]",
-            '->table name is correct';
+            '->table with dot in name has correct name';
 
+# Test that column defaults are set correctly
+        ok(($rs = eval {
+            $schema->resultset("${vendor_titlecased}LoaderTest2") }),
+            'got a resultset for table with column with default value');
+
+        my $rsrc = $rs->result_source;
+
+        is eval { $rsrc->column_info('dat')->{default_value} }, 'foo',
+            'correct default_value for column with literal default';
+
+        ok((my $function_default =
+            eval { $rsrc->column_info('ts')->{default_value} }),
+            'got default_value for column with function default');
+
+        is ref($function_default), 'SCALAR',
+            'default_value for function default is a SCALAR ref';
+
+        is eval { $$function_default }, 'getdate()',
+            'default_value for function default is correct';
+
 # Test that identity columns do not have 'identity' in the data_type, and do
 # have is_auto_increment.
         my $identity_col_info = $schema->resultset('LoaderTest10')




More information about the Bast-commits mailing list