通用分页查询表的存储过程

通用分页查询表的存储过程

  • 存储过程:
DELIMITER $$

USE `smarttour`$$

DROP PROCEDURE IF EXISTS `prc_page_result`$$

CREATE DEFINER=`root`@`%` PROCEDURE `prc_page_result`(
IN currpage      INT,
IN COLUMNS       VARCHAR(500),
IN tablename     VARCHAR(500),
IN sCondition    VARCHAR(500),
IN order_field   VARCHAR(100),
IN asc_field     INT,
IN primary_field VARCHAR(100),
IN pagesize      INT)
BEGIN
        DECLARE sTemp  VARCHAR(1000);
    DECLARE sSql   VARCHAR(4000);
    DECLARE sOrder VARCHAR(1000);
    
    IF asc_field = 1 THEN
        SET sOrder = CONCAT(' order by ', order_field, ' desc ');
        SET sTemp  = '<(select min';
    ELSE
        SET sOrder = CONCAT(' order by ', order_field, ' asc ');
        SET sTemp  = '>(select max';
    END IF;
    
    IF currpage = 1 THEN
        IF sCondition <> '' THEN
            SET sSql = CONCAT('select ', COLUMNS, ' from ', tablename, ' where ');
            SET sSql = CONCAT(sSql, sCondition, sOrder, ' limit ?');
        ELSE
            SET sSql = CONCAT('select ', COLUMNS, ' from ', tablename, sOrder, ' limit ?');
        END IF;
    ELSE
        IF sCondition <> '' THEN
            SET sSql = CONCAT('select ', COLUMNS, ' from ', tablename);
            SET sSql = CONCAT(sSql, ' where ', sCondition, ' and ', primary_field, sTemp);
            SET sSql = CONCAT(sSql, '(', primary_field, ')', ' from (select ');
            SET sSql = CONCAT(sSql, ' ', primary_field, ' from ', tablename, sOrder);
            SET sSql = CONCAT(sSql, ' limit ', (currpage-1)*pagesize, ') as tabtemp)', sOrder);
            SET sSql = CONCAT(sSql, ' limit ?');
        ELSE
            SET sSql = CONCAT('select ', COLUMNS, ' from ', tablename);
            SET sSql = CONCAT(sSql, ' where ', primary_field, sTemp);
            SET sSql = CONCAT(sSql, '(', primary_field, ')', ' from (select ');
            SET sSql = CONCAT(sSql, ' ', primary_field, ' from ', tablename, sOrder);
            SET sSql = CONCAT(sSql, ' limit ', (currpage-1)*pagesize, ') as tabtemp)', sOrder);
            SET sSql = CONCAT(sSql, ' limit ?');
        END IF;
    END IF;
    SET @iPageSize = pagesize;
    SET @sQuery = sSql;
    PREPARE stmt FROM @sQuery;
    EXECUTE stmt USING @iPageSize;
    END$$

DELIMITER ;
  • lua

ngx.ctx.mt = "json";

ngx.req.read_body()

local args, posterr = ngx.req.get_post_args()
if not args then
   ngx.say("failed to get post args: ", posterr)
   return
end

local currpage = ngx.unescape_uri(args.currpage or 1)
local quoted_currpage = ngx.quote_sql_str(currpage)

local columns = ngx.unescape_uri(args.columns or '*')
local quoted_columns = ngx.quote_sql_str(columns)

local tablename = ngx.unescape_uri(args.tablename)
local quoted_tablename = ngx.quote_sql_str(tablename)

local sCondition = ngx.unescape_uri(args.sCondition)
local quoted_sCondition = ngx.quote_sql_str(sCondition)

local orderField = ngx.unescape_uri(args.orderField)
local quoted_orderField = ngx.quote_sql_str(orderField)

local ascField = ngx.unescape_uri(args.ascField )
local quoted_ascField = ngx.quote_sql_str(ascField)

local primaryField = ngx.unescape_uri(args.primaryField )
local quoted_primaryField = ngx.quote_sql_str(primaryField)

local pagesize = ngx.unescape_uri(args.pagesize or 20)
local quoted_pagesize = ngx.quote_sql_str(pagesize)

local tbl = {}
table.insert(tbl,quoted_currpage) --当前页
table.insert(tbl,quoted_columns)--查询的字段
table.insert(tbl,quoted_tablename)--表名字
table.insert(tbl,quoted_sCondition)--where条件语句
table.insert(tbl,quoted_orderField)--排序的字段
table.insert(tbl,quoted_ascField)--1-递减 2-递增
table.insert(tbl,quoted_primaryField)--主键
table.insert(tbl,quoted_pagesize)--每一页显示的大小

local sql = "call prc_page_result(" .. table.concat(tbl,",") .. ")"

local mysql = require "resty.mysql"
local db,error = mysql:new()

if not db then
    ngx.say("Failed to instantiate mysqldb", error)
    return
end

db:set_timeout(1000)

local ok,err,errcode,sqlstate = db:connect{
    host = "127.0.0.1",
    port = 3306,
    database = "smarttour",
    user = "root",
    password = "*******",
    max_packet_size = 1024 * 1024
}

if not ok then
    ngx.say("failed to connect: ", err, ": ", errcode, " ", sqlstate)
    return
end

local res, reserr, reserrcode, ressqlstate = db:query(sql)
if not res then
    ngx.say("bad result: ", reserr, ": ", reserrcode, ": ", ressqlstate, ".")
    return
end

ngx.say( cjson.encode(res))
db:set_keepalive(10000, 100)

你可能感兴趣的:(通用分页查询表的存储过程)