::DBI::Replicated - don't build pool/balancer from connect_info unless necessary
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Pg.pm
CommitLineData
843f8ecd 1package DBIx::Class::Storage::DBI::Pg;
2
3use strict;
4use warnings;
5
a71859b4 6use DBD::Pg qw(:pg_types);
2feba391 7
843f8ecd 8use base qw/DBIx::Class::Storage::DBI/;
9
10# __PACKAGE__->load_components(qw/PK::Auto/);
11
17614944 12# Warn about problematic versions of DBD::Pg
13warn "DBD::Pg 1.49 is strongly recommended"
14 if ($DBD::Pg::VERSION < 1.49);
15
e96a93df 16sub with_deferred_fk_checks {
17 my ($self, $sub) = @_;
18
19 $self->dbh->do('SET CONSTRAINTS ALL DEFERRED');
20 $sub->();
21}
22
d4f16b21 23sub _dbh_last_insert_id {
24 my ($self, $dbh, $seq) = @_;
25 $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq});
0680ac39 26}
27
843f8ecd 28sub last_insert_id {
ca48cd7d 29 my ($self,$source,$col) = @_;
30 my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
d12926f6 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;
373940e1 34 $self->dbh_do('_dbh_last_insert_id', $seq);
0680ac39 35}
36
d4f16b21 37sub _dbh_get_autoinc_seq {
38 my ($self, $dbh, $schema, $table, @pri) = @_;
0680ac39 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;
843f8ecd 50}
51
52sub get_autoinc_seq {
ca48cd7d 53 my ($self,$source,$col) = @_;
54
843f8ecd 55 my @pri = $source->primary_columns;
34470972 56 my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2)
57 : (undef,$source->name);
a9f32dbc 58
373940e1 59 $self->dbh_do('_dbh_get_autoinc_seq', $schema, $table, @pri);
843f8ecd 60}
61
4f533e8c 62sub sqlt_type {
63 return 'PostgreSQL';
64}
65
45fa8288 66sub datetime_parser_type { return "DateTime::Format::Pg"; }
67
a71859b4 68sub bind_attribute_by_data_type {
69 my ($self,$data_type) = @_;
70
71 my $bind_attributes = {
eda28767 72 bytea => { pg_type => DBD::Pg::PG_BYTEA },
5ba88f68 73 blob => { pg_type => DBD::Pg::PG_BYTEA },
a71859b4 74 };
75
76 if( defined $bind_attributes->{$data_type} ) {
9fdf90df 77 return $bind_attributes->{$data_type};
a71859b4 78 }
79 else {
80 return;
81 }
82}
83
f04b2839 84sub _sequence_fetch {
85 my ( $self, $type, $seq ) = @_;
86 my ($id) = $self->dbh->selectrow_array("SELECT nextval('${seq}')");
87 return $id;
88}
89
adb3554a 90sub _svp_begin {
eeb8cfeb 91 my ($self, $name) = @_;
adb3554a 92
eeb8cfeb 93 $self->dbh->pg_savepoint($name);
adb3554a 94}
95
96sub _svp_release {
eeb8cfeb 97 my ($self, $name) = @_;
adb3554a 98
d6feb60f 99 $self->dbh->pg_release($name);
adb3554a 100}
101
102sub _svp_rollback {
eeb8cfeb 103 my ($self, $name) = @_;
adb3554a 104
eeb8cfeb 105 $self->dbh->pg_rollback_to($name);
adb3554a 106}
107
843f8ecd 1081;
109
75d07914 110=head1 NAME
843f8ecd 111
112DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
113
114=head1 SYNOPSIS
115
116 # In your table classes
117 __PACKAGE__->load_components(qw/PK::Auto Core/);
118 __PACKAGE__->set_primary_key('id');
119 __PACKAGE__->sequence('mysequence');
120
121=head1 DESCRIPTION
122
123This class implements autoincrements for PostgreSQL.
124
125=head1 AUTHORS
126
127Marcus Ramberg <m.ramberg@cpan.org>
128
129=head1 LICENSE
130
131You may distribute this code under the same terms as Perl itself.
132
133=cut