📄 dbidorestructure.html
字号:
<HTML>
<HEAD>
<TITLE>BDE API Examples (DbiDoRestructurex)</TITLE>
<META NAME="KEYWORDS" CONTENT="BDE, Borland">
<META NAME="DESCRIPTION" CONTENT="">
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#000000" ALINK="#999999" VLINK="#666666">
<CENTER>
<TABLE CELLPADDING="4" CELLSPACING="4" BORDER="0" WIDTH="600">
<TR>
<TD ALIGN="CENTER" VALIGN="TOP" COLSPAN="1"><NOBR><A HREF="/"><IMG SRC="/images/bolus.gif" ALT="[View Borland Home Page]" BORDER="0"></A><A HREF="/products.html"><IMG SRC="/images/products.gif" ALT="[View Product List]" BORDER="0"></A><A HREF="/searchsite/"><IMG SRC="/images/search.gif" ALT="[Search This Web Site]" BORDER="0"></A><A HREF="/download.html"><IMG SRC="/images/downloads.gif" ALT="[View Available Downloads]" BORDER="0"></A><A HREF="/membership.html"><IMG SRC="/images/membership.gif" ALT="[Join Borland Online]" BORDER="0"></A><A HREF="/newsgroups/"><IMG SRC="/images/newsgroups.gif" ALT="[Enter Discussion Area]" BORDER="0"></A><A HREF="/feedback/"><IMG SRC="/images/feedback.gif" ALT="[Send Email To Webmaster]" BORDER="0"></A></NOBR></TD>
</TR>
<TR>
<TR><TD ALIGN="LEFT" VALIGN="TOP" COLSPAN="1"><font size=2>
<h2>BDE API Examples (DbiDoRestructure)</h2>
Changes the properties of a table.
<hr size=2 noshade>
<H3>Example 1: Add a validity check to the specified field.</H3>
The field must be a longint, and the TTable must be open. This example uses
the following input:<BR>
<I>fDbiDoRestructure(Table4, Table4.Fields[0], @Min, @Max, nil, True)</I> NOTE: This input works for the EMPLOYEE.DB table.
<PRE>procedure fDbiDoRestructure(Tbl: TTable; Field: TField; MinVal, MaxVal,
DefVal: pLongint; Required: Bool);
var
hDb: hDbiDb;
TblDesc: CRTblDesc;
VChk: pVChkDesc;
Dir: String;
NumVChks: Word;
OpType: CROpType;
begin
NumVChks := 0;
SetLength(Dir, dbiMaxNameLen + 1);
Check(DbiGetDirectory(Tbl.DBHandle, False, PChar(Dir)));
SetLength(Dir, StrLen(PChar(Dir)));
VChk := AllocMem(sizeof(VChkDesc));
try
FillChar(TblDesc, sizeof(CRTblDesc), #0);
VChk.iFldNum := Field.Index + 1;
Tbl.DisableControls;
Tbl.Close;
Check(DbiOpenDatabase(nil, nil, dbiReadWrite, dbiOpenExcl,
nil, 0, nil, nil, hDb));
Check(DbiSetDirectory(hDb, PChar(Dir)));
with VChk^ do
begin
bRequired := Required;
if MinVal <> nil then
begin
Inc(NumVChks);
bHasMinVal := True;
move(MinVal^, aMinVal, sizeof(MinVal^));
end
else
bHasMinVal := False;
if MaxVal <> nil then
begin
Inc(NumVChks);
bHasMaxVal := True;
move(MaxVal^, aMaxVal, sizeof(MaxVal^));
end
else
bHasMaxVal := False;
if DefVal <> nil then
begin
Inc(NumVChks);
bHasDefVal := True;
move(DefVal^, aDefVal, sizeof(DefVal^));
end
else
bHasDefVal := False;
end;
TblDesc.iValChkCount := NumVChks;
TblDesc.pVChkDesc := VChk;
OpType := crADD;
TblDesc.pecrValChkOp := @OpType;
StrPCopy(TblDesc.szTblName, Tbl.TableName);
StrCopy(TblDesc.szTblType, szParadox);
Check(DbiDoRestructure(hDb, 1, @TblDesc, nil, nil, nil, False));
finally
Check(DbiCloseDatabase(hDb));
FreeMem(VChk, sizeof(VChkDesc));
Tbl.EnableControls;
Tbl.Open;
end;
end;</PRE>
<!-- SF -->
<HR SIZE=2 NOSHADE>
<A NAME="Ex2"></A>
<H3>Example 2: Pack a Paradox or dBASE table.</H3>
This example will pack a Paradox or dBASE table therfore removing already
deleted rows in a table. This function will also regenerate all out-of-date
indexes (maintained indexes) This example uses the following input:<BR>
<I>PackTable(Table1)</I>
<PRE>// Pack a Paradox or dBASE table
// The table must be opened execlusively before calling this function...
procedure PackTable(Table: TTable);
var
Props: CURProps;
hDb: hDBIDb;
TableDesc: CRTblDesc;
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 pack');
if Table.Exclusive = False then
raise EDatabaseError.Create('Table must be opened exclusively to pack');
// Get the table properties to determine table type...
Check(DbiGetCursorProps(Table.Handle, Props));
// If the table is a Paradox table, you must call DbiDoRestructure...
if Props.szTableType = szPARADOX then
begin
// 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);
// Set the Pack option in the table descriptor to TRUE...
TableDesc.bPack := True;
// Close the table so the restructure can complete...
Table.Close;
// Call DbiDoRestructure...
Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, FALSE));
end
else
// If the table is a dBASE table, simply call DbiPackTable...
if Props.szTableType = szDBASE then
Check(DbiPackTable(Table.DBHandle, Table.Handle, nil, szDBASE, TRUE))
else
// Pack only works on PAradox or dBASE; nothing else...
raise EDatabaseError.Create('Table must be either of Paradox or dBASE ' +
'type to pack');
Table.Open;
end;</PRE>
<!-- SF -->
<HR SIZE=2 NOSHADE>
<H3>Example 3: Alter a field in a Paradox or dBASE table.</H3>
This example will alter an existing field in a Paradox or dBASE 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>ChangeField(Table1, Table1.FieldByName('FOO'), 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 ChangeField(Table: TTable; Field: TField; Rec: ChangeRec);
var
Props: CURProps;
hDb: hDBIDb;
TableDesc: CRTblDesc;
pFields: pFLDDesc;
pOp: pCROpType;
B: byte;
begin
// Initialize the pointers...
pFields := nil; pOp := nil;
// 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');
Check(DbiSetProp(hDBIObj(Table.Handle), curxltMODE, integer(xltNONE)));
// Get the table properties to determine table type...
Check(DbiGetCursorProps(Table.Handle, Props));
// 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');
// Allocate memory for the field descriptor...
pFields := AllocMem(Table.FieldCount * sizeof(FLDDesc));
// Allocate memory for the operation descriptor...
pOp := AllocMem(Table.FieldCount * sizeof(CROpType));
try
// Set the pointer to the index in the operation descriptor to put
// crMODIFY (This means a modification to the record is going to happen)...
Inc(pOp, Field.Index);
pOp^ := crMODIFY;
Dec(pOp, Field.Index);
// Fill the field descriptor with the existing field information...
Check(DbiGetFieldDescs(Table.Handle, pFields));
// Set the pointer to the index in the field descriptor to make the
// midifications to the field
Inc(pFields, Field.Index);
// If the szName portion of the ChangeRec has something in it, change it...
if Length(Rec.szName) > 0 then
pFields^.szName := Rec.szName;
// If the iType portion of the ChangeRec has something in it, change it...
if Rec.iType > 0 then
pFields^.iFldType := Rec.iType;
// If the iSubType portion of the ChangeRec has something in it, change it...
if Rec.iSubType > 0 then
pFields^.iSubType := Rec.iSubType;
// If the iLength portion of the ChangeRec has something in it, change it...
if Rec.iLength > 0 then
pFields^.iUnits1 := Rec.iLength;
// If the iPrecision portion of the ChangeRec has something in it, change it...
if Rec.iPrecision > 0 then
pFields^.iUnits2 := Rec.iPrecision;
Dec(pFields, Field.Index);
for B := 1 to Table.FieldCount do begin
pFields^.iFldNum := B;
Inc(pFields, 1);
end;
Dec(pFields, 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);
// The following three lines are necessary when doing any field restructure
// operations on a table...
// Set the field count for the table
TableDesc.iFldCount := Table.FieldCount;
// Link the operation descriptor to the table descriptor...
TableDesc.pecrFldOp := pOp;
// Link the field descriptor to the table descriptor...
TableDesc.pFldDesc := pFields;
// Close the table so the restructure can complete...
Table.Close;
// Call DbiDoRestructure...
Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, FALSE));
finally
if pFields <> nil then
FreeMem(pFields);
if pOp <> nil then
FreeMem(pOp);
end;
end;</PRE>
<!-- SF -->
<HR SIZE=2 NOSHADE>
<H3>Example 4: Add a master password to a Paradox table.</H3>
This example uses the following input:<BR>
<I>AddMasterPassword(Table1, 'MyNewPassword')</I>
<PRE>procedure AddMasterPassword(Table: TTable; pswd: string);
const
RESTRUCTURE_TRUE = WordBool(1);
var
TblDesc: CRTblDesc;
hDb: hDBIDb;
begin
{ Make sure that the table is opened and is exclusive }
if (Table.Active = False) or (Table.Exclusive = False) then
raise EDatabaseError.Create('Table must be opened in exclusive mode to add passwords');
{ Initialize the table descriptor }
FillChar(TblDesc, SizeOf(CRTblDesc), 0);
with TblDesc do
begin
{ Place the table name in descriptor }
StrPCopy(szTblName, Table.TableName);
{ Place the table type in descriptor }
StrCopy(szTblType, szPARADOX);
{ Master Password, Password }
StrPCopy(szPassword, pswd);
{ Set bProtected to True }
bProtected := RESTRUCTURE_TRUE;
end;
{ Get the database handle from the cursor handle }
Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb)));
{ Close the table }
Table.Close;
{ Add the master password to the Paradox table }
Check(DbiDoRestructure(hDb, 1, @TblDesc, nil, nil, nil, FALSE));
{ Add the new password to the session }
Session.AddPassword(pswd);
{ Re-Open the table }
Table.Open;
end;</PRE>
<!-- SF -->
<HR SIZE=2 NOSHADE>
<H3>Example 5: Add an auxilary password to a Paradox table.</H3>
This example uses the following input:<BR>
<I>AddAuxPassword(Table1, 'MyMstrPswd', 'MyNewAuxPswd', pvrINSDEL)</I><BR>
In order to add an auxilary password, the master password MUST also be added at
the same time; even if the master password is already on the table! A privelage
type of prvINSDEL gives full rights without the ability to restructure or delete.
<PRE>procedure AddAuxPassword(Table: TTable; mstrpswd, pswd: string; rights: PRVType);
const
RESTRUCTURE_TRUE = WordBool(1);
var
{ Specific information about the table structure, indexes, etc. }
TblDesc: CRTblDesc;
{ Security descriptor }
SDesc: SECDesc;
{ Uses as a handle to the database }
hDb: hDBIDb;
{ crAdd }
crType: CROpType;
W: word;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -