@@ -2,7 +2,8 @@ use super::AuroraConnection;
22use crate :: aurora:: error:: AuroraDatabaseError ;
33use crate :: aurora:: statement:: AuroraStatementMetadata ;
44use crate :: aurora:: {
5- Aurora , AuroraArguments , AuroraColumn , AuroraDone , AuroraRow , AuroraStatement , AuroraTypeInfo ,
5+ Aurora , AuroraArguments , AuroraColumn , AuroraDbType , AuroraDone , AuroraRow , AuroraStatement ,
6+ AuroraTypeInfo ,
67} ;
78use crate :: describe:: Describe ;
89use crate :: error:: Error ;
@@ -16,6 +17,8 @@ use futures_core::stream::BoxStream;
1617use futures_core:: Stream ;
1718use futures_util:: stream;
1819use futures_util:: { pin_mut, TryStreamExt } ;
20+ use once_cell:: sync:: Lazy ;
21+ use regex:: Regex ;
1922use rusoto_rds_data:: { ExecuteStatementRequest , ExecuteStatementResponse , RdsData } ;
2023use std:: borrow:: Cow ;
2124use std:: sync:: Arc ;
@@ -24,15 +27,47 @@ impl AuroraConnection {
2427 async fn run < ' e , ' c : ' e , ' q : ' e > (
2528 & ' c mut self ,
2629 query : & ' q str ,
27- arguments : Option < AuroraArguments > ,
30+ mut arguments : Option < AuroraArguments > ,
2831 ) -> Result < impl Stream < Item = Result < Either < AuroraDone , AuroraRow > , Error > > + ' e , Error > {
2932 let mut logger = QueryLogger :: new ( query, self . log_settings . clone ( ) ) ;
3033
34+ static MYSQL_PARAMS_RE : Lazy < Regex > = Lazy :: new ( || Regex :: new ( r"\?" ) . unwrap ( ) ) ;
35+ static POSTGRES_PARAMS_RE : Lazy < Regex > = Lazy :: new ( || Regex :: new ( r"\$\d+" ) . unwrap ( ) ) ;
36+
37+ let regex = match self . db_type {
38+ AuroraDbType :: MySQL => & MYSQL_PARAMS_RE ,
39+ AuroraDbType :: Postgres => & POSTGRES_PARAMS_RE ,
40+ } ;
41+
42+ let mut offset = 0 ;
43+ let mut owned_query = query. to_owned ( ) ;
44+
45+ if let Some ( arguments) = arguments. as_mut ( ) {
46+ regex
47+ . find_iter ( query)
48+ . zip ( arguments. parameters . iter_mut ( ) )
49+ . enumerate ( )
50+ . for_each ( |( idx, ( mat, param) ) | {
51+ let name = format ! ( "param_{}" , idx + 1 ) ;
52+
53+ owned_query. replace_range (
54+ ( mat. start ( ) + offset) ..( mat. end ( ) + offset) ,
55+ & format ! ( ":{}" , name) ,
56+ ) ;
57+
58+ offset += name. len ( ) + 1 - mat. as_str ( ) . len ( ) ;
59+
60+ param. name = Some ( name) ;
61+ } ) ;
62+ }
63+
64+ dbg ! ( & owned_query) ;
65+
3166 // TODO: is this correct?
3267 let transaction_id = self . transaction_ids . last ( ) . cloned ( ) ;
3368
3469 let request = ExecuteStatementRequest {
35- sql : query . to_owned ( ) ,
70+ sql : owned_query ,
3671 parameters : arguments. map ( |m| m. parameters ) ,
3772 resource_arn : self . resource_arn . clone ( ) ,
3873 secret_arn : self . secret_arn . clone ( ) ,
0 commit comments