Delphi Examples: DbiTranslateRecordStructure

<< Click to Display Table of Contents >>

Navigation:  »No topics above this level«

Delphi Examples: DbiTranslateRecordStructure

Return to chapter overview

Creates an empty version of SrcTbl to DestTbl. This will convert from any source type to any destination type--Paradox to InterBase and so on. The Table does not have any indexes.

This example uses the following input:

 fDbiTranslateRecordStructure(AnimalTbl, NewTbl, AnimalTbl.DBHandle);

 

The procedure is:

procedure fDbiTranslateRecordStructure(SrcTbl, DestTbl: TTable; DestDB: hDBIDb);

var

 pSrcFlds, pDestFlds: pFLDDesc;

 TblDesc: CRTblDesc;

 DBType: String;

 W: Word;

begin

 pSrcFlds := AllocMem(SrcTbl.FieldCount * sizeof(FLDDesc));

 pDestFlds := AllocMem(SrcTbl.FieldCount * sizeof(FLDDesc));

try

   SetLength(DBType, DBIMAXNAMELEN);

   // Get the destination database type

   Check(DbiGetProp(hDBIObj(DestDb), dbDATABASETYPE,

     PChar(DBType), DBIMAXNAMELEN, W));

   SetLength(DBType, StrLen(PChar(DBType)));

  if (DBType = 'STANDARD') then begin

    if (UpperCase(ExtractFileExt(DestTbl.TableName)) = '.DB') then

       DBType := szParadox

    else if (UpperCase(ExtractFileExt(DestTbl.TableName)) = '.DBF') then

       DBType := szDbase

    else if (UpperCase(ExtractFileExt(DestTbl.TableName)) = '.') then

       DBType := szParadox

    else

      raise EDBEngineError.Create(DBIERR_UNKNOWNDRIVER);

  end;

   // Get the source field information

   Check(DbiGetFieldDescs(SrcTbl.Handle, pSrcFlds));

   // Translate the source fields into the destination fields

   Check(DbiTranslateRecordStructure(nil, SrcTbl.FieldCount, pSrcFlds,

     PChar(DBType), nil, pDestFlds, False));

   FillChar(TblDesc, sizeof(TblDesc), #0);

   StrPCopy(TblDesc.szTblName, DestTbl.TableName);

   StrPCopy(TblDesc.szTblType, DBType);

   TblDesc.iFldCount := SrcTbl.FieldCount;

   TblDesc.pFldDesc := pDestFlds;

   // Create the destination table

   Check(DbiCreateTable(DestDB, True, TblDesc));

finally

   FreeMem(pSrcFlds, SrcTbl.FieldCount * sizeof(FLDDesc));

   FreeMem(pDestFlds, SrcTbl.FieldCount * sizeof(FLDDesc));

end;

end;