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 | |
6718c5f0 |
14 | # In your table classes (replace PK::Auto::SQLite with your |
15 | # database) |
16 | __PACKAGE__->load_components(qw/PK::Auto::SQLite Core/); |
17 | __PACKAGE__->set_primary_key('id'); |
18 | |
34d52be2 |
19 | =head1 DESCRIPTION |
20 | |
21 | This class overrides the insert method to get automatically |
22 | incremented primary keys. |
23 | |
6718c5f0 |
24 | You don't want to be using this directly - instead load the |
f4ccda68 |
25 | appropriate one for your database, e.g. C<PK::Auto::SQLite>, in your |
26 | table classes: |
27 | |
28 | __PACKAGE__->load_components(qw/PK::Auto::SQLite Core/); |
29 | |
30 | Note that C<PK::Auto::SQLite> is specified as the leftmost argument. |
31 | |
32 | Alternatively, you can load the components separately: |
33 | |
34 | __PACKAGE__->load_components(qw/Core/); |
35 | __PACKAGE__->load_components(qw/PK::Auto::SQLite/); |
36 | |
37 | This can be used, for example, if you have different databases and |
38 | need to determine the appropriate C<PK::Auto> class at runtime. |
7624b19f |
39 | |
c8f4b52b |
40 | =head1 LOGIC |
41 | |
f4ccda68 |
42 | C<PK::Auto> does this by letting the database assign the primary key |
6718c5f0 |
43 | field and fetching the assigned value afterwards. |
c8f4b52b |
44 | |
34d52be2 |
45 | =head1 METHODS |
46 | |
130c6439 |
47 | =head2 insert |
34d52be2 |
48 | |
49 | Overrides insert so that it will get the value of autoincremented |
50 | primary keys. |
51 | |
52 | =cut |
53 | |
b8e1e21f |
54 | sub insert { |
55 | my ($self, @rest) = @_; |
147dd158 |
56 | my $ret = $self->next::method(@rest); |
0675cd04 |
57 | |
58 | # if all primaries are already populated, skip auto-inc |
59 | my $populated = 0; |
9a15732e |
60 | map { $populated++ if defined $self->get_column($_) } $self->primary_columns; |
103647d5 |
61 | return $ret if ( $populated == scalar $self->primary_columns ); |
0675cd04 |
62 | |
b8e1e21f |
63 | my ($pri, $too_many) = |
103647d5 |
64 | (grep { $self->column_info($_)->{'auto_increment'} } |
65 | $self->primary_columns) |
66 | || $self->primary_columns; |
78bab9ca |
67 | $self->throw( "More than one possible key found for auto-inc on ".ref $self ) |
b8e1e21f |
68 | if $too_many; |
c1d23573 |
69 | unless (defined $self->get_column($pri)) { |
78bab9ca |
70 | $self->throw( "Can't auto-inc for $pri on ".ref $self.": no _last_insert_id method" ) |
126042ee |
71 | unless $self->can('last_insert_id'); |
72 | my $id = $self->last_insert_id; |
78bab9ca |
73 | $self->throw( "Can't get last insert id" ) unless $id; |
b8e1e21f |
74 | $self->store_column($pri => $id); |
75 | } |
76 | return $ret; |
77 | } |
78 | |
130c6439 |
79 | =head2 sequence |
97cc0025 |
80 | |
81 | Manually define the correct sequence for your table, to avoid the overhead |
82 | associated with looking up the sequence automatically. |
83 | |
84 | =cut |
85 | |
86 | __PACKAGE__->mk_classdata('sequence'); |
87 | |
b8e1e21f |
88 | 1; |
34d52be2 |
89 | |
34d52be2 |
90 | =head1 AUTHORS |
91 | |
daec44b8 |
92 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
34d52be2 |
93 | |
94 | =head1 LICENSE |
95 | |
96 | You may distribute this code under the same terms as Perl itself. |
97 | |
98 | =cut |
99 | |