Page 1 of 1

Constants for ProviderName

Posted: Thu 31 Mar 2016 10:25
by Bert1234
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?

Re: Constants for ProviderName

Posted: Thu 31 Mar 2016 11:25
by AlexP
hello,

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

Re: Constants for ProviderName

Posted: Thu 31 Mar 2016 12:23
by Bert1234
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.

Re: Constants for ProviderName

Posted: Thu 31 Mar 2016 13:09
by AlexP
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;