Constants for ProviderName

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Bert1234
Posts: 8
Joined: Fri 25 Mar 2016 15:36

Constants for ProviderName

Post by Bert1234 » Thu 31 Mar 2016 10:25

Instead of writing something like:
if UniConnection.ProviderName = 'PostgreSQL' then
I would like to use constants like:
if UniConnection.ProviderType = UNIDAC_POSTGRESQL then
The drawback of comparing strings is that it is vulnerable to mistakes (case sensitivity).

Are such constants available or can they be added in a next release?

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Constants for ProviderName

Post by AlexP » Thu 31 Mar 2016 11:25

hello,

The provider name is set as a string value, since it can also be specified in the ConnectString property.

Bert1234
Posts: 8
Joined: Fri 25 Mar 2016 15:36

Re: Constants for ProviderName

Post by Bert1234 » Thu 31 Mar 2016 12:23

AlexP wrote: The provider name is set as a string value, since it can also be specified in the ConnectString property.
Yes, I know that. At startup of the application the provider is set, but later, in a different part of the code I want to do something depending on the provider chosen earlier.

For example:

Code: Select all

if UniConnection.ProviderName = 'PostgreSQL' then
begin
  // This code will get executed.
end;

if UniConnection.ProviderName = 'Postgres' then
begin
  // This code will NEVER get executed, but you will only 
  // discover this when stepping through the code with the debugger.
end;
Compare this to:

Code: Select all

if UniConnection.ProviderType = UNIDAC_POSTGRESQL then
begin
  // This code will get executed.
end;

if UniConnection.ProviderType = UNIDAC_POSTGRES then
begin
  // This code will not compile if the particular constant hasn't been defined.
  // You will notice your error right away.
end;
The use of constants is preferred above string compares.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Constants for ProviderName

Post by AlexP » Thu 31 Mar 2016 13:09

You can implement such behavior by yourself by creating a descendant from TuniConnection, e.g., as follows:

Code: Select all

type
  TProviderType = (ptPOSTGRESQL, ptORACLE, ...);

  TMyUniConnection = class(TUniConnection)
  private
    FProviderType: TProviderType;
    procedure SetProviderType(const Value: TProviderType);
  public
    property ProviderType: TProviderType read FProviderType write SetProviderType;
  end;

procedure TMyUniConnection.SetProviderType(const Value: TProviderType);
begin
  if Value <> FProviderType then begin
    FProviderType := Value;
    case FProviderType of
      ptPOSTGRESQL: Self.ProviderName := 'PostgreSQL';
      ptORACLE: Self.ProviderName := 'Oracle';
      ...
      else
        raise Exception.Create('provider doesn''t exists');
    end;
  end;
end;

Post Reply