Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
Abstraction of Runtime Queries from Code Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
23-Jun-03
Category
DB-General
Language
Delphi 2.x
Views
150
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Dave Murray

Programming methodology books such as 'The Pragmatic Programmer' by Andrew Hunt and 
David Thomas teach the principles of decoupling, abstraction and non-repetition. 
This article shows how to achieve some of these goals when codeing queries whose 
SQL statements are set at runtime.

Answer:

In article "Auxiliary TQuery used with queries built at run time", Fernando Martins 
suggested a way to avoid code replication when using TQuery objects with SQL 
statements set at runtime. Here I show how to take this a step further and remove 
the SQL from your code so that the queries can be changed without recompiling your 
program. 

My queries are stored within an inifile in the program's directory. This provides 
us with a simple file format which is easy to use both in and out of Delphi. My 
inifile has the following syntax: 

[QUERIES] 
1=SELECT CAPITAL FROM COUNTRY WHERE NAME = 'Argentina' 
2=SELECT NAME FROM COUNTRY WHERE CONTINENT = 'South America' 

To perform the query call the ExecuteQuery procedure which in turn will call the 
GetQuery function. ExecuteQuery must be passed the following paramaters: 

myQuery : TQuery  - TQuery component used to perform query 
queryID : integer - ID number in inifile for the SQL satement 
myDB : string     - optional Database Name 

1   ExecuteQuery(qryRuntime, 1, );
2   ExecuteQuery(qryRuntime, 2, 'DBDEMOS');


Now for the code: 

3   uses IniFiles;
4   
5   const
6     queryFileName = 'queries.ini';
7   
8   procedure ExecuteQuery(myQuery: TQuery; const queryID: integer; const myDB: string =
9     '');
10  {performs query getting SQL statement at runtime from inifile}
11  begin
12    if not (myDB = '') then
13      myQuery.DatabaseName := myDB;
14    try
15      myQuery.Close;
16      myQuery.SQL.Clear;
17      myQuery.SQL.Add(GetQuery(queryID));
18      myQuery.Open;
19    except
20      on E: Exception do
21        MessageDlg(E.message, mtError, [mbYes], 0);
22    end; {try..except}
23  end; {procedure ExecuteQuery}
24  
25  function GetQuery(const qID: integer): string;
26  {reads SQL statement from inifile}
27  var
28    DirPath: string;
29    queryIni: TIniFile;
30  begin
31    DirPath := ExtractFilePath(ParamStr(0));
32    queryIni := TIniFile.Create(DirPath + queryFileName);
33    try
34      if not (queryIni.ValueExists('QUERIES', IntToStr(qID))) then
35        raise Exception.Create('ERROR: Query ID not found in file!')
36      else
37        result := queryIni.ReadString('QUERIES', IntToStr(qID), '');
38    finally
39      queryIni.Free;
40    end; {try..finally}
41  end; {function GetQuery}


Finally, to avoid having to look up long lists of query IDs when programming, 
incorporate them into a unit of constant values so that you can use code like the 
following: 

42  ExecuteQuery(qryRuntime, GET_CAPITAL, );
43  ExecuteQuery(qryRuntime, GET_COUNTRIES, 'DBDEMOS');


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC