more coverage in Pg sequence-discovery tests. i think this shows why last_insert_id...
[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
843f8ecd 22sub last_insert_id {
6ea2d01b 23 my ($self,$source,@cols) = @_;
24
dc7bcc46 25 return map $self->dbh_do('_dbh_last_insert_id', $source, $_ ), @cols;
0680ac39 26}
27
9a0b7b26 28sub _dbh_last_insert_id {
dc7bcc46 29 my ($self, $dbh, $source, $col ) = @_;
9a0b7b26 30
dc7bcc46 31 # if a sequence is defined, explicitly specify it to DBD::Pg's last_insert_id()
32 if( my $seq = $source->column_info($col)->{sequence} ) {
33 return $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq});
9a0b7b26 34
dc7bcc46 35 }
36 # if not, parse out the schema and table names, pass them to
37 # DBD::Pg, and let it figure out (and cache) the sequence name
38 # itself.
39 else {
0063119f 40
dc7bcc46 41 my $schema;
42 my $table = $source->name;
0063119f 43
dc7bcc46 44 # deref table name if necessary
45 $table = $$table if ref $table eq 'SCALAR';
0063119f 46
dc7bcc46 47 # parse out schema name if present
48 if ( $table =~ /^(.+)\.(.+)$/ ) {
49 ( $schema, $table ) = ( $1, $2 );
50 }
0680ac39 51
dc7bcc46 52 return $dbh->last_insert_id( undef, $schema, $table, undef );
777f7527 53 }
777f7527 54}
55
4f533e8c 56sub sqlt_type {
57 return 'PostgreSQL';
58}
59
45fa8288 60sub datetime_parser_type { return "DateTime::Format::Pg"; }
61
a71859b4 62sub bind_attribute_by_data_type {
63 my ($self,$data_type) = @_;
64
65 my $bind_attributes = {
eda28767 66 bytea => { pg_type => DBD::Pg::PG_BYTEA },
5ba88f68 67 blob => { pg_type => DBD::Pg::PG_BYTEA },
a71859b4 68 };
d4daee7b 69
a71859b4 70 if( defined $bind_attributes->{$data_type} ) {
9fdf90df 71 return $bind_attributes->{$data_type};
a71859b4 72 }
73 else {
74 return;
75 }
76}
77
f04b2839 78sub _sequence_fetch {
79 my ( $self, $type, $seq ) = @_;
9ae966b9 80 my ($id) = $self->_get_dbh->selectrow_array("SELECT nextval('${seq}')");
f04b2839 81 return $id;
82}
83
adb3554a 84sub _svp_begin {
eeb8cfeb 85 my ($self, $name) = @_;
adb3554a 86
9ae966b9 87 $self->_get_dbh->pg_savepoint($name);
adb3554a 88}
89
90sub _svp_release {
eeb8cfeb 91 my ($self, $name) = @_;
adb3554a 92
9ae966b9 93 $self->_get_dbh->pg_release($name);
adb3554a 94}
95
96sub _svp_rollback {
eeb8cfeb 97 my ($self, $name) = @_;
adb3554a 98
9ae966b9 99 $self->_get_dbh->pg_rollback_to($name);
adb3554a 100}
101
843f8ecd 1021;
103
fd159e2a 104__END__
105
75d07914 106=head1 NAME
843f8ecd 107
108DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
109
110=head1 SYNOPSIS
111
112 # In your table classes
113 __PACKAGE__->load_components(qw/PK::Auto Core/);
114 __PACKAGE__->set_primary_key('id');
115 __PACKAGE__->sequence('mysequence');
116
117=head1 DESCRIPTION
118
119This class implements autoincrements for PostgreSQL.
120
7c0176a1 121=head1 POSTGRESQL SCHEMA SUPPORT
122
4f609014 123This driver supports multiple PostgreSQL schemas, with one caveat: for
6ff1d58c 124performance reasons, data about the search path, sequence names, and
125so forth is queried as needed and CACHED for subsequent uses.
7c0176a1 126
4f609014 127For this reason, once your schema is instantiated, you should not
128change the PostgreSQL schema search path for that schema's database
129connection. If you do, Bad Things may happen.
130
131You should do any necessary manipulation of the search path BEFORE
132instantiating your schema object, or as part of the on_connect_do
133option to connect(), for example:
7c0176a1 134
135 my $schema = My::Schema->connect
136 ( $dsn,$user,$pass,
137 { on_connect_do =>
138 [ 'SET search_path TO myschema, foo, public' ],
139 },
140 );
141
7ff926e6 142=head1 AUTHORS
7c0176a1 143
7ff926e6 144See L<DBIx::Class/CONTRIBUTORS>
843f8ecd 145
146=head1 LICENSE
147
148You may distribute this code under the same terms as Perl itself.
149
150=cut