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 | |
d4f16b21 |
16 | sub _dbh_last_insert_id { |
17 | my ($self, $dbh, $seq) = @_; |
18 | $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq}); |
0680ac39 |
19 | } |
20 | |
843f8ecd |
21 | sub last_insert_id { |
ca48cd7d |
22 | my ($self,$source,$col) = @_; |
23 | my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col)); |
d4f16b21 |
24 | $self->dbh_do($self->can('_dbh_last_insert_id'), $seq); |
0680ac39 |
25 | } |
26 | |
d4f16b21 |
27 | sub _dbh_get_autoinc_seq { |
28 | my ($self, $dbh, $schema, $table, @pri) = @_; |
0680ac39 |
29 | |
30 | while (my $col = shift @pri) { |
31 | my $info = $dbh->column_info(undef,$schema,$table,$col)->fetchrow_hashref; |
32 | if(defined $info->{COLUMN_DEF} and |
33 | $info->{COLUMN_DEF} =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/) { |
34 | my $seq = $1; |
35 | # may need to strip quotes -- see if this works |
36 | return $seq =~ /\./ ? $seq : $info->{TABLE_SCHEM} . "." . $seq; |
37 | } |
38 | } |
39 | return; |
843f8ecd |
40 | } |
41 | |
42 | sub get_autoinc_seq { |
ca48cd7d |
43 | my ($self,$source,$col) = @_; |
44 | |
843f8ecd |
45 | my @pri = $source->primary_columns; |
34470972 |
46 | my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2) |
47 | : (undef,$source->name); |
a9f32dbc |
48 | |
d4f16b21 |
49 | $self->dbh_do($self->can('_dbh_get_autoinc_seq'), $schema, $table, @pri); |
843f8ecd |
50 | } |
51 | |
4f533e8c |
52 | sub sqlt_type { |
53 | return 'PostgreSQL'; |
54 | } |
55 | |
45fa8288 |
56 | sub datetime_parser_type { return "DateTime::Format::Pg"; } |
57 | |
843f8ecd |
58 | 1; |
59 | |
75d07914 |
60 | =head1 NAME |
843f8ecd |
61 | |
62 | DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL |
63 | |
64 | =head1 SYNOPSIS |
65 | |
66 | # In your table classes |
67 | __PACKAGE__->load_components(qw/PK::Auto Core/); |
68 | __PACKAGE__->set_primary_key('id'); |
69 | __PACKAGE__->sequence('mysequence'); |
70 | |
71 | =head1 DESCRIPTION |
72 | |
73 | This class implements autoincrements for PostgreSQL. |
74 | |
75 | =head1 AUTHORS |
76 | |
77 | Marcus Ramberg <m.ramberg@cpan.org> |
78 | |
79 | =head1 LICENSE |
80 | |
81 | You may distribute this code under the same terms as Perl itself. |
82 | |
83 | =cut |