Commit | Line | Data |
b8e1e21f |
1 | package DBIx::Class::PK::Auto; |
2 | |
773e3015 |
3 | #use base qw/DBIx::Class::PK/; |
4 | use base qw/DBIx::Class/; |
b8e1e21f |
5 | use strict; |
6 | use warnings; |
7 | |
34d52be2 |
8 | =head1 NAME |
9 | |
10 | DBIx::Class::PK::Auto - Automatic Primary Key class |
11 | |
12 | =head1 SYNOPSIS |
13 | |
14 | =head1 DESCRIPTION |
15 | |
16 | This class overrides the insert method to get automatically |
17 | incremented primary keys. |
18 | |
7624b19f |
19 | You don't want to be using this directly - instead load the appropriate |
20 | one for your database, e.g. PK::Auto::SQLite |
21 | |
34d52be2 |
22 | =head1 METHODS |
23 | |
130c6439 |
24 | =head2 insert |
34d52be2 |
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) = @_; |
147dd158 |
33 | my $ret = $self->next::method(@rest); |
0675cd04 |
34 | |
35 | # if all primaries are already populated, skip auto-inc |
36 | my $populated = 0; |
9a15732e |
37 | map { $populated++ if defined $self->get_column($_) } $self->primary_columns; |
103647d5 |
38 | return $ret if ( $populated == scalar $self->primary_columns ); |
0675cd04 |
39 | |
b8e1e21f |
40 | my ($pri, $too_many) = |
103647d5 |
41 | (grep { $self->column_info($_)->{'auto_increment'} } |
42 | $self->primary_columns) |
43 | || $self->primary_columns; |
78bab9ca |
44 | $self->throw( "More than one possible key found for auto-inc on ".ref $self ) |
b8e1e21f |
45 | if $too_many; |
c1d23573 |
46 | unless (defined $self->get_column($pri)) { |
78bab9ca |
47 | $self->throw( "Can't auto-inc for $pri on ".ref $self.": no _last_insert_id method" ) |
126042ee |
48 | unless $self->can('last_insert_id'); |
49 | my $id = $self->last_insert_id; |
78bab9ca |
50 | $self->throw( "Can't get last insert id" ) unless $id; |
b8e1e21f |
51 | $self->store_column($pri => $id); |
52 | } |
53 | return $ret; |
54 | } |
55 | |
130c6439 |
56 | =head2 sequence |
97cc0025 |
57 | |
58 | Manually define the correct sequence for your table, to avoid the overhead |
59 | associated with looking up the sequence automatically. |
60 | |
61 | =cut |
62 | |
63 | __PACKAGE__->mk_classdata('sequence'); |
64 | |
b8e1e21f |
65 | 1; |
34d52be2 |
66 | |
34d52be2 |
67 | =head1 AUTHORS |
68 | |
daec44b8 |
69 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
34d52be2 |
70 | |
71 | =head1 LICENSE |
72 | |
73 | You may distribute this code under the same terms as Perl itself. |
74 | |
75 | =cut |
76 | |