| 1 | |||
| 2 | package OOPS::OOPS1004::pg; | ||
| 3 | |||
| 4 | @ISA = qw(OOPS::OOPS1004); | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use Carp qw(confess); | ||
| 9 | |||
| 10 | BEGIN { | ||
| 11 | Filter::Util::Call::filter_add(\&OOPS::OOPS1004::SelfFilter::filter) | ||
| 12 | unless $OOPS::SelfFilter::defeat; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub initialize | ||
| 16 | { | ||
| 17 | my $oops = shift; | ||
| 18 | |||
| 19 | my $dbh = $oops->{dbh}; | ||
| 20 | |||
| 21 | unless ($oops->{readonly}) { | ||
| 22 | my $tmode = $dbh->prepare('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE') || die; | ||
| 23 | $tmode->execute() || die; | ||
| 24 | } | ||
| 25 | |||
| 26 | $oops->{counterdbh} = $oops->dbiconnect(); | ||
| 27 | # READ COMMITTED is the default | ||
| 28 | # my $tmode2 = $oops->{counterdbh}->prepare('SET TRANSACTION ISOLATION LEVEL READ COMMITTED') || die; | ||
| 29 | # $tmode2->execute() || die $tmode2->errstr; | ||
| 30 | |||
| 31 | $oops->{id_pool_start} = 0; | ||
| 32 | $oops->{id_pool_end} = 0; | ||
| 33 | |||
| 34 | my $queries = $oops->{queries}; | ||
| 35 | for my $q (keys %$queries) { | ||
| 36 | my $count = ($queries->{$q} =~ tr/?/?/); | ||
| 37 | $oops->{param_count}{$q} = $count; | ||
| 38 | $oops->{binary_params}{$q} = []; | ||
| 39 | for my $i (split(' ',$oops->{binary_q_list}{$q})) { | ||
| 40 | next unless $i > 0; | ||
| 41 | $oops->{binary_params}{$q}[$i] = 1; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | sub tabledefs | ||
| 47 | { | ||
| 48 | my $x = <<'END'; | ||
| 49 | |||
| 50 | CREATE TABLE TP_object ( | ||
| 51 | id BIGINT, | ||
| 52 | loadgroup BIGINT, | ||
| 53 | class BYTEA, # 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 | PRIMARY KEY (id)); | ||
| 62 | |||
| 63 | CREATE INDEX TP_group_index ON TP_object (loadgroup); | ||
| 64 | |||
| 65 | CREATE TABLE TP_attribute ( | ||
| 66 | id BIGINT NOT NULL, | ||
| 67 | pkey BYTEA, | ||
| 68 | pval BYTEA, | ||
| 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 BYTEA, | ||
| 77 | pval BYTEA, | ||
| 78 | PRIMARY KEY (id, pkey)); | ||
| 79 | |||
| 80 | CREATE TABLE TP_counters ( | ||
| 81 | name VARCHAR(128), | ||
| 82 | cval BIGINT, | ||
| 83 | PRIMARY KEY (name)); | ||
| 84 | |||
| 85 | END | ||
| 86 | $x =~ s/#.*//mg; | ||
| 87 | return $x; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub table_list | ||
| 91 | { | ||
| 92 | return (qw(TP_object TP_attribute TP_big TP_counters)); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub db_initial_values | ||
| 96 | { | ||
| 97 | return <<END; | ||
| 98 | INSERT INTO TP_counters values ('objectid', 101); | ||
| 99 | END | ||
| 100 | } | ||
| 101 | |||
| 102 | sub initial_query_set | ||
| 103 | { | ||
| 104 | return <<END; | ||
| 105 | allocate_id: | ||
| 106 | UPDATE TP_counters | ||
| 107 | SET cval = cval + ? | ||
| 108 | WHERE name = 'objectid' | ||
| 109 | get_id: | ||
| 110 | SELECT cval | ||
| 111 | FROM TP_counters | ||
| 112 | WHERE name = 'objectid' | ||
| 113 | bigload: 2 | ||
| 114 | SELECT pval FROM TP_big | ||
| 115 | WHERE id = ? AND pkey = ? | ||
| 116 | savebig: 2 3 | ||
| 117 | INSERT INTO TP_big | ||
| 118 | VALUES (?, ?, ?) | ||
| 119 | updatebig: 1 3 | ||
| 120 | UPDATE TP_big | ||
| 121 | SET pval = ? | ||
| 122 | WHERE id = ? AND pkey = ? | ||
| 123 | lock_object: | ||
| 124 | SELECT loadgroup | ||
| 125 | FROM TP_object | ||
| 126 | WHERE id = ? FOR UPDATE | ||
| 127 | lock_attribute: | ||
| 128 | SELECT ptype | ||
| 129 | FROM TP_attribute | ||
| 130 | WHERE id = ? AND pkey = ? FOR UPDATE | ||
| 131 | END | ||
| 132 | } | ||
| 133 | |||
| 134 | sub query | ||
| 135 | { | ||
| 136 | my ($oops, $q, %args) = @_; | ||
| 137 | |||
| 138 | my $query; | ||
| 139 | confess "no query '$q'" unless $query = $oops->{queries}{$q}; | ||
| 140 | |||
| 141 | $query =~ s/TP_/$oops->{table_prefix}/g; | ||
| 142 | |||
| 143 | $oops->query_debug('pg', $q, %args); | ||
| 144 | |||
| 145 | my $dbh = $args{dbh} || $oops->{dbh}; | ||
| 146 | my $sth = $dbh->prepare_cached($query, undef, 3) || die $dbh->errstr; | ||
| 147 | |||
| 148 | if (exists $args{execute}) { | ||
| 149 | my @a = defined($args{execute}) | ||
| 150 | ? (ref($args{execute}) | ||
| 151 | ? @{$args{execute}} | ||
| 152 | : $args{execute}) | ||
| 153 | : (); | ||
| 154 | |||
| 155 | my $e; | ||
| 156 | if ($oops->{binary_params}{$q}) { | ||
| 157 | for (my $i = 0; $i <= $#a; $i++) { | ||
| 158 | if ($oops->{binary_params}{$q}[$i+1]) { | ||
| 159 | $sth->bind_param($i+1, $a[$i], | ||
| 160 | { pg_type => DBD::Pg::PG_BYTEA }); | ||
| 161 | } else { | ||
| 162 | $sth->bind_param($i+1, $a[$i]); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | $sth->execute() or $e = "Could Not Execute '$query' with '@a':" . ($sth->errstr); | ||
| 166 | } else { | ||
| 167 | $sth->execute(@a) or $e = "could not execute '$query' with '@a':".$sth->errstr; | ||
| 168 | } | ||
| 169 | if ($e) { | ||
| 170 | $e =~ s/\n/\\n /g; # debug | ||
| 171 | confess($e); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | return $sth; | ||
| 176 | } | ||
| 177 | |||
| 178 | sub lock_object | ||
| 179 | { | ||
| 180 | my ($oops, $id) = @_; | ||
| 181 | my $q = $oops->query('lock_object', execute => [ $id ]); | ||
| 182 | (undef) = $q->fetchrow_array; | ||
| 183 | $q->finish() | ||
| 184 | } | ||
| 185 | |||
| 186 | sub lock_attribute | ||
| 187 | { | ||
| 188 | my ($oops, $id, $pkey) = @_; | ||
| 189 | my $q = $oops->query('lock_attribute', execute => [ $id, $pkey ]); | ||
| 190 | (undef) = $q->fetchrow_array; | ||
| 191 | $q->finish() | ||
| 192 | } | ||
| 193 | |||
| 194 | sub save_big | ||
| 195 | { | ||
| 196 | my $oops = shift; | ||
| 197 | my $id = shift; | ||
| 198 | my $pkey = shift; | ||
| 199 | $oops->query('savebig', execute => [ $id, $pkey, $_[0] ]); | ||
| 200 | } | ||
| 201 | |||
| 202 | sub update_big | ||
| 203 | { | ||
| 204 | my $oops = shift; | ||
| 205 | my $id = shift; | ||
| 206 | my $pkey = shift; | ||
| 207 | my $updatebigQ = $oops->query('updatebig', execute => [ $_[0], $id, $pkey ]); | ||
| 208 | } | ||
| 209 | |||
| 210 | sub allocate_id | ||
| 211 | { | ||
| 212 | my $oops = shift; | ||
| 213 | my $id; | ||
| 214 | if ($oops->{id_pool_start} && $oops->{id_pool_start} < $oops->{id_pool_end}) { | ||
| 215 | $id = $oops->{id_pool_start}++; | ||
| 216 | print "in allocate_id, allocating $id from pool\n" if $OOPS::OOPS1004::debug_object_id; | ||
| 217 | } else { | ||
| 218 | my $allocate_idQ = $oops->query('allocate_id', dbh => $oops->{counterdbh}, execute => $OOPS::OOPS1004::id_alloc_size); | ||
| 219 | my $get_idQ = $oops->query('get_id', dbh => $oops->{counterdbh}, execute => []); | ||
| 220 | (($id) = $get_idQ->fetchrow_array) || die $get_idQ->errstr; | ||
| 221 | $get_idQ->finish; | ||
| 222 | $oops->{id_pool_start} = $id+1; | ||
| 223 | $oops->{id_pool_end} = $id+$OOPS::OOPS1004::id_alloc_size; | ||
| 224 | $oops->{counterdbh}->commit || die $oops->{counterdbh}->errstr; | ||
| 225 | print "in allocate_id, new pool: $oops->{id_pool_start} to $oops->{id_pool_end}\n" if $OOPS::OOPS1004::debug_object_id; | ||
| 226 | print "in allocate_id, allocated $id from before pool\n" if $OOPS::OOPS1004::debug_object_id; | ||
| 227 | } | ||
| 228 | return $id; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub post_new_object | ||
| 232 | { | ||
| 233 | my $oops = shift; | ||
| 234 | return $_[0]; | ||
| 235 | } | ||
| 236 | |||
| 237 | sub byebye | ||
| 238 | { | ||
| 239 | my $oops = shift; | ||
| 240 | $oops->{counterdbh}->disconnect() if $oops->{counterdbh}; | ||
| 241 | } | ||
| 242 | |||
| 243 | 1; | ||
| 244 |