| 1 | |||
| 2 | BEGIN { | ||
| 3 | Filter::Util::Call::filter_add(\&OOPS::SelfFilter::filter) | ||
| 4 | unless $OOPS::SelfFilter::defeat; | ||
| 5 | } | ||
| 6 | |||
| 7 | package OOPS::Setup; # dummy | ||
| 8 | |||
| 9 | package OOPS; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | # Regular expression to match error returned when the database | ||
| 15 | # has not been initialized: | ||
| 16 | # | ||
| 17 | # mysql Table 'PREFIXobject' doesn't exist | ||
| 18 | # sqlite no such table: PREFIXobject(1) at dbdimp.c | ||
| 19 | # postgresql ERROR: relation "PREFIXobject" does not exist | ||
| 20 | # | ||
| 21 | our $gcgenstart = 10_000; | ||
| 22 | our $last_reserved_oid = 100; | ||
| 23 | |||
| 24 | sub initial_setup_real | ||
| 25 | { | ||
| 26 | my ($pkg, %args) = @_; | ||
| 27 | |||
| 28 | my $dbo; | ||
| 29 | if (ref $pkg) { | ||
| 30 | $dbo = $pkg->dbo; | ||
| 31 | } else { | ||
| 32 | $dbo = OOPS::DBO->dboconnect(%args); | ||
| 33 | } | ||
| 34 | |||
| 35 | # create tables, initial objects, etc. | ||
| 36 | my ($oldout, $olderr, $obuf, $ebuf); | ||
| 37 | $dbo->db_domany( | ||
| 38 | $dbo->tabledefs() | ||
| 39 | . db_initial_values() | ||
| 40 | . $dbo->db_initial_values(), | ||
| 41 | args => \%args, | ||
| 42 | silent => $ENV{HARNESS_ACTIVE}); | ||
| 43 | my $dbms = $dbo->{dbms}; | ||
| 44 | $dbo->commit(); | ||
| 45 | $dbo->disconnect unless ref $pkg; | ||
| 46 | return $dbms; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub db_initial_values | ||
| 50 | { | ||
| 51 | return <<END; | ||
| 52 | INSERT INTO TP_object values (1, 1, 'HASH', 'H', 'V', '0', '0', $OOPS::SCHEMA_VERSION, 1, 1, $gcgenstart); | ||
| 53 | INSERT INTO TP_attribute values (2, 'user objects', '1', 'R'); | ||
| 54 | |||
| 55 | INSERT INTO TP_object values (2, 2, 'HASH', 'H', 'V', '0', '0', 0, 1, 1, $gcgenstart); | ||
| 56 | INSERT INTO TP_attribute values (2, 'internal objects', '2', 'R'); | ||
| 57 | INSERT INTO TP_attribute values (2, 'VERSION', '$OOPS::VERSION', '0'); | ||
| 58 | INSERT INTO TP_attribute values (2, 'SCHEMA_VERSION', '$OOPS::SCHEMA_VERSION', '0'); | ||
| 59 | INSERT INTO TP_attribute values (2, 'GC GENERATION', '$gcgenstart', '0'); | ||
| 60 | |||
| 61 | INSERT INTO TP_object values (3, 3, 'HASH', 'H', 'V', '0', '0', 0, 1, 1, $gcgenstart); | ||
| 62 | INSERT INTO TP_attribute values (2, 'counters', '3', 'R'); | ||
| 63 | |||
| 64 | INSERT INTO TP_object values ($OOPS::gc_overflow_id, $OOPS::gc_overflow_id, 'HASH', 'H', 'V', '0', '0', 0, 1, 1, $gcgenstart); | ||
| 65 | INSERT INTO TP_attribute values (2, 'gc extra todo', '$OOPS::gc_overflow_id', 'R'); | ||
| 66 | END | ||
| 67 | } | ||
| 68 | |||
| 69 | # | ||
| 70 | # On a failure to load the named_objects hash, auto-initialize the | ||
| 71 | # database. | ||
| 72 | # | ||
| 73 | sub load_failure | ||
| 74 | { | ||
| 75 | my ($oops, $err) = @_; | ||
| 76 | |||
| 77 | my $nodatarx = $oops->{dbo}->nodata_rx; | ||
| 78 | print "load_failure($err) -- compare to $nodatarx\n" if $OOPS::debug_setup; | ||
| 79 | return 0 unless $err =~ /$nodatarx/; | ||
| 80 | |||
| 81 | die "DBMS not initialized - use auto_initialize or initial_setup()\n" | ||
| 82 | unless $oops->{args}{auto_initialize} || $ENV{OOPS_INIT}; | ||
| 83 | |||
| 84 | print STDERR "Initializing database...\n"; | ||
| 85 | |||
| 86 | $oops->{dbo}->rollback || confess $oops->{dbo}->errstr; | ||
| 87 | |||
| 88 | print "rollback complete\n" if $OOPS::debug_setup; | ||
| 89 | |||
| 90 | $oops->initial_setup_real(%{$oops->{args}}); | ||
| 91 | |||
| 92 | print "Initial setup done\n" if $OOPS::debug_setup; | ||
| 93 | |||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | package OOPS::DBO; | ||
| 98 | |||
| 99 | use strict; | ||
| 100 | use warnings; | ||
| 101 | |||
| 102 | # | ||
| 103 | # method invocation as either a $dbo or $oops method | ||
| 104 | # or pass dbiconnect args as $opts{args} | ||
| 105 | # | ||
| 106 | # also: | ||
| 107 | # $opts{silent} - don't print on errors | ||
| 108 | # $opts{nonfatal} - don't die on errors | ||
| 109 | # $opts{autocommit} - autocommit each command | ||
| 110 | # | ||
| 111 | sub db_domany | ||
| 112 | { | ||
| 113 | my ($something, $command, %opts) = @_; | ||
| 114 | my $dbo; | ||
| 115 | if (ref $something) { | ||
| 116 | $dbo = $something; | ||
| 117 | } elsif ($opts{args}) { | ||
| 118 | $dbo = OOPS::DBO->dboconnect(%{$opts{args}}); | ||
| 119 | } else { | ||
| 120 | confess; | ||
| 121 | } | ||
| 122 | my @ret; | ||
| 123 | die unless $command; | ||
| 124 | $command .= ";\n" unless $command =~ /;/; # if there's just one query... | ||
| 125 | |||
| 126 | while ($command =~ /\G\s*(\S.*?);\n/sgc) { | ||
| 127 | my $query = $1; | ||
| 128 | print STDERR "do $query\n" if $OOPS::debug_initialize; | ||
| 129 | my $nonfatal = $query =~ s/^-// || $opts{nonfatal}; | ||
| 130 | my $q = $dbo->adhoc_query($query); | ||
| 131 | if ($nonfatal) { | ||
| 132 | my $r; | ||
| 133 | if (eval { $r = $q->execute() }) { | ||
| 134 | push(@ret, $r); | ||
| 135 | } else { | ||
| 136 | warn "do '$query':".$dbo->errstr | ||
| 137 | unless $opts{silent}; | ||
| 138 | $dbo->rollback || confess $dbo->errstr; | ||
| 139 | } | ||
| 140 | } else { | ||
| 141 | my $r = $q->execute() | ||
| 142 | or confess("<<$query>>" . $dbo->errstr); | ||
| 143 | push(@ret, $r); | ||
| 144 | } | ||
| 145 | $dbo->commit if $opts{autocommit}; | ||
| 146 | } | ||
| 147 | $dbo->commit if $opts{commit} || | ||
| 148 | ! (ref($something) || $opts{autocommit}); | ||
| 149 | die "x='$command'" unless $command =~ /\G\s*\Z/sg; | ||
| 150 | unless (ref $something) { | ||
| 151 | $dbo->disconnect(); | ||
| 152 | } | ||
| 153 | return(@ret); | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | 1; |