[Dbix-class] PK::Auto::Pg patch

mbailey at vortexit.net mbailey at vortexit.net
Sun Dec 11 07:01:28 CET 2005


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

-------------- next part --------------
Index: t/run/12pg.tl
===================================================================
--- t/run/12pg.tl	(revision 380)
+++ t/run/12pg.tl	(working copy)
@@ -7,7 +7,7 @@
 plan skip_all, 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
   unless ($dsn && $user);
 
-plan tests => 1;
+plan tests => 2;
 
 DBICTest::Schema->compose_connection('PgTest' => $dsn, $user, $pass);
 
@@ -23,8 +23,12 @@
 
 my $new = PgTest::Artist->create({ name => 'foo' });
 
-ok($new->artistid, "Auto-PK worked");
+is($new->artistid, 1, "Auto-PK worked");
 
+my $new = PgTest::Artist->create({ name => 'bar' });
+
+is($new->artistid, 2, "Auto-PK worked");
+
 }
 
 1;
Index: lib/DBIx/Class/PK/Auto/Pg.pm
===================================================================
--- lib/DBIx/Class/PK/Auto/Pg.pm	(revision 380)
+++ lib/DBIx/Class/PK/Auto/Pg.pm	(working copy)
@@ -16,6 +16,7 @@
 
 sub get_autoinc_seq {
   my $self=shift;
+  my ( @pri_keys); 
   
   # return the user-defined sequence if known
   if ($self->sequence) {
@@ -23,13 +24,15 @@
   }
   
   my $dbh= $self->storage->dbh;
-    my $sth	= $dbh->column_info( undef, undef, $self->_table_name, '%');
-    while (my $foo = $sth->fetchrow_arrayref){
-      if(defined $foo->[12] && $foo->[12] =~ /^nextval/) {
-        ($self->{_autoinc_seq}) = $foo->[12] =~ 
-          m!^nextval\('"?([^"']+)"?'::(?:text|regclass)\)!;
-      }
+  (@pri_keys) = map ( ($dbh->column_info(undef,undef,$self->_table_name,$_)),
+                    ($dbh->primary_key(undef,undef,$self->_table_name)));
+  while( my $p_key = pop @pri_keys){
+    my $d_seq_name = $p_key->fetchrow_arrayref->[12];
+    if(defined $d_seq_name && $d_seq_name =~ /^nextval/) {
+   	  ($self->{_autoinc_seq}) = $d_seq_name =~ 
+  	   m!^nextval\('"?([^"']+)"?'::(?:text|regclass)\)!;
     }
+  }
 }
 
 1;
@@ -39,7 +42,30 @@
 DBIx::Class::PK::Auto::Pg - Automatic Primary Key class for Postgresql
 
 =head1 SYNOPSIS
+# Inside Base Class
 
+Package MyApp::DB;
+use base qw/DBIx::Class/;
+...
+...
+
+# Inside Table Class
+
+Package MyApp::DB::Artist;
+
+use base qw/MyApp::DB/;
+
+__PACKAGE__->load_components('PK::Auto::Pg');
+...
+...
+
+# Inside your App
+# assuming that there is an auto-incrementing column artist_id in this table
+my $artist1 = MyApp::DB::Artist->create({ name => 'artist1' });
+
+# this should be the value stored in the auto-incrementing primary key column of object
+my $id = $artist1->artist_id;
+
 =head1 DESCRIPTION
 
 This class implements autoincrements for Postgresql.


More information about the Dbix-class mailing list