1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
unit UBde;
interface
uses
windows,
dialogs,
SysUtils,
Db,
DBTables,
BDE;
type TinfoAlias = ^PInfoAlias;
PInfoAlias = record
DriverAlias,
PathAlias,
NameAlias: string;
end;
function fDbiOpenDatabaseList(FindAlias: string): string;
function GetInfoAlias(FindAlias: string; Ainfo: PinfoAlias): boolean;
function AddStandardAliasEX(AliasName, AliasPath: string): integer;//obsolète pour compatibilité ascen
function AddStandardAliasEX2(AliasName, AliasPath, Atype : string): integer;
function GetBdeExists: integer;
function InitBde: boolean;
procedure CloseInitBde;
implementation
procedure SetSlash(var value: string);
begin
if value = '' then exit;
if value[length(value)] <> '\' then value:= value + '\';
end;
function GetWinTemp: string;
var
s: array[0..MAX_PATH - 1] of Char;
begin
GetTempPath(sizeof(s), s);
result:= strpas(s);
if result <> '' then
setslash(result);
end;
function StrToOem(const AnsiStr: string): string;
begin
SetLength(Result, Length(AnsiStr));
if Length(Result) > 0 then
CharToOem(PChar(AnsiStr), PChar(Result));
end;
function fDbiOpenDatabaseList(FindAlias: string): string;
var
TmpCursor: hDbiCur;
Database: DBDesc;
rslt: DbiResult;
tmp: string;
infoAlias: PinfoAlias;
begin
FindAlias:= uppercase(FindAlias);
Check(DbiOpenDatabaseList(TmpCursor));
repeat
rslt:= DbiGetNextRecord(TmpCursor, dbiNOLOCK, @Database, nil);
if (rslt <> DBIERR_EOF) then begin
InfoAlias.NameAlias:= StrPas(Database.szName);
tmp:= StrPas(Database.szPhyName);
if tmp <> '' then
if tmp[length(tmp)] <> '\' then tmp:= tmp + '\';
InfoAlias.PathAlias:= tmp;
InfoAlias.DriverAlias:= StrPas(Database.szDbType);
if uppercase(StrPas(Database.szName)) = FindAlias then result:= InfoAlias.PathAlias;
end;
until rslt <> DBIERR_NONE;
Check(DbiCloseCursor(TmpCursor));
end;
procedure initInfo(ainfo: PinfoAlias);
begin
ainfo.DriverAlias:= '';
ainfo.NameAlias:= '';
ainfo.PathAlias:= '';
end;
function GetInfoAlias(FindAlias: string; Ainfo: PinfoAlias): boolean;
var
TmpCursor: hDbiCur;
Database: DBDesc;
rslt: DbiResult;
tmp: string;
infoAlias: PinfoAlias;
begin
result:= false;
FindAlias:= uppercase(FindAlias);
initInfo(Ainfo);
Check(DbiOpenDatabaseList(TmpCursor));
try
repeat
rslt:= DbiGetNextRecord(TmpCursor, dbiNOLOCK, @Database, nil);
if (rslt <> DBIERR_EOF) then begin
InfoAlias.NameAlias:= StrPas(Database.szName);
tmp:= StrPas(Database.szPhyName);
if tmp <> '' then
if tmp[length(tmp)] <> '\' then tmp:= tmp + '\';
InfoAlias.PathAlias:= tmp;
InfoAlias.DriverAlias:= StrPas(Database.szDbType);
if uppercase(StrPas(Database.szName)) = FindAlias then begin
Ainfo:= InfoAlias;
result:= true;
exit;
end;
end;
until rslt <> DBIERR_NONE;
finally
Check(DbiCloseCursor(TmpCursor));
end;
end;
function AddStandardAliasEX(AliasName, AliasPath: string): integer;
var
info: PinfoAlias;
begin
//ajoute/modifie un alias de type paradox
//Si l'alias existe il est supprimé et recréé
//avec le nouveau path
//********************
//Exemple :
//AddStandardAliasEX('test9', 'C:\BDE32\EXAMPLES\TABLES');
//AliasPath n'est pas vérifié, ni dans la pertinence du nom, ni dans l'accèssibilité
Check(dbiInit(nil));
try
try
if GetInfoAlias(AliasName, info) then begin
//l'alias existe donc le supprime
result:= DbiDeleteAlias(nil,pchar(AliasName));
if result <> DBIERR_NONE then exit;
end;
AliasPath:= 'PATH:' + AliasPath;
result:= DbiAddAlias(nil, PChar(AliasName), PChar(StrToOem('PARADOX')), PChar(AliasPath), True);
if result = DBIERR_NONE then
//Check(DbiCfgSave(nil, nil, Bool(-1)));
result:= DbiCfgSave(nil, nil, Bool(-1));
{Pour mémoire....
case result of
DBIERR_INVALIDPARAM: msg:= ' invalid alias name. Invalid characters include a colon (":") and backslash ("\"). szASCII, szDBASE, and szPARADOX are entered as a STANDARD alias with the respective default driver.';
DBIERR_NONE: msg:= ' The alias was added successfully.';
DBIERR_NAMENOTUNIQUE : msg:= 'Another alias with the same name already exists (applicable only when bPersistent is TRUE).';
DBIERR_OBJNOTFOUND: msg:= ' One (or more) of the optional parameters passed in through pszParams was not found as a valid type in the driver section of the configuration file.';
DBIERR_UNKNOWNDRIVER: msg:= ' No driver name found in configuration file matching pszDriverType.';
end;}
except
result:= -1;
end;
finally
Check(DbiExit);
end;
end;
function AddStandardAliasEX2(AliasName, AliasPath, Atype : string): integer;
var
info: PinfoAlias;
begin
//ajoute/modifie un alias de type Atype
//Si l'alias existe il est supprimé et recréé
//avec le nouveau path
//********************
//Exemple :
//AddStandardAliasEX2('test9', 'C:\BDE32\EXAMPLES\TABLES', 'PARADOX');
//AliasPath n'est pas vérifié, ni dans la pertinence du nom, ni dans l'accèssibilité
Check(dbiInit(nil));
try
try
if GetInfoAlias(AliasName, info) then begin
//l'alias existe donc le supprime
result:= DbiDeleteAlias(nil,pchar(AliasName));
if result <> DBIERR_NONE then exit;
end;
AliasPath:= 'PATH:' + AliasPath;
result:= DbiAddAlias(nil, PChar(AliasName), PChar(StrToOem(Atype)), PChar(AliasPath), True);
if result = DBIERR_NONE then
//Check(DbiCfgSave(nil, nil, Bool(-1)));
result:= DbiCfgSave(nil, nil, Bool(-1));
{Pour mémoire....
case result of
DBIERR_INVALIDPARAM: msg:= ' invalid alias name. Invalid characters include a colon (":") and backslash ("\"). szASCII, szDBASE, and szPARADOX are entered as a STANDARD alias with the respective default driver.';
DBIERR_NONE: msg:= ' The alias was added successfully.';
DBIERR_NAMENOTUNIQUE : msg:= 'Another alias with the same name already exists (applicable only when bPersistent is TRUE).';
DBIERR_OBJNOTFOUND: msg:= ' One (or more) of the optional parameters passed in through pszParams was not found as a valid type in the driver section of the configuration file.';
DBIERR_UNKNOWNDRIVER: msg:= ' No driver name found in configuration file matching pszDriverType.';
end;}
except
result:= -1;
end;
finally
Check(DbiExit);
end;
end;
function GetBdeExists: integer;
begin
//à voir pour NT - voir initBDE
try
result:= dbiInit(nil);
if result = DBIERR_NONE then begin
DbiExit;
end else
raise Exception.create('');
except
result:= -1;
end;
end;
function InitBde: boolean;
var
flag: integer;
begin
Flag:= -1;
try
//if Win32Platform = VER_PLATFORM_WIN32_NT then begin
try
session.PrivateDir:= GetWinTemp;
except
end;
session.open;
if session.active then
Flag:= DBIERR_NONE;
//end else Flag:= dbiInit(nil);
result:= (Flag = DBIERR_NONE);
except
result:= false;
end;
end;
procedure CloseInitBde;
begin
try
//if Win32Platform = VER_PLATFORM_WIN32_NT then begin
if session.active then
session.close;
//end else DbiExit;
except
end;
end;
end. |
Partager