From b26102b097ef6353a20d18ccdaf7a3a6a30185c7 Mon Sep 17 00:00:00 2001
From: ok2cqr <petr@petrhlozek.cz>
Date: Sat, 15 Sep 2018 07:12:30 +0200
Subject: [PATCH] database connection refactoring

---
 src/dData.pas | 126 +++++++-------------------------------------------
 1 file changed, 17 insertions(+), 109 deletions(-)

diff --git a/src/dData.pas b/src/dData.pas
index 9d72d27..bb56598 100644
--- a/src/dData.pas
+++ b/src/dData.pas
@@ -162,7 +162,6 @@ type
     function  FindLib(const Path,LibName : String) : String;
     function  GetMysqldPath : String;
     function  TableExists(TableName : String) : Boolean;
-    function  GetMySQLLib : String;
     function  GetDebugLevel : Integer;
 
     procedure CreateDBConnections;
@@ -267,6 +266,7 @@ type
     function  RbnCallExistsInLog(callsign,band,mode,LastDate,LastTime : String) : Boolean;
     function  CallNoteExists(Callsign : String) : Boolean;
     function  GetNewLogNumber : Integer;
+    function  getNewMySQLConnectionObject : TMySQL57Connection;
 
     procedure SaveQSO(date : TDateTime; time_on,time_off,call : String; freq : Currency;mode,rst_s,
                       rst_r, stn_name,qth,qsl_s,qsl_r,qsl_via,iota,pwr : String; itu,waz : Integer;
@@ -1092,49 +1092,6 @@ begin
     Writeln('   ',DLLUtilName)
   end;
 
-  lib := GetMySQLLib;
-  if fDebugLevel>=1 then Writeln('Loading libmysqlclient: ',lib);
-  if lib <> '' then
-    InitialiseMySQL(lib);
-
-  try try
-    c := TConnectionName.Create(nil);
-    MySQLVer := copy(c.ClientInfo,1,3);
-
-    if fDebugLevel>=1 then
-    begin
-      Writeln('**************************');
-      Writeln('MySQL version: ',MySQLVer);
-      Writeln('**************************')
-    end;
-
-    if MySQLVer = '10.' then
-      MySQLVer := '5.6';
-    if MySQLVer = '10.1' then
-      MySQLVer := '5.7'
-
-  except
-    on E : Exception do
-    begin
-      Writeln('FATAL ERROR: Can not get MySQL client library version version!',LineEnding,
-              'Setting to default version (5.1)');
-      MySQLVer := '5.1'
-    end
-  end
-  finally
-    FreeAndNil(c)
-  end;
-
-  if not TryStrToCurr(MySQLVer,fMySQLVersion) then
-    fMySQLVersion := 5.6;
-
-  if fDebugLevel>=1 then
-  begin
-    Writeln('**********************************');
-    Writeln('MySQL version assigned: ',FloatToStr(fMySQLVersion));
-    Writeln('**********************************')
-  end;
-
   CreateDBConnections;
 
   MainCon.KeepConnection := True;
@@ -1148,13 +1105,11 @@ begin
   end;
 
   //special connection for band map thread
-  BandMapCon.KeepConnection := True;
   BandMapCon.Transaction    := trBandMapFil;
   qBandMapFil.Transaction   := trBandMapFil;
   qBandMapFil.DataBase      := BandMapCon;
   trBandMapFil.DataBase     := BandMapCon;
 
-  RbnMonCon.KeepConnection := True;
   RbnMonCon.Transaction    := trRbnMon;
   qRbnMon.Transaction      := trRbnMon;
   qRbnMon.DataBase         := RbnMonCon;
@@ -1194,7 +1149,6 @@ begin
     Writeln('ZIP code directory:     ',fZipCodeDir);
     Writeln('Binary dir:             ',ExtractFilePath(Paramstr(0)));
     Writeln('Share dir:              ',fShareDir);
-    Writeln('TConnection to MySQL:   ',FloatToStr(fMySQLVersion));
     Writeln('*')
   end;
 
@@ -4166,70 +4120,13 @@ end;
 
 procedure TdmData.CreateDBConnections;
 begin
-  if fMySQLVersion < 5.5 then
-  begin
-    MainCon      := TMySQL51Connection.Create(self);
-    BandMapCon   := TMySQL51Connection.Create(self);
-    RbnMonCon    := TMySQL51Connection.Create(self);
-    LogUploadCon := TMySQL51Connection.Create(self);
-    dbDXC        := TMySQL51Connection.Create(self)
-  end
-  else  if fMySQLVersion < 5.6 then
-  begin
-    MainCon      := TMySQL55Connection.Create(self);
-    BandMapCon   := TMySQL55Connection.Create(self);
-    RbnMonCon    := TMySQL55Connection.Create(self);
-    LogUploadCon := TMySQL55Connection.Create(self);
-    dbDXC        := TMySQL55Connection.Create(self)
-  end
-  else begin
-    if fMySQLVersion < 5.7 then
-    begin
-      MainCon      := TMySQL56Connection.Create(self);
-      BandMapCon   := TMySQL56Connection.Create(self);
-      RbnMonCon    := TMySQL56Connection.Create(self);
-      LogUploadCon := TMySQL56Connection.Create(self);
-      dbDXC        := TMySQL56Connection.Create(self)
-    end
-    else begin
-      MainCon      := TMySQL57Connection.Create(self);
-      BandMapCon   := TMySQL57Connection.Create(self);
-      RbnMonCon    := TMySQL57Connection.Create(self);
-      LogUploadCon := TMySQL57Connection.Create(self);
-      dbDXC        := TMySQL57Connection.Create(self)
-    end
-  end
+  MainCon      := getNewMySQLConnectionObject();
+  BandMapCon   := getNewMySQLConnectionObject();
+  RbnMonCon    := getNewMySQLConnectionObject();
+  LogUploadCon := getNewMySQLConnectionObject();
+  dbDXC        := getNewMySQLConnectionObject();
 end;
 
-function TdmData.GetMySQLLib : String;
-var
-  lib : String;
-  Paths : TStringList;
-begin
-  Result := '';
-  Paths := TStringList.Create;
-  try
-    Paths.Add('/usr/lib64/');
-    Paths.Add('/lib64/');
-    Paths.Add('/usr/lib/x86_64-linux-gnu/');
-    Paths.Add('/usr/lib64/mysql/');
-    Paths.Add('/lib/x86_64-linux-gnu/');
-
-    Paths.Add('/usr/lib/i386-linux-gnu/');
-    Paths.Add('/lib/i386-linux-gnu/');
-    Paths.Add('/usr/lib/');
-    Paths.Add('/lib/');
-    Paths.Add('/usr/lib/mysql/');
-
-    Result := MyFindFile('libmariadbclient.so*', Paths);
-    if (Result='') then
-    begin
-      Result := MyFindFile('libmysqlclient.so*', Paths)
-    end
-  finally
-    FreeAndNil(Paths)
-  end
-end;
 
 function TdmData.GetDebugLevel : Integer;
 var
@@ -4315,5 +4212,16 @@ begin
     QSOColorDate := now
 end;
 
+function TdmData.getNewMySQLConnectionObject : TMySQL57Connection;
+var
+  Connection : TMySQL57Connection;
+begin
+  Connection := TMySQL57Connection.Create(self);
+  Connection.SkipLibraryVersionCheck := True;
+  Connection.KeepConnection := True;
+
+  result := Connection
+end;
+
 end.
 
-- 
2.18.1

