OOPS/OOPS1001/sqlite.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 99 CC 3 SCAR 11.0
CC
3
Cov
0%
initialize
6.0
load_big
6.0
tabledefs
2.0
CRAP 3.0
1  
2 package OOPS::OOPS1001::sqlite;
3  
4 @ISA = qw(OOPS::OOPS1001);
5  
6 use strict;
7 use warnings;
8 use Carp qw(confess);
9  
10 our $big_blob_size = 900*1024;
11  
12 # PRAGME integrity_check; ???
13  
14 sub initialize
15 {
16 my $oops = shift;
17  
18 my $dbh = $oops->{dbh};
19 $dbh->{sqlite_handle_binary_nulls} = 1;
20  
21 if ($oops->{args}{default_synchronous}) {
22 my $sm = $dbh->prepare("PRAGMA default_synchronous = $oops->{args}{default_synchronous};") || die;
23 $sm->execute || die $sm->errstr;
24 }
25  
26 #my $tmode = $dbh->prepare('END TRANSACTION; BEGIN TRANSACTION ON CONFLICT ROLLBACK') || die;
27 #$tmode->execute() || die $tmode->errstr;
28 }
29  
30 sub tabledefs
31 {
32 my $x = <<'END';
33  
34 CREATE TABLE TP_object (
35 id INTEGER PRIMARY KEY,
36 loadgroup BIGINT,
37 class VARCHAR(255), # ref($object)
38 otype CHAR(1), # 'S'calar/ref, 'A'rray, 'H'ash
39 virtual CHAR(1), # load virutal ('V' or '0')
40 reftarg CHAR(1), # reference target ('T' or '0')
41 rfe CHAR(1), # reserved for future expansion
42 alen INT, # array length
43 refs INT, # references
44 counter SMALLINT
45 );
46  
47 CREATE INDEX TP_group_index ON TP_object (loadgroup);
48  
49 CREATE TABLE TP_attribute (
50 id BIGINT NOT NULL,
51 pkey VARCHAR(128) NOT NULL,
52 pval VARCHAR(255),
53 ptype VARCHAR(1), # type '0'-normal or 'R'eference 'B'ig
54 PRIMARY KEY (id, pkey));
55  
56 CREATE INDEX TP_value_index ON TP_attribute (pval);
57  
58 CREATE TABLE TP_big (
59 id BIGINT NOT NULL,
60 pkey VARCHAR(128) NOT NULL,
61 fragno INT,
62 pval TEXT,
63 PRIMARY KEY (id, pkey, fragno));
64  
65 END
66  
67 $x =~ s/#.*//mg;
68 return $x;
69 }
70  
71 sub table_list
72 {
73 return (qw(TP_object TP_attribute TP_big));
74 }
75  
76 sub db_initial_values
77 {
78 return <<END;
79 INSERT INTO TP_object values(100, 100, 'HASH', 'H', 'V', '0', '0', 0, 1, 1);
80 INSERT INTO TP_attribute values(2, 'last reserved object id', 100, 'R');
81 END
82 }
83  
84 sub allocate_id
85 {
86 return undef;
87 }
88  
89 sub post_new_object
90 {
91 my $oops = shift;
92 return $oops->{dbh}->func('last_insert_rowid');
93 }
94  
95 sub byebye
96 {
97 }
98  
99 sub initial_query_set
100 {
101 return <<END;
102 bigload: 2
103 SELECT pval, fragno FROM TP_big
104 WHERE id = ? AND pkey = ?
105 ORDER BY fragno
106 savebig: 2 3
107 INSERT INTO TP_big
108 VALUES (?, ?, ?, ?)
109 updatebig: 1 3
110 UPDATE TP_big
111 SET pval = ?
112 WHERE id = ? AND pkey = ?
113 END
114 }
115  
116 sub load_big
117 {
118 my ($oops, $id, $pkey) = @_;
119 my $bigloadQ = $oops->query('bigload');
120 $bigloadQ->execute($id, $pkey) || die $bigloadQ->errstr()." ";
121 my $val;
122 my ($frag, $fragno);
123 while (($frag, $fragno) = $bigloadQ->fetchrow_array()) {
124 $val .= $frag;
125 }
126 $bigloadQ->finish();
127 confess "null big *$id/'$pkey'" if ! defined($val) || $val eq '';
128 return $val;
129 }
130  
131 sub save_big
132 {
133 my $oops = shift;
134 my $id = shift;
135 my $pkey = shift;
136 my $savebigQ = $oops->query('savebig');
137 for (my $fragno = 0; $fragno * $big_blob_size < length($_[0]); $fragno++) {
138 $savebigQ->execute($id, $pkey, $fragno, substr($_[0], $fragno * $big_blob_size, $big_blob_size)) || die;
139 }
140 }
141  
142 sub update_big
143 {
144 my $oops = shift;
145 my $id = shift;
146 my $pkey = shift;
147 $oops->query('deletebig', execute => [ $id, $pkey ]);
148 $oops->save_big($id, $pkey, $_[0]);
149 }
150  
151 1;
152  
153 __END__