OOPS/pg.pm untested

filter: Statementstmt 0.0% 0 / 161 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 330 CC 30 SCAR 48.5
CC
30
Cov
0%
query
380.0
allocate_id
30.0
execute
20.0
CRAP 128.1
1  
2 package OOPS::pg;
3  
4 @ISA = qw(OOPS::DBO);
5  
6 require OOPS;
7 use OOPS::DBO;
8 use strict;
9 use warnings;
10 use Carp qw(confess);
11 use DBD::Pg qw(:pg_types);
12  
13 BEGIN {
14 Filter::Util::Call::filter_add(\&OOPS::SelfFilter::filter)
15 unless $OOPS::SelfFilter::defeat;
16 }
17  
18 sub tmode
19 {
20 my ($dbo, $dbh) = @_;
21 $dbh = $dbo->{dbh} unless $dbh;
22  
23 # READ COMMITTED is the default
24 # my $tmode2 = $dbo->{counterdbh}->prepare('SET TRANSACTION ISOLATION LEVEL READ COMMITTED') || die;
25 # $tmode2->execute() || die $tmode2->errstr;
26 unless ($dbo->{readonly}) {
27 my $tmode = $dbh->prepare('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE') || die;
28 $tmode->execute() || die;
29 }
30 }
31  
32 #
33 # Error code that indicates deadlock or clashing transactions.
34 #
35 sub deadlock_rx
36 {
37 return (
38 qr{ERROR: could not serialize access due to concurrent update}, # pg 8.1
39 qr{ERROR: could not serialize access due to read/write dependencies among transactions}, # pg 9.1
40 qr{ERROR: deadlock detected},
41 qr{ERROR: duplicate key value violates unique constraint}, # pg 9.1
42 qr{ERROR: duplicate key violates unique constraint}, # pg 8.1
43 );
44 }
45  
46 sub nodata_rx
47 {
48 return qr/ERROR: relation "\S+object" does not exist/;
49 }
50  
51 sub initialize
52 {
53 my ($dbo) = @_;
54  
55 # $dbo->tmode;
56  
57 $dbo->{counterdbh} = OOPS::dbiconnect(undef, %$dbo);
58  
59 $dbo->{id_pool_start} = 0;
60 $dbo->{id_pool_end} = 0;
61 }
62  
63 #
64 # Postgres SERIALIZABLE doesn't really work: adding a new
65 # row that would have been returned by query in another process
66 # is allowed. Setting this to 1 forces the object record
67 # to be updated every time the object contents change.
68 #
69 sub do_forcesave { 1 };
70  
71 sub tabledefs
72 {
73 my $x = <<'END';
74  
75 CREATE TABLE TP_object (
76 id BIGINT,
77 loadgroup BIGINT,
78 class BYTEA, # ref($object)
79 otype CHAR(1), # 'S'calar/ref, 'A'rray, 'H'ash
80 virtual CHAR(1), # load virutal ('V' or '0')
81 reftarg CHAR(1), # reference target ('T' or '0')
82 rfe CHAR(1), # reserved for future expansion
83 alen INT, # array length
84 refs INT, # references
85 counter SMALLINT,
86 gcgeneration INT DEFAULT 1,
87 PRIMARY KEY (id));
88  
89 CREATE INDEX TP_group_index ON TP_object (loadgroup);
90  
91 CREATE TABLE TP_attribute (
92 id BIGINT NOT NULL,
93 pkey BYTEA,
94 pval BYTEA,
95 ptype VARCHAR(1), # type '0'-normal or 'R'eference 'B'ig
96 PRIMARY KEY (id, pkey));
97  
98 CREATE INDEX TP_value_index ON TP_attribute (pval);
99  
100 CREATE TABLE TP_big (
101 id BIGINT NOT NULL,
102 pkey BYTEA,
103 pval BYTEA,
104 PRIMARY KEY (id, pkey));
105  
106 CREATE TABLE TP_counters (
107 name VARCHAR(128),
108 cval BIGINT,
109 PRIMARY KEY (name));
110  
111 END
112 $x =~ s/#.*//mg;
113 return $x;
114 }
115  
116 sub table_list
117 {
118 return (qw(TP_object TP_attribute TP_big TP_counters));
119 }
120  
121 sub db_initial_values
122 {
123 require OOPS::Setup;
124 return <<END;
125 INSERT INTO TP_counters values ('objectid', $OOPS::last_reserved_oid + 1);
126 END
127 }
128  
129 sub initial_query_set
130 {
131 return <<END;
132 allocate_id:
133 UPDATE TP_counters
134 SET cval = cval + ?
135 WHERE name = 'objectid'
136 get_id:
137 SELECT cval
138 FROM TP_counters
139 WHERE name = 'objectid'
140 bigload: 2
141 SELECT pval FROM TP_big
142 WHERE id = ? AND pkey = ?
143 savebig: 2 3
144 INSERT INTO TP_big
145 VALUES (?, ?, ?)
146 updatebig: 1 3
147 UPDATE TP_big
148 SET pval = ?
149 WHERE id = ? AND pkey = ?
150 lock_object:
151 SELECT loadgroup
152 FROM TP_object
153 WHERE id = ? FOR UPDATE
154 lock_attribute:
155 SELECT ptype
156 FROM TP_attribute
157 WHERE id = ? AND pkey = ? FOR UPDATE
158 END
159 }
160  
161  
162 #
163 # bind_param only has to happen once.
164 #
165 sub query
166 {
167 my ($dbo, $q, %args) = @_;
168  
169 my $query;
170 my $dbh;
171 my $sth;
172 my $fresh = 0;
173  
174 $dbo->query_debug('pg', $q, %args);
175  
176 if (($sth = $dbo->{cached_queries}{$q})) {
177 # great
178 if ($sth->{Active}) {
179 print "Query $q was still active\n" if $OOPS::debug_queries;
180 delete $dbo->{cached_queries}{$q};
181 delete $dbo->{bind_done}{$q};
182 return query($dbo, $q, %args);
183 }
184 if ($dbo->{binary_q_list}{$q} && ! $dbo->{bind_done}{$q}) {
185 $fresh = 1;
186 }
187 } elsif (($query = $dbo->{queries}{$q})) {
188 1 while $query =~ s/DBO:CAST:PG2INT\(($pmatch)\)/CAST($1 AS integer)/s;
189 1 while $query =~ s/DBO:CAST:PGBYTEA2INT\(($pmatch)\)/CAST(encode($1, 'escape') AS integer)/s;
190 1 while $query =~ s/DBO:CAST:PG2BYTEA\(($pmatch)\)/decode(CAST($1 AS text), 'escape')/s;
191 die if $query =~ /\bDBO:[A-Z]+:PG/;
192 $query = $dbo->clean_query($query);
193 $dbh = $args{dbh} || $dbo->{dbh};
194 $sth = $dbh->prepare($query) || die $dbh->errstr;
195 $dbo->{cached_queries}{$q} = $sth;
196 $fresh = 1;
197 } else {
198 confess "no query <$q>";
199 }
200  
201 if ($dbo->{binary_q_list}{$q} && ! $dbo->{binary_params}{$q}) {
202 $dbo->{binary_params}{$q} = [];
203 for my $i (grep($_ > 0, split(' ', $dbo->{binary_q_list}{$q}))) {
204 $dbo->{binary_params}{$q}[$i] = 1;
205 }
206 }
207  
208  
209 my $debug_x = ++$dbo->{invoke_count}{$q};
210 print $dbo->{binary_q_list}{$q} ? "BINARY: $q - $debug_x/$fresh\n" : "NOT B: $q - $debug_x/$fresh\n"
211 if $OOPS::debug_dbd;
212  
213 if (exists $args{execute}) {
214 my @a = defined($args{execute})
215 ? (ref($args{execute})
216 ? @{$args{execute}}
217 : $args{execute})
218 : ();
219  
220 my $e;
221 if ($dbo->{binary_params}{$q} && $fresh) {
222 for (my $i = 0; $i <= $#a; $i++) {
223 if ($dbo->{binary_params}{$q}[$i+1]) {
224 $sth->bind_param($i+1, $a[$i],
225 { pg_type => PG_BYTEA });
226 printf "Bind-param %s #%d - binary\n", $q, $i+1 if $OOPS::debug_dbd;
227 } else {
228 $sth->bind_param($i+1, $a[$i]);
229 }
230 }
231 $dbo->{bind_done}{$q} = 1;
232 $sth->execute() or $e = "Could Not Execute '$query' with '@a':" . ($sth->errstr);
233 } else {
234 $sth->execute(@a) or $e = "could not execute '$query' with '@a':".$sth->errstr;
235 }
236 if ($e) {
237 $e =~ s/\n/\\n /g; # debug
238 confess($e);
239 }
240 } elsif ($dbo->{binary_params}{$q} && $fresh) {
241 print "Using wrapper...\n" if $OOPS::debug_dbd;
242 return OOPS::pg::sth->new($sth, $q, $dbo->{binary_params}{$q}, \$dbo->{bind_done}{$q});
243 }
244  
245 return $sth;
246 }
247  
248 sub lock_object
249 {
250 my ($dbo, $id) = @_;
251 my $q = $dbo->query('lock_object', execute => [ $id ]);
252 (undef) = $q->fetchrow_array;
253 $q->finish()
254 }
255  
256 sub lock_attribute
257 {
258 my ($dbo, $id, $pkey) = @_;
259 my $q = $dbo->query('lock_attribute', execute => [ $id, $pkey ]);
260 (undef) = $q->fetchrow_array;
261 $q->finish()
262 }
263  
264 sub allocate_id
265 {
266 my $dbo = shift;
267 my $id;
268 if ($dbo->{id_pool_start} && $dbo->{id_pool_start} < $dbo->{id_pool_end}) {
269 $id = $dbo->{id_pool_start}++;
270 print "in allocate_id, allocating $id from pool\n" if $OOPS::debug_object_id;
271 } else {
272 my $allocate_idQ = $dbo->query('allocate_id', dbh => $dbo->{counterdbh}, execute => $OOPS::id_alloc_size);
273 my $get_idQ = $dbo->query('get_id', dbh => $dbo->{counterdbh}, execute => []);
274 (($id) = $get_idQ->fetchrow_array) || die $get_idQ->errstr;
275 $get_idQ->finish;
276 $dbo->{id_pool_start} = $id+1;
277 $dbo->{id_pool_end} = $id+$OOPS::id_alloc_size;
278 $dbo->{counterdbh}->commit || die $dbo->{counterdbh}->errstr;
279 print "in allocate_id, new pool: $dbo->{id_pool_start} to $dbo->{id_pool_end}\n" if $OOPS::debug_object_id;
280 print "in allocate_id, allocated $id from before pool\n" if $OOPS::debug_object_id;
281 }
282 return $id;
283 }
284  
285 sub post_new_object
286 {
287 my $dbo = shift;
288 return $_[0];
289 }
290  
291 sub disconnect
292 {
293 my $dbo = shift;
294 $dbo->{counterdbh}->disconnect() if $dbo->{counterdbh};
295 delete $dbo->{counterdbh};
296 $dbo->SUPER::disconnect();
297 }
298  
299 package OOPS::pg::sth;
300  
301 use strict;
302 use warnings;
303 use Carp qw(confess);
304 use DBD::Pg qw(:pg_types);
305  
306 sub new
307 {
308 my ($pkg, $sth, $q, $binary_params, $doneref) = @_;
309 return bless [ $sth, $q, $binary_params, $doneref];
310 }
311  
312 sub execute
313 {
314 my ($self, @values) = @_;
315 my ($sth, $q, $binary_params, $doneref) = @$self;
316 $$doneref = 2;
317  
318 for (my $i = 0; $i <= $#values; $i++) {
319 die if ref $values[$i];
320 if ($binary_params->[$i+1]) {
321 $sth->bind_param($i+1, $values[$i],
322 { pg_type => PG_BYTEA });
323 printf "Bind-param %s #%d - binary\n", $q, $i+1 if $OOPS::debug_dbd;
324 } else {
325 $sth->bind_param($i+1, $values[$i]);
326 }
327 }
328 @$self = ($sth);
329 $sth->execute();
330 }
331  
332 sub AUTOLOAD
333 {
334 my $self = shift;
335 our $AUTOLOAD;
336 my $a = $AUTOLOAD;
337 $a =~ s/.*:://;
338 my $method = $self->[0]->can($a) || $self->[0]->can($AUTOLOAD) || confess "cannot find method $a for $self->[0]";
339 &$method($self->[0], @_);
340 }
341  
342 1;
343