1 |
#!/usr/bin/perl |
#!/usr/bin/perl -T |
2 |
use warnings; |
use warnings; |
3 |
use strict; |
use strict; |
4 |
|
|
22 |
You can also run script from command line, and it will produce |
You can also run script from command line, and it will produce |
23 |
C<< sql_reports.xls >> file. |
C<< sql_reports.xls >> file. |
24 |
|
|
25 |
|
If run within directory, it will use files in it to produce file. |
26 |
|
|
27 |
|
When called as CGI, directory name can be appended to name of script |
28 |
|
to produce report for any sub-directory within directory where |
29 |
|
C<sql2xls.cgi> is installed. |
30 |
|
|
31 |
=head1 INSTALLATION |
=head1 INSTALLATION |
32 |
|
|
33 |
Only required file is this script C<< sql2xls.cgi >> |
Only required file is this script C<< sql2xls.cgi >> |
48 |
use Spreadsheet::WriteExcel; |
use Spreadsheet::WriteExcel; |
49 |
use DBI; |
use DBI; |
50 |
use CGI::Carp qw(fatalsToBrowser); |
use CGI::Carp qw(fatalsToBrowser); |
|
use CGI qw(path_translated); |
|
51 |
use Encode qw/decode/; |
use Encode qw/decode/; |
52 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
53 |
|
|
63 |
|
|
64 |
our $debug = 1; |
our $debug = 1; |
65 |
|
|
66 |
my $sql_dir = path_translated || '.'; |
my $sql_dir = $ENV{SCRIPT_FILENAME} || '.'; |
67 |
$sql_dir =~ s,/[^/]+$,,; |
$sql_dir =~ s,/[^/]+$,,; |
68 |
|
|
|
opendir(DIR, $sql_dir) || die "can't opendir $sql_dir: $!"; |
|
|
my @sql_files = sort grep { /\.sql$/i && -f "$sql_dir/$_" } readdir(DIR); |
|
|
closedir DIR; |
|
|
|
|
69 |
my $config_path = "$sql_dir/config.pl"; |
my $config_path = "$sql_dir/config.pl"; |
70 |
warn "# using $config_path\n"; |
warn "# using $config_path\n"; |
71 |
require $config_path if -e $config_path; |
require $config_path if -e $config_path; |
72 |
|
|
73 |
|
my $reports_path = $ENV{PATH_INFO}; |
74 |
|
$reports_path =~ s/\.\.//g; # some protection against path exploits |
75 |
|
$reports_path ||= shift @ARGV; # for CLI invocation |
76 |
|
$sql_dir .= "/$reports_path" if -e "$sql_dir/$reports_path"; |
77 |
|
|
78 |
|
warn "# reading SQL queries from $sql_dir\n" if $debug; |
79 |
|
|
80 |
|
opendir(DIR, $sql_dir) || die "can't opendir $sql_dir: $!"; |
81 |
|
my @sql_files = sort grep { /\.sql$/i && -f "$sql_dir/$_" } readdir(DIR); |
82 |
|
closedir DIR; |
83 |
|
|
84 |
my $workbook; |
my $workbook; |
85 |
if ($ENV{GATEWAY_INTERFACE} && $ENV{GATEWAY_INTERFACE} =~ m/CGI/i) { |
if ($ENV{GATEWAY_INTERFACE} && $ENV{GATEWAY_INTERFACE} =~ m/CGI/i) { |
86 |
# use as cgi script |
# use as cgi script |
113 |
|
|
114 |
print STDERR "working on $sql_file\n" if ($debug); |
print STDERR "working on $sql_file\n" if ($debug); |
115 |
|
|
116 |
open(SQL,$sql_file) || die "can't open sql file '$sql_file': $!"; |
open(SQL,"$sql_dir/$sql_file") || die "can't open sql file '$sql_dir/$sql_file': $!"; |
117 |
my $comment = ''; |
my $comment = ''; |
118 |
my $sql = ""; |
my $sql = ""; |
119 |
while(<SQL>) { |
while(<SQL>) { |