⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbidorestructure.html

📁 Delphi API应用手册
💻 HTML
📖 第 1 页 / 共 3 页
字号:

  // If the tables are dBASE, then put the sequence number in the descriptor...
  if MasterProps.szTableType = szDBASE then
  begin
    Check(DbiGetIndexSeqNo(Master.Handle, MIndex.szName, MIndex.szTagName, 0, MNo));
    Check(DbiGetIndexSeqNo(Detail.Handle, DIndex.szName, DIndex.szTagName, 0, DNo));
    // Put the detail field index in the array...
    RInt.aiThisTabFld[0] := DNo;
    // Put the master field index in the array...
    RInt.aiOthTabFld[0] := MNo;
  end;

  try
    Master.Close;
    Detail.Close;
    Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, FALSE));
  finally
    Master.Open;
    Detail.Open;
  end;
end;</PRE>

<HR SIZE=2 NOSHADE>

<H3>Example 10: Add a field to a Paradox or dBASE table.</H3>
This example will add a field to the end of an existing table.<BR>
NOTE: You must fill in all options in the ChangeRec with 0 or '' if the option is
not used in the restructure.  FillChar can be used to do this: Fillchar(MyChangeRec,
sizeof(MyChangeRec), 0);<BR>
This example uses the following input:<BR>
<I>AddField(Table1, MyChangeRec);</I><BR>
ChangeRec is defind as follows:
<PRE>ChangeRec = packed record
    szName: DBINAME;
    iType: word;
    iSubType: word;
    iLength: word;
    iPrecision: byte;
  end;</PRE>
The function is defined as follows:
<PRE>procedure AddField(Table: TTable; NewField: ChangeRec);
var
  Props: CURProps;
  hDb: hDBIDb;
  TableDesc: CRTblDesc;
  pFlds: pFLDDesc;
  pOp: pCROpType;
  B: byte;

begin
  // Make sure the table is open exclusively so we can get the db handle...
  if Table.Active = False then
    raise EDatabaseError.Create('Table must be opened to restructure');
  if Table.Exclusive = False then
    raise EDatabaseError.Create('Table must be opened exclusively to restructure');

  // Get the table properties to determine table type...
  Check(DbiSetProp(hDBIObj(Table.Handle), curxltMODE, integer(xltNONE)));
  Check(DbiGetCursorProps(Table.Handle, Props));
  pFlds := AllocMem((Table.FieldCount + 1) * sizeof(FLDDesc));
  FillChar(pFlds^, (Table.FieldCount + 1) * sizeof(FLDDesc), 0);
  Check(DbiGetFieldDescs(Table.handle, pFlds));

  for B := 1 to Table.FieldCount do begin
    pFlds^.iFldNum := B;
    Inc(pFlds, 1);
  end;
  try
    StrCopy(pFlds^.szName, NewField.szName);
    pFlds^.iFldType := NewField.iType;
    pFlds^.iSubType := NewField.iSubType;
    pFlds^.iUnits1  := NewField.iLength;
    pFlds^.iUnits2  := NewField.iPrecision;
    pFlds^.iFldNum  := Table.FieldCount + 1;
  finally
    Dec(pFlds, Table.FieldCount);
  end;

  pOp := AllocMem((Table.FieldCount + 1) * sizeof(CROpType));
  Inc(pOp, Table.FieldCount);
  pOp^ := crADD;
  Dec(pOp, Table.FieldCount);

  // Blank out the structure...
  FillChar(TableDesc, sizeof(TableDesc), 0);
  //  Get the database handle from the table's cursor handle...
  Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb)));
  // Put the table name in the table descriptor...
  StrPCopy(TableDesc.szTblName, Table.TableName);
  // Put the table type in the table descriptor...
  StrPCopy(TableDesc.szTblType, Props.szTableType);
  // Close the table so the restructure can complete...
  TableDesc.iFldCount := Table.FieldCount + 1;
  Tabledesc.pfldDesc := pFlds;
  TableDesc.pecrFldOp := pOp;
  Table.Close;
  // Call DbiDoRestructure...
  try
    Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, FALSE));
  finally
    FreeMem(pFlds);
    FreeMem(pOp);
    Table.Open;
  end;
end;</PRE>

<HR SIZE=2 NOSHADE>

<H3>Example 11: Remove a referential integrity constraint from a dBASE or Paradox table.</H3>
This example uses the following input:<BR>
<I>RemoveRI(DetailRITable, 0, 'RIName');</I> OR <BR>
<I>RemoveRI(DetailRITable, 1, nil);</I><BR>

The function is defined as follows:
<PRE>procedure RemoveRI(Detail: TTable; SeqNo: word; RIName: PChar);
var
  Props: CURProps;
  B: byte;
  pRI: pRINTDesc;
  pOP: pcrOpType;
  TblDesc: CRTblDesc;
  hDb: hDBIDb;
  RIFound: Boolean;

begin
  pRI := nil; pOP := nil;
  if (SeqNo = 0) and (RIName = nil) then
    raise EDatabaseError.Create('SeqNo must be positive or a RI name must be specified');

  if (SeqNo <> 0) and (RIName <> nil) then
    raise EDatabaseError.Create('You must either use the SeqNo or RIName to specify" + 
            " the constraint; not both');

  { Make sure that the table is opened and is exclusive... }
  if (Detail.Active = False) or (Detail.Exclusive = False) then
    raise EDatabaseError.Create('Table must be opened and in exclusive mode');

  Check(DbiGetCursorProps(Detail.Handle, Props));
  if Props.iRefIntChecks < 1 then
    raise EDatabaseError.Create('There are no RI constraints on this table');

  { Make sure the table is either Paradox or dBASE... }
  if (Props.szTableType <> szPARADOX) and (Props.szTableType <> szDBASE) then
    raise EDatabaseError.Create('Field altering can only occur on Paradox' +
                ' or dBASE tables');

  pRI := AllocMem(Props.iRefIntChecks * sizeof(RINTDesc));
  pOp := AllocMem(Props.iRefIntChecks * sizeof(CROpType));
  try
    { Fill the RInt descriptor will all RI information... }
    for B := 1 to Props.iRefIntChecks do
    begin
      Check(DbiGetRIntDesc(Detail.Handle, B, pRI));
      { If the SeqNo was given use it to determine the constraint to remove... }
      if SeqNo > 0 then
      begin
        if pRI^.iRintNum = SeqNo then
          pOp^ := crDROP;
      end;
      { If the name was given use it to determine the constraint to remove... }
      if RIName <> nil then
      begin
        if StrComp(pRI^.szRintName, RIName) = 0 then
          pOp^ := crDROP;
      end;
      Inc(pOP);
      Inc(pRI);
    end;
    Dec(pRI, Props.iRefIntChecks);
    Dec(pOP, Props.iRefIntChecks);

    { Make sure the constraint was found... }
    RIFound := False;
    for B := 0 to Props.iRefIntChecks - 1 do
    begin
      Inc(pOP, B);
      if pOP^ <> crNOOP then
        RIFound := True;
      Dec(pOP, B);
    end;
    if RIFound = False then
      raise EDatabaseError.Create('Constraint was not found');

    { Get the database handle... }
    Check(DbiGetObjFromObj(hDBIObj(Detail.Handle), objDATABASE, hDBIObj(hDb)));
    { Close the table }
    Detail.Close;
    { Setup the Table descriptor for DbiDoRestructure }
    FillChar(TblDesc, SizeOf(TblDesc), #0);
    StrPCopy(TblDesc.szTblName, Detail.Tablename);
    StrCopy(TblDesc.szTblType, Props.szTableType);
    TblDesc.iRintCount := Props.iRefIntchecks;
    TblDesc.printDesc := pRI;
    TblDesc.pecrRintOp := pOP;
    { Restructure the table }
    Check(DbiDoRestructure(hDb, 1, @TblDesc, nil, nil, nil, FALSE));
  finally
    if pRI <> nil then
      FreeMem(pRI);
    if pOP <> nil then
      FreeMem(pOP);
  end;
end;</PRE>

<HR SIZE=2 NOSHADE>

<H3>Example 12: Change the amount of fields in the primary index (Paradox and dBASE 7 only).</H3>
This example uses the following input:<BR>
<I>ChangePIdx(Table1, 2);</I> This input will take an existing primary index and change it to apply 
to the first 2 fields in the table.<BR>
<B>NOTE:</B> If you define an index that is more restrictive on the table's data (usually by removing
fields from the index) all records that violate the new primary index will be removed from the table.
In other words data will be lost!<BR>

The function is defined as follows:
<PRE>procedure ChangePIdx(Table: TTable; Fields: word);
var
  Props: CURProps;
  hDb: hDBIDb;
  TableDesc: CRTblDesc;
  Index: IDXDesc;
  OP: CROpType;
  W: word;

begin
  // Make sure that at least one field is specified...
  if Fields < 1 then
    raise EDatabaseError.Create('Field count must be greater than 0');
  // Make sure the table is open exclusively so we can get the db handle...
  if Table.Active = False then
    raise EDatabaseError.Create('Table must be opened to change the index');
  if Table.Exclusive = False then
    raise EDatabaseError.Create('Table must be opened exclusively to change the index');

  // Get the table properties to determine table type...
  Check(DbiGetCursorProps(Table.Handle, Props));
  // Make sure that enough fields exist to create the index...
  if Props.iFields < Fields then
    raise EDatabaseError.Create('Cannot specify more fields in an index than existing fields');
  // If the table is a Paradox table, you must call DbiDoRestructure...
  if (Props.szTableType <> szPARADOX) and (Props.szTableType <> szDBASE) then
    raise EDatabaseError.Create('Table must be of type Paradox or dBASE 7');


  // Blank out the structure...
  FillChar(TableDesc, sizeof(TableDesc), 0);
  //  Get the database handle from the table's cursor handle...
  Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb)));
  // Put the table name in the table descriptor...
  StrPCopy(TableDesc.szTblName, Table.TableName);
  // Put the table type in the table descriptor...
  StrPCopy(TableDesc.szTblType, Props.szTableType);
  // Fill in index descriptor...
  FillChar(Index, sizeof(IDXDesc), 0);
  Index.bPrimary:= TRUE;
  Index.bUnique:= TRUE;
  Index.bMaintained:= TRUE;
  Index.iFldsInKey:= Fields;
  for W := 1 to Fields do
    Index.aiKeyFld[W - 1]:= W;
  // Modify an existing index only...
  OP := crMODIFY;
  // Only one index to change...
  TableDesc.iIdxCount := 1;
  TableDesc.pecrIdxOp := @OP;
  TableDesc.pidxDesc := @Index;
  // Close the table so the restructure can complete...
  Table.Close;
  try
    // Call DbiDoRestructure...
    Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, FALSE));
  finally
    Table.Open;
  end;
end;</PRE>

<HR SIZE=2 NOSHADE>

<A HREF="/devsupport/bde/bdeapiex/index.html">Back to BDE API Reference Page</A>

</TD></TR>

<TR><TD ALIGN="LEFT" VALIGN="TOP" COLSPAN="1"><font size=2>


<HR SIZE="2" NOSHADE>

<FONT SIZE = 2><i><b>DISCLAIMER:</b> 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.</FONT></i></B>


</dl>
</td>

</TR>

<TR>

<!-- THIS IS THE BOTTOM COPYRIGHT & UPDATED MESSAGE -->

<TD ALIGN="CENTER" VALIGN="TOP" COLSPAN="3"><FONT SIZE="2"><A HREF="/copyright.html">Trademarks &amp; Copyright</A> &#169; 1998 Borland International, Inc.

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM OLD BROWSERS

// THIS WILL WRITE OUT THE LAST MODIFIED DATE
// YOU DO NOT NEED TO CHANGE ANYTHING HERE

function makeArray(arraySize) {
     this.length = arraySize
     return this
}

monthNames = new makeArray(12)
monthNames[1] = "January"
monthNames[2] = "February"
monthNames[3] = "March"
monthNames[4] = "April"
monthNames[5] = "May"
monthNames[6] = "June"
monthNames[7] = "July"
monthNames[8] = "August"
monthNames[9] = "September"
monthNames[10] = "October"
monthNames[11] = "November"
monthNames[12] = "December"

updated = new Date(document.lastModified)
theMonth = monthNames[updated.getMonth() + 1]
theDate = updated.getDate()
theYear = updated.getYear() + 1900

document.write("Last modified on " + theDate +  "-" + theMonth + "-" + theYear + ".")
// END HIDING -->
</SCRIPT></FONT></TD>
</TR>

</TABLE>
</CENTER>

</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -