1 # Copyright 2002-2009 Interchange Development Group and others
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version. See the LICENSE file for details.
8 UserTag backup-database Order tables
9 UserTag backup-database AddAttr
10 UserTag backup-database Version 1.11
11 UserTag backup-database Routine <<EOR
13 my ($tables, $opt) = @_;
14 my (@tables) = grep /\S/, split /['\s\0]+/, $tables;
15 my $backup_dir = $opt->{dir}
16 || $::Variable->{BACKUP_DIRECTORY}
17 || "$Vend::Cfg->{VendRoot}/backup";
18 my $gnum = $opt->{gnumeric};
19 my $agg = "$backup_dir/DBDOWNLOAD.all";
21 my $Max_xls_string = 255;
24 require Compress::Zlib;
25 } if $opt->{compress};
31 require Spreadsheet::WriteExcel;
32 import Spreadsheet::WriteExcel;
33 $xls = Spreadsheet::WriteExcel->new("$backup_dir/DBDOWNLOAD.xls");
36 if ($opt->{max_xls_string}) {
37 $Max_xls_string = int($opt->{max_xls_string}) || 255;
38 $xls->{_xls_strmax} = $Max_xls_string;
52 or die "Cannot write aggregate file $agg; $!\n";
55 for my $table (@tables) {
57 my $db = Vend::Data::database_exists_ref($table);
58 my $fn = $db->config('file');
60 my $file = "$backup_dir/$fn";
70 where => $opt->{where},
78 "Error exporting %s to %s: %s",
86 if($opt->{compress}) {
90 $gz = Compress::Zlib::gzopen($new, "wb")
91 or die errmsg("error compressing %s to %s: %s", $new, $agg, $!);
93 or die errmsg("error opening %s: %s", $file, $!);
97 errmsg("gzwrite error on %s: %s", $new, $gz->gzerror());
109 print AGG "\f" if $done;
110 print AGG "$table\n";
114 errmsg("Can't read written file %s: %s", $file, $!);
122 s/\t( *\d[^\t]*[-A-Za-z ])/\t'$1/g
129 my $sheet = $xls->addworksheet($table);
130 $sheet->{_xls_strmax} = $Max_xls_string
131 if defined $opt->{max_xls_string};
132 $sheet->activate($table) if $table eq $Vend::Cfg->{ProductFiles}[0];
136 errmsg("Can't read written file %s: %s", $file, $!);
139 my $fstring = <RECENT>;
141 my @fields = split /\t/, $fstring;
142 my $maxcol = scalar @fields - 1;
144 for($j = 0; $j <= $maxcol; $j++) {
145 $sheet->write_string(0, $j, $fields[$j])
146 if length $fields[$j];
153 @fields = split /\t/, $_;
154 for($j = 0; $j <= $maxcol; $j++) {
157 if ( length($fields[$j]) > $Max_xls_string) {
158 $overflow[$j] = $fields[$j];
160 while ( length($overflow[$j]) > $Max_xls_string) {
161 for( ' ', "\n", " " ) {
162 $ptr = rindex $overflow[$j], $_, $Max_xls_string;
163 #::logDebug("char='$_' ptr=$ptr length=" . length($overflow[$j]) ) if $l < 10;
166 #::logDebug("char='$_' ptr=$ptr\nstring=$overflow[$j]") if $l++ < 10;
168 $ptr = 254 if $ptr < 0;
171 my $string = substr $overflow[$j], 0, $ptr;
172 $overflow[$j] = substr $overflow[$j], $ptr;
173 push @{$extra[$j]}, $string;
175 push @{$extra[$j]}, $overflow[$j];
176 $fields[$j] = shift @{$extra[$j]};
178 $sheet->write_string($i, $j, $fields[$j]);
184 my $current = scalar @$_;
185 $max = $current if $max < $current;
187 for (my $k = 0; $k < $max; $k++) {
189 for( $j = 0; $j < scalar @extra; $j++) {
191 $sheet->write_string($i, $j, $extra[$j][$k]);
200 unlink($file) if $unlink;
205 close AGG if $opt->{compress};
207 if($opt->{compress} and $gnum and $gnum =~ /^compress/i) {
209 my $new = "$file.gz";
211 my $gz = Compress::Zlib::gzopen($new, "wb")
212 or die errmsg("error compressing %s to %s: %s", $new, $agg, $!);
214 or die errmsg("error opening %s: %s", $file, $!);
218 errmsg("gzwrite error on %s: %s", $new, $gz->gzerror());
231 $::Scratch->{ui_error} = '<ul><li>';
232 $::Scratch->{ui_error} .= join "</li>\n<li>", @errors;
233 $::Scratch->{ui_error} .= '</li></ul>';
235 return $opt->{hide} ? "" : $done;