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