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