@@ -9,6 +9,7 @@ use nom_sql::{
9
9
Literal , LiteralExpression , Operator , SqlQuery , TableKey , UpdateStatement ,
10
10
} ;
11
11
use regex:: Regex ;
12
+ use std:: borrow:: Cow ;
12
13
use std:: collections:: HashMap ;
13
14
14
15
lazy_static ! {
@@ -45,12 +46,20 @@ lazy_static! {
45
46
vec![ ( "lockstatus" , "1" ) ] ,
46
47
) ,
47
48
] ;
49
+ pub ( crate ) static ref COMMENTS : Vec <( Regex , & ' static str ) > = vec![
50
+ ( Regex :: new( r"(?s)/\*.*\*/" ) . unwrap( ) , "" ) ,
51
+ ( Regex :: new( r"--.*\n" ) . unwrap( ) , "\n " ) ,
52
+ ] ;
53
+ pub ( crate ) static ref COLLAPSE_SPACES : ( Regex , & ' static str ) =
54
+ ( Regex :: new( r" +" ) . unwrap( ) , " " ) ;
48
55
}
49
56
50
57
pub ( crate ) fn sanitize_query ( query : & str ) -> String {
51
- let query = Regex :: new ( r"(?s)/\*.*\*/" ) . unwrap ( ) . replace_all ( query, "" ) ;
52
- let query = Regex :: new ( r"--.*\n" ) . unwrap ( ) . replace_all ( & query, "\n " ) ;
53
- let query = Regex :: new ( r" +" ) . unwrap ( ) . replace_all ( & query, " " ) ;
58
+ let query = Cow :: from ( query) ;
59
+ for & ( ref pattern, replacement) in & * COMMENTS {
60
+ pattern. replace_all ( & query, replacement) ;
61
+ }
62
+ let query = COLLAPSE_SPACES . 0 . replace_all ( & query, COLLAPSE_SPACES . 1 ) ;
54
63
let query = query. replace ( '"' , "'" ) ;
55
64
let query = query. trim ( ) ;
56
65
query. to_owned ( )
0 commit comments