OOPS/OOPS1004/sqlite_v3.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 111 CC 3 SCAR 10.8
CC
3
Cov
0%
initialize
6.0
load_big
6.0
tabledefs
2.0
CRAP 2.9
1  
2 #
3 # This handles DBD::SQLite 1.x which uses SQLite 3.x
4 #
5  
6 package OOPS::OOPS1004::sqlite_v3;
7  
8 @ISA = qw(OOPS::OOPS1004 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  
24 sub initialize
25 {
26 my $oops = shift;
27  
28 my $dbh = $oops->{dbh};
29 $dbh->{sqlite_handle_binary_nulls} = 1;
30  
31 #
32 # SQLite will sometimes error out with a locked database
33 # error when it should have just waited instead. Oh well.
34 #
35 # http://rt.cpan.org/Ticket/Display.html?id=11680
36 #
37 $dbh->func(10_000, 'busy_timeout');
38  
39 if ($oops->{args}{default_synchronous} || $ENV{OOPS_SYNC}) {
40 my $sm = $dbh->prepare("PRAGMA default_synchronous = $oops->{args}{default_synchronous};") || confess $dbh->errstr;
41 $sm->execute || confess $sm->errstr;
42 }
43  
44 #my $tmode = $dbh->prepare('END TRANSACTION; BEGIN TRANSACTION ON CONFLICT ROLLBACK') || confess;
45 #$tmode->execute() || die $tmode->errstr;
46 }
47  
48 # subroutine
49 sub tabledefs
50 {
51 my $x = <<'END';
52  
53 CREATE TABLE TP_object (
54 id INTEGER PRIMARY KEY,
55 loadgroup BIGINT,
56 class VARCHAR(255), # ref($object)
57 otype CHAR(1), # 'S'calar/ref, 'A'rray, 'H'ash
58 virtual CHAR(1), # load virutal ('V' or '0')
59 reftarg CHAR(1), # reference target ('T' or '0')
60 rfe CHAR(1), # reserved for future expansion
61 alen INT, # array length
62 refs INT, # references
63 counter SMALLINT
64 );
65  
66 CREATE INDEX TP_group_index ON TP_object (loadgroup);
67  
68 CREATE TABLE TP_attribute (
69 id BIGINT NOT NULL,
70 pkey VARCHAR(128) NOT NULL,
71 pval VARCHAR(255),
72 ptype VARCHAR(1), # type '0'-normal or 'R'eference 'B'ig
73 PRIMARY KEY (id, pkey));
74  
75 CREATE INDEX TP_value_index ON TP_attribute (pval);
76  
77 CREATE TABLE TP_big (
78 id BIGINT NOT NULL,
79 pkey VARCHAR(128) NOT NULL,
80 fragno INT,
81 pval TEXT,
82 PRIMARY KEY (id, pkey, fragno));
83  
84 END
85  
86 $x =~ s/#.*//mg;
87 return $x;
88 }
89  
90 sub lock
91 {
92 return 1;
93 }
94  
95 # subroutine
96 sub table_list
97 {
98 return (qw(TP_object TP_attribute TP_big));
99 }
100  
101 # subroutine
102 sub db_initial_values
103 {
104 return <<END;
105 INSERT INTO TP_object values(100, 100, 'HASH', 'H', 'V', '0', '0', 0, 1, 1);
106 INSERT INTO TP_attribute values(2, 'last reserved object id', 100, 'R');
107 END
108 }
109  
110 sub allocate_id
111 {
112 my $oops = shift;
113 return undef;
114 }
115  
116 sub post_new_object
117 {
118 my $oops = shift;
119 return $oops->{dbh}->func('last_insert_rowid');
120 }
121  
122 sub byebye
123 {
124 my $oops = shift;
125 }
126  
127 our $retry_count = 0;
128  
129 # subroutine
130 sub initial_query_set
131 {
132 return <<END;
133 bigload: 2
134 SELECT pval, fragno FROM TP_big
135 WHERE id = ? AND pkey = ?
136 ORDER BY fragno
137 savebig: 2 3
138 INSERT INTO TP_big
139 VALUES (?, ?, ?, ?)
140 updatebig: 1 3
141 UPDATE TP_big
142 SET pval = ?
143 WHERE id = ? AND pkey = ?
144 END
145 }
146  
147 sub load_big
148 {
149 my ($oops, $id, $pkey) = @_;
150 my $bigloadQ = $oops->query('bigload');
151 $bigloadQ->execute($id, $pkey) || confess $bigloadQ->errstr()." ";
152 my $val;
153 my ($frag, $fragno);
154 while (($frag, $fragno) = $bigloadQ->fetchrow_array()) {
155 $val .= $frag;
156 }
157 $bigloadQ->finish();
158 confess "null big *$id/'$pkey'" if ! defined($val) || $val eq '';
159 return $val;
160 }
161  
162 sub save_big
163 {
164 my $oops = shift;
165 my $id = shift;
166 my $pkey = shift;
167 my $savebigQ = $oops->query('savebig');
168 for (my $fragno = 0; $fragno * $big_blob_size < length($_[0]); $fragno++) {
169 $savebigQ->execute($id, $pkey, $fragno, substr($_[0], $fragno * $big_blob_size, $big_blob_size)) || confess;
170 }
171 }
172  
173 sub update_big
174 {
175 my $oops = shift;
176 my $id = shift;
177 my $pkey = shift;
178 $oops->query('deletebig', execute => [ $id, $pkey ]);
179 $oops->save_big($id, $pkey, $_[0]);
180 }
181  
182 1;
183