This commit is contained in:
djez smith 2017-03-28 20:22:07 +02:00
parent 1a902df1b8
commit 7afc4a58a8
2 changed files with 25 additions and 32 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/env perl #!/usr/bin/env perl
#=============================================================================== #===============================================================================
# #
# FILE: SQL2XLS.pl # FILE: SQL2XLS.pl
@ -10,10 +10,10 @@
# BUGS: --- # BUGS: ---
# NOTES: --- # NOTES: ---
# AUTHOR: grouch' # AUTHOR: grouch'
# ORGANIZATION: # ORGANIZATION:
# VERSION: 1.0 # VERSION: 1.0
# CREATED: 27/03/17 # CREATED: 27/03/17
# CHANGE_LOG: # CHANGE_LOG:
# #
#=============================================================================== #===============================================================================
@ -37,15 +37,15 @@ my $db = {
}; };
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Requête SQL # Requête SQL
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
my $prog = 'ATO045'; my $prog = 'ATO045';
my $datedebut = '20000101000000'; my $datedebut = '20000101000000';
#my $datedebut = '20170201000000'; #my $datedebut = '20170201000000';
my $datefin = '20191231235959'; my $datefin = '20191231235959';
# une première requête # une première requête
my $sql1 = qq{SELECT my $sql1 = qq{SELECT
MPOS MPOS
,NOM ,NOM
,MINI ,MINI
@ -55,35 +55,28 @@ my $sql1 = qq{SELECT
,Ppk ,Ppk
,NbPieces ,NbPieces
,nb_total_occurences ,nb_total_occurences
,CRITICAL ,CRITICAL
,MAJOR ,MAJOR
,ICD ,ICD
,COMMENTAIRE_DCM ,COMMENTAIRE_DCM
FROM calcul_capa($datedebut, $datefin, '$prog') FROM calcul_capa($datedebut, $datefin, '$prog')
WHERE MINI notNULL AND MAXI notNULL WHERE MINI notNULL AND MAXI notNULL
--AND Pp > 1.33 AND Ppk > 1 --AND Pp > 1.33 AND Ppk > 1
}; };
#une deuxième requête #une deuxième requête
my $sql2 = qq{SELECT * FROM DATA WHERE PROG = '$prog'}; my $sql2 = qq{SELECT * FROM DATA WHERE PROG = '$prog'};
#la sub prend une ref de hash en argument #la sub prend une ref de hash en argument
# nom_de_l'onglet => requete_sql # nom_de_l'onglet => requete_sql
my $requete = {"capa" => $sql1, "data" => $sql2}; my $requete = {"capa" => $sql1, "data" => $sql2};
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# APPEL SUB # APPEL SUB
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
SQL2XLS::SQL2XLS($db, $requete, 'toto.xlsx'); SQL2XLS::SQL2XLS($db, $requete, 'toto.xlsx');

View File

@ -13,7 +13,7 @@ package SQL2XLS;
# ORGANIZATION: non # ORGANIZATION: non
# VERSION: 1.0 # VERSION: 1.0
# CREATED: 28/03/17 # CREATED: 28/03/17
# CHANGE_LOG: # CHANGE_LOG:
# #
#=============================================================================== #===============================================================================
use Carp; use Carp;
@ -32,13 +32,13 @@ use subs qw(SQL2XLS);
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# SUB SQL2XLS # SUB SQL2XLS
# arguments : \%db \%requete, $filename # arguments : \%db \%requete, $filename
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
sub SQL2XLS{ sub SQL2XLS{
my $ref_db = shift; my $ref_db = shift;
my $ref_requete = shift; my $ref_requete = shift;
my $filename = shift; my $filename = shift;
# connexion base # connexion base
say "dbi:Pg:dbname=${$ref_db}{dbname};host=${$ref_db}{host};port=${$ref_db}{port}"; say "dbi:Pg:dbname=${$ref_db}{dbname};host=${$ref_db}{host};port=${$ref_db}{port}";
my $dbh = DBI->connect("dbi:Pg:dbname=$ref_db->{dbname};host=$ref_db->{host};port=$ref_db->{port}", my $dbh = DBI->connect("dbi:Pg:dbname=$ref_db->{dbname};host=$ref_db->{host};port=$ref_db->{port}",
@ -46,24 +46,24 @@ sub SQL2XLS{
$ref_db->{password}, $ref_db->{password},
{AutoCommit => 1, RaiseError => 1, PrintError => 0} {AutoCommit => 1, RaiseError => 1, PrintError => 0}
); );
#création du fichier Excel #création du fichier Excel
my $wb = Excel::Writer::XLSX->new($filename); my $wb = Excel::Writer::XLSX->new($filename);
$wb->set_properties( $wb->set_properties(
title => 'SQL2XLS', title => 'SQL2XLS',
author => 'C.GRUSZKA', author => 'C.GRUSZKA',
); );
#on boucle pour créer tous les onglets du workbook #on boucle pour créer tous les onglets du workbook
foreach my $tab (keys %{$ref_requete}){ foreach my $tab (keys %{$ref_requete}){
#création de l'onglet #création de l'onglet
say "\n\nOnglet $tab"; say "\n\nOnglet $tab";
my $ws = $wb->add_worksheet($tab); my $ws = $wb->add_worksheet($tab);
$ws->set_zoom(85); $ws->set_zoom(85);
#récupèration de la requête SQL associée à l'onglet #récupèration de la requête SQL associée à l'onglet
my $sql = $ref_requete->{$tab}; my $sql = $ref_requete->{$tab};
#requêtage ! #requêtage !
my $sth = $dbh->prepare($sql); my $sth = $dbh->prepare($sql);
$sth->execute; $sth->execute;
@ -73,15 +73,15 @@ sub SQL2XLS{
say "Nombre de colonnes : $nb_col"; say "Nombre de colonnes : $nb_col";
my $nb_items = scalar @$list; my $nb_items = scalar @$list;
say "Nombre de lignes : $nb_items"; say "Nombre de lignes : $nb_items";
#construction de l'array de hash des entêtes de colonnes (à tes souhaits) #construction de l'array de hash des entêtes de colonnes (à tes souhaits)
my @arraycol; my @arraycol;
foreach my $col (@{$colonnes}){ foreach my $col (@{$colonnes}){
push @arraycol, { header => "$col" }; push @arraycol, { header => "$col" };
} }
#table EXCEL #table EXCEL
$ws->add_table(0,0,$nb_items,$nb_col-1, { data => $list, header_row => 1, autofilter => 1,style => 'Table Style Medium 5', columns => \@arraycol }); $ws->add_table(0,0,$nb_items,$nb_col-1, { data => $list, header_row => 1, autofilter => 1,style => 'Table Style Medium 5', columns => \@arraycol });
} }
} }
1; 1;