[View Borland Home Page][View Product List][Search This Web Site][View Available Downloads][Join Borland Online][Enter Discussion Area][Send Email To Webmaster]

BDE API Examples (DbiAddIndex)

Creates an index on an existing table.

Add an index to a Paradox 4.0 or 5.0 version table:

This example is included with Delphi in the BDE32.HLP file.

Add an index to a Paradox 4.0 or 5.0 version table:

This example is included with Delphi in the BDE32.HLP file.

Add an index to a Paradox 7.0 version table:

This example is included with Delphi in the BDE32.HLP file.

Add an index to a dBASE for Windows version table:

This example is included with Delphi in the BDE32.HLP file.

Add an expression index to a dBASE for Windows version table:

This example is included with Delphi in the BDE32.HLP file.

Example 1: Create a maintained, case insensitive index on the first and second fields on a Paradox tables. The first field of the index is descending and the second is ascending.

This example uses the following input:
fDbiAddIndex6(Table1)
Procedure fDbiAddIndex6(Tbl: TTable);
var
  NewIndex: IDXDesc;
begin
  FillChar(NewIndex, SizeOf(NewIndex), 0);
  if Tbl.Exclusive = False then
    raise EDatabaseError.Create
    ('TTable.Exclusive must be set to true in order to add an index to the table');
  NewIndex.szName:= 'NewIndex2';
  NewIndex.iIndexId:= 0;
  NewIndex.bPrimary:= FALSE;
  NewIndex.bUnique:= FALSE;
  NewIndex.bDescending:= TRUE;
  NewIndex.bMaintained:= TRUE;
  NewIndex.bSubset:= FALSE;
  NewIndex.bExpIdx:= FALSE;
  NewIndex.iFldsInKey:= 2;
  NewIndex.aiKeyFld[0]:= 1;
  NewIndex.aiKeyFld[1]:=2;
  NewIndex.abDescending[0]:=TRUE;
  NewIndex.abDescending[1]:=FALSE;
  NewIndex.bCaseInsensitive:= TRUE;
  Check(DbiAddIndex(Tbl.dbhandle, Tbl.handle, PChar(Tbl.TableName),
          szParadox, NewIndex, nil));
end;

Example 2: Create a maintained, expression index with a subset filter.

This example uses the following input:
AddExpFilter(Table1, 'OUTRISK', 'STR(OUTLOOK)+RISK', 'RISK=''HIGH''');
NOTE: You can use this input with the master.dbf table in the delphi/demos/data directory.
procedure AddExpFilter(Table: TTable; TagName, Expression, Filter: string);
var
  NewIndex: IDXDesc;
  
begin
  if TagName = '' then
    raise EDatabaseError.Create('A tag name must be supplied to create an expression index');
  if Expression = '' then
    raise EDatabaseError.Create('A expression be supplied to create an index');
  if Table.exclusive = False then
    raise EDatabaseError.Create('Table: ' + Table.Tablename + ' must be opened exclusively ' +
                   'to create an index');
  FillChar(NewIndex, sizeof(NewIndex), 0);
  StrPCopy(NewIndex.szTagName, TagName);
  NewIndex.bPrimary:= False;
  NewIndex.bUnique:= False;
  NewIndex.bDescending:= False;
  NewIndex.bMaintained:= True;
  if Filter = '' then
    NewIndex.bSubset:= False
  else
    NewIndex.bSubset:= True;
  NewIndex.bExpIdx:= True;
  StrPCopy(NewIndex.szKeyExp, Expression);
  StrPCopy(NewIndex.szKeyCond, Filter);
  NewIndex.bCaseInsensitive:= False;
  Check(DbiAddIndex(Table.dbhandle, Table.handle, PChar(Table.TableName),
          szParadox, NewIndex, nil));
end;

Example 3: Create a multi-field secondary index on a Paradox table.

This example uses the following input:
AddMultiFieldIndex(Table2, 'Multi', False, [Table2.Fields[0], Table2.Fields[2], Table2.Fields[1]]);
procedure AddMultiFieldIndex(Table: TTable; IndexName: string; Unique: boolean;
              const Fields: array of TField);
var
  NewIndex: IDXDesc;
  b: byte;
  Props: CURProps;

begin
  // Make sure an index name is supplied...
  if IndexName = '' then
    raise EDatabaseError.Create('An index name must be supplied');
  // Make sure the table is opened exclusively...
  if Table.exclusive = False then
    raise EDatabaseError.Create('Table: ' + Table.Tablename +
       ' must be opened exclusively to create an index');
  Check(DbiGetCursorProps(Table.Handle, Props));
  // Make sure the table is of type PARADOX...
  if StrComp(Props.szTableType, szPARADOX) <> 0 then
    raise EDatabaseError.Create('Table must be of type PARADOX');

  FillChar(NewIndex, sizeof(NewIndex), 0);
  StrPCopy(NewIndex.szName, IndexName);
  NewIndex.bUnique := Unique;
  NewIndex.bMaintained := True;
  // Set the field mappings for the multi field index...
  NewIndex.iFldsInKey := sizeof(Fields) div sizeof(TField);
  for b := 0 to NewIndex.iFldsInkey - 1 do
    NewIndex.aiKeyFld[b] := Fields[b].Index + 1;
  // Create the index...
  Check(DbiAddIndex(Table.dbhandle, Table.handle, nil, nil, NewIndex, nil));
end;

Back to BDE API Reference Page


DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.
Trademarks & Copyright © 1998 Borland International, Inc.