2010. január 9., szombat

How to determine the path of a TTable


Problem/Question/Abstract:

How to determine the path of a TTable

Answer:

Solve 1:

When a Table is referenced through an alias, the physical path is not readily available. To obtain this path, use the DbiGetDatabaseDesc BDE function. This function takes the alias name and a pointer to a DBDesc structure. The DBDesc structure will be filled with the information pertaining to that alias. This structure is defined as:


pDBDesc = ^DBDesc;
DBDesc = packed record { A given Database Description }
  szName: DBINAME; { Logical name (Or alias) }
  szText: DBINAME; { Descriptive text }
  szPhyName: DBIPATH; { Physical name/path }
  szDbType: DBINAME; { Database type }
end;

The physical name/path will be contained in the szPhyName field of the DBDesc structure. Possible return values for the DBIGetDatbaseDesc function are:

DBIERR_NONE
The database description for pszName was retrieved successfully.

DBIERR_OBJNOTFOUND
The database named in pszName was not found.

The code example below illustrates how to obtain the physical path name of a TTable component using the DBDemos alias:

var
  vDBDesc: DBDesc;
  DirTable: string;
begin
  Check(DbiGetDatabaseDesc(PChar(Table1.DatabaseName), @vDBDesc));
  DirTable := Format('%s\%s', [vDBDesc.szPhyName, Table1.TableName]);
  ShowMessage(DirTable);
end;


Solve 2:

Here are three ways to get the path associated with an alias. a) is for permanent aliases only. b) works on BDE and local aliases and c) works with BDE and local aliases as well as with tables with a hardcoded path, using DBI calls.

a) For permanent aliases only

function GetDBPath1(AliasName: string): TFileName;
var
  ParamList: TStringList;
begin
  ParamList := TStringList.Create;
  with Session do
  try
    GetAliasParams(AliasName, ParamList);
    Result := UpperCase(ParamList.Values['PATH']) + '\';
  finally
    Paramlist.Free;
  end;
end;

b) Works on BDE and local aliases

function GetDBPath2(AliasName: string): TFileName;
var
  ParamList: TStringList;
  i: integer;
begin
  ParamList := TStringList.Create;
  with Session do
  try
    try
      GetAliasParams(AliasName, ParamList);
    except
      for i := 0 to pred(DatabaseCount) do
        if (Databases[i].DatabaseName = AliasName) then
          ParamList.Assign(Databases[i].Params);
    end;
    Result := UpperCase(ParamList.Values['PATH']) + '\';
  finally
    Paramlist.Free;
  end;
end;

c) The following example assumes the TTable being active

function GetDBPath3(ATable: TTable): TFileName;
var
  TblProps: CURProps;
  pTblName, pFullName: DBITblName;
begin
  with ATable do
  begin
    AnsiToNative(Locale, TableName, pTblName, 255);
    Check(DBIGetCursorProps(Handle, TblProps));
    Check(DBIFormFullName(DBHandle, pTblName, TblProps.szTableType, pFullName));
    Result := ExtractFilePath(StrPas(pFullName));
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése