| 1 | |||
| 2 | package OOPS::sqlite2; | ||
| 3 | |||
| 4 | @ISA = qw(OOPS::DBO Exporter); | ||
| 5 | @EXPORT = qw( | ||
| 6 | tabledefs | ||
| 7 | table_list | ||
| 8 | db_initial_values | ||
| 9 | initial_query_set | ||
| 10 | $big_blob_size | ||
| 11 | $retry_count | ||
| 12 | ); | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | use Carp qw(confess); | ||
| 17 | |||
| 18 | our $big_blob_size = 900*1024; | ||
| 19 | |||
| 20 | # PRAGME integrity_check; ??? | ||
| 21 | |||
| 22 | sub new | ||
| 23 | { | ||
| 24 | my $pkg = shift; | ||
| 25 | my $dbo = OOPS::DBO->new(@_); | ||
| 26 | $dbo->{dbms} = 'sqlite2'; | ||
| 27 | bless $dbo, $pkg; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub tmode {} | ||
| 31 | |||
| 32 | sub lock_object {} | ||
| 33 | |||
| 34 | sub deadlock_rx | ||
| 35 | { | ||
| 36 | return ( | ||
| 37 | qr/database is locked(?:\(\d+\) at dbdimp\.c line )?/, | ||
| 38 | qr/unable to open database file\(\d+\) at dbdimp\.c line/, | ||
| 39 | ); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub nodata_rx | ||
| 43 | { | ||
| 44 | return qr/no such table: \S+object/; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub initialize | ||
| 48 | { | ||
| 49 | my $dbo = shift; | ||
| 50 | |||
| 51 | my $dbh = $dbo->{dbh}; | ||
| 52 | $dbh->{sqlite_handle_binary_nulls} = 1; | ||
| 53 | |||
| 54 | # | ||
| 55 | # SQLite will sometimes error out with a locked database | ||
| 56 | # error when it should have just waited instead. Oh well. | ||
| 57 | # | ||
| 58 | # http://rt.cpan.org/Ticket/Display.html?id=11680 | ||
| 59 | # | ||
| 60 | $dbh->func(10_000, 'busy_timeout'); | ||
| 61 | |||
| 62 | my $sync = $dbo->{default_synchronous} || $ENV{OOPS_SYNC}; | ||
| 63 | if ($sync) { | ||
| 64 | my $sm = $dbh->prepare("PRAGMA default_synchronous = $sync;") || confess $dbh->errstr; | ||
| 65 | $sm->execute || confess $sm->errstr; | ||
| 66 | } | ||
| 67 | |||
| 68 | $dbo->{sqlite_version} = 2; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub tabledefs | ||
| 72 | { | ||
| 73 | my $x = <<'END'; | ||
| 74 | |||
| 75 | CREATE TABLE TP_object ( | ||
| 76 | id INTEGER PRIMARY KEY, | ||
| 77 | loadgroup BIGINT, | ||
| 78 | class VARCHAR(255), # 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 INT, | ||
| 86 | gcgeneration INT DEFAULT 1 | ||
| 87 | ); | ||
| 88 | |||
| 89 | CREATE INDEX TP_group_index ON TP_object (loadgroup); | ||
| 90 | |||
| 91 | CREATE TABLE TP_attribute ( | ||
| 92 | id BIGINT NOT NULL, | ||
| 93 | pkey VARCHAR(128) NOT NULL, | ||
| 94 | pval VARCHAR(255), | ||
| 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 VARCHAR(128) NOT NULL, | ||
| 103 | fragno INT, | ||
| 104 | pval TEXT, | ||
| 105 | PRIMARY KEY (id, pkey, fragno)); | ||
| 106 | |||
| 107 | END | ||
| 108 | |||
| 109 | $x =~ s/#.*//mg; | ||
| 110 | return $x; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub lock | ||
| 114 | { | ||
| 115 | return 1; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub table_list | ||
| 119 | { | ||
| 120 | return (qw(TP_object TP_attribute TP_big)); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub db_initial_values | ||
| 124 | { | ||
| 125 | require OOPS::Setup; | ||
| 126 | return <<END; | ||
| 127 | INSERT INTO TP_object values(100, 100, 'HASH', 'H', 'V', '0', '0', 0, 1, 1, $OOPS::gcgenstart); | ||
| 128 | INSERT INTO TP_attribute values(2, 'last reserved object id', $OOPS::last_reserved_oid, 'R'); | ||
| 129 | END | ||
| 130 | } | ||
| 131 | |||
| 132 | sub allocate_id | ||
| 133 | { | ||
| 134 | return undef; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub post_new_object | ||
| 138 | { | ||
| 139 | my $dbo = shift; | ||
| 140 | return $dbo->{dbh}->func('last_insert_rowid'); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub byebye | ||
| 144 | { | ||
| 145 | } | ||
| 146 | |||
| 147 | our $retry_count = 0; | ||
| 148 | |||
| 149 | sub initial_query_set | ||
| 150 | { | ||
| 151 | return <<END; | ||
| 152 | bigload: 2 | ||
| 153 | SELECT pval, fragno FROM TP_big | ||
| 154 | WHERE id = ? AND pkey = ? | ||
| 155 | ORDER BY fragno | ||
| 156 | savebig: 2 3 | ||
| 157 | INSERT INTO TP_big | ||
| 158 | VALUES (?, ?, ?, ?) | ||
| 159 | updatebig: 1 3 | ||
| 160 | UPDATE TP_big | ||
| 161 | SET pval = ? | ||
| 162 | WHERE id = ? AND pkey = ? | ||
| 163 | END | ||
| 164 | } | ||
| 165 | |||
| 166 | sub rebless | ||
| 167 | { | ||
| 168 | my ($dbo, $oops) = @_; | ||
| 169 | bless $oops, 'OOPS::sqlite2::subclass'; | ||
| 170 | } | ||
| 171 | |||
| 172 | package OOPS::sqlite2::subclass; | ||
| 173 | |||
| 174 | use strict; | ||
| 175 | use Carp qw(confess); | ||
| 176 | |||
| 177 | our @ISA = qw(OOPS Exporter); | ||
| 178 | |||
| 179 | sub load_big | ||
| 180 | { | ||
| 181 | my ($oops, $id, $pkey) = @_; | ||
| 182 | my $bigloadQ = $oops->query('bigload'); | ||
| 183 | $bigloadQ->execute($id, $pkey) || confess $bigloadQ->errstr()." "; | ||
| 184 | my $val; | ||
| 185 | my ($frag, $fragno); | ||
| 186 | while (($frag, $fragno) = $bigloadQ->fetchrow_array()) { | ||
| 187 | $val .= $frag; | ||
| 188 | } | ||
| 189 | $bigloadQ->finish(); | ||
| 190 | confess "null big *$id/'$pkey'" if ! defined($val) || $val eq ''; | ||
| 191 | return $val; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub save_big | ||
| 195 | { | ||
| 196 | my $oops = shift; | ||
| 197 | my $id = shift; | ||
| 198 | my $pkey = shift; | ||
| 199 | my $savebigQ = $oops->query('savebig'); | ||
| 200 | for (my $fragno = 0; $fragno * $OOPS::sqlite2::big_blob_size < length($_[0]); $fragno++) { | ||
| 201 | $savebigQ->execute($id, $pkey, $fragno, substr($_[0], $fragno * $OOPS::sqlite2::big_blob_size, $OOPS::sqlite2::big_blob_size)) || confess $savebigQ->errstr; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | sub update_big | ||
| 206 | { | ||
| 207 | my $oops = shift; | ||
| 208 | my $id = shift; | ||
| 209 | my $pkey = shift; | ||
| 210 | $oops->query('deletebig', execute => [ $id, $pkey ]); | ||
| 211 | $oops->save_big($id, $pkey, $_[0]); | ||
| 212 | } | ||
| 213 | |||
| 214 | 1; | ||
| 215 |