A couple of typos, and general whitespace cleanup (ick)
[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 base qw/DBIx::Class::Storage::DBI::MultiColumnIn/;
7 use mro 'c3';
8
9 use DBD::Pg qw(:pg_types);
10
11 # Ask for a DBD::Pg with array support
12 warn "DBD::Pg 2.9.2 or greater is strongly recommended\n"
13   if ($DBD::Pg::VERSION < 2.009002);  # pg uses (used?) version::qv()
14
15 sub with_deferred_fk_checks {
16   my ($self, $sub) = @_;
17
18   $self->dbh->do('SET CONSTRAINTS ALL DEFERRED');
19   $sub->();
20 }
21
22 sub _dbh_last_insert_id {
23   my ($self, $dbh, $seq) = @_;
24   $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq});
25 }
26
27 sub last_insert_id {
28   my ($self,$source,$col) = @_;
29   my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
30   $self->throw_exception("could not fetch primary key for " . $source->name . ", could not "
31     . "get autoinc sequence for $col (check that table and column specifications are correct "
32     . "and in the correct case)") unless defined $seq;
33   $self->dbh_do('_dbh_last_insert_id', $seq);
34 }
35
36 sub _dbh_get_autoinc_seq {
37   my ($self, $dbh, $schema, $table, @pri) = @_;
38
39   while (my $col = shift @pri) {
40     my $info = $dbh->column_info(undef,$schema,$table,$col)->fetchrow_hashref;
41     if(defined $info->{COLUMN_DEF} and
42        $info->{COLUMN_DEF} =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/) {
43       my $seq = $1;
44       # may need to strip quotes -- see if this works
45       return $seq =~ /\./ ? $seq : $info->{TABLE_SCHEM} . "." . $seq;
46     }
47   }
48   return;
49 }
50
51 sub get_autoinc_seq {
52   my ($self,$source,$col) = @_;
53
54   my @pri = $source->primary_columns;
55   my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2)
56     : (undef,$source->name);
57
58   $self->dbh_do('_dbh_get_autoinc_seq', $schema, $table, @pri);
59 }
60
61 sub sqlt_type {
62   return 'PostgreSQL';
63 }
64
65 sub datetime_parser_type { return "DateTime::Format::Pg"; }
66
67 sub bind_attribute_by_data_type {
68   my ($self,$data_type) = @_;
69
70   my $bind_attributes = {
71     bytea => { pg_type => DBD::Pg::PG_BYTEA },
72     blob  => { pg_type => DBD::Pg::PG_BYTEA },
73   };
74
75   if( defined $bind_attributes->{$data_type} ) {
76     return $bind_attributes->{$data_type};
77   }
78   else {
79     return;
80   }
81 }
82
83 sub _sequence_fetch {
84   my ( $self, $type, $seq ) = @_;
85   my ($id) = $self->dbh->selectrow_array("SELECT nextval('${seq}')");
86   return $id;
87 }
88
89 sub _svp_begin {
90     my ($self, $name) = @_;
91
92     $self->dbh->pg_savepoint($name);
93 }
94
95 sub _svp_release {
96     my ($self, $name) = @_;
97
98     $self->dbh->pg_release($name);
99 }
100
101 sub _svp_rollback {
102     my ($self, $name) = @_;
103
104     $self->dbh->pg_rollback_to($name);
105 }
106
107 1;
108
109 =head1 NAME
110
111 DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
112
113 =head1 SYNOPSIS
114
115   # In your table classes
116   __PACKAGE__->load_components(qw/PK::Auto Core/);
117   __PACKAGE__->set_primary_key('id');
118   __PACKAGE__->sequence('mysequence');
119
120 =head1 DESCRIPTION
121
122 This class implements autoincrements for PostgreSQL.
123
124 =head1 AUTHORS
125
126 Marcus Ramberg <m.ramberg@cpan.org>
127
128 =head1 LICENSE
129
130 You may distribute this code under the same terms as Perl itself.
131
132 =cut