You've already forked lazarus-ccr
new sort engine for TSQLQuery - thx BugMaker from FreePascal.ru
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1841 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
@ -59,10 +59,12 @@
|
|||||||
<Filename Value="RxDBGridDemo.lpr"/>
|
<Filename Value="RxDBGridDemo.lpr"/>
|
||||||
<IsPartOfProject Value="True"/>
|
<IsPartOfProject Value="True"/>
|
||||||
<UnitName Value="RxDBGridDemo"/>
|
<UnitName Value="RxDBGridDemo"/>
|
||||||
|
<EditorIndex Value="1"/>
|
||||||
<WindowIndex Value="0"/>
|
<WindowIndex Value="0"/>
|
||||||
<TopLine Value="1"/>
|
<TopLine Value="1"/>
|
||||||
<CursorPos X="5" Y="17"/>
|
<CursorPos X="5" Y="17"/>
|
||||||
<UsageCount Value="53"/>
|
<UsageCount Value="53"/>
|
||||||
|
<Loaded Value="True"/>
|
||||||
</Unit0>
|
</Unit0>
|
||||||
<Unit1>
|
<Unit1>
|
||||||
<Filename Value="rxdbgridmainunit.pas"/>
|
<Filename Value="rxdbgridmainunit.pas"/>
|
||||||
@ -70,9 +72,10 @@
|
|||||||
<ComponentName Value="RxDBGridMainForm"/>
|
<ComponentName Value="RxDBGridMainForm"/>
|
||||||
<ResourceBaseClass Value="Form"/>
|
<ResourceBaseClass Value="Form"/>
|
||||||
<UnitName Value="RxDBGridMainUnit"/>
|
<UnitName Value="RxDBGridMainUnit"/>
|
||||||
|
<IsVisibleTab Value="True"/>
|
||||||
<EditorIndex Value="0"/>
|
<EditorIndex Value="0"/>
|
||||||
<WindowIndex Value="0"/>
|
<WindowIndex Value="0"/>
|
||||||
<TopLine Value="1"/>
|
<TopLine Value="87"/>
|
||||||
<CursorPos X="11" Y="9"/>
|
<CursorPos X="11" Y="9"/>
|
||||||
<UsageCount Value="53"/>
|
<UsageCount Value="53"/>
|
||||||
<Loaded Value="True"/>
|
<Loaded Value="True"/>
|
||||||
@ -393,8 +396,7 @@
|
|||||||
<Unit45>
|
<Unit45>
|
||||||
<Filename Value="/usr/local/share/lazarus/components/rxnew/rxdbgrid.pas"/>
|
<Filename Value="/usr/local/share/lazarus/components/rxnew/rxdbgrid.pas"/>
|
||||||
<UnitName Value="rxdbgrid"/>
|
<UnitName Value="rxdbgrid"/>
|
||||||
<IsVisibleTab Value="True"/>
|
<EditorIndex Value="2"/>
|
||||||
<EditorIndex Value="1"/>
|
|
||||||
<WindowIndex Value="0"/>
|
<WindowIndex Value="0"/>
|
||||||
<TopLine Value="2537"/>
|
<TopLine Value="2537"/>
|
||||||
<CursorPos X="3" Y="2542"/>
|
<CursorPos X="3" Y="2542"/>
|
||||||
@ -522,7 +524,7 @@
|
|||||||
<Unit59>
|
<Unit59>
|
||||||
<Filename Value="/usr/local/share/lazarus/lcl/grids.pas"/>
|
<Filename Value="/usr/local/share/lazarus/lcl/grids.pas"/>
|
||||||
<UnitName Value="Grids"/>
|
<UnitName Value="Grids"/>
|
||||||
<EditorIndex Value="2"/>
|
<EditorIndex Value="3"/>
|
||||||
<WindowIndex Value="0"/>
|
<WindowIndex Value="0"/>
|
||||||
<TopLine Value="3866"/>
|
<TopLine Value="3866"/>
|
||||||
<CursorPos X="1" Y="3891"/>
|
<CursorPos X="1" Y="3891"/>
|
||||||
@ -555,7 +557,7 @@
|
|||||||
</Unit62>
|
</Unit62>
|
||||||
<Unit63>
|
<Unit63>
|
||||||
<Filename Value="/usr/local/share/lazarus/lcl/include/canvas.inc"/>
|
<Filename Value="/usr/local/share/lazarus/lcl/include/canvas.inc"/>
|
||||||
<EditorIndex Value="3"/>
|
<EditorIndex Value="4"/>
|
||||||
<WindowIndex Value="0"/>
|
<WindowIndex Value="0"/>
|
||||||
<TopLine Value="1116"/>
|
<TopLine Value="1116"/>
|
||||||
<CursorPos X="1" Y="1132"/>
|
<CursorPos X="1" Y="1132"/>
|
||||||
|
105
components/rx/exsortsql.pas
Normal file
105
components/rx/exsortsql.pas
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
{ RxDBGrid sort engine module for FBDataSet
|
||||||
|
|
||||||
|
Copyright (C) 2011 BugMaker from freepascal.ru
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or modify it
|
||||||
|
under the terms of the GNU Library General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or (at your
|
||||||
|
option) any later version with the following modification:
|
||||||
|
|
||||||
|
As a special exception, the copyright holders of this library give you
|
||||||
|
permission to link this library with independent modules to produce an
|
||||||
|
executable, regardless of the license terms of these independent modules,and
|
||||||
|
to copy and distribute the resulting executable under terms of your choice,
|
||||||
|
provided that you also meet, for each linked independent module, the terms
|
||||||
|
and conditions of the license of that module. An independent module is a
|
||||||
|
module which is not derived from or based on this library. If you modify
|
||||||
|
this library, you may extend this exception to your version of the library,
|
||||||
|
but you are not obligated to do so. If you do not wish to do so, delete this
|
||||||
|
exception statement from your version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
|
||||||
|
for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Library General Public License
|
||||||
|
along with this library; if not, write to the Free Software Foundation,
|
||||||
|
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
}
|
||||||
|
|
||||||
|
unit exsortsql;
|
||||||
|
|
||||||
|
{$mode objfpc}{$H+}
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
uses
|
||||||
|
Classes, SysUtils, DB, RxDBGrid;
|
||||||
|
|
||||||
|
type
|
||||||
|
|
||||||
|
{ TSQLQuerySortEngine }
|
||||||
|
|
||||||
|
TSQLQuerySortEngine = class(TRxDBGridSortEngine)
|
||||||
|
public
|
||||||
|
procedure Sort(Field:TField; ADataSet:TDataSet; Asc:boolean; SortOptions:TRxSortEngineOptions);override;
|
||||||
|
end;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
uses SQLDB, synRegExpr, strUtils;
|
||||||
|
|
||||||
|
|
||||||
|
procedure TSQLQuerySortEngine.Sort(Field: TField; ADataSet: TDataSet;
|
||||||
|
Asc: boolean; SortOptions: TRxSortEngineOptions);
|
||||||
|
var cmd:string;
|
||||||
|
strLen:Integer;
|
||||||
|
RegExpr: TRegExpr;
|
||||||
|
Mask:String;
|
||||||
|
s:String;
|
||||||
|
begin
|
||||||
|
if Assigned(ADataSet) and ADataset.Active then begin
|
||||||
|
cmd:=(ADataSet as TSQLQuery).SQL.Text;
|
||||||
|
strlen:=length(cmd);
|
||||||
|
//Регулярное выражение позволяет найти уже имеющуюся конструкцию ORDER BY,
|
||||||
|
//если она написана одной строкой, и между словами не понапихали комментариев :)
|
||||||
|
//Работоспособные примеры:
|
||||||
|
//ORDER BY FIELD1, FIELD2 DESC, FIELD100500
|
||||||
|
//oRdeR bY fielD1 , FiElD2,FieLD100500 DESC
|
||||||
|
//Неработоспособный:
|
||||||
|
//ORDER BY FIELD1,
|
||||||
|
//FIELD2,
|
||||||
|
//FIELD100500
|
||||||
|
mask:='(?i)(^|\s)\s*order\s+by\s+\S+\.?\S*(\s+desc)?\s*(,\s*\S+\.?\S*(\s+desc)?(^|s*))*';
|
||||||
|
with TRegExpr.Create do begin
|
||||||
|
|
||||||
|
Expression := mask;
|
||||||
|
if Exec(cmd) then begin
|
||||||
|
s:=LeftStr(cmd,MatchPos[0]-1)
|
||||||
|
+slinebreak+'order by '
|
||||||
|
+Field.FieldName
|
||||||
|
+' ';
|
||||||
|
if not asc then s:=s+'DESC';
|
||||||
|
s:=s+slineBreak
|
||||||
|
+RightStr(cmd, strlen-MatchPos[0]-MatchLen[0]+1);
|
||||||
|
end
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
s:=cmd+slinebreak+'order by '
|
||||||
|
+Field.FieldName
|
||||||
|
+' ';
|
||||||
|
if not asc then s:=s+'DESC';
|
||||||
|
s:=s+slineBreak
|
||||||
|
end;
|
||||||
|
ADataSet.Active:=False;
|
||||||
|
(ADataSet as TSQLQuery).SQL.Text:=s;
|
||||||
|
ADataSet.Active:=True;
|
||||||
|
|
||||||
|
Free;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
initialization
|
||||||
|
RegisterRxDBGridSortEngine(TSQLQuerySortEngine, TSQLQuery);
|
||||||
|
end.
|
@ -325,6 +325,7 @@ type
|
|||||||
{ TRxDBGrid }
|
{ TRxDBGrid }
|
||||||
TRxDBGrid = class(TCustomDBGrid)
|
TRxDBGrid = class(TCustomDBGrid)
|
||||||
private
|
private
|
||||||
|
FSortingNow:Boolean;
|
||||||
FInProcessCalc: integer;
|
FInProcessCalc: integer;
|
||||||
FAllowedOperations: TRxDBGridAllowedOperations;
|
FAllowedOperations: TRxDBGridAllowedOperations;
|
||||||
FFooterColor: TColor;
|
FFooterColor: TColor;
|
||||||
@ -1932,8 +1933,10 @@ begin
|
|||||||
if SelectedRows.Count > 0 then
|
if SelectedRows.Count > 0 then
|
||||||
SelectedRows.Clear;
|
SelectedRows.Clear;
|
||||||
end;
|
end;
|
||||||
FSortField := nil;
|
if not FSortingNow then begin
|
||||||
FSortOrder := smNone;
|
FSortField := nil;
|
||||||
|
FSortOrder := smNone;
|
||||||
|
end;
|
||||||
|
|
||||||
F_SortListField.Clear;
|
F_SortListField.Clear;
|
||||||
if not (csDestroying in ComponentState) and not (csDesigning in ComponentState) then
|
if not (csDestroying in ComponentState) and not (csDesigning in ComponentState) then
|
||||||
@ -2080,8 +2083,10 @@ begin
|
|||||||
FSortField := AField;
|
FSortField := AField;
|
||||||
FSortOrder := smUp;
|
FSortOrder := smUp;
|
||||||
end;
|
end;
|
||||||
|
FSortingNow:=true;
|
||||||
FSortEngine.Sort(FSortField, DataSource.DataSet, FSortOrder =
|
FSortEngine.Sort(FSortField, DataSource.DataSet, FSortOrder =
|
||||||
smUp, SortEngineOptions);
|
smUp, SortEngineOptions);
|
||||||
|
FSortingNow:=false;
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
HeaderClick(True, ACol);
|
HeaderClick(True, ACol);
|
||||||
|
Reference in New Issue
Block a user