summaryrefslogtreecommitdiff
path: root/lib/Test/Database/Driver/Pg.pm
blob: cbae71de61a3ac3660f2dea81de1b9bab867c503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package Test::Database::Driver::Pg;
$Test::Database::Driver::Pg::VERSION = '1.112';
use strict;
use warnings;
use Carp;

use Test::Database::Driver;
our @ISA = qw( Test::Database::Driver );

sub _version {
    DBI->connect_cached( $_[0]->connection_info() )
        ->selectcol_arrayref('SELECT VERSION()')->[0] =~ /^PostgreSQL (\S+)/;
    return $1;
}

sub create_database {
    my ($self) = @_;
    my $dbname = $self->available_dbname();

    DBI->connect_cached( $self->connection_info() )
        ->do( "CREATE DATABASE $dbname"
            . ( $self->{template} ? " TEMPLATE $self->{template}" : '' ) );

    # return the handle
    return Test::Database::Handle->new(
        dsn      => $self->dsn($dbname),
        name     => $dbname,
        username => $self->username(),
        password => $self->password(),
        driver   => $self,
    );
}

sub drop_database {
    my ( $self, $dbname ) = @_;

    DBI->connect_cached( $self->connection_info() )
        ->do("DROP DATABASE $dbname")
        if grep { $_ eq $dbname } $self->databases();
}

sub databases {
    my ($self)    = @_;
    my $basename  = qr/^@{[$self->_basename()]}/;
    my $databases = eval {
        DBI->connect_cached( $self->connection_info(), { PrintError => 0 } )
            ->selectall_arrayref(
            'SELECT datname FROM pg_catalog.pg_database');
    };
    return grep {/$basename/} map {@$_} @$databases;
}

'Pg';

__END__

=head1 NAME

Test::Database::Driver::Pg - A Test::Database driver for Pg

=head1 SYNOPSIS

    use Test::Database;
    my @handles = Test::Database->handles( 'Pg' );

=head1 DESCRIPTION

This module is the L<Test::Database> driver for L<DBD::Pg>.

=head1 EXTRA PARAMETERS

This driver understands the following extra parameters in the configuration
file:

=over 4

=item template

The template to use when creating a new database.

=back

=head1 SEE ALSO

L<Test::Database::Driver>

=head1 AUTHOR

Philippe Bruhat (BooK), C<< <book@cpan.org> >>

=head1 COPYRIGHT

Copyright 2008-2010 Philippe Bruhat (BooK), all rights reserved.

=head1 LICENSE

This module is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut