OOPS/sqlite_v3.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 201 CC 12 SCAR 37.6
CC
12
Cov
0%
initialize
132.0
load_big
6.0
tmode
2.0
CRAP 43.1
1  
2 #
3 # This handles DBD::SQLite 1.x which uses SQLite 3.x
4 #
5  
6 package OOPS::sqlite_v3;
7  
8 @ISA = qw(OOPS::DBO Exporter);
9 @EXPORT = qw(
10 tabledefs
11 table_list
12 db_initial_values
13 initial_query_set
14 $big_blob_size
15 $retry_count
16 );
17  
18 use strict;
19 use warnings;
20 use Carp qw(confess);
21  
22 our $big_blob_size = 900*1024;
23 our $enable_fd_leak_workaround = 500;
24 our $invocations = 0;
25  
26 sub tmode {}
27  
28 sub lock_object {}
29  
30 sub deadlock_rx
31 {
32 return (
33 qr/database is locked(?:\(\d+\) at dbdimp\.c line )?/,
34 qr/unable to open database file\(\d+\) at dbdimp\.c line/,
35 );
36 }
37  
38 sub nodata_rx
39 {
40 return qr/no such table: \S+object(?:\(1\) at dbdimp\.c)?/;
41 }
42  
43 sub initialize
44 {
45 my $dbo = shift;
46  
47 my $dbh = $dbo->{dbh};
48 $dbh->{sqlite_handle_binary_nulls} = 1;
49  
50  
51 #
52 # SQLite will sometimes error out with a locked database
53 # error when it should have just waited instead. Oh well.
54 #
55 # http://rt.cpan.org/Ticket/Display.html?id=11680
56 #
57 $dbh->func(10_000, 'busy_timeout');
58  
59 my $sync = $dbo->{default_synchronous} || $ENV{OOPS_SYNC};
60 if ($sync) {
61 my $sm = $dbh->prepare("PRAGMA default_synchronous = $sync;") || confess $dbh->errstr;
62 $sm->execute || confess $sm->errstr;
63 }
64  
65 $dbo->{sqlite_version} = 3;
66  
67 return unless $invocations++ >= $enable_fd_leak_workaround;
68 return unless $enable_fd_leak_workaround;
69 return undef unless -d "/proc/$$/fd";
70 return undef unless $dbo->{database} =~ /dbname=(\S+)/;
71  
72 my $dbfile = $1;
73 my ($dev, $ino) = (stat($dbfile))[0,1];
74  
75 my @r;
76 local(*D);
77 opendir(D, "/proc/$$/fd") || return undef;
78 @r = sort { $a <=> $b } grep($_ ne "." && $_ ne "..", readdir(D));
79 closedir(D);
80  
81 require POSIX;
82  
83 pop(@r); pop(@r); pop(@r); pop(@r);
84  
85 #
86 # The very-recently created sqlite open file is a
87 # pipe rather than a link so this won't accidently
88 # close it.
89 #
90 for my $r (@r) {
91 next unless $r > $enable_fd_leak_workaround;
92 my $f = readlink("/proc/$$/fd/$r");
93 next unless $f;
94 unless ($f eq $dbfile) {
95 my ($fddev, $fdino) = (stat($f))[0,1];
96 next unless $fddev == $dev;
97 next unless $fdino == $ino;
98 }
99 warn "Closing fd probably leaked by SQLite ($r);";
100 POSIX::close($r);
101 }
102 }
103  
104 # subroutine
105 sub tabledefs
106 {
107 my $x = <<'END';
108  
109 CREATE TABLE TP_object (
110 id INTEGER PRIMARY KEY,
111 loadgroup BIGINT,
112 class VARCHAR(255), # ref($object)
113 otype CHAR(1), # 'S'calar/ref, 'A'rray, 'H'ash
114 virtual CHAR(1), # load virutal ('V' or '0')
115 reftarg CHAR(1), # reference target ('T' or '0')
116 rfe CHAR(1), # reserved for future expansion
117 alen INT, # array length
118 refs INT, # references
119 counter INT,
120 gcgeneration INT DEFAULT 1
121 );
122  
123 CREATE INDEX TP_group_index ON TP_object (loadgroup);
124  
125 CREATE TABLE TP_attribute (
126 id BIGINT NOT NULL,
127 pkey VARCHAR(128) NOT NULL,
128 pval VARCHAR(255),
129 ptype VARCHAR(1), # type '0'-normal or 'R'eference 'B'ig
130 PRIMARY KEY (id, pkey));
131  
132 CREATE INDEX TP_value_index ON TP_attribute (pval);
133  
134 CREATE TABLE TP_big (
135 id BIGINT NOT NULL,
136 pkey VARCHAR(128) NOT NULL,
137 fragno INT,
138 pval TEXT,
139 PRIMARY KEY (id, pkey, fragno));
140  
141 END
142  
143 $x =~ s/#.*//mg;
144 return $x;
145 }
146  
147 sub lock
148 {
149 return 1;
150 }
151  
152 # subroutine
153 sub table_list
154 {
155 return (qw(TP_object TP_attribute TP_big));
156 }
157  
158 # subroutine
159 sub db_initial_values
160 {
161 require OOPS::Setup;
162 return <<END;
163 INSERT INTO TP_object values(100, 100, 'HASH', 'H', 'V', '0', '0', 0, 1, 1, $OOPS::gcgenstart);
164 INSERT INTO TP_attribute values(2, 'last reserved object id', $OOPS::last_reserved_oid, 'R');
165 END
166 }
167  
168 sub allocate_id
169 {
170 my $dbo = shift;
171 return undef;
172 }
173  
174 sub post_new_object
175 {
176 my $dbo = shift;
177 return $dbo->{dbh}->func('last_insert_rowid');
178 }
179  
180 our $retry_count = 0;
181  
182 # subroutine
183 sub initial_query_set
184 {
185 return <<END;
186 bigload: 2
187 SELECT pval, fragno FROM TP_big
188 WHERE id = ? AND pkey = ?
189 ORDER BY fragno
190 savebig: 2 3
191 INSERT INTO TP_big
192 VALUES (?, ?, ?, ?)
193 updatebig: 1 3
194 UPDATE TP_big
195 SET pval = ?
196 WHERE id = ? AND pkey = ?
197 END
198 }
199  
200 sub rebless
201 {
202 my ($dbo, $oops) = @_;
203 bless $oops, 'OOPS::sqlite2::subclass';
204 }
205  
206 package OOPS::sqlite2::subclass;
207  
208 use strict;
209 use warnings;
210 use Carp;
211  
212 our @ISA = qw(OOPS Exporter);
213  
214 sub load_big
215 {
216 my ($oops, $id, $pkey) = @_;
217 my $bigloadQ = $oops->query('bigload');
218 $bigloadQ->execute($id, $pkey) || confess $bigloadQ->errstr()." ";
219 my $val;
220 my ($frag, $fragno);
221 while (($frag, $fragno) = $bigloadQ->fetchrow_array()) {
222 $val .= $frag;
223 }
224 $bigloadQ->finish();
225 confess "null big *$id/'$pkey'" if ! defined($val) || $val eq '';
226 return $val;
227 }
228  
229 sub save_big
230 {
231 my $oops = shift;
232 my $id = shift;
233 my $pkey = shift;
234 my $savebigQ = $oops->query('savebig');
235 for (my $fragno = 0; $fragno * $big_blob_size < length($_[0]); $fragno++) {
236 $savebigQ->execute($id, $pkey, $fragno, substr($_[0], $fragno * $big_blob_size, $big_blob_size)) || confess;
237 }
238 }
239  
240 sub update_big
241 {
242 my $oops = shift;
243 my $id = shift;
244 my $pkey = shift;
245 $oops->query('deletebig', execute => [ $id, $pkey ]);
246 $oops->save_big($id, $pkey, $_[0]);
247 }
248  
249 1;
250