Commit | Line | Data |
843f8ecd |
1 | package DBIx::Class::Storage::DBI::Pg; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
2feba391 |
6 | use DBD::Pg; |
7 | |
843f8ecd |
8 | use base qw/DBIx::Class::Storage::DBI/; |
9 | |
10 | # __PACKAGE__->load_components(qw/PK::Auto/); |
11 | |
17614944 |
12 | # Warn about problematic versions of DBD::Pg |
13 | warn "DBD::Pg 1.49 is strongly recommended" |
14 | if ($DBD::Pg::VERSION < 1.49); |
15 | |
843f8ecd |
16 | sub last_insert_id { |
ca48cd7d |
17 | my ($self,$source,$col) = @_; |
18 | my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col)); |
d12926f6 |
19 | $self->throw_exception("could not fetch primary key for " . $source->name . ", could not " |
20 | . "get autoinc sequence for $col (check that table and column specifications are correct " |
21 | . "and in the correct case)") unless defined $seq; |
ca48cd7d |
22 | $self->_dbh->last_insert_id(undef,undef,undef,undef, {sequence => $seq}); |
843f8ecd |
23 | } |
24 | |
25 | sub get_autoinc_seq { |
ca48cd7d |
26 | my ($self,$source,$col) = @_; |
27 | |
843f8ecd |
28 | my @pri = $source->primary_columns; |
29 | my $dbh = $self->_dbh; |
34470972 |
30 | my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2) |
31 | : (undef,$source->name); |
843f8ecd |
32 | while (my $col = shift @pri) { |
4d272ce5 |
33 | my $info = $dbh->column_info(undef,$schema,$table,$col)->fetchrow_hashref; |
34 | if (defined $info->{COLUMN_DEF} and $info->{COLUMN_DEF} =~ |
32da1042 |
35 | /^nextval\(+'([^']+)'::(?:text|regclass)\)/) |
843f8ecd |
36 | { |
4d272ce5 |
37 | my $seq = $1; |
38 | return $seq =~ /\./ ? $seq : $info->{TABLE_SCHEM} . "." . $seq; # may need to strip quotes -- see if this works |
75d07914 |
39 | } |
843f8ecd |
40 | } |
41 | } |
42 | |
4f533e8c |
43 | sub sqlt_type { |
44 | return 'PostgreSQL'; |
45 | } |
46 | |
45fa8288 |
47 | sub datetime_parser_type { return "DateTime::Format::Pg"; } |
48 | |
843f8ecd |
49 | 1; |
50 | |
75d07914 |
51 | =head1 NAME |
843f8ecd |
52 | |
53 | DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL |
54 | |
55 | =head1 SYNOPSIS |
56 | |
57 | # In your table classes |
58 | __PACKAGE__->load_components(qw/PK::Auto Core/); |
59 | __PACKAGE__->set_primary_key('id'); |
60 | __PACKAGE__->sequence('mysequence'); |
61 | |
62 | =head1 DESCRIPTION |
63 | |
64 | This class implements autoincrements for PostgreSQL. |
65 | |
66 | =head1 AUTHORS |
67 | |
68 | Marcus Ramberg <m.ramberg@cpan.org> |
69 | |
70 | =head1 LICENSE |
71 | |
72 | You may distribute this code under the same terms as Perl itself. |
73 | |
74 | =cut |