Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (1)


Abstraction of Runtime Queries from CodeGo to Dave Murray's websiteComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
How to remove SQL statements from your code so that the queries can be changed without recompiling your program.
Product:
Delphi all versions
Category:
DB-General
Skill Level:
Scoring:
Last Update:
12/23/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet SQL TQuery Abstraction Decoupling
Times Scored:
7
Visits:
4779
Uploader: Dave Murray
Company: Conspiracy Software
Reference: Irongut's Delphi Pages
Component Download: http://www.paranoia.clara.net/downloads/abstraction_of_runtime_queries.zip
 
Question/Problem/Abstract:
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 #2887, 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.

An updated version of this article can be found on Irongut's Delphi Pages.

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

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


Now for the code:

uses IniFiles;

const
  queryFileName = 'queries.ini';

procedure ExecuteQuery(myQuery : TQuery; const queryID : integer; const myDB : string = '');
{performs query getting SQL statement at runtime from inifile}
begin
  if not(myDB = '') then myQuery.DatabaseName := myDB;
  try
    myQuery.Close;
    myQuery.SQL.Clear;
    myQuery.SQL.Add(GetQuery(queryID));
    myQuery.Open;
  except
    on E : Exception do MessageDlg(E.message, mtError, [mbYes], 0);
    end; {try..except}
end; {procedure ExecuteQuery}


function GetQuery(const qID : integer) : string;
{reads SQL statement from inifile}
var
  DirPath : string;
  queryIni : TIniFile;
begin
  DirPath := ExtractFilePath(ParamStr(0));
  queryIni := TIniFile.Create(DirPath + queryFileName);
  try
    if not(queryIni.ValueExists('QUERIES', IntToStr(qID))) then
      raise Exception.Create('ERROR: Query ID not found in file!')
    else result := queryIni.ReadString('QUERIES', IntToStr(qID), '');
  finally
    queryIni.Free;
    end; {try..finally}
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:

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





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Sugestions... for better use
    Siki2004 Nenkov (Dec 12 2003 4:58PM)

You don't need:
SQL.Clear;
SQL.Add(...)
Use:
SQL.Text := YourSQLStatement;
And you can do procedure
ReadAndExec(Query,database,ID_SQL)
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
S. Kucherov
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)