From bf8f6db45ca748d1374001c8bd2b5537869284af Mon Sep 17 00:00:00 2001 From: Dave Morriss Date: Wed, 7 May 2025 17:21:29 +0100 Subject: [PATCH 1/2] Enhanced placeholder/argument check algorithm --- Database/query2csv | 6 ++++-- Database/query2json | 17 +++++++---------- Database/query2tt2 | 9 +++++++-- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Database/query2csv b/Database/query2csv index 517d909..a77f893 100755 --- a/Database/query2csv +++ b/Database/query2csv @@ -21,7 +21,7 @@ # AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com # VERSION: 0.0.4 # CREATED: 2015-07-11 15:53:01 -# REVISION: 2025-05-06 21:12:08 +# REVISION: 2025-05-07 17:20:00 # #=============================================================================== @@ -136,8 +136,10 @@ else { _debug( $DEBUG >= 3, '$query: ' . Dumper(\$query) ); # -# Count placeholders in the query and the arguments provided +# Count placeholders in the query and the arguments provided. First remove all +# comments which may contain '?' characters, then count any that are left. # +$query = join("\n", grep {!/^--/} split( "\n", $query ) ); $pcount = grep {/\?/} split( '', $query ); $acount = scalar(@dbargs); diff --git a/Database/query2json b/Database/query2json index ac2ddc6..b1a678e 100755 --- a/Database/query2json +++ b/Database/query2json @@ -20,7 +20,7 @@ # AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com # VERSION: 0.0.3 # CREATED: 2021-06-18 13:24:49 -# REVISION: 2025-05-07 09:50:34 +# REVISION: 2025-05-07 17:20:07 # #=============================================================================== @@ -132,8 +132,10 @@ else { _debug( $DEBUG >= 3, '$query: ' . Dumper(\$query) ); # -# Count placeholders in the query and the arguments provided +# Count placeholders in the query and the arguments provided. First remove all +# comments which may contain '?' characters, then count any that are left. # +$query = join("\n", grep {!/^--/} split( "\n", $query ) ); $pcount = grep {/\?/} split( '', $query ); $acount = scalar(@dbargs); @@ -212,9 +214,9 @@ catch ($e) { } # -# Prepare for JSON, forcing object key sorting (expensive) +# Prepare for JSON, forcing object key sorting (expensive), and prettification # -$json = JSON->new->utf8->canonical; +$json = JSON->new->utf8->canonical->pretty; # # Grab everything as an arrayref of hashrefs @@ -391,7 +393,7 @@ This documentation refers to query2json version 0.0.3 =head1 USAGE query2json [-help] [-documentation|-man] [-debug=N] [-config=FILE] - [-query=FILE] [-output=FILE] [-[no]header] [QUERY] + [-query=FILE] [-output=FILE] [QUERY] =head1 OPTIONS @@ -461,11 +463,6 @@ these placeholders can be passed to the script through the B<-dbarg=ARG> option. The option can be repeated as many times as required and the order of B values is preserved. -=item B<-[no-]header> - -This option allows a header to be added to the CSV output with the names of -the database columns in CSV format. By default this is not produced. - =back =head1 DESCRIPTION diff --git a/Database/query2tt2 b/Database/query2tt2 index 1411ca0..6de93e9 100755 --- a/Database/query2tt2 +++ b/Database/query2tt2 @@ -28,7 +28,7 @@ # AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com # VERSION: 0.0.8 # CREATED: 2021-06-18 13:24:49 -# REVISION: 2025-05-06 16:39:25 +# REVISION: 2025-05-07 17:19:15 # #=============================================================================== @@ -164,11 +164,16 @@ else { _debug( $DEBUG >= 3, '$query: ' . Dumper(\$query) ); # -# Count placeholders in the query and the arguments provided +# Count placeholders in the query and the arguments provided. First remove all +# comments which may contain '?' characters, then count any that are left. # +$query = join("\n", grep {!/^--/} split( "\n", $query ) ); $pcount = grep {/\?/} split( '', $query ); $acount = scalar(@dbargs); +# +# Check the placeholder and argument counts are the same +# if ( $pcount ne $acount) { say STDERR "Query placeholder vs argument mismatch"; say STDERR "Placeholders = $pcount, Arguments = $acount"; From fdd0823f933c27c82292e5944c8d50e9cd8f4401 Mon Sep 17 00:00:00 2001 From: Dave Morriss Date: Fri, 9 May 2025 14:15:41 +0100 Subject: [PATCH 2/2] Finishing off 'query2*' scripts Database/hosts_list.tpl, Database/hosts_showcount.sqlite.sql: examples of using 'query2tt2' Database/query2csv, Database/query2json, Database/query2tt2: minor bug fix relating to '-dbarg=ARG' option --- Database/hosts_list.tpl | 39 +++++++++++++++++++++++++++++ Database/hosts_showcount.sqlite.sql | 15 +++++++++++ Database/query2csv | 8 +++--- Database/query2json | 6 ++--- Database/query2tt2 | 9 +++---- 5 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 Database/hosts_list.tpl create mode 100644 Database/hosts_showcount.sqlite.sql diff --git a/Database/hosts_list.tpl b/Database/hosts_list.tpl new file mode 100644 index 0000000..dfd4efd --- /dev/null +++ b/Database/hosts_list.tpl @@ -0,0 +1,39 @@ +[%# ========================================================================== +This is the TT2 file for making a list of hosts contributing to HPR in the + current year which is run in conjunction with 'query2tt2'. It's invoked + thus: + year="2022" # or whatever + ./query2tt2 -query=$HOME/HPR/Community_News/hosts_showcount.sql \ + -conf=$HOME/HPR/.hpr_livedb.cfg \ + -dbargs "${year}-01-01" -dbargs "${year}-12-31" -def year=${year} \ + -template=$HOME/HPR/Community_News/hosts_list.tpl \ + > $HOME/HPR/Community_News/hosts_showcount_${year}.html + + [We can't use the planned pure TT2 version since Template::Plugin::DBI + can't run over the SSH tunnel.] + +2023-10-30 The correspondent URL has changed with the static site, and needs +the hostid to be zero-padded. +========================================================================== -%] +[%- USE date -%] +[%- DEFAULT + year = date.format(date.now,'%Y','UTC') +-%] +

Thanks to all [% result.size %] HPR contributors in [% year %]!

+ +[% limit = 8 -%] +[% count = 0 -%] +

  • +[% FOREACH h = result -%] +[% h.hostname %] +[%- IF loop.count mod limit == 0 || loop.count == result.size -%].[% ELSE %],[% END %] +[% count = count + 1 -%] +[% IF count == limit -%] +[% count = 0 -%] +
  • +[% END -%] +[% END -%] +

+[%# + # vim: syntax=tt2:ts=8:sw=4:ai:et:tw=78:fo=tcrqn21:fdm=marker +-%] diff --git a/Database/hosts_showcount.sqlite.sql b/Database/hosts_showcount.sqlite.sql new file mode 100644 index 0000000..5d6d186 --- /dev/null +++ b/Database/hosts_showcount.sqlite.sql @@ -0,0 +1,15 @@ +-- +-- Query for use with 'query2tt2' to generate a list of hosts who contributed +-- shows in a particular year. Designed to be used with the 'hosts_list.tpl' +-- template. +-- The two '?' placeholders in the query are to be filled with 'YYYY-01-01' +-- for the start of the year and 'YYYY-12-31'. The values can be passed using +-- the '-dbargs' option to 'query2tt2'. +-- +SELECT DISTINCT + printf('%04d',h.hostid) AS hostid, h.host AS hostname +FROM eps e +JOIN hosts h ON e.hostid = h.hostid +WHERE e.date BETWEEN ? AND ? +AND title != 'Reserved' +ORDER BY h.host diff --git a/Database/query2csv b/Database/query2csv index a77f893..0d3b19f 100755 --- a/Database/query2csv +++ b/Database/query2csv @@ -21,7 +21,7 @@ # AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com # VERSION: 0.0.4 # CREATED: 2015-07-11 15:53:01 -# REVISION: 2025-05-07 17:20:00 +# REVISION: 2025-05-09 14:13:17 # #=============================================================================== @@ -359,8 +359,8 @@ sub _dbargs { my @args; - if ( defined( $opts->{dbargs} ) ) { - @args = @{ $opts->{dbargs} }; + if ( defined( $opts->{dbarg} ) ) { + @args = @{ $opts->{dbarg} }; } return (@args); @@ -381,7 +381,7 @@ sub Options { my @options = ( "help", "documentation|man", "debug=i", "config=s", - "output=s", "query=s", "dbargs=s@", "header!", + "output=s", "query=s", "dbarg=s@", "header!", ); if ( !GetOptions( $optref, @options ) ) { diff --git a/Database/query2json b/Database/query2json index b1a678e..3e6af1b 100755 --- a/Database/query2json +++ b/Database/query2json @@ -20,7 +20,7 @@ # AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com # VERSION: 0.0.3 # CREATED: 2021-06-18 13:24:49 -# REVISION: 2025-05-07 17:20:07 +# REVISION: 2025-05-09 14:13:27 # #=============================================================================== @@ -343,8 +343,8 @@ sub _dbargs { my @args; - if ( defined( $opts->{dbargs} ) ) { - @args = @{ $opts->{dbargs} }; + if ( defined( $opts->{dbarg} ) ) { + @args = @{ $opts->{dbarg} }; } return (@args); diff --git a/Database/query2tt2 b/Database/query2tt2 index 6de93e9..02b015c 100755 --- a/Database/query2tt2 +++ b/Database/query2tt2 @@ -28,7 +28,7 @@ # AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com # VERSION: 0.0.8 # CREATED: 2021-06-18 13:24:49 -# REVISION: 2025-05-07 17:19:15 +# REVISION: 2025-05-09 14:13:04 # #=============================================================================== @@ -119,7 +119,6 @@ pod2usage( -noperldoc => 0, ) if ( $options{'documentation'} ); - # # Collect options # @@ -420,8 +419,8 @@ sub _dbargs { my @args; - if ( defined( $opts->{dbargs} ) ) { - @args = @{ $opts->{dbargs} }; + if ( defined( $opts->{dbarg} ) ) { + @args = @{ $opts->{dbarg} }; } return (@args); @@ -469,7 +468,7 @@ sub Options { "help", "documentation|man", "debug=i", "config=s", "output=s", "query=s", - "template=s", "dbargs=s@", + "template=s", "dbarg=s@", "define=s%", );