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