From bf8f6db45ca748d1374001c8bd2b5537869284af Mon Sep 17 00:00:00 2001 From: Dave Morriss Date: Wed, 7 May 2025 17:21:29 +0100 Subject: [PATCH] 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";