Version bumped to 0.03999_03, fix for Auto::PK issues in MySQL tests
[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
10DBIx::Class::PK::Auto - Automatic Primary Key class
11
12=head1 SYNOPSIS
13
14=head1 DESCRIPTION
15
16This class overrides the insert method to get automatically
17incremented primary keys.
18
7624b19f 19You don't want to be using this directly - instead load the appropriate
20one for your database, e.g. PK::Auto::SQLite
21
34d52be2 22=head1 METHODS
23
24=over 4
25
26=item insert
27
28Overrides insert so that it will get the value of autoincremented
29primary keys.
30
31=cut
32
b8e1e21f 33sub insert {
34 my ($self, @rest) = @_;
147dd158 35 my $ret = $self->next::method(@rest);
0675cd04 36
37 # if all primaries are already populated, skip auto-inc
38 my $populated = 0;
9a15732e 39 map { $populated++ if defined $self->get_column($_) } $self->primary_columns;
103647d5 40 return $ret if ( $populated == scalar $self->primary_columns );
0675cd04 41
b8e1e21f 42 my ($pri, $too_many) =
103647d5 43 (grep { $self->column_info($_)->{'auto_increment'} }
44 $self->primary_columns)
45 || $self->primary_columns;
78bab9ca 46 $self->throw( "More than one possible key found for auto-inc on ".ref $self )
b8e1e21f 47 if $too_many;
c1d23573 48 unless (defined $self->get_column($pri)) {
78bab9ca 49 $self->throw( "Can't auto-inc for $pri on ".ref $self.": no _last_insert_id method" )
126042ee 50 unless $self->can('last_insert_id');
51 my $id = $self->last_insert_id;
78bab9ca 52 $self->throw( "Can't get last insert id" ) unless $id;
b8e1e21f 53 $self->store_column($pri => $id);
54 }
55 return $ret;
56}
57
97cc0025 58=item sequence
59
60Manually define the correct sequence for your table, to avoid the overhead
61associated with looking up the sequence automatically.
62
63=cut
64
65__PACKAGE__->mk_classdata('sequence');
66
b8e1e21f 671;
34d52be2 68
69=back
70
71=head1 AUTHORS
72
daec44b8 73Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 74
75=head1 LICENSE
76
77You may distribute this code under the same terms as Perl itself.
78
79=cut
80