delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
497 Users Online NOW
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



Loremo - the 1.5 liter car coming in 2009




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 (0)


Methods for Cutting and Pasting SQL Select Order by ClauseGo to Alex Wijoyo's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
A TParser usage example
Product:
Delphi 3.x (or higher)
Category:
Database-SQL
Skill Level:
Scoring:
Last Update:
06/30/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet query sql order-by tparser
Times Scored:
3
Visits:
3414
Uploader: Alex Wijoyo
Company: Excellont
Reference: N/A
 
Question/Problem/Abstract:
Most of data access components don't have method for sorting in memory records. So the only way for sorting records is add an order by clause to the query and reopen it. These method make this task easier to do.
Answer:



Here are the methods:
procedure CutOrderByClause(ASQL:TStrings);
var
  aParser:TParser;
  aStream:TMemoryStream;
  tokenPos,orderByClausePos:integer;
begin
  orderByClausePos:=-1;
  aParser:=nil;
  aStream:=TMemoryStream.Create;
  try
    ASQL.SaveToStream(aStream);
    aStream.Position:=0;
    aParser:=TParser.Create(aStream);
//find previous order by clause position
    while aParser.Token<>toEof do
    begin
      tokenPos:=aParser.SourcePos;
      if lowercase(aParser.TokenString)='order' then
      begin
        orderByClausePos:=tokenPos;
        aParser.NextToken;
        if (aParser.Token=toEof)or(lowercase(aParser.TokenString)<>'by') then
          raise Exception.Create('Incorrect order by clause!')
        else
        begin
          aParser.NextToken;
          while aParser.Token<>toEof do
          begin
            if lowercase(aParser.TokenString)='select' then
              raise Exception.Create('Unable to cut order by clause!');
            aParser.NextToken;
          end;
          break;
        end;
      end;
      aParser.NextToken;
    end;
//cut previous order by clause
    if orderByClausePos>=0 then
    begin
      aStream.Size:=orderByClausePos;
      aStream.Position:=0;
      ASQL.LoadFromStream(aStream);
    end;
  finally
    aParser.Free;
    aStream.Free;
  end;
end;

procedure SortSQL(ASQL:TStrings;OrderByClauseArr:array of string);
var
  i,n,u:integer;
  orderByClause:string;
begin
  CutOrderByClause(ASQL);
  if length(orderByClauseArr)>0 then
  begin
//construct new order by clause
    u:=high(orderByClauseArr);
    orderByClause:='order by ';
    n:=length(orderByClause);
    for i:=0 to u do
    begin
      if length(orderByClause)>n then
        orderByClause:=orderByClause+', ';
      orderByClause:=orderByClause+orderByClauseArr[i];
    end;
//append new order by clause into SQL
    if length(orderByClause)>n then
      ASQL.Append(orderByClause);
  end;
end;

This event handler shows you how to use them:
procedure TfrmSortSQL.btnChangeSQLClick(Sender: TObject);
var
  orderByClauseArr:array of string;
  i,u:integer;
begin
  u:=memSortFields.Lines.Count;
  SetLength(orderByClauseArr,u);
  dec(u);
  for i:=0 to u do
    orderByClauseArr[i]:=memSortFields.Lines[i];
  if u>=0 then
    SortSQL(memSQL.Lines,orderByClauseArr)
  else
    CutOrderByClause(memSQL.Lines);
end;

Or you can simply use it like below:
  SortSQL(memSQL.Lines,['deptno desc','empno','1',inttostr(aField.FieldNo)]);

OK that's all. I hope this article can help your database project development.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Shkolnik
 
   














 







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