1 |
#!/usr/bin/perl -w |
#!/usr/bin/perl |
2 |
# |
use warnings; |
|
# convert sql queries on file system to excel file (one query per |
|
|
# sheet) |
|
|
# |
|
|
# Dobrica Pavlinsic <dpavlin@rot13.org>, 2002-04-10 |
|
|
|
|
3 |
use strict; |
use strict; |
4 |
|
|
5 |
|
=head1 NAME |
6 |
|
|
7 |
|
sql2xls.pl - convert sql queries on file system to Excel file |
8 |
|
|
9 |
|
=head1 USAGE |
10 |
|
|
11 |
|
Each file in current directory which ends in C<< *.sql >> will |
12 |
|
be converted to Excel sheet. If you want to have specific order, you can |
13 |
|
prefix filenames with numbers which will be striped when creating sheet |
14 |
|
names. |
15 |
|
|
16 |
|
Comments in sql files (lines beginning with --) will be placed |
17 |
|
in first line in bold. |
18 |
|
|
19 |
|
To specify database on which SQL query is executed |
20 |
|
C<< \c database >> syntax is supported. |
21 |
|
|
22 |
|
You can also run script from command line, and it will produce |
23 |
|
C<< sql_reports.xls >> file. |
24 |
|
|
25 |
|
=head1 AUTHOR |
26 |
|
|
27 |
|
Dobrica Pavlinusic, dpavlin@rot13.org |
28 |
|
|
29 |
|
=cut |
30 |
|
|
31 |
use Spreadsheet::WriteExcel; |
use Spreadsheet::WriteExcel; |
32 |
use DBI; |
use DBI; |
33 |
use CGI::Carp qw(fatalsToBrowser); |
use CGI::Carp qw(fatalsToBrowser); |
35 |
use Encode qw/decode/; |
use Encode qw/decode/; |
36 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
37 |
|
|
38 |
# Connect to DB |
# edit following to set defaults |
39 |
my $connect = "DBI:Pg:dbname=new"; |
my $dsn = 'DBI:Pg:dbname='; |
40 |
my $user = "web"; |
my $database = 'template1'; |
41 |
my $passwd = ""; |
my $user = 'dpavlin'; |
42 |
|
my $passwd = ''; |
43 |
|
my $path = 'sql_reports.xls'; |
44 |
|
|
45 |
my $db_encoding = 'iso-8859-2'; |
my $db_encoding = 'iso-8859-2'; |
46 |
my $xls_date_format = 'dd.mm.yyyy'; |
my $xls_date_format = 'dd.mm.yyyy'; |
47 |
|
|
48 |
my $debug = 1; |
my $debug = 1; |
55 |
closedir DIR; |
closedir DIR; |
56 |
|
|
57 |
my $workbook; |
my $workbook; |
58 |
if ($0 =~ m/\.cgi$/i) { |
if ($ENV{GATEWAY_INTERFACE} && $ENV{GATEWAY_INTERFACE} =~ m/CGI/i) { |
59 |
# use as cgi script |
# use as cgi script |
60 |
print "Content-type: application/vnd.ms-excel\n\n"; |
print "Content-type: application/vnd.ms-excel\n\n"; |
61 |
$workbook = Spreadsheet::WriteExcel->new("-"); |
$workbook = Spreadsheet::WriteExcel->new("-"); |
62 |
} else { |
} else { |
63 |
# Create a new Excel workbook |
# Create a new Excel workbook |
64 |
$workbook = Spreadsheet::WriteExcel->new("sql_result.xls"); |
$workbook = Spreadsheet::WriteExcel->new( $path ); |
65 |
|
warn "Creating XLS file $path\n"; |
66 |
} |
} |
67 |
|
|
68 |
my $date_format = $workbook->add_format(num_format => $xls_date_format); |
my $date_format = $workbook->add_format(num_format => $xls_date_format); |
69 |
|
|
70 |
my $dbh = DBI->connect($connect,$user,$passwd) || die $DBI::errstr; |
my $dbh = DBI->connect($dsn . $database,$user,$passwd, { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr; |
71 |
|
|
72 |
sub _c { |
sub _c { |
73 |
return decode( $db_encoding, shift ); |
return decode( $db_encoding, shift ); |
92 |
chomp; |
chomp; |
93 |
if (/^\\c\s+(\S+)/) { |
if (/^\\c\s+(\S+)/) { |
94 |
warn "## connect to $1\n" if $debug; |
warn "## connect to $1\n" if $debug; |
95 |
$dbh = DBI->connect('DBI:Pg:dbname=' . $1,$user,$passwd) || die $DBI::errstr; |
$dbh = DBI->connect($dsn . $1,$user,$passwd, { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr; |
96 |
} elsif (/^--(.+)/) { |
} elsif (/^--(.+)/) { |
97 |
$comment.=$1; |
$comment.=$1; |
98 |
} else { |
} else { |
117 |
$row+=2; |
$row+=2; |
118 |
} |
} |
119 |
|
|
120 |
my $sth = $dbh->prepare($sql) || die $dbh->errstr(); |
my $sth = $dbh->prepare($sql); |
121 |
$sth->execute() || die $sth->errstr(); |
$sth->execute(); |
122 |
|
|
123 |
my $fmt_header = $workbook->addformat(); # Add a format |
my $fmt_header = $workbook->addformat(); # Add a format |
124 |
$fmt_header->set_italic(); |
$fmt_header->set_italic(); |
153 |
|
|
154 |
__END__ |
__END__ |
155 |
|
|
|
=head1 NAME |
|
|
|
|
|
sql2xls.pl - convert sql queries on file system to excel file |
|
|
|
|
|
=head1 USAGE |
|
|
|
|
|
Edit top of script and edit @sql_files array which describes |
|
|
files which are going to be loaded and executed producing |
|
|
Excel sheets (one sheet per file) |
|
|
|
|
|
Comments in sql files (lines beginning with --) will be placed |
|
|
in outout sheet on top in bold. |
|
|
|
|
|
Sheet will have name same as sql file. |
|
|
|
|
|
Run script and examine b<sql_result.xls> file. |
|
|
|
|
|
=head1 AUTHOR |
|
|
|
|
|
Dobrica Pavlinusic, dpavlin@rot13.org |
|
|
|
|