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