Copy one TUniConnection to another TUniConnection

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
ertank
Posts: 172
Joined: Wed 13 Jan 2016 16:00

Copy one TUniConnection to another TUniConnection

Post by ertank » Thu 28 Dec 2017 10:43

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

FredS
Posts: 272
Joined: Mon 10 Nov 2014 17:52

Re: Copy one TUniConnection to another TUniConnection

Post by FredS » Sat 30 Dec 2017 22:14

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.

azyk
Devart Team
Posts: 1119
Joined: Fri 11 Apr 2014 11:47
Location: Alpha Centauri A

Re: Copy one TUniConnection to another TUniConnection

Post by azyk » Wed 03 Jan 2018 13:31

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.

ertank
Posts: 172
Joined: Wed 13 Jan 2016 16:00

Re: Copy one TUniConnection to another TUniConnection

Post by ertank » Wed 03 Jan 2018 23:02

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.

FredS
Posts: 272
Joined: Mon 10 Nov 2014 17:52

Re: Copy one TUniConnection to another TUniConnection

Post by FredS » Wed 03 Jan 2018 23:07

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.

Post Reply