| 1 | |||
| 2 | package OOPS::OOPS1003::pg; | ||
| 3 | |||
| 4 | @ISA = qw(OOPS::OOPS1003); | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use Carp qw(confess); | ||
| 9 | |||
| 10 | BEGIN { | ||
| 11 | Filter::Util::Call::filter_add(\&OOPS::OOPS1003::SelfFilter::filter) | ||
| 12 | unless $OOPS::OOPS1003::SelfFilter::defeat; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub initialize | ||
| 16 | { | ||
| 17 | my $oops = shift; | ||
| 18 | |||
| 19 | my $dbh = $oops->{dbh}; | ||
| 20 | |||
| 21 | my $tmode = $dbh->prepare('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE') || die; | ||
| 22 | $tmode->execute() || die; | ||
| 23 | |||
| 24 | $oops->{counterdbh} = $oops->dbiconnect(); | ||
| 25 | my $tmode2 = $oops->{counterdbh}->prepare('SET TRANSACTION ISOLATION LEVEL READ COMMITTED') || die; | ||
| 26 | $tmode2->execute() || die $tmode2->errstr; | ||
| 27 | |||
| 28 | $oops->{id_pool_start} = 0; | ||
| 29 | $oops->{id_pool_end} = 0; | ||
| 30 | |||
| 31 | my $queries = $oops->{queries}; | ||
| 32 | for my $q (keys %$queries) { | ||
| 33 | my $count = ($queries->{$q} =~ tr/?/?/); | ||
| 34 | $oops->{param_count}{$q} = $count; | ||
| 35 | $oops->{binary_params}{$q} = []; | ||
| 36 | for my $i (split(' ',$oops->{binary_q_list}{$q})) { | ||
| 37 | next unless $i > 0; | ||
| 38 | $oops->{binary_params}{$q}[$i] = 1; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | sub tabledefs | ||
| 44 | { | ||
| 45 | my $x = <<'END'; | ||
| 46 | |||
| 47 | CREATE TABLE TP_object ( | ||
| 48 | id BIGINT, | ||
| 49 | loadgroup BIGINT, | ||
| 50 | class BYTEA, # ref($object) | ||
| 51 | otype CHAR(1), # 'S'calar/ref, 'A'rray, 'H'ash | ||
| 52 | virtual CHAR(1), # load virutal ('V' or '0') | ||
| 53 | reftarg CHAR(1), # reference target ('T' or '0') | ||
| 54 | rfe CHAR(1), # reserved for future expansion | ||
| 55 | alen INT, # array length | ||
| 56 | refs INT, # references | ||
| 57 | counter SMALLINT, | ||
| 58 | PRIMARY KEY (id)); | ||
| 59 | |||
| 60 | CREATE INDEX TP_group_index ON TP_object (loadgroup); | ||
| 61 | |||
| 62 | CREATE TABLE TP_attribute ( | ||
| 63 | id BIGINT NOT NULL, | ||
| 64 | pkey BYTEA, | ||
| 65 | pval BYTEA, | ||
| 66 | ptype VARCHAR(1), # type '0'-normal or 'R'eference 'B'ig | ||
| 67 | PRIMARY KEY (id, pkey)); | ||
| 68 | |||
| 69 | CREATE INDEX TP_value_index ON TP_attribute (pval); | ||
| 70 | |||
| 71 | CREATE TABLE TP_big ( | ||
| 72 | id BIGINT NOT NULL, | ||
| 73 | pkey BYTEA, | ||
| 74 | pval TEXT, | ||
| 75 | PRIMARY KEY (id, pkey)); | ||
| 76 | |||
| 77 | CREATE TABLE TP_counters ( | ||
| 78 | name VARCHAR(128), | ||
| 79 | cval BIGINT, | ||
| 80 | PRIMARY KEY (name)); | ||
| 81 | |||
| 82 | END | ||
| 83 | $x =~ s/#.*//mg; | ||
| 84 | return $x; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub table_list | ||
| 88 | { | ||
| 89 | return (qw(TP_object TP_attribute TP_big TP_counters)); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub db_initial_values | ||
| 93 | { | ||
| 94 | return <<END; | ||
| 95 | INSERT INTO TP_counters values ('objectid', 101); | ||
| 96 | END | ||
| 97 | } | ||
| 98 | |||
| 99 | sub initial_query_set | ||
| 100 | { | ||
| 101 | return <<END; | ||
| 102 | allocate_id: | ||
| 103 | UPDATE TP_counters | ||
| 104 | SET cval = cval + ? | ||
| 105 | WHERE name = 'objectid' | ||
| 106 | get_id: | ||
| 107 | SELECT cval | ||
| 108 | FROM TP_counters | ||
| 109 | WHERE name = 'objectid' | ||
| 110 | bigload: 2 | ||
| 111 | SELECT DECODE(pval, 'escape') FROM TP_big | ||
| 112 | WHERE id = ? AND pkey = ? | ||
| 113 | savebig: 2 3 | ||
| 114 | INSERT INTO TP_big | ||
| 115 | VALUES (?, ?, ENCODE(?, 'escape')) | ||
| 116 | updatebig: 1 3 | ||
| 117 | UPDATE TP_big | ||
| 118 | SET pval = ENCODE(?, 'escape') | ||
| 119 | WHERE id = ? AND pkey = ? | ||
| 120 | END | ||
| 121 | } | ||
| 122 | |||
| 123 | sub query | ||
| 124 | { | ||
| 125 | my ($oops, $q, %args) = @_; | ||
| 126 | |||
| 127 | my $query; | ||
| 128 | confess unless $query = $oops->{queries}{$q}; | ||
| 129 | $query =~ s/TP_/$oops->{table_prefix}/g; | ||
| 130 | |||
| 131 | my $debug_match = $query =~ /$OOPS::debug_q_regex/ | ||
| 132 | || $oops->{debug_q}{$q} =~ /$OOPS::debug_q_regex/; | ||
| 133 | |||
| 134 | my $dbh = $args{dbh} || $oops->{dbh}; | ||
| 135 | my $sth = $dbh->prepare_cached($query, undef, 3) || die $dbh->errstr; | ||
| 136 | |||
| 137 | print STDERR "Q1003: $q\n" if ($OOPS::debug_queries & 2) | ||
| 138 | && (($OOPS::debug_queries & 1) || $debug_match); | ||
| 139 | print STDERR "Q1003: $query" if ($OOPS::debug_queries & 4) | ||
| 140 | && (($OOPS::debug_queries & 1) || $debug_match); | ||
| 141 | |||
| 142 | if (exists $args{execute}) { | ||
| 143 | my @a = defined($args{execute}) | ||
| 144 | ? (ref($args{execute}) | ||
| 145 | ? @{$args{execute}} | ||
| 146 | : $args{execute}) | ||
| 147 | : (); | ||
| 148 | |||
| 149 | if ($oops->{binary_params}{$q}) { | ||
| 150 | for (my $i = 0; $i <= $#a; $i++) { | ||
| 151 | if ($oops->{binary_params}{$q}[$i+1]) { | ||
| 152 | $sth->bind_param($i+1, $a[$i], | ||
| 153 | { pg_type => DBD::Pg::PG_BYTEA }); | ||
| 154 | } else { | ||
| 155 | $sth->bind_param($i+1, $a[$i]); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | $sth->execute() || confess "could not execute '$query' with '@a':".$sth->errstr; | ||
| 159 | } else { | ||
| 160 | $sth->execute(@a) || confess "could not execute '$query' with '@a':".$sth->errstr; | ||
| 161 | } | ||
| 162 | print STDERR "A1003: ".join(',', $q, @a)."\n" if ($OOPS::debug_queries & 8) | ||
| 163 | && (($OOPS::debug_queries & 1) || $debug_match); | ||
| 164 | } else { | ||
| 165 | print STDERR "A1003: ".join(',', $q, '-none-')."\n" if ($OOPS::debug_queries & 8) | ||
| 166 | && (($OOPS::debug_queries & 1) || $debug_match); | ||
| 167 | } | ||
| 168 | |||
| 169 | return $sth; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub save_big | ||
| 173 | { | ||
| 174 | my $oops = shift; | ||
| 175 | my $id = shift; | ||
| 176 | my $pkey = shift; | ||
| 177 | $oops->query('savebig', execute => [ $id, $pkey, $_[0] ]); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub update_big | ||
| 181 | { | ||
| 182 | my $oops = shift; | ||
| 183 | my $id = shift; | ||
| 184 | my $pkey = shift; | ||
| 185 | my $updatebigQ = $oops->query('updatebig', execute => [ $_[0], $id, $pkey ]); | ||
| 186 | } | ||
| 187 | |||
| 188 | sub allocate_id | ||
| 189 | { | ||
| 190 | my $oops = shift; | ||
| 191 | my $id; | ||
| 192 | if ($oops->{id_pool_start} && $oops->{id_pool_start} < $oops->{id_pool_end}) { | ||
| 193 | $id = $oops->{id_pool_start}++; | ||
| 194 | print "in allocate_id, allocating $id from pool\n" if $OOPS::OOPS1003::debug_object_id; | ||
| 195 | } else { | ||
| 196 | my $allocate_idQ = $oops->query('allocate_id', dbh => $oops->{counterdbh}, execute => $OOPS::OOPS1003::id_alloc_size); | ||
| 197 | my $get_idQ = $oops->query('get_id', dbh => $oops->{counterdbh}, execute => []); | ||
| 198 | (($id) = $get_idQ->fetchrow_array) || die $get_idQ->errstr; | ||
| 199 | $get_idQ->finish; | ||
| 200 | $oops->{id_pool_start} = $id+1; | ||
| 201 | $oops->{id_pool_end} = $id+$OOPS::OOPS1003::id_alloc_size; | ||
| 202 | $oops->{counterdbh}->commit || die $oops->{counterdbh}->errstr; | ||
| 203 | print "in allocate_id, new pool: $oops->{id_pool_start} to $oops->{id_pool_end}\n" if $OOPS::OOPS1003::debug_object_id; | ||
| 204 | print "in allocate_id, allocated $id from before pool\n" if $OOPS::OOPS1003::debug_object_id; | ||
| 205 | } | ||
| 206 | return $id; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub post_new_object | ||
| 210 | { | ||
| 211 | my $oops = shift; | ||
| 212 | return $_[0]; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub byebye | ||
| 216 | { | ||
| 217 | my $oops = shift; | ||
| 218 | $oops->{counterdbh}->disconnect() if $oops->{counterdbh}; | ||
| 219 | } | ||
| 220 | |||
| 221 | 1; | ||
| 222 |