Alzabo/Driver/PostgreSQL.pm untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 190 CC 14 SCAR 22.5
CC
14
Cov
0%
next_sequence_number
30.0
schemas
12.0
create_database
12.0
CRAP 9.5
1 package Alzabo::Driver::PostgreSQL;
2  
3 use strict;
4 use vars qw($VERSION);
5  
6 use Alzabo::Driver;
7  
8 use DBD::Pg;
9 use DBI;
10  
11 use Params::Validate qw( :all );
12 Params::Validate::validation_options( on_fail => sub { Alzabo::Exception::Params->throw( error => join '', @_ ) } );
13  
14 $VERSION = 2.0;
15  
16 use base qw(Alzabo::Driver);
17  
18 sub new
19 {
20 my $proto = shift;
21 my $class = ref $proto || $proto;
22  
23 return bless {}, $class;
24 }
25  
26 sub connect
27 {
28 my $self = shift;
29  
30 $self->{tran_count} = undef;
31  
32 # This database handle is stale or nonexistent, so we need to (re)connect
33 $self->disconnect if $self->{dbh};
34 $self->{dbh} = $self->_make_dbh( @_,
35 name => $self->{schema}->db_schema_name
36 );
37 }
38  
39 sub supports_referential_integrity { 1 }
40  
41 sub schemas
42 {
43 my $self = shift;
44  
45 my %p = validate( @_, { user => { type => SCALAR | UNDEF,
46 optional => 1 },
47 password => { type => SCALAR | UNDEF,
48 optional => 1 },
49 host => { type => SCALAR | UNDEF,
50 optional => 1 },
51 port => { type => SCALAR | UNDEF,
52 optional => 1 },
53 options => { type => SCALAR | UNDEF,
54 optional => 1 },
55 tty => { type => SCALAR | UNDEF,
56 optional => 1 },
57 } );
58  
59 local %ENV;
60 foreach ( grep { defined $p{$_} && length $p{$_} } keys %p )
61 {
62 my $key = uc "pg$_";
63 $ENV{$key} = $p{$_};
64 }
65  
66 my @schemas = ( map { if ( defined )
67 {
68 /dbi:\w+:dbname="?(\w+)"?/i;
69 $1 ? $1 : ();
70 }
71 else
72 {
73 ();
74 }
75 }
76 DBI->data_sources( $self->dbi_driver_name ) );
77  
78 return @schemas;
79  
80 }
81  
82 sub tables
83 {
84 my $self = shift;
85  
86 # It seems that with DBD::Pg 1.31 & 1.32 you can't just the
87 # database's table, you also get the system tables back
88 return grep { ! /^(?:pg_catalog|information_schema)\./ } $self->SUPER::tables( @_ );
89 }
90  
91 sub create_database
92 {
93 my $self = shift;
94  
95 # Obviously we can't connect to the main database if it doesn't
96 # exist yet, but postgres doesn't let us be databaseless, so we
97 # connect to something else. "template1" should always be there.
98 my $dbh = $self->_make_dbh( @_, name => 'template1' );
99  
100 eval { $dbh->do( "CREATE DATABASE " . $dbh->quote_identifier( $self->{schema}->db_schema_name ) ); };
101  
102 my $e = $@;
103  
104 eval { $dbh->disconnect; };
105  
106 Alzabo::Exception::Driver->throw( error => $e ) if $e;
107 Alzabo::Exception::Driver->throw( error => $@ ) if $@;
108 }
109  
110 sub drop_database
111 {
112 my $self = shift;
113  
114 # We can't drop the current database, so we have to connect to
115 # something else. "template1" should always be there.
116 $self->disconnect;
117  
118 my $dbh = $self->_make_dbh( @_, name => 'template1' );
119  
120 eval { $dbh->do( "DROP DATABASE " . $dbh->quote_identifier( $self->{schema}->db_schema_name ) ); };
121 my $e = $@;
122  
123 eval { $dbh->disconnect; };
124 $e ||= $@;
125  
126 Alzabo::Exception::Driver->throw( error => $e ) if $e;
127 }
128  
129 sub _connect_params
130 {
131 my $self = shift;
132 my %p = @_;
133  
134 %p = validate( @_, { name => { type => SCALAR },
135 user => { type => SCALAR | UNDEF,
136 optional => 1 },
137 password => { type => SCALAR | UNDEF,
138 optional => 1 },
139 host => { type => SCALAR | UNDEF,
140 optional => 1 },
141 port => { type => SCALAR | UNDEF,
142 optional => 1 },
143 options => { type => SCALAR | UNDEF,
144 optional => 1 },
145 tty => { type => SCALAR | UNDEF,
146 optional => 1 },
147 service => { type => SCALAR | UNDEF,
148 optional => 1 },
149 sslmode => { type => SCALAR | UNDEF,
150 optional => 1 },
151 map { $_ => 0 } grep { /^pg_/ } keys %p,
152 } );
153  
154 my $dsn = "dbi:Pg:dbname=$p{name}";
155 foreach ( qw( host port options tty service sslmode ) )
156 {
157 $dsn .= ";$_=$p{$_}" if grep { defined && length } $p{$_};
158 }
159  
160 my %pg_keys = map { $_ => $p{$_} } grep { /^pg_/ } keys %p;
161  
162 return [ $dsn, $p{user}, $p{password},
163 { RaiseError => 1,
164 AutoCommit => 1,
165 PrintError => 0,
166 %pg_keys,
167 },
168 ];
169 }
170  
171 sub next_sequence_number
172 {
173 my $self = shift;
174 my $col = shift;
175  
176 $self->_ensure_valid_dbh;
177  
178 Alzabo::Exception::Params->throw
179 ( error => "This column (" . $col->name . ") is not sequenced" )
180 unless $col->sequenced;
181  
182 my $seq_name;
183  
184 if ( $col->type =~ /SERIAL/ )
185 {
186 $seq_name = join '_', $col->table->name, $col->name;
187 my $maxlen = $self->identifier_length;
188 $seq_name = substr( $seq_name, 0, $maxlen - 4 ) if length $seq_name > ($maxlen - 4);
189  
190 $seq_name .= '_seq';
191 }
192 else
193 {
194 $seq_name = join '___', $col->table->name, $col->name;
195 }
196  
197 $seq_name = $self->{dbh}->quote_identifier($seq_name)
198 if $self->{schema}->quote_identifiers;
199  
200 $self->{last_id} = $self->one_row( sql => "SELECT NEXTVAL('$seq_name')" );
201  
202 return $self->{last_id};
203 }
204  
205 sub get_last_id
206 {
207 my $self = shift;
208 return $self->{last_id};
209 }
210  
211 sub driver_id
212 {
213 return 'PostgreSQL';
214 }
215  
216 sub dbi_driver_name
217 {
218 return 'Pg';
219 }
220  
221 sub rdbms_version
222 {
223 my $self = shift;
224  
225 my $version_string = $self->one_row( sql => 'SELECT version()' );
226 my ($version) = $version_string =~ /^PostgreSQL ([\d.]+)/
227 or die "Couldn't determine version number from version string '$version_string'";
228  
229 return $version;
230 }
231  
232 sub identifier_length
233 {
234 my $self = shift;
235  
236 return $self->{identifier_length} if $self->{identifier_length};
237  
238 return
239 $self->{identifier_length} = $self->rdbms_version ge '7.3' ? 63 : 31;
240 }
241  
242 1;
243  
244 __END__