CREATE OR REPLACE PACKAGE PKG_QUERY_PAGEDATA IS
-- 原创 : 三界.
-- CREATED : 2011-12-06 09:00
-- EDIT : 2012-01-11 10:30/2014-09-09 15:00/
--留档备查.
TYPE PageDataCursor IS REF CURSOR;
PROCEDURE TABLEPAGEDATA(QueryTableName Varchar2,
WhereClause Varchar2,Tohoma; font-size:14px; line-height:22.4px">
OrderFields Varchar2,Tohoma; font-size:14px; line-height:22.4px">
PageIndex int default 1,Tohoma; font-size:14px; line-height:22.4px">
PageSize int default 1000,Tohoma; font-size:14px; line-height:22.4px">
TotalRows out number,Tohoma; font-size:14px; line-height:22.4px">
PageData_out out PageDataCursor);
Function PAGEDATA(ModuleNo Varchar2,Tohoma; font-size:14px; line-height:22.4px">
WhereClause Varchar2,Tohoma; font-size:14px; line-height:22.4px">
OrderFields Varchar2,Tohoma; font-size:14px; line-height:22.4px">
PageIndex int default 1,Tohoma; font-size:14px; line-height:22.4px">
PageSize int default 1000,Tohoma; font-size:14px; line-height:22.4px">
PageData_out out PageDataCursor) RETURN NUMBER;
END;
CREATE OR REPLACE PACKAGE BODY PKG_QUERY_PAGEDATA IS
PROCEDURE TABLEPAGEDATA(QueryTableName Varchar2,
WhereClause Varchar2,
OrderFields Varchar2,
PageIndex int default 1,
PageSize int default 1000,
TotalRows out number,
PageData_out out PageDataCursor) AS
AStringsql varchar2(8000);
BEGIN
--WhereClause:查询过滤条件.
--OrderFields:排序字段,允许包含关键字DESC/ASC.
--PageSize:每页记录数.默认1000.
--TotalRows:输出所有页总记录行数.
if (WhereClause is not null) then
AStringsql := 'SELECT COUNT(*) FROM ' || QueryTableName || ' WHERE ' || WhereClause;
else
AStringsql := 'SELECT COUNT(*) FROM ' || QueryTableName;
end if;
execute immediate AStringsql into TotalRows;
if (WhereClause is not null) then
AStringsql := 'SELECT * FROM ' || QueryTableName || ' WHERE ' || WhereClause;
else
AStringsql := 'SELECT COUNT(*) FROM ' || QueryTableName;
end if;
if (OrderFields is not null) then
if REGEXP_LIKE(OrderFields,'^(\s*ORDER\s+BY\s)','min') THEN
else
end if;
end if;
AStringsql := 'SELECT * FROM (SELECT T.*,ROWNUM AS ROWNO FROM (' || AStringsql || ') T WHERE ROWNUM < ' ||
to_char(PageSize * PageIndex + 1) || ') TT WHERE ROWNO > ' || to_char(PageSize * (PageIndex - 1));
open PageData_out for AStringsql;
END;
Function PAGEDATA(ModuleNo Varchar2,
WhereClause Varchar2,
OrderFields Varchar2,
PageIndex int default 1,
PageSize int default 1000,
PageData_out out PageDataCursor) RETURN NUMBER AS
AStringsqlFind varchar2(8000);
AStringsqlCount varchar2(8000);
APattern varchar2(1000);
TotalRows number;
BEGIN
--ModuleNo:模块编号.
--WhereClause:查询过滤条件.允许空值.
--OrderFields:排序字段,也允许包含关键字DESC/ASC.
--PageSize:每页记录数.默认每页1000行.
--TotalRows:输出所有页总记录行数.
--预定义的统计记录数的sql Example:SELECT COUNT(*) FROM USERS A JOIN COMPANY B ON (A.CORP_ID=B.CORP_ID) WHERE (1=1)
--预定义的分页查询的sql Example:SELECT A.*,B.CORP_NAME FROM USERS A JOIN COMPANY B ON (A.CORP_ID=B.CORP_ID) WHERE (1=1) ORDER BY USER_NAME DESC
FROM A_sqlMASTER
WHERE MODULE_NO = ModuleNo
AND ROWNUM = 1;
--换替where (1 = 1)字符串为参数过滤条件.
if (WhereClause is not null) then
AStringsqlCount := REGEXP_REPLACE(AStringsqlCount,'\(\d\s*=\s*\d\)','(' || WhereClause || ')',1,'min');
end if;
execute immediate AStringsqlCount
into TotalRows;
--替换where (1 = 1)字符串为参数过滤条件.
if (WhereClause is not null) then
end if;
--替换最后位置的ORDER BY语句段.
if (OrderFields is not null) then
APattern := '\s+ORDER\s+BY\s+(\w+\.)?\w+(\s+DESC|\s+ASC)?(,\s+(\w+\.)?\w+(\s+DESC|\s+ASC)?)?';
if REGEXP_LIKE(OrderFields,'min') then
REGEXP_COUNT(AStringsqlFind,'min'),'min');
else
end if;
end if;
--注意对rownum别名的使用,内层嵌套用rownum,外层嵌套用别名ROWNO.
AStringsqlFind := 'SELECT * FROM (SELECT T.*,ROWNUM AS ROWNO FROM (' || AStringsqlFind || ') T WHERE ROWNUM < ' ||
to_char(PageSize * PageIndex + 1) || ') TT WHERE ROWNO > ' ||
to_char(PageSize * (PageIndex - 1));
open PageData_out for AStringsqlFind;
RETURN TotalRows;
END;
END;