Page 1 of 1

Copy one TUniConnection to another TUniConnection

Posted: Thu 28 Dec 2017 10:43
by ertank
Hello,

I am using Delphi 10.2.2, UniDAC 7.1.3, SQL Server 2012, SQL Server 2016 and PostgreSQL 10.1

My application can use SQL Server and PostgreSQL databases depending on user choice. Databases can be switched to another if user would like so. All connection parameters as well as some SpecificOptions are saved and loaded when application is starting.

That application at some point runs some operations in Thread. We know that each Thread require its own TUniConnection for safe operation.

My question is:
Is there a ready function/method to copy my already connected Main Application TUniConnection properties into Thread TUniConnection properties?

Please note that I do need almost identical DataModule.UniConection1 and MyThread.UniConnection1. Meaning all parameters, Options and SpecificOptions to be copied except MyThread.UniConnection1 will not be connected to database until I tell it to do so.

I am hesitating to call all these procedures for MyThread.UniConnection1 parameters to be loaded again and again from file system for each thread use.

I can implement my own copy procedure, however, I would like to learn if there is already something available.

Thanks & regards,
Ertan

Re: Copy one TUniConnection to another TUniConnection

Posted: Sat 30 Dec 2017 22:14
by FredS
I couldn't find anything that worked for me so here is what I added:

Code: Select all

function TUniConnection.CloneConnection(Owner: TComponent = nil): TUniConnection;
begin
  if not Connected then raise Exception.Create('Cannot clone a connection without being connected.');

  Result := TUniConnection.Create(Owner);
  Result.ConnectString := ConnectString;
  Result.Macros.Assign(Macros);
end;
Once the main Connection is connected your ConnectString will/should contain all relevant info, including those loaded from SpecificOptions.
But that wasn't enough, dealing with multiple dbs required the Macros to be available as well.

Re: Copy one TUniConnection to another TUniConnection

Posted: Wed 03 Jan 2018 13:31
by azyk
In UniDAC, there is no separate functionality for copying TUniConnection properties and SpecificOptions for all providers from one instance to another. You can implement this functionality by yourself.

Re: Copy one TUniConnection to another TUniConnection

Posted: Wed 03 Jan 2018 23:02
by ertank
FredS wrote:I couldn't find anything that worked for me so here is what I added
Thank you for sharing. As my license do not include source codes, I am not able to directly use your suggestion. However, I can still implement something similar out of it.

Re: Copy one TUniConnection to another TUniConnection

Posted: Wed 03 Jan 2018 23:07
by FredS
ertank wrote:
FredS wrote:I am not able to directly use your suggestion. However, I can still implement something similar out of it.
A Class Helper would work just as well.