Renamed to TmpTbl16.pas 6/7/1998 Paul Rice to avoid conflict with 32-bit version.
This is an TempTable example. Free for anyone to use, modify and do
whatever else you wish.
This version is quite an improvement, mainly in the area of indexes.
Both primary and secondary indexes should work just like on a regular
TTable.
TempTables are supposedly also in-memory tables and provide all of the
functionality of regular tables. The problem with these is that in DB.PAS
there is a line in TDataSet.InternalOpen that sets CanModify to False if
the table is temporary. Why - I don't know, but if you have the VCL source
(which I highly recommend if you're serious about Delphi programming) then
you can just comment that part out. Go into DB.PAS, change the line in
TDataSet.InternalOpen that reads
FCanModify := (CursorProps.eOpenMode = dbiReadWrite) and
not CursorProps.bTempTable;
to
FCanModify := (CursorProps.eOpenMode = dbiReadWrite); { and
not CursorProps.bTempTable; }
Just like all things free this unit comes with no guarantees. I cannot be
responsible for any damage this code may cause.
Thanks to Steve Garland <72700.2407@compuserve.com> for his help. He
created his own variation of an in-memory table component and I used it
to get started.
If you have comments - please contact me at INTERNET:grisha@mira.com
Happy hacking!
Gregory Trubetskoy
P.S. Here is what I had to do to overcome certain problems with Delphi's
implementation of BDE. For a BDE novice like me, it wasn't nearly as simple
as it would seem from reading this!
First, when you call DbiCreateTempTable it creates a table with a different
name. BDE uses whatever file name you give it as the beginning and appends it
with a number, e.g. XXX.DB will become XXXn.DB where n is some number that
gets incremented every time you call DbiCreateTempTable.
So the first thing I did was to retrieve the new name of the table by using
DbiGetProp and assign it to TableName. (See TTempTable.CreateTable)
Another problem was with the indexes. When you create a primary index, you
must switch to it with DbiSwitchToIndex to make it active. DbiSwitchToIndex
will also return you a new cursor handle to use for the new index. Delphi
makes sure that any value you assign to IndexValue is not the same as the old
value, or it will do nothing. (See TTable.SetIndex in DBTABLES.PAS) Therefore,
when you create a primary index where none existed before and IndexName is '',
Delphi VCL does NOT call DbiSwitchToIndex to activate the new index, even if
you do IndexName := ''. For some reason it works for regular tables (I guess
Borland programmers know what they are doing, or I missed something), but
for TempTables, I would get a GPF shortly after using the old cursor. So I had
to call TDataSet.SwitchToIndex manually (See TTempTable.AddIndex).
Last problem I had was with either Delphi VLC or BDE itself confused with
what the file name for the table was. I would get 'Table does not exist' or
'Operation not applicable' errors when adding indexes. I didn't study the
problem in detail, but found it can easily be solved by DbiMakePermanent
function (See TTempTable.Create). Description of MakePermanent sais that it
doesn't save the table until convenient or the cursor is closed, so no
benefit of TTempTable being fast because it's in RAM should be lost. I simply
delete the table in the destructor to reverse the effect of DbiMakePermanent.
Set the Permanent property to True, to prevent the table from being deleted.
Speaking of performance benefits - my test showed that writing and indexing
5000 small records takes 5.77 seconds with TTable and 5.40 seconds with
TTempTable - so I'm not sure where the performance benefit is.
I do think TempTable is highly useful, mainly because it's a neat way to
manage temporary tables without having to do all the boring stuff, like
creating temporary filenames, if not for the supposed performance improvement.
Happy Hacking again, and make sure to drop me a thank you note if you're using
this!
My e-mail is (without brackets, of course)
TTempTable -