Commit | Line | Data |
b8e1e21f |
1 | package DBIx::Class::PK::Auto; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
34d52be2 |
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 | |
7624b19f |
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 | |
34d52be2 |
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 | |
b8e1e21f |
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 }); |
78bab9ca |
38 | $self->throw( "More than one possible key found for auto-inc on ".ref $self ) |
b8e1e21f |
39 | if $too_many; |
c1d23573 |
40 | unless (defined $self->get_column($pri)) { |
78bab9ca |
41 | $self->throw( "Can't auto-inc for $pri on ".ref $self.": no _last_insert_id method" ) |
126042ee |
42 | unless $self->can('last_insert_id'); |
43 | my $id = $self->last_insert_id; |
78bab9ca |
44 | $self->throw( "Can't get last insert id" ) unless $id; |
b8e1e21f |
45 | $self->store_column($pri => $id); |
46 | } |
47 | return $ret; |
48 | } |
49 | |
50 | 1; |
34d52be2 |
51 | |
52 | =back |
53 | |
54 | =head1 AUTHORS |
55 | |
56 | Matt S. Trout <perl-stuff@trout.me.uk> |
57 | |
58 | =head1 LICENSE |
59 | |
60 | You may distribute this code under the same terms as Perl itself. |
61 | |
62 | =cut |
63 | |