File Coverage

blib/lib/Module/Build/DBD/Pg.pm
Criterion Covered Total %
statement 28 28 100.0
branch 7 8 87.5
condition 1 3 33.3
subroutine 11 11 100.0
pod 9 9 100.0
total 56 59 94.9


line stmt bran cond sub pod time code
1             package Module::Build::DBD::Pg;
2              
3 3     3   33051 use strict;
  3         9  
  3         113  
4 3     3   16 use warnings;
  3         7  
  3         1863  
5             our $VERSION = '0.10';
6              
7 1     1 1 22 sub get_client { 'psql' }
8              
9             sub get_db_and_command {
10 4     4 1 1020 my ($class, $client, $p) = @_;
11              
12 4   33     26 my $user = $p->{db_super_user} || $p->{username} || $p->{user};
13 4 50       21 my @cmd = (
14             $client,
15             ($user ? ( '--username' => $user ) : ()),
16             '--quiet',
17             '--no-psqlrc',
18             '--no-align',
19             '--tuples-only',
20             '--set' => 'ON_ERROR_ROLLBACK=1',
21             '--set' => 'ON_ERROR_STOP=1',
22             );
23 4 100       14 push @cmd, '--host' => $p->{host} if $p->{host};
24 4 100       13 push @cmd, '--port' => $p->{port} if $p->{port};
25              
26             # Hopefully this is sufficiently OS-independant for us to get away with it.
27 4 100       13 unshift @cmd, $^X, '-e', '$ENV{PGPASSWORD} = shift; exec @ARGV', $p->{db_super_pass}
28             if $p->{db_super_pass};
29              
30 4         26 return $p->{dbname}, \@cmd;
31             }
32              
33             sub get_db_option {
34 6     6 1 11 my ($class, $db) = @_;
35 6         42 return ('--dbname' => $db);
36             }
37              
38             sub get_create_db_command {
39 1     1 1 4 my ($class, $cmd, $db) = @_;
40 1         6 $class->get_execute_command( $cmd, 'template1', qq{CREATE DATABASE "$db"});
41             }
42              
43             sub get_drop_db_command {
44 1     1 1 3 my ($class, $cmd, $db) = @_;
45 1         5 $class->get_execute_command( $cmd, 'template1', qq{DROP DATABASE IF EXISTS "$db"});
46             }
47              
48             sub get_check_db_command {
49 1     1 1 3 my ($class, $cmd, $db) = @_;
50 1         6 $class->get_execute_command( $cmd, 'template1', qq{
51             SELECT 1
52             FROM pg_catalog.pg_database
53             WHERE datname = '$db';
54             });
55             }
56              
57             sub get_execute_command {
58 4     4 1 8 my ($class, $cmd, $db, $sql) = @_;
59             return (
60 4         10 @$cmd,
61             $class->get_db_option($db),
62             '--command' => $sql,
63             );
64             }
65              
66             sub get_file_command {
67 1     1 1 4 my ($class, $cmd, $db, $fn) = @_;
68             return (
69 1         5 @$cmd,
70             $class->get_db_option($db),
71             '--file' => $fn,
72             );
73             }
74              
75             sub get_meta_table_sql {
76 1     1 1 2 my ($class, $table) = @_;
77 1         20 return qq{
78             SET client_min_messages=warning;
79             CREATE TABLE $table (
80             label TEXT PRIMARY KEY,
81             value INT NOT NULL DEFAULT 0,
82             note TEXT NOT NULL
83             );
84             RESET client_min_messages;
85             }
86             }
87              
88             1;
89              
90             =head1 Name
91              
92             Module::Build::DBD:Pg - PostgreSQL specifics for Module::Build::DB
93              
94             =head1 Description
95              
96             This module contains a number of class methods called by L
97             to handle PostgreSQL specific tasks when detecting, building, and updating a
98             database.
99              
100             =head2 Methods
101              
102             All methods are class methods.
103              
104             =head3 C
105              
106             my $client = Module::Build::DBD::Pg->get_client;
107              
108             Returns the name of the client to use to connect to PostgreSQL. For now,
109             that's just C, which is fine if it's in your path. Some code to search
110             for a client might be added in the future. Either way, it's best to specify
111             use the C<--db_client> option to avoid all ambiguity.
112              
113             =head3 C
114              
115             my ($db_name, $cmd) = Module::Build::DBD::Pg->get_db_and_command($client, $params);
116              
117             Returns a database name culled from C<$params> and an array reference with
118             C<$client> and all required options for all access to the database. C<$params>
119             contains both the contents of the context configuration file's DBI section and
120             the attributes defined in the driver DSN (e.g., C in
121             C).
122              
123             =head3 C
124              
125             my @opts = Module::Build::DBD::Pg->get_db_option($db_name);
126              
127             Returns a list of options to be appended to the command returned by
128             C to connect to a specific database. For PostgreSQL,
129             that's simply C<< ('--dbname' => $dbname) >>.
130              
131             =head3 C
132              
133             my @command = Module::Build::DBD::Pg->get_create_db_command($cmd, $db);
134              
135             Returns a command list suitable for passing to C that will create a
136             new database. C<$cmd> is the command returned by C and
137             C<$db> is the name of the database to be created.
138              
139             =head3 C
140              
141             my @command = Module::Build::DBD::Pg->get_drop_db_command($cmd, $db);
142              
143             Returns a command list suitable for passing to C that will drop an
144             existing database. C<$cmd> is the command returned by C
145             and C<$db> is the name of the database to be dropped.
146              
147             =head3 C
148              
149             my @command = Module::Build::DBD::Pg->get_check_db_command($cmd, $db);
150              
151             Returns a command list suitable for passing to C that will, when
152             executed, output a 1 when C<$db> exists and nothing when C<$db> does not
153             exist. C<$cmd> is the command returned by C and C<$db>
154             is the name of the database to be checked.
155              
156             =head3 C
157              
158             my @command = Module::Build::DBD::Pg->get_execute_command($cmd, $db, $sql);
159              
160             Returns a command list suitable for passing to C that will execute
161             the SQL in C<$sql> and return its output, if any. C<$cmd> is the command
162             returned by C, C<$db> is the name of the database to be
163             connect to for the query, and C<$sql> is the SQL command or commands to be
164             executed.
165              
166             =head3 C
167              
168             my @command = Module::Build::DBD::Pg->get_file_command($cmd, $db, $sql);
169              
170             Returns a command list suitable for passing to C that will execute
171             the SQL in C<$file> and return its output, if any. C<$cmd> is the command
172             returned by C, C<$db> is the name of the database to be
173             connect to for the query, and C<$file> is a file with SQL commands.
174              
175             =head3 C
176              
177             my $sql = Module::Build::DBD::Pg->get_meta_table_sql;
178              
179             Returns an SQL string that creates a metadata table named C<$table_name>.
180              
181             =head1 Author
182              
183             David E. Wheeler
184              
185             =head1 Copyright
186              
187             Copyright (c) 2008-2010 David E. Wheeler. Some Rights Reserved.
188              
189             This module is free software; you can redistribute it and/or modify it under
190             the same terms as Perl itself.
191              
192              
193             =cut