home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / s_to_z / tblinfo / tblinfo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-15  |  11.1 KB  |  392 lines

  1. {************************************************}
  2. { TBLINFO.PAS                                    }
  3. { Wrapper unit for TblInfo component             }
  4. { Compiled with Borland Delphi 1.0               }
  5. { ⌐1995, SJHDesign Inc.                          }
  6. {************************************************}
  7.  
  8. unit TblInfo;
  9.  
  10. interface
  11.  
  12. uses
  13.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  14.   Forms, Dialogs, TblDlg, DB, DBTables, dbiprocs, dbitypes,dbierrs;
  15.  
  16. type
  17.   TTblInfoDlg = class(TComponent)
  18.  
  19.   private
  20.     fColor : TColor;
  21.     fFont: TFont;
  22.     fCaption :string;
  23.     fTable : TTable;
  24.     tblsize,tbldate : string;
  25.     nIndexes : integer;
  26.     procedure DlgColor(value : TColor);
  27.     procedure DlgFont(value : TFont);
  28.     procedure DlgCaption(value : string);
  29.     procedure DlgTable(value : TTable);
  30.     function GetTableType(table : tTable) : string;
  31.     procedure GetTableDate_Size(table : tTable);
  32.     procedure BDE_Error(code:integer);
  33.     procedure DisplayIndexes(table : tTable);
  34.     procedure DisplayFields(table : tTable);
  35.  
  36.   protected
  37.     { Protected declarations }
  38.  
  39.   public
  40.     constructor Create(AOwner: TComponent); override;
  41.     function Execute : Boolean;
  42.     destructor Destroy; override;
  43.  
  44.   published
  45.     property Color : TColor read FColor write DlgColor default clBtnFace;
  46.     property Font : TFont read FFont write DlgFont;
  47.     property Caption: String read FCaption write DlgCaption;
  48.     property Table : TTable read FTable write DlgTable;
  49.   end;
  50. var
  51.   MultPageDlg: TMultPageDlg;
  52.  
  53. function TranslateFieldType(fldtyp,subtyp :Integer) : string;
  54. procedure Register;
  55.  
  56. implementation
  57.  
  58. constructor TTblInfoDlg.Create(AOwner: TComponent);
  59. var val:boolean;
  60. begin
  61.   inherited Create(AOwner);
  62.   fColor := ClBtnFace;
  63.   fFont := TFont.Create;
  64.   fCaption := 'Table Information';
  65.   fTable := nil;
  66.   nIndexes := 0;
  67. end;
  68.  
  69.  
  70. function TTblInfoDlg.Execute : Boolean;
  71. begin
  72.   MultPageDlg := TMultPageDlg.Create(Application);
  73.   MultPageDlg.Color := fColor;
  74.   MultPageDlg.TabSet.SelectedColor := fColor;
  75.   MultPageDlg.Font := fFont;
  76.   MultPageDlg.Caption := fCaption;
  77.   if fTable.active = false then
  78.   begin
  79.     multpagedlg.free;
  80.     messagebox(0, 'Table Not Found','Error',mb_ok);
  81.     exit;
  82.   end;
  83.   MultPageDlg.Caption := ftable.tablename;
  84.   MultPageDlg.NumFlds.Caption := inttostr(ftable.FieldCount);
  85.   MultPageDlg.NumRecs.Caption := inttostr(ftable.RecordCount);
  86.   MultPageDlg.TblType.Caption := GetTableType(ftable);
  87.   nIndexes := ftable.IndexFieldCount;
  88.   DisplayIndexes(ftable);
  89.   MultPageDlg.NumIndex.Caption := inttostr(nIndexes);
  90.   GetTableDate_Size(ftable);
  91.   MultPageDlg.TblSize.Caption := tblsize;
  92.   MultPageDlg.LastChng.Caption := tbldate;
  93.   DisplayFields(ftable);
  94.   try
  95.     result := (multpagedlg.showModal = IDOK);
  96.   finally
  97.     multpagedlg.free;
  98.   end;
  99. end;
  100.  
  101.  
  102. destructor TTblInfoDlg.Destroy;
  103. begin
  104.   fFont.free;
  105.   inherited Destroy;
  106. end;
  107.  
  108.  
  109. procedure TTblInfoDlg.DlgColor(value : TColor);
  110. begin
  111.   if FColor <> Value then
  112.   begin
  113.     FColor := Value;
  114.   end;
  115. end;
  116.  
  117. procedure TTblInfoDlg.DlgFont(value : TFont);
  118. begin
  119.   if FFont <> Value then
  120.   begin
  121.     FFont.Assign(Value);
  122.   end;
  123. end;
  124.  
  125.  
  126. procedure TTblInfoDlg.DlgCaption(value : string);
  127. begin
  128.   FCaption := Value;
  129. end;
  130.  
  131. procedure TTblInfoDlg.DlgTable(value : TTable);
  132. begin
  133.   ftable := Value;
  134. end;
  135.  
  136. function TTblInfoDlg.GetTableType(table : tTable):string;
  137. begin
  138.   if Table.tableType = ttDefault then
  139.   begin
  140.     if CompareText(ExtractFileExt(Table.TableName), '.dbf') = 0 then
  141.     begin
  142.       GetTableType := 'DBase';
  143.       exit;
  144.     end
  145.     else if CompareText(ExtractFileExt(Table.TableName), '.db') = 0 then
  146.     begin
  147.       GetTableType := 'Paradox';
  148.       exit;
  149.     end
  150.     else GetTableType := 'ODBC or other';
  151.   end                                     
  152.   else if table.tableType = ttDBase then GetTableType := 'DBase'
  153.   else if Table.TableType = ttParadox then GetTableType := 'Paradox'
  154.   else GetTableType := 'ODBC or other';
  155. end;
  156.  
  157. procedure TTblInfoDlg.GetTableDate_Size(table : tTable);
  158. var
  159.   hCur : hDBICur;
  160.   name : array[0..80]of Char;
  161.   recbuf :pbyte;
  162.   tblprops :curprops;
  163.   szTemp : array[0..255] of CHAR;
  164.   szTempLong: LongInt absolute szTemp;
  165.   dtTemp : longint;
  166.   bIsBlank: bool;
  167.  
  168. begin
  169.   strPcopy(name,Table.TableName);
  170.   if dbiOpenTableList(Table.DBHandle,false,false,name, hcur) = DBIERR_NONE then
  171.   begin
  172.     if dbigetcursorprops(hCur,tblprops)= DBIERR_NONE then
  173.     begin
  174.       {$I+}
  175.       try
  176.         GetMem(RecBuf, tblProps.iRecBufSize*sizeof(BYTE));
  177.       except
  178.         on EOutOfMemory do
  179.         begin
  180.          DbiCloseCursor(hCur);
  181.          BDE_Error(4);
  182.          exit;
  183.         end;
  184.       {$I-}
  185.       end;
  186.       dbisettobegin(hCur);
  187.       if DbiGetNextRecord(hCur, dbiNOLOCK, RecBuf, nil) = DBIERR_NONE then
  188.       begin
  189.         if DbiGetField(hCur, 7, RecBuf, (@szTemp), bisblank) = DBIERR_NONE then
  190.         begin
  191.           if DbiGetField(hCur, 5, RecBuf, (@dtTemp), bIsBlank) <> DBIERR_NONE then BDE_Error(1)
  192.           else
  193.           begin
  194.             tbldate := DateToStr(dtTemp);
  195.             Str(szTempLong,tblsize);
  196.           end;
  197.           if recbuf <> nil then freemem(recbuf, tblprops.iRecBufSize * sizeof(BYTE));
  198.           DbiCloseCursor(hCur);
  199.         end
  200.         else
  201.         begin
  202.           if recbuf <> nil then freemem(recbuf, tblprops.iRecBufSize * sizeof(BYTE));
  203.           DbiCloseCursor(hCur);
  204.           BDE_Error(1);
  205.         end;
  206.       end
  207.       else
  208.       begin
  209.         if recbuf <> nil then freemem(recbuf, tblprops.iRecBufSize * sizeof(BYTE));
  210.         DbiCloseCursor(hCur);
  211.         BDE_Error(1);
  212.       end;
  213.     end
  214.     else
  215.     begin
  216.       DbiCloseCursor(hCur);
  217.       BDE_Error(1);
  218.     end;
  219.   end
  220.   else BDE_Error(1);
  221. end;
  222.  
  223.  
  224. procedure TTblInfoDlg.DisplayIndexes(table:TTable);
  225. var
  226.   hCur: hdbicur;
  227.   name : array[0..80]of Char;
  228.   recbuf :pbyte;
  229.   tblprops :curprops;
  230.   nmTemp : array[0..255] of CHAR;
  231.   BoolTemp, bIsBlank: bool;
  232.   i: integer;
  233. begin
  234.   i := 0;
  235.   bisblank := false;
  236.   strPcopy(name,Table.TableName);
  237.   if DbiOpenIndexList(Table.DBHandle,name, nil,hCur) = DBIERR_NONE then
  238.   begin
  239.     if dbigetcursorprops(hCur,tblprops)= DBIERR_NONE then
  240.     begin
  241.       dbisettobegin(hCur);
  242. {This requires some explanation-the TTable.IndexFieldCount property is used to
  243.  get an idea of how many indexed fields we have(nIndexes), but it does not count
  244.  Paradox .PX or Dbase .MDX maintained indexes. DbiOpenIndexList WILL show these
  245.  index types as an incomplete record and return an error value, so if we want them to
  246.  show up in our index listing, we must read the record, ignore the DBIERR_EOF from
  247.  DbiGetNextRecord and run through the loop once more. This is the reason we loop
  248.  nIndexes times instead of nIndexes-1 times. The number of strings in the resulting
  249.  index list is used as the Index Fields number on the first page of the dialog to make
  250.  it agree with the number of Indexes listed on page 3. It's a kludge, but it works...}
  251.       for i := 0 to nIndexes do
  252.       begin
  253.         {$I+}
  254.         try
  255.           GetMem(RecBuf, tblProps.iRecBufSize*sizeof(BYTE));
  256.         except
  257.           on EOutOfMemory do
  258.           begin
  259.             DbiCloseCursor(hCur);
  260.             BDE_Error(4);
  261.             exit;
  262.           end;
  263.         {$I-}
  264.         end;
  265.         if DbiGetNextRecord(hCur, dbiNOLOCK, RecBuf, nil) = DBIERR_NONE then
  266.         begin
  267.           DbiGetField(hCur, 1, RecBuf, (@nmTemp), bisblank);
  268.           MultPageDlg.Outline4.add(i,StrPas(nmtemp));
  269.           DbiGetField(hCur, 5, RecBuf, (@BoolTemp), bisblank);
  270.           if BoolTemp then MultPageDlg.Outline5.add(i,'Yes')
  271.           else MultPageDlg.Outline5.add(i,'No');
  272.           DbiGetField(hCur, 6, RecBuf, (@BoolTemp),bisblank);
  273.           if BoolTemp then MultPageDlg.Outline6.add(i,'Yes')
  274.           else MultPageDlg.Outline6.add(i,'No');
  275.           if recbuf <> nil then freemem(recbuf, tblprops.iRecBufSize * sizeof(BYTE));
  276.         end
  277.         else if recbuf <> nil then freemem(recbuf, tblprops.iRecBufSize * sizeof(BYTE));
  278.       end;
  279.     end
  280.     else
  281.     begin
  282.       DbiCloseCursor(hCur);
  283.       BDE_Error(3);
  284.     end;
  285.     DbiCloseCursor(hCur);
  286.     nindexes := MultPageDlg.Outline4.ItemCount;
  287.   end
  288.   else BDE_Error(3);
  289. end;
  290.  
  291.  
  292. procedure TTblInfoDlg.DisplayFields(table:TTable);
  293. var
  294.   hCur : hDBICur;
  295.   name : array[0..80]of Char;
  296.   recbuf :pbyte;
  297.   tblprops :curprops;
  298.   nmTemp : array[0..255] of CHAR;
  299.   typTemp,lntemp,subtyptemp : Integer;
  300.   bIsBlank: bool;
  301.   i,recs: integer;
  302.   lnTempStr : string;
  303. begin
  304.   i := 0;
  305.   recs := table.FieldCount-1;
  306.   strPcopy(name,Table.TableName);
  307.   if DbiOpenFieldList(table.DBHandle, name, nil, False, hCur) = DBIERR_NONE then
  308.   begin
  309.     if dbiGetCursorProps(hCur,tblprops)= DBIERR_NONE then
  310.     begin
  311.       dbiSetToBegin(hCur);
  312.       for i := 0 to recs do
  313.       begin
  314.       {$I+}
  315.       try
  316.         GetMem(RecBuf, tblProps.iRecBufSize*sizeof(BYTE));
  317.       except
  318.         on EOutOfMemory do
  319.         begin
  320.           DbiCloseCursor(hCur);
  321.           BDE_Error(4);
  322.           exit;
  323.         end;
  324.       end;
  325.       {$I-}
  326.       DbiGetNextRecord(hCur, dbiNOLOCK, RecBuf, nil);
  327.       DbiGetField(hCur, 2, RecBuf, (@nmTemp), bisblank);
  328.       DbiGetField(hCur, 5, RecBuf, (@lnTemp), bisblank);
  329.       DbiGetField(hCur, 3, RecBuf, (@typTemp),bisblank);
  330.       if typTemp = 7 then DbiGetField(hCur, 4, RecBuf, (@subtypTemp),bisblank)
  331.       else if typTemp = 3 then DbiGetField(hCur, 4, RecBuf, (@subtypTemp),bisblank)
  332.       else subtypTemp := 0;
  333.       MultPageDlg.Outline1.add(i,StrPas(nmtemp));
  334.       Str(lnTemp,lnTempStr);
  335.       MultPageDlg.Outline2.add(i,lnTempStr);
  336.       MultPageDlg.Outline3.add(i,TranslateFieldType(typTemp, subtypTemp));
  337.       if recbuf <> nil then freemem(recbuf, tblprops.iRecBufSize * sizeof(BYTE));
  338.       end;
  339.       DbiCloseCursor(hCur);
  340.     end
  341.     else
  342.     begin
  343.       DbiCloseCursor(hCur);
  344.       BDE_Error(2);
  345.     end;
  346.   end
  347.   else BDE_Error(2);
  348. end;
  349.  
  350. function TranslateFieldType(fldtyp,subtyp:integer) : string;
  351. const
  352.    TypIndex : array [0..16] of string [10] =
  353.           ('Unknown', 'String', 'Date', 'Blob', 'Boolean', 'ShortInt',
  354.            'LongInt', 'Float', 'Decimal', 'Bytes', 'Time', 'DateTime',
  355.            'UShortInt', 'ULongInt', 'Float80', 'VarBytes', 'LockInfo');
  356.    SubTypIndex: array [21..28] of string [10] =
  357.              ('Currency', 'Memo', 'Binary', 'Fmt.Memo', 'OLE',
  358.               'Graphic', 'dBase OLE', 'User Typed');
  359.  
  360. begin
  361.   if subtyp > 0 then TranslateFieldType := SubTypIndex[subtyp]
  362.   else TranslateFieldType := TypIndex[fldtyp];
  363. end;
  364.  
  365.  
  366. procedure TTblInfoDlg.BDE_Error(code:integer);
  367. var
  368.   str1 : string;
  369.   NTstr1 : array[0..28] of char;
  370. begin
  371.   case code of
  372.     1:
  373.     begin
  374.       StrPCopy(NTStr1,'Table Info Incomplete');
  375.       tbldate := 'Unknown';
  376.       tblsize := 'Unknown';
  377.     end;
  378.     2: StrPCopy(NTStr1,'Unable to Open Field List');
  379.     3: StrPCopy(NTStr1,'Unable to Open Index List');
  380.     4: StrPCopy(NTStr1,'Not Enough Memory');
  381.   else exit;
  382.   end;
  383.   messagebox(0,NTStr1,'Database Error',mb_ok or mb_iconstop);
  384. end;
  385.  
  386. procedure Register;
  387. begin
  388.   RegisterComponents('Data Controls', [TTblInfoDlg]);
  389. end;
  390.  
  391. end.
  392.