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
Auxiliary TQuery used with queries built at run time 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
19-Jun-03
Category
Database-SQL
Language
Delphi 3.x
Views
146
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Fernando Martins 

Auxiliary queries built at run time make you copy-paste a lot, replicating your 
code. Why not keep it to a minimum, making it easy to read and mantain ?

Answer:

Do you have an auxiliary TQuery on your form that you use to build dynamic queries, 
like 

'SELECT Count(id) FROM Clients' 

and a bit latter you use the same TQuery to 

'SELECT Count(Phone_numbers) FROM Clients WHERE area = '1'' 

and latter on you use it again to 

'SELECT Count(area) FROM Contacts' 

and so on... 

If you have an auxiliar TQuery to run all these queries, you probably have a lot of 
similar code replicated in your application to load the query string, prepare the 
query, run the query and finally close the query. 
Since replication is not a good thing when it comes to maintenance, why not 
abstract the queries from the code so that you just have to pass the TQuery object, 
the query string and, optionally, the database, if you use different databases. 

Here's sometinh I've been using for a while that creates that abstraction layer: 
1   
2   procedure Execute(Q: TQuery; S: string; DBName = '');
3   begin
4     with Q do
5     begin
6       if DBName <> '' then
7         DatabaseName := DBName;
8       try
9         Close;
10      finally
11        SQL.Clear;
12      end;
13  
14      SQL.Add(S);
15      try
16        Prepare;
17        while not (Prepared) do
18          ;
19        Open;
20      finally
21        ;
22      end;
23    end;
24  end;


Using this procedure, you can reduce the amount of code and maintenance effort to a 
minimum, since you can prepare and open the queries just by using:
25  
26  Execute(MyTQuery, 'SELECT Count(id) FROM Clients');
27  
28  Execute(MyTQuery, 'SELECT Count(Phone_numbers) FROM Clients WHERE area = ' 1 '');
29  
30  Execute(MyTQuery, 'SELECT Count(area) FROM Contacts', 'Contacts_database');
31  
32  Execute(MyTQuery, 'SELECT Count(ZIP) FROM Zip_Codes', 'Address_database');
33  
34  Execute(MyTQuery, 'SELECT Names FROM Vip_Clients', 'clients_database');

After calling the Execute procedure, you are able to read the result from the 
TQuery as usual. 
This procedure simplifies the process of checking if the object is opened, close it if necessary, prepare the query for execution, release the resources to other processes while not ready and finally run the query. 

			
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