From d3845bea5d9f8dd0269cbc45e840e334fc1f9558 Mon Sep 17 00:00:00 2001
From: Matthew Newton <matthew-git@newtoncomputing.co.uk>
Date: Tue, 24 Jul 2018 22:46:59 +0100
Subject: [PATCH] radsqlrelay: actually do something in debug mode

---
 scripts/sql/radsqlrelay | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/scripts/sql/radsqlrelay b/scripts/sql/radsqlrelay
index f72acec0117..e2c1f5ead20 100755
--- a/scripts/sql/radsqlrelay
+++ b/scripts/sql/radsqlrelay
@@ -41,12 +41,18 @@ my $lastinsert;
 my @values;
 
 my $need_exit = 0;
+my $debug = 0;
 
 sub got_signal()
 {
     $need_exit = 1;
 }
 
+sub debug
+{
+    print shift if $debug;
+}
+
 # /!\ OS-dependent structure
 # Linux struct flock
 #   short l_type;
@@ -91,13 +97,16 @@ sub connect_wait($)
 {
     my $dbinfo = shift;
     my $dbh;
+    debug "Connecting to " . $dbinfo->{base};
     while (!$dbh) {
+        debug ".";
 	$dbh = DBI->connect($dbinfo->{base}, $dbinfo->{user}, $dbinfo->{pass},
 			    { RaiseError => 0, PrintError => 0,
 			      AutoCommit => 1 });
 	sleep (1) if !$dbh;
 	exit if $need_exit;
     }
+    debug "\n";
     $dbinfo->{handle} = $dbh;
 }
 
@@ -109,6 +118,7 @@ sub process_file($$)
 
     sub do_inserts($) {
         my $dbinfo = shift;
+        debug "I";
         if (scalar(@values) > 0) {
             my $query = $lastinsert . " ";
             $query .= join(" ), ( ",@values);
@@ -120,6 +130,7 @@ sub process_file($$)
 
     sub do_query($$) {
         my ($dbinfo,$query) = @_;
+        debug ">";
         until ($dbinfo->{handle}->do($query)) {
             print $dbinfo->{handle}->errstr."\n";
             if ($dbinfo->{handle}->ping) {
@@ -133,6 +144,7 @@ sub process_file($$)
     }
 
     unless (-e $path.'.work') {
+        debug "Waiting for $path\n";
 	until (rename($path, $path.'.work')) {
 	    if ($! == ENOENT) {
 		sleep(1);
@@ -142,27 +154,35 @@ sub process_file($$)
 		exit 1;
 	    }
 	}
+        debug "Renamed $path to $path.work\n";
     }
 
+    debug "\nOpening $path.work\n";
     open(FILE, "+< $path.work") or die "error: Couldn't open $path.work: $!\n";
+    debug "Getting file lock\n";
     setlock(\*FILE) or die "error: Couldn't lock $path.work: $!\n";
 
     $lastinsert = "";
     @values = ();
 
+    debug "Reading: ";
+    my $lines = 0;
     while (<FILE>) {
         chomp (my $line = $_);
+        $lines++;
 
         if (!($line =~ /^\s*insert\s+into\s+`?\w+`?\s+(?:\(.*?\)\s+)?
                             values\s*\(.*\)\s*;\s*$/ix)) {
             # This is no INSERT, so start new collection
             do_inserts($dbinfo);
+            debug ".";
             $lastinsert = "";
             # must output this line
             do_query($dbinfo, "$line");
 
 	} else {
             # This is an INSERT, so collect it
+            debug "+";
             my $insert = $line;
             my $values = $line;
             $insert =~ s/^\s*(insert\s+into\s+`?\w+`?\s+(?:\(.*?\)\s+)?
@@ -180,13 +200,18 @@ sub process_file($$)
 
         # limit to $maxcollect collected lines
         if (scalar(@values) >= $maxcollect) {
+            debug "hit maxcollect limit, doing inserts";
             do_inserts($dbinfo);
         }
     }
 
     # Cleanup
+    debug "\nNo more lines to read, doing any final inserts: ";
     do_inserts($dbinfo);
+    debug "\n";
 
+    debug "Processed $lines lines\n";
+    debug "Removing and closing $path.work\n\n";
     unlink($path.'.work');
     close(FILE); # and unlock
 }
@@ -209,6 +234,7 @@ if ($args{'?'}) {
     usage();
     exit 0;
 }
+$debug = 1 if $args{'x'};
 
 my $data_source;
 if (lc($args{d}) eq 'mysql') {
@@ -251,8 +277,10 @@ my $path = shift @ARGV;
 until ($need_exit) {
     process_file(\%dbinfo, $path);
     last if ($args{1} || $need_exit);
+    debug "Sleeping\n";
     sleep(10);
 }
 
+debug "Disconnecting from database\n";
 $dbinfo{handle}->disconnect;
 
