Commit | Line | Data |
843f8ecd |
1 | package DBIx::Class::Storage::DBI::Pg; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use base qw/DBIx::Class::Storage::DBI/; |
7 | |
8 | # __PACKAGE__->load_components(qw/PK::Auto/); |
9 | |
10 | sub last_insert_id { |
11 | my ($self, $source) = @_; |
34470972 |
12 | $self->get_autoinc_seq($source) unless $source->{_autoinc_seq}; |
843f8ecd |
13 | $self->_dbh->last_insert_id(undef,undef,undef,undef, |
34470972 |
14 | {sequence => $source->{_autoinc_seq}}); |
843f8ecd |
15 | } |
16 | |
17 | sub get_autoinc_seq { |
34470972 |
18 | my ($self,$source) = @_; |
843f8ecd |
19 | |
20 | # return the user-defined sequence if known |
34470972 |
21 | my $result_class = $source->result_class; |
22 | if ($result_class->can('sequence') and $result_class->sequence) { |
23 | return ($source->{_autoinc_seq} = $result_class->sequence); |
843f8ecd |
24 | } |
25 | |
26 | my @pri = $source->primary_columns; |
27 | my $dbh = $self->_dbh; |
34470972 |
28 | my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2) |
29 | : (undef,$source->name); |
843f8ecd |
30 | while (my $col = shift @pri) { |
31 | my $info = $dbh->column_info(undef,$schema,$table,$col)->fetchrow_arrayref; |
32 | if (defined $info->[12] and $info->[12] =~ |
33 | /^nextval\('"?([^"']+)"?'::(?:text|regclass)\)/) |
34 | { |
34470972 |
35 | $source->{_autoinc_seq} = $1; |
843f8ecd |
36 | last; |
37 | } |
38 | } |
39 | } |
40 | |
41 | 1; |
42 | |
43 | =head1 NAME |
44 | |
45 | DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL |
46 | |
47 | =head1 SYNOPSIS |
48 | |
49 | # In your table classes |
50 | __PACKAGE__->load_components(qw/PK::Auto Core/); |
51 | __PACKAGE__->set_primary_key('id'); |
52 | __PACKAGE__->sequence('mysequence'); |
53 | |
54 | =head1 DESCRIPTION |
55 | |
56 | This class implements autoincrements for PostgreSQL. |
57 | |
58 | =head1 AUTHORS |
59 | |
60 | Marcus Ramberg <m.ramberg@cpan.org> |
61 | |
62 | =head1 LICENSE |
63 | |
64 | You may distribute this code under the same terms as Perl itself. |
65 | |
66 | =cut |