Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision a1d1605e

Von Moritz Bunkus vor fast 14 Jahren hinzugefügt

  • ID a1d1605e7abd276f97660dbcdbe54ee9a8929a1e
  • Vorgänger 7e0ad798
  • Nachfolger 8b39e389

Funktion "process_query" von User.pm nach DBUpgrade2.pm verschoben

Unterschiede anzeigen:

SL/DBUpgrade2.pm
1 1
package SL::DBUpgrade2;
2 2

  
3
use IO::File;
4

  
3 5
use SL::Common;
6
use SL::Iconv;
4 7

  
5 8
use strict;
6 9

  
......
10 13
  return bless($self, $package);
11 14
}
12 15

  
16
sub set_dbcharset {
17
  my $self           = shift;
18
  $self->{dbcharset} = shift;
19
  return $self;
20
}
21

  
13 22
sub parse_dbupdate_controls {
14 23
  $main::lxdebug->enter_sub();
15 24

  
......
101 110
  return \%all_controls;
102 111
}
103 112

  
113
sub process_query {
114
  $main::lxdebug->enter_sub();
115

  
116
  my ($self, $dbh, $filename, $version_or_control, $db_charset) = @_;
117

  
118
  my $form  = $self->{form};
119
  my $fh    = IO::File->new($filename, "r") or $form->error("$filename : $!\n");
120
  my $query = "";
121
  my $sth;
122
  my @quote_chars;
123

  
124
  my $file_charset = Common::DEFAULT_CHARSET;
125
  while (<$fh>) {
126
    last if !/^--/;
127
    next if !/^--\s*\@charset:\s*(.+)/;
128
    $file_charset = $1;
129
    last;
130
  }
131
  $fh->seek(0, SEEK_SET);
132

  
133
  $db_charset ||= Common::DEFAULT_CHARSET;
134

  
135
  $dbh->begin_work();
136

  
137
  while (<$fh>) {
138
    $_ = SL::Iconv::convert($file_charset, $db_charset, $_);
139

  
140
    # Remove DOS and Unix style line endings.
141
    chomp;
142

  
143
    # remove comments
144
    s/--.*$//;
145

  
146
    for (my $i = 0; $i < length($_); $i++) {
147
      my $char = substr($_, $i, 1);
148

  
149
      # Are we inside a string?
150
      if (@quote_chars) {
151
        if ($char eq $quote_chars[-1]) {
152
          pop(@quote_chars);
153
        }
154
        $query .= $char;
155

  
156
      } else {
157
        if (($char eq "'") || ($char eq "\"")) {
158
          push(@quote_chars, $char);
159

  
160
        } elsif ($char eq ";") {
161

  
162
          # Query is complete. Send it.
163

  
164
          $sth = $dbh->prepare($query);
165
          if (!$sth->execute()) {
166
            my $errstr = $dbh->errstr;
167
            $sth->finish();
168
            $dbh->rollback();
169
            $form->dberror("The database update/creation did not succeed. " .
170
                           "The file ${filename} containing the following " .
171
                           "query failed:<br>${query}<br>" .
172
                           "The error message was: ${errstr}<br>" .
173
                           "All changes in that file have been reverted.");
174
          }
175
          $sth->finish();
176

  
177
          $char  = "";
178
          $query = "";
179
        }
180

  
181
        $query .= $char;
182
      }
183
    }
184

  
185
    # Insert a space at the end of each line so that queries split
186
    # over multiple lines work properly.
187
    if ($query ne '') {
188
      $query .= @quote_chars ? "\n" : ' ';
189
    }
190
  }
191

  
192
  if (ref($version_or_control) eq "HASH") {
193
    $dbh->do("INSERT INTO schema_info (tag, login) VALUES (" .
194
             $dbh->quote($version_or_control->{"tag"}) . ", " .
195
             $dbh->quote($form->{"login"}) . ")");
196
  } elsif ($version_or_control) {
197
    $dbh->do("UPDATE defaults SET version = " .
198
             $dbh->quote($version_or_control));
199
  }
200
  $dbh->commit();
201

  
202
  $fh->close();
203

  
204
  $main::lxdebug->leave_sub();
205
}
206

  
104 207
sub _check_for_loops {
105 208
  my ($form, $file_name, $controls, $tag, @path) = @_;
106 209

  

Auch abrufbar als: Unified diff