@@ -9,7 +9,7 @@ use std::sync::Arc;
9
9
10
10
/// make count sql remove `limit`
11
11
/// make select sql append limit ${page_no},${page_size}
12
- /// notice:
12
+ /// notice:
13
13
/// ```log
14
14
/// sql must be starts with 'select ' and ' from '
15
15
/// this PageIntercept only support sqlite,mysql,mssql,postgres...
@@ -48,6 +48,14 @@ impl PageIntercept {
48
48
count_ids : Arc :: new ( SyncHashMap :: new ( ) ) ,
49
49
}
50
50
}
51
+
52
+ //driver_type=['postgres','pg','mssql','mysql','sqlite'...],but sql default is use '?'
53
+ pub fn count_param_count ( & self , _driver_type : & str , sql : & str ) ->usize {
54
+ sql. replace ( "$" , "?" )
55
+ . replace ( "@p" , "?" )
56
+ . matches ( '?' )
57
+ . count ( )
58
+ }
51
59
}
52
60
#[ async_trait]
53
61
impl Intercept for PageIntercept {
@@ -56,7 +64,7 @@ impl Intercept for PageIntercept {
56
64
_task_id : i64 ,
57
65
executor : & dyn Executor ,
58
66
sql : & mut String ,
59
- _args : & mut Vec < Value > ,
67
+ args : & mut Vec < Value > ,
60
68
result : ResultType < & mut Result < ExecResult , Error > , & mut Result < Vec < Value > , Error > > ,
61
69
) ->Result < Option < bool > , Error > {
62
70
if let ResultType :: Exec ( _) = result{
@@ -73,6 +81,14 @@ impl Intercept for PageIntercept {
73
81
* sql =( & sql[ ..idx] ) . to_string ( ) ;
74
82
}
75
83
if let Some ( idx) = sql. rfind ( " order by " ) {
84
+ //remove args(args.pop())
85
+ let order_by_clause =& sql[ idx..] ;
86
+ let driver_type = executor. driver_type ( ) . unwrap_or_default ( ) ;
87
+ let param_count =self . count_param_count ( driver_type, & order_by_clause) ;
88
+ // 移除对应的参数
89
+ for _in 0 ..param_count{
90
+ args. pop ( ) ;
91
+ }
76
92
* sql =( & sql[ ..idx] ) . to_string ( ) ;
77
93
}
78
94
}