Minor fixes
[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
4ce3b851 8use base qw/DBIx::Class::Storage::DBI::MultiColumnIn/;
843f8ecd 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) = @_;
80625830 54
843f8ecd 55 my @pri = $source->primary_columns;
80625830 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 }
a9f32dbc 66
373940e1 67 $self->dbh_do('_dbh_get_autoinc_seq', $schema, $table, @pri);
843f8ecd 68}
69
4f533e8c 70sub sqlt_type {
71 return 'PostgreSQL';
72}
73
45fa8288 74sub datetime_parser_type { return "DateTime::Format::Pg"; }
75
a71859b4 76sub bind_attribute_by_data_type {
77 my ($self,$data_type) = @_;
78
79 my $bind_attributes = {
eda28767 80 bytea => { pg_type => DBD::Pg::PG_BYTEA },
5ba88f68 81 blob => { pg_type => DBD::Pg::PG_BYTEA },
a71859b4 82 };
83
84 if( defined $bind_attributes->{$data_type} ) {
9fdf90df 85 return $bind_attributes->{$data_type};
a71859b4 86 }
87 else {
88 return;
89 }
90}
91
f04b2839 92sub _sequence_fetch {
93 my ( $self, $type, $seq ) = @_;
94 my ($id) = $self->dbh->selectrow_array("SELECT nextval('${seq}')");
95 return $id;
96}
97
adb3554a 98sub _svp_begin {
eeb8cfeb 99 my ($self, $name) = @_;
adb3554a 100
eeb8cfeb 101 $self->dbh->pg_savepoint($name);
adb3554a 102}
103
104sub _svp_release {
eeb8cfeb 105 my ($self, $name) = @_;
adb3554a 106
d6feb60f 107 $self->dbh->pg_release($name);
adb3554a 108}
109
110sub _svp_rollback {
eeb8cfeb 111 my ($self, $name) = @_;
adb3554a 112
eeb8cfeb 113 $self->dbh->pg_rollback_to($name);
adb3554a 114}
115
843f8ecd 1161;
117
75d07914 118=head1 NAME
843f8ecd 119
120DBIx::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
131This class implements autoincrements for PostgreSQL.
132
133=head1 AUTHORS
134
135Marcus Ramberg <m.ramberg@cpan.org>
136
137=head1 LICENSE
138
139You may distribute this code under the same terms as Perl itself.
140
141=cut