-# Process a Perl script which updates the database.
-# If the script returns 1 then the update was successful.
-# Return code "2" means "needs more interaction; remove
-# users/nologin and exit".
-# All other return codes are fatal errors.
-sub process_perl_script {
- $main::lxdebug->enter_sub();
-
- my ($self, $form, $dbh, $filename, $version_or_control, $db_charset) = @_;
-
- my $fh = IO::File->new($filename, "r") or $form->error("$filename : $!\n");
-
- my $file_charset = Common::DEFAULT_CHARSET;
-
- if (ref($version_or_control) eq "HASH") {
- $file_charset = $version_or_control->{charset};
-
- } else {
- while (<$fh>) {
- last if !/^--/;
- next if !/^--\s*\@charset:\s*(.+)/;
- $file_charset = $1;
- last;
- }
- $fh->seek(0, SEEK_SET);
- }
-
- my $contents = join "", <$fh>;
- $fh->close();
-
- $db_charset ||= Common::DEFAULT_CHARSET;
-
- my $iconv = SL::Iconv::get_converter($file_charset, $db_charset);
-
- $dbh->begin_work();
-
- my %dbup_myconfig = ();
- map({ $dbup_myconfig{$_} = $form->{$_}; }
- qw(dbname dbuser dbpasswd dbhost dbport dbconnect));
-
- my $nls_file = $filename;
- $nls_file =~ s|.*/||;
- $nls_file =~ s|.pl$||;
- my $dbup_locale = Locale->new($main::language, $nls_file);
-
- my $result = eval($contents);
-
- if (1 != $result) {
- $dbh->rollback();
- $dbh->disconnect();
- }
-
- if (!defined($result)) {
- print $form->parse_html_template("dbupgrade/error",
- { "file" => $filename,
- "error" => $@ });
- exit(0);
- } elsif (1 != $result) {
- unlink("users/nologin") if (2 == $result);
- exit(0);
- }
-
- if (ref($version_or_control) eq "HASH") {
- $dbh->do("INSERT INTO schema_info (tag, login) VALUES (" .
- $dbh->quote($version_or_control->{"tag"}) . ", " .
- $dbh->quote($form->{"login"}) . ")");
- } elsif ($version_or_control) {
- $dbh->do("UPDATE defaults SET version = " .
- $dbh->quote($version_or_control));
- }
- $dbh->commit();
-
- $main::lxdebug->leave_sub();
-}
-
-sub process_query {
- $main::lxdebug->enter_sub();
-
- my ($self, $form, $dbh, $filename, $version_or_control, $db_charset) = @_;
-
- my $fh = IO::File->new($filename, "r") or $form->error("$filename : $!\n");
- my $query = "";
- my $sth;
- my @quote_chars;
-
- my $file_charset = Common::DEFAULT_CHARSET;
- while (<$fh>) {
- last if !/^--/;
- next if !/^--\s*\@charset:\s*(.+)/;
- $file_charset = $1;
- last;
- }
- $fh->seek(0, SEEK_SET);
-
- $db_charset ||= Common::DEFAULT_CHARSET;
-
- $dbh->begin_work();
-
- while (<$fh>) {
- $_ = SL::Iconv::convert($file_charset, $db_charset, $_);
-
- # Remove DOS and Unix style line endings.
- chomp;
-
- # remove comments
- s/--.*$//;
-
- for (my $i = 0; $i < length($_); $i++) {
- my $char = substr($_, $i, 1);
-
- # Are we inside a string?
- if (@quote_chars) {
- if ($char eq $quote_chars[-1]) {
- pop(@quote_chars);
- }
- $query .= $char;
-
- } else {
- if (($char eq "'") || ($char eq "\"")) {
- push(@quote_chars, $char);
-
- } elsif ($char eq ";") {
-
- # Query is complete. Send it.
-
- $sth = $dbh->prepare($query);
- if (!$sth->execute()) {
- my $errstr = $dbh->errstr;
- $sth->finish();
- $dbh->rollback();
- $form->dberror("The database update/creation did not succeed. " .
- "The file ${filename} containing the following " .
- "query failed:<br>${query}<br>" .
- "The error message was: ${errstr}<br>" .
- "All changes in that file have been reverted.");
- }
- $sth->finish();
-
- $char = "";
- $query = "";
- }
-
- $query .= $char;
- }
- }
-
- # Insert a space at the end of each line so that queries split
- # over multiple lines work properly.
- if ($query ne '') {
- $query .= @quote_chars ? "\n" : ' ';
- }
- }
-
- if (ref($version_or_control) eq "HASH") {
- $dbh->do("INSERT INTO schema_info (tag, login) VALUES (" .
- $dbh->quote($version_or_control->{"tag"}) . ", " .
- $dbh->quote($form->{"login"}) . ")");
- } elsif ($version_or_control) {
- $dbh->do("UPDATE defaults SET version = " .
- $dbh->quote($version_or_control));
- }
- $dbh->commit();
-
- $fh->close();
-
- $main::lxdebug->leave_sub();
-}
-