Updated main docs, altered mail address in POD for 0.01
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / PK / Auto.pm
1 package DBIx::Class::PK::Auto;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME 
7
8 DBIx::Class::PK::Auto - Automatic Primary Key class
9
10 =head1 SYNOPSIS
11
12 =head1 DESCRIPTION
13
14 This class overrides the insert method to get automatically
15 incremented primary keys.
16
17 You don't want to be using this directly - instead load the appropriate
18 one for your database, e.g. PK::Auto::SQLite
19
20 =head1 METHODS
21
22 =over 4
23
24 =item insert
25
26 Overrides insert so that it will get the value of autoincremented
27 primary keys.
28
29 =cut
30
31 sub insert {
32   my ($self, @rest) = @_;
33   my $ret = $self->NEXT::ACTUAL::insert(@rest);
34   my ($pri, $too_many) =
35     (grep { $self->_primaries->{$_}{'auto_increment'} }
36        keys %{ $self->_primaries })
37     || (keys %{ $self->_primaries });
38   $self->throw( "More than one possible key found for auto-inc on ".ref $self )
39     if $too_many;
40   unless (defined $self->get_column($pri)) {
41     $self->throw( "Can't auto-inc for $pri on ".ref $self.": no _last_insert_id method" )
42       unless $self->can('last_insert_id');
43     my $id = $self->last_insert_id;
44     $self->throw( "Can't get last insert id" ) unless $id;
45     $self->store_column($pri => $id);
46   }
47   return $ret;
48 }
49
50 1;
51
52 =back
53
54 =head1 AUTHORS
55
56 Matt S. Trout <mst@shadowcatsystems.co.uk>
57
58 =head1 LICENSE
59
60 You may distribute this code under the same terms as Perl itself.
61
62 =cut
63