Caelum was right to make _get_dbh private - reverting (and some code refactoring)
[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
4ce3b851 6use base qw/DBIx::Class::Storage::DBI::MultiColumnIn/;
ac45262b 7use mro 'c3';
843f8ecd 8
ac45262b 9use DBD::Pg qw(:pg_types);
843f8ecd 10
ac45262b 11# Ask for a DBD::Pg with array support
c70c716e 12warn "DBD::Pg 2.9.2 or greater is strongly recommended\n"
ac45262b 13 if ($DBD::Pg::VERSION < 2.009002); # pg uses (used?) version::qv()
17614944 14
e96a93df 15sub with_deferred_fk_checks {
16 my ($self, $sub) = @_;
17
9ae966b9 18 $self->_get_dbh->do('SET CONSTRAINTS ALL DEFERRED');
e96a93df 19 $sub->();
20}
21
d4f16b21 22sub _dbh_last_insert_id {
23 my ($self, $dbh, $seq) = @_;
24 $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq});
0680ac39 25}
26
843f8ecd 27sub last_insert_id {
ca48cd7d 28 my ($self,$source,$col) = @_;
29 my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
d12926f6 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;
373940e1 33 $self->dbh_do('_dbh_last_insert_id', $seq);
0680ac39 34}
35
d4f16b21 36sub _dbh_get_autoinc_seq {
37 my ($self, $dbh, $schema, $table, @pri) = @_;
0680ac39 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;
843f8ecd 49}
50
51sub get_autoinc_seq {
ca48cd7d 52 my ($self,$source,$col) = @_;
80625830 53
843f8ecd 54 my @pri = $source->primary_columns;
80625830 55
56 my $schema;
57 my $table = $source->name;
58
59 if (ref $table eq 'SCALAR') {
60 $table = $$table;
61 }
62 elsif ($table =~ /^(.+)\.(.+)$/) {
63 ($schema, $table) = ($1, $2);
64 }
a9f32dbc 65
373940e1 66 $self->dbh_do('_dbh_get_autoinc_seq', $schema, $table, @pri);
843f8ecd 67}
68
4f533e8c 69sub sqlt_type {
70 return 'PostgreSQL';
71}
72
45fa8288 73sub datetime_parser_type { return "DateTime::Format::Pg"; }
74
a71859b4 75sub bind_attribute_by_data_type {
76 my ($self,$data_type) = @_;
77
78 my $bind_attributes = {
eda28767 79 bytea => { pg_type => DBD::Pg::PG_BYTEA },
5ba88f68 80 blob => { pg_type => DBD::Pg::PG_BYTEA },
a71859b4 81 };
d4daee7b 82
a71859b4 83 if( defined $bind_attributes->{$data_type} ) {
9fdf90df 84 return $bind_attributes->{$data_type};
a71859b4 85 }
86 else {
87 return;
88 }
89}
90
f04b2839 91sub _sequence_fetch {
92 my ( $self, $type, $seq ) = @_;
9ae966b9 93 my ($id) = $self->_get_dbh->selectrow_array("SELECT nextval('${seq}')");
f04b2839 94 return $id;
95}
96
adb3554a 97sub _svp_begin {
eeb8cfeb 98 my ($self, $name) = @_;
adb3554a 99
9ae966b9 100 $self->_get_dbh->pg_savepoint($name);
adb3554a 101}
102
103sub _svp_release {
eeb8cfeb 104 my ($self, $name) = @_;
adb3554a 105
9ae966b9 106 $self->_get_dbh->pg_release($name);
adb3554a 107}
108
109sub _svp_rollback {
eeb8cfeb 110 my ($self, $name) = @_;
adb3554a 111
9ae966b9 112 $self->_get_dbh->pg_rollback_to($name);
adb3554a 113}
114
843f8ecd 1151;
116
75d07914 117=head1 NAME
843f8ecd 118
119DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
120
121=head1 SYNOPSIS
122
123 # In your table classes
124 __PACKAGE__->load_components(qw/PK::Auto Core/);
125 __PACKAGE__->set_primary_key('id');
126 __PACKAGE__->sequence('mysequence');
127
128=head1 DESCRIPTION
129
130This class implements autoincrements for PostgreSQL.
131
132=head1 AUTHORS
133
134Marcus Ramberg <m.ramberg@cpan.org>
135
136=head1 LICENSE
137
138You may distribute this code under the same terms as Perl itself.
139
140=cut