documenting!
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / PK / Auto.pm
CommitLineData
b8e1e21f 1package DBIx::Class::PK::Auto;
2
773e3015 3#use base qw/DBIx::Class::PK/;
4use base qw/DBIx::Class/;
b8e1e21f 5use strict;
6use warnings;
7
34d52be2 8=head1 NAME
9
eb49d4e3 10DBIx::Class::PK::Auto - Automatic primary key class
34d52be2 11
12=head1 SYNOPSIS
13
eb49d4e3 14 # In your table classes (replace PK::Auto::SQLite with your database)
15 __PACKAGE__->load_components(qw/PK::Auto::SQLite Core/);
16 __PACKAGE__->set_primary_key('id');
6718c5f0 17
34d52be2 18=head1 DESCRIPTION
19
eb49d4e3 20This class overrides the insert method to get automatically incremented primary
21keys.
34d52be2 22
eb49d4e3 23You don't want to be using this directly - instead load the appropriate one for
24your database, e.g. C<PK::Auto::SQLite>, in your table classes:
f4ccda68 25
eb49d4e3 26 __PACKAGE__->load_components(qw/PK::Auto::SQLite Core/);
f4ccda68 27
28Note that C<PK::Auto::SQLite> is specified as the leftmost argument.
29
30Alternatively, you can load the components separately:
31
eb49d4e3 32 __PACKAGE__->load_components(qw/Core/);
33 __PACKAGE__->load_components(qw/PK::Auto::SQLite/);
f4ccda68 34
eb49d4e3 35This can be used, for example, if you have different databases and need to
36determine the appropriate C<PK::Auto> class at runtime.
7624b19f 37
c8f4b52b 38=head1 LOGIC
39
eb49d4e3 40C<PK::Auto> does this by letting the database assign the primary key field and
41fetching the assigned value afterwards.
c8f4b52b 42
34d52be2 43=head1 METHODS
44
130c6439 45=head2 insert
34d52be2 46
eb49d4e3 47Overrides C<insert> so that it will get the value of autoincremented primary
48keys.
34d52be2 49
50=cut
51
b8e1e21f 52sub insert {
53 my ($self, @rest) = @_;
147dd158 54 my $ret = $self->next::method(@rest);
0675cd04 55
ca48cd7d 56 my ($pri, $too_many) = grep { !defined $self->get_column($_) } $self->primary_columns;
57 return $ret unless defined $pri; # if all primaries are already populated, skip auto-inc
701da8c4 58 $self->throw_exception( "More than one possible key found for auto-inc on ".ref $self )
ca48cd7d 59 if defined $too_many;
60
fefe2816 61 my $storage = $self->result_source->storage;
62 $self->throw_exception( "Missing primary key but Storage doesn't support last_insert_id" ) unless $storage->can('last_insert_id');
63 my $id = $storage->last_insert_id($self->result_source,$pri);
ca48cd7d 64 $self->throw_exception( "Can't get last insert id" ) unless $id;
65 $self->store_column($pri => $id);
66
b8e1e21f 67 return $ret;
68}
69
130c6439 70=head2 sequence
97cc0025 71
72Manually define the correct sequence for your table, to avoid the overhead
73associated with looking up the sequence automatically.
74
75=cut
76
ecb6488f 77sub sequence {
78 my ($self,$seq) = @_;
79 foreach my $pri ($self->primary_columns) {
80 $self->column_info($pri)->{sequence} = $seq;
81 }
82}
97cc0025 83
b8e1e21f 841;
34d52be2 85
34d52be2 86=head1 AUTHORS
87
daec44b8 88Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 89
90=head1 LICENSE
91
92You may distribute this code under the same terms as Perl itself.
93
94=cut