--- sql2xls.cgi 2008/11/03 21:39:44 15 +++ sql2xls.cgi 2008/11/03 22:29:05 19 @@ -85,15 +85,21 @@ my $sql_dir = $ENV{SCRIPT_FILENAME} || '.'; $sql_dir =~ s,/[^/]+$,,; -my $config_path = $1 if "$sql_dir/config.pl" =~ m/^(.+)$/; # untaint -warn "# using $config_path\n"; -require $config_path if -e $config_path; +sub require_config { + my $config_path = $1 if "$sql_dir/config.pl" =~ m/^(.+)$/; # untaint + warn "# using $config_path\n"; + require $config_path if -e $config_path; +} + +require_config; my $reports_path = $ENV{PATH_INFO}; $reports_path =~ s/\.\.//g; # some protection against path exploits $reports_path ||= shift @ARGV; # for CLI invocation $sql_dir .= "/$reports_path" if -e "$sql_dir/$reports_path"; +require_config; + warn "# reading SQL queries from $sql_dir\n" if $debug; opendir(DIR, $sql_dir) || die "can't opendir $sql_dir: $!"; @@ -116,6 +122,7 @@ my $dbh = DBI->connect($dsn . $database,$user,$passwd, { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr; sub _c { + return shift unless $db_encoding; return decode( $db_encoding, shift ); } @@ -186,9 +193,12 @@ my $data = $row[$col]; if ( $types[$col] =~ m/^date/i ) { $data .= 'T' if $data =~ m/^\d\d\d\d-\d\d-\d\d$/; - $data =~ s/^(\d\d\d\d-\d\d-\d\d)\s(\d\d:\S+)$/$1T$2/; - warn "## $data\n"; + $data =~ s/^(\d\d\d\d-\d\d-\d\d)\s(\d\d:\d\d:\d\d)$/$1T$2/; + warn "## by type datetime $data\n"; $worksheet->write_date_time( $row, $col, $data, $date_format ); + } elsif ( $data =~ s/^(\d\d\d\d-\d\d-\d\d)[\sT](\d\d:\d\d:\d\d)$/$1T$2/ ) { + warn "## heuristic date time: $1T$2\n"; + $worksheet->write_date_time( $row, $col, "$1T$2", $date_format ); } else { $worksheet->write($row, $col, _c( $data ) ); }