Subject Re: [firebird-support] Check if an alias is defined
Author Gustavo
Hello:

Finally, I did what eMeL suggested. I wrote two UDFs, one
(MMDirectorioDelAlias) to get the path of an alias. If the alias is defined
in aliases.conf, it returns the path; if the alias is not defined, it
returns an empty string. The other UDF (MMPudeEstablecerAlias) can be used
to add an alias to aliases.conf. To use this, I need to be connected to a
database. So I use a "master" database which I am sure that always exist and
is defined in aliases.conf. As eMeL said, it would be much better if
FireBird has two services to do this and so we don´t need this "master"
database.

These are the first UDFs I write. They work perfectly but perhaps they are
not written in the best way. Here you have the code (in Delphi 5). Perhaps
they are usefull to anyone.

Thanks for your help.

Gustavo


unit MMudfP;

interface

uses
Classes,
INIFiles,
Registry,
SysUtils,
Windows;

function ib_util_malloc(l: integer): pointer; cdecl; external 'ib_util.dll';

function MMDirectorioDelAlias(CualAlias: PChar): PChar; export;
function MMPudeEstablecerAlias(CualAlias,
NuevoDirectorio: PChar): PChar;

implementation

function MMDirectorioDelAlias(CualAlias: PChar): PChar;
{
Declaration
DECLARE EXTERNAL FUNCTION MMDirectorioDelAlias
CSTRING(1000)
RETURNS CSTRING(1000) FREE_IT
ENTRY_POINT 'MMDirectorioDelAlias'
MODULE_NAME 'MMudf';

Example
SELECT MMDirectorioDelAlias('MyAlias1') FROM RDB$DATABASE
}
var
DirAliases,
AliasABuscar,
Directorio : ANSIString;
ii : Longint;
begin
Result:='';

with TRegistry.Create do
try
Access:=KEY_READ;
RootKey:=HKEY_LOCAL_MACHINE;
OpenKey('\SOFTWARE\Firebird Project\Firebird Server\Instances',False);
DirAliases:=ReadString('DefaultInstance');
finally
Free;
end;

with TStringList.Create do
try
LoadFromFile(DirAliases+'aliases.conf');
AliasABuscar:=UpperCase(CualAlias);

for ii:=0 to Count-1 do
if (UpperCase(Copy(Trim(Strings[ii]),1,Length(AliasABuscar))) =
AliasABuscar) then
begin
Directorio:=Trim(Strings[ii]);
System.Delete(Directorio,1,Length(AliasABuscar));
Directorio:=Trim(Directorio);
if (Directorio <> '') and (Copy(Directorio,1,1) = '=') then
begin
System.Delete(Directorio,1,1);
Result:=PChar(Trim(Directorio));
Break;
end;
end;
finally
Free;
end;
end;

function MMPudeEstablecerAlias(CualAlias,
NuevoDirectorio: PChar): PChar;
{
Declaration
DECLARE EXTERNAL FUNCTION MMPudeEstablecerAlias
CSTRING(1000), CSTRING(1000)
RETURNS CSTRING(1) FREE_IT
ENTRY_POINT 'MMPudeEstablecerAlias'
MODULE_NAME 'MMudf';

Example
SELECT MMPudeEstablecerAlias('MyAlias1','C:\Archivos de
programa\MyApplications\Gestión comercial\General\GENERAL.FDB') FROM
RDB$DATABASE
}
var
DirAliases,
AliasABuscar,
Directorio : ANSIString;
ii : Longint;
begin
Result:='S';

with TRegistry.Create do
try
Access:=KEY_READ;
RootKey:=HKEY_LOCAL_MACHINE;
OpenKey('\SOFTWARE\Firebird Project\Firebird Server\Instances',False);
DirAliases:=ReadString('DefaultInstance');
finally
Free;
end;

with TStringList.Create do
try
LoadFromFile(DirAliases+'aliases.conf');
AliasABuscar:=UpperCase(CualAlias);

for ii:=0 to Count-1 do
if (UpperCase(Copy(Trim(Strings[ii]),1,Length(AliasABuscar))) =
AliasABuscar) then
begin
Directorio:=Trim(Strings[ii]);
System.Delete(Directorio,1,Length(AliasABuscar));
Directorio:=Trim(Directorio);
if (Directorio <> '') and (Copy(Directorio,1,1) = '=') then
begin
System.Delete(Directorio,1,1);
Directorio:=Trim(Directorio);
if (Directorio <> NuevoDirectorio) then
begin
Strings[ii]:=CualAlias+' = '+NuevoDirectorio;
try
SaveToFile(DirAliases+'aliases.conf');
except
Result:='N';
end;
end;
Exit;
end;
end;

Add(CualAlias+' = '+NuevoDirectorio);
try
SaveToFile(DirAliases+'aliases.conf');
except
Result:='N';
end;

finally
Free;
end;
end;

end.


----- Original Message -----
From: "eMeL" <emel@...>
To: <firebird-support@yahoogroups.com>
Sent: Thursday, August 23, 2007 6:03 AM
Subject: Re: [firebird-support] Check if an alias is defined


>
>> > Correctly the 'alias name' place be in 'create database' statement!
>> >
>> I must say I don't agree on this one. Extending create database should be
>> done only if
>> it's compliant with SQL standard, but I don't know if that's so, though I
>> doubt it.
>
> It's only a theoretical idea ;)
> When you create a database, immediatly [together] must register it.
> We shouldn't use database path in client.
>
> But...
>
> if I can create database in client, I _must_ register it from client
> application.
>
> My idea for this problem: http://tracker.firebirdsql.org/browse/CORE-1422
>
> eMeL
>
>
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> Visit http://www.firebirdsql.org and click the Resources item
> on the main (top) menu. Try Knowledgebase and FAQ links !
>
> Also search the knowledgebases at http://www.ibphoenix.com
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> Yahoo! Groups Links
>
>
>
>
>
> __________ Información de NOD32, revisión 2478 (20070823) __________
>
> Este mensaje ha sido analizado con NOD32 antivirus system
> http://www.nod32.com
>
>