I think (and hope) that I've found the magic combination to allow the
database connection on my development computer to contain the name of the
database on that computer, but allow that to be changed without error
on my laptop (see
here
for previous discussion). The dpr code and the initial code from the
datamodule are as follows
var db: integer; begin db:= TAboutBox.Execute; Application.Initialize; Application.CreateForm(Tdm, dm); dm.OpenDatabase (db); Application.CreateForm(TMainForm, MainForm); Application.CreateForm(TDimmerForm, DimmerForm); Application.Run; end. ... procedure TDm.DataModuleCreate(Sender: TObject); begin // prevent the compile time definition causing a problem with sqlconnection1 do begin close; params.values['database']:= ''; end; end; procedure TDm.OpenDatabase (index: word); var pname, dir: string; begin case index of 0: begin pname:= 'Perceptions'; fileprefix:= pname; end; 1: begin pname:= 'PPP'; fileprefix:= 'Programming pitfalls in Priority'; end; end; with TRegIniFile.create (regpath) do begin dir:= ReadString ('firebird', pname, ''); free end; with sqlconnection1 do begin close; params.values['database']:= dir; loginprompt:= false; end;
Initially the database 'number' is obtained from the 'about' dialog. The datamodule starts up with the database value defined, but the ModuleCreate code clears it. Then the OpenDatabase procedure is called with the index; this sets some local variables then obtains from the registry the name of the database required.
So far, so good.
I was thinking about the function that returns a new instance for the temp
table. This was as simple as could be
Function TDm.NewInstance: word; begin qNewInstance.Open; // select max (instance) from temp result:= qNewInstance.fields[0].asinteger + 1; qNewInstance.close; end;
It occurred to me, however, that there could be a call to NewInstance before any values with the new instance had managed to get written to the table, in which case the second call to NewInstance would be given the same instance number. I had to 'protect' this value by immediately entering a tuple with this instance number by means of a new insert query, qSentinel. Not satisfied with this, I thought it best if the entire function was protected by a mutex - I borrowed the code from the linked web site.
Quite pleased with myself, I went looking for examples where the database
has to be changed at run time. I haven't found anything suitable yet, but I
did find something very interesting: the sqlconnection has a method called
'ExecuteDirect' that receives a string and executes it. As it happens, I was
thinking of something like this yesterday afternoon, as that's how Priority
works, but I didn't think it possible in Delphi. Well, it is, and here is
the final code for the NewInstance function. I'm sure that I'll find many
places where I can use 'ExecuteDirect' instead of a predefined query (note
that this requires no parameters - I have to 'inject' a value for
'instance').
Function TDm.NewInstance: word; var s: string; begin mutex.Lock; qNewInstance.Open; result:= qNewInstance.fields[0].asinteger + 1; qNewInstance.close; s:= 'insert into temp (instance, id) values (' + inttostr (result) + ', -1)'; sqlconnection1.executedirect (s); mutex.Unlock; end;
I feel at times like Lawrence Waterhouse in 'Cryptonomicon' in chapter one,
'Barrens', where he is out with the fictional Alan Turing and Rudy von
'something or other', when Turing says "Shut up about Leibniz for a moment,
Rudy, because look here: You—Rudy—and I are on a train, as it were, sitting
in the dining car, having a nice conversation, and that train is being
pulled along at a terrific clip by certain locomotives named The Bertrand Russell and Riemann
and Euler and others. And our friend Lawrence is running alongside the
train, trying to keep up with us—it’s not that we’re smarter than he is,
necessarily, but that he’s a farmer who didn’t get a ticket." In other
words, I learned the basics of Delphi from a few books and from magazine
articles, but after that, I was on my own and so probably there are many
topics that I don't know about that could ease my work.
Internal links
[1] 1844
Title | Tags | ||
---|---|---|---|
1352 | Overcoming the 'leading zero' problem in Excel | Programming, Delphi, Excel, Office automation |
No comments:
Post a Comment