📄 unitpagecut.pas
字号:
unit UnitPageCut;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, ComCtrls;
type
TUnitPageCut = class
private
{ Private declarations }
public
function GetMidPageIndex(pageCount:integer):integer;
function IsAscending(orderType:string):Boolean;
function GetSortType(ascending:Boolean):string;
function GetPageCount(recordCount:integer;pageSize:integer):integer;
function getSQLStr(pageIndex:integer;recordcount:integer;
pagesize:integer;queryFields:string;tablename:string;primaryKey:string;
ascending:Boolean;condition:string): string;
end;
implementation
function TUnitPageCut.getSQLStr(pageIndex:integer;recordcount:integer;
pagesize:integer;queryFields:string;tablename:string;primaryKey:string;
ascending:Boolean;condition:string): string;
var
orderType:string;
middleIndex:integer;
firstIndex:integer;
lastIndex :integer;
pageCount:integer;
sqlstr:string;
begin
Result :='';
orderType:=GetSortType(ascending);
pageCount:=GetPageCount(recordcount,pagesize);
middleIndex:=GetMidPageIndex(pageCount);
firstIndex := 1;
lastIndex := pageCount;
if pageIndex <= firstIndex then
begin
Result:='select TOP '+IntToStr(PageSize)+' '+queryFields+' FROM '
+TableName+' WHERE '+Condition+' ORDER BY '+ PrimaryKey
+' '+orderType;
exit;
end;
if (pageIndex > firstIndex) and (pageIndex <= middleIndex) then
begin
sqlstr:='SELECT TOP '+IntToStr(pageSize)+' '+queryFields+' FROM '+tableName
+' WHERE '+primaryKey;
if ascending then
begin
sqlstr:=sqlstr+' > ('+' SELECT MAX(';
end else
begin
sqlstr:=sqlstr+' < ('+' SELECT MIN(';
end;
sqlstr:=sqlstr+primaryKey+') FROM ( SELECT TOP '+
IntToStr(pageSize*(pageIndex-1))+' '+primaryKey+' FROM '+tableName;
if condition <> '' then
sqlstr:=sqlstr+' WHERE '+condition+' ORDER BY '+primaryKey+' '
+GetSortType(ascending)+'))';
if condition <> '' then
sqlstr:=sqlstr+' AND '+condition+' ORDER BY '+primaryKey+' '+
GetSortType(ascending);
Result:=sqlstr;
exit;
end;
if (pageIndex > middleIndex) and (pageIndex < lastIndex) then
begin
sqlstr:='SELECT * FROM ( SELECT TOP '+IntToStr(pageSize)+' '+queryFields+' FROM '+tableName
+' WHERE '+primaryKey;
if ascending then
begin
sqlstr:=sqlstr+' < ('+' SELECT MIN(';
end else
begin
sqlstr:=sqlstr+' > ('+' SELECT MAX(';
end;
sqlstr:=sqlstr+primaryKey+') FROM ( SELECT TOP '+
IntToStr(recordCount-pageSize*pageIndex)+' '+primaryKey+' FROM '+tableName;
if condition <> '' then
sqlstr:=sqlstr+' WHERE '+condition+' ORDER BY '+primaryKey+' ';
if ascending then
begin
sqlstr:=sqlstr+' DESC)TableA)';
if condition <> '' then
sqlstr:=sqlstr+' and '+condition+' ORDER BY '+primaryKey+' DESC'+
') TableB ORDER BY '+PrimaryKey+' ASC';
end else
begin
sqlstr:=sqlstr+' ASC))';
if condition <> '' then
sqlstr:=sqlstr+' and '+condition+' ORDER BY '+primaryKey+' ASC'+
') TableB ORDER BY '+PrimaryKey+' DESC';
end;
Result:=sqlstr;
exit;
end;
if pageIndex >=lastIndex then
begin
sqlstr:='select '+queryFields+' from (select top '+
IntToStr(recordcount-(pageCount-1)*pagesize)+' '+queryFields+' from '+
tablename+' ';
if condition<>'' then
sqlstr:=sqlstr+'where '+condition+' '+'order by '+primaryKey+' ';
if ascending then
begin
sqlstr:=sqlstr+'DESC)'+' order by '+primaryKey+' ';
end else
begin
sqlstr:=sqlstr+'ASC)'+' order by '+primaryKey+' ';
end;
if ascending then
sqlstr:=sqlstr+GetSortType(ascending);
Result:=sqlstr;
exit;
end;
end;
//---GetPageCount
function TUnitPageCut.GetPageCount(recordCount:integer;pageSize:integer):integer;
begin
if recordCount mod pageSize>0 then
begin
Result:=(recordCount div pageSize)+1;
end else
begin
Result:=recordCount div pageSize;
end;
end;
//GetMidPageIndex
function TUnitPageCut.GetMidPageIndex(pageCount:integer):integer;
begin
Result:=(pageCount div 2)+1;
end;
//----GetSortType
function TUnitPageCut.GetSortType(ascending:Boolean):string;
begin
Result:='DESC';
if ascending =true then
Result:='ASC';
end;
//---- IsAscending
function TUnitPageCut.IsAscending(orderType:string):Boolean;
begin
Result:=true;
if uppercase(orderType) ='DESC' then
Result:=false ;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -