From 66c057d2af20aad7e46d0a7d2346f302cff268d7 Mon Sep 17 00:00:00 2001
From: Gerhard Gonter <ggonter@gmail.com>
Date: Mon, 20 Jun 2016 13:55:37 +0200
Subject: [PATCH] add delete method and reorganized redmine specific methods

---
 lib/Redmine/DB/MySQL.pm | 127 ++++++++++++++++++++++++----------------
 1 file changed, 78 insertions(+), 49 deletions(-)

diff --git a/lib/Redmine/DB/MySQL.pm b/lib/Redmine/DB/MySQL.pm
index e750243..be0d852 100644
--- a/lib/Redmine/DB/MySQL.pm
+++ b/lib/Redmine/DB/MySQL.pm
@@ -11,9 +11,9 @@ use Data::Dumper;
 my $show_query= 0;
 my $show_fetched= 0;
 
-sub show_fetched { shift; $show_fetched= shift; }
-sub show_query { shift; $show_query= shift; }
-sub verbose { shift; $show_fetched= $show_query= shift; }
+sub show_fetched { shift; my $ret= $show_fetched; $show_fetched= shift; $ret; }
+sub show_query   { shift; my $ret= $show_query; $show_query= shift; $ret }
+sub verbose      { shift; my @ret= ($show_fetched, $show_query); $show_fetched= $show_query= shift; @ret; }
 
 sub connect
 {
@@ -65,9 +65,6 @@ sub get_all_x
   my $dbh= $self->connect();
   return undef unless (defined ($dbh));
 
-  # my $project= new Redmine::DB::Project (%par);
-  # print "project: ", Dumper ($project);
-
   my $ss= "SELECT $field_ref FROM $table";
 
   my @v= ();
@@ -103,6 +100,38 @@ sub get_all_x
   $tt;
 }
 
+sub delete_all_x
+{
+  my $self= shift;
+  my $table= shift;
+  my $where= shift;
+  my $field_ref= shift || '*';
+
+  my $dbh= $self->connect();
+  return undef unless (defined ($dbh));
+
+  my $ss= "DELETE $field_ref FROM $table";
+
+  my @v= ();
+  if (defined ($where))
+  {
+    # print "where: ", Dumper ($where) if ($show_query);
+    $ss .= ' WHERE ' . shift (@$where);
+    @v= @$where;
+  }
+
+  if ($show_query)
+  {
+    print "ss=[$ss]";
+    print ' vars: ', join (',', @v) if (@v);
+    print "\n";
+  }
+
+  my $sth= $dbh->prepare($ss) or print $dbh->errstr;
+  # print "sth=[$sth]\n";
+  $sth->execute(@v);
+}
+
 sub tables
 {
   my $self= shift;
@@ -186,48 +215,6 @@ sub desc
   $td;
 }
 
-# BEGIN Redmine specific part
-sub fetch_custom
-{
-  my $db= shift;
-  my $cfid= shift;
-  my $cfty= shift || 'Issue';
-
-  my $res= $db->get_all_x ('custom_values',
-      [ "custom_field_id=? and customized_type=?", $cfid, $cfty ]);
-  $res;
-}
-
-sub change_custom_value
-{
-  my $db= shift;
-  my $cfid= shift;
-  my $cfty= shift || 'Issue';
-  my $cfref= shift; # ticket number or whatever
-  my $cfrid= shift; # record id
-  my $cfval= shift;
-
-  my $data=
-      { customized_type => $cfty, customized_id => $cfref,
-        custom_field_id => $cfid, value => $cfval };
-
-  print "change_custom_value: cfrid=[$cfrid] ", join (' ', %$data), "\n";
-  # return 0; # TODO: add flag to supress changes
-
-  my $res;
-  if (defined ($cfrid))
-  {
-    $db->update ('custom_values', $cfrid, $data);
-    $res= $cfrid;
-  }
-  else
-  {
-    $res= $db->insert ('custom_values', $data);
-  }
-
-  $res;
-}
-
 sub insert
 {
   my $self= shift;
@@ -297,7 +284,8 @@ sub mysql
   my $self= shift;
   print "self: ", Dumper ($self);
 
-  my @cmd= ('mysql', '-h', $self->{'host'}, '-u', $self->{'username'}, $self->{'database'}, '--password='.$self->{'password'});
+  $ENV{MYSQL_PWD}= $self->{password};
+  my @cmd= ('mysql', '-h', $self->{'host'}, '-u', $self->{'username'}, $self->{'database'});
   print ">> cmd=[", join (' ', @cmd), "]\n";
   system (@cmd);
 }
@@ -467,6 +455,47 @@ sub pcx_user_preferences
   $res;
 }
 
+sub fetch_custom
+{
+  my $db= shift;
+  my $cfid= shift;
+  my $cfty= shift || 'Issue';
+
+  my $res= $db->get_all_x ('custom_values',
+      [ "custom_field_id=? and customized_type=?", $cfid, $cfty ]);
+  $res;
+}
+
+sub change_custom_value
+{
+  my $db= shift;
+  my $cfid= shift;
+  my $cfty= shift || 'Issue';
+  my $cfref= shift; # ticket number or whatever
+  my $cfrid= shift; # record id
+  my $cfval= shift;
+
+  my $data=
+      { customized_type => $cfty, customized_id => $cfref,
+        custom_field_id => $cfid, value => $cfval };
+
+  print "change_custom_value: cfrid=[$cfrid] ", join (' ', %$data), "\n";
+  # return 0; # TODO: add flag to supress changes
+
+  my $res;
+  if (defined ($cfrid))
+  {
+    $db->update ('custom_values', $cfrid, $data);
+    $res= $cfrid;
+  }
+  else
+  {
+    $res= $db->insert ('custom_values', $data);
+  }
+
+  $res;
+}
+
 1;
 __END__
 
-- 
GitLab