@@ -26,16 +26,21 @@ private static string ReplaceFirst(this string text, string search, string repla
2626 return text . Substring ( 0 , pos ) + replace + text . Substring ( pos + search . Length ) ;
2727 }
2828
29- public static string _PrepareClear ( string q , List < string > args )
29+ public static string _PrepareClear ( string q , List < string > args , Func < string , string > escape_func = null )
3030 {
3131 var new_q = q ;
32- if ( args != null ) foreach ( var arg in args . ToList ( ) )
33- {
34- var new_q2 = ReplaceFirst ( new_q , "{ARG}" , _PrepareArg ( arg ) ) ;
35-
36- if ( new_q2 == new_q ) throw new Exception ( "Mailformed query [Too many args in params]" ) ;
37- new_q = new_q2 ;
38- }
32+ if ( args != null )
33+ foreach ( string arg in args . ToList ( ) )
34+ {
35+ var new_q2 = "" ;
36+ if ( escape_func == null )
37+ new_q2 = ReplaceFirst ( new_q , "{ARG}" , _PrepareArg ( arg ) ) ;
38+ else
39+ new_q2 = ReplaceFirst ( new_q , "{ARG}" , escape_func ( arg ) ) ;
40+
41+ if ( new_q2 == new_q ) throw new Exception ( "Mailformed query [Too many args in params]" ) ;
42+ new_q = new_q2 ;
43+ }
3944 if ( new_q . Contains ( "{ARG}" ) ) throw new Exception ( "Mailformed query [Not enough args in params]" ) ;
4045 return new_q ;
4146 }
@@ -44,23 +49,33 @@ public static string _PrepareArg(string arg)
4449 {
4550 if ( arg == null ) return "" ;
4651
47- var new_arg = arg ;
52+ // var new_arg = arg;
4853
4954 //string[] escapes = ["'", "\"", "`", "%", "-", "_"];
50- string [ ] escapes = [ "'" , "\" " , "`" , "%" , "\\ " ] ;
55+ string [ ] escapes = [ "\\ " , "'" , "\" " , "`" , "%" ] ;
56+ //string[] escapes = ["\\","'", "`", "%"];
5157
52- foreach ( var escape in escapes )
58+
59+
60+ //foreach (var escape in escapes)
61+ //{
62+ var new_arg = "" ;
63+ foreach ( var ch in arg )
5364 {
54- new_arg = new_arg . Replace ( escape , $ "\\ { escape } ") ;
65+ if ( escapes . Contains ( ch . ToString ( ) ) )
66+ new_arg += "\\ " ;
67+ new_arg += ch . ToString ( ) ;
5568 }
56-
69+ //new_arg = new_arg.Replace(escape, $"\\{escape}");
70+
5771 return new_arg ;
5872 }
5973
6074
6175 public static List < List < string > > _Query ( DbConnection conn , string q , bool non_query )
6276 {
6377 if ( conn . State != ConnectionState . Open ) conn . Open ( ) ;
78+
6479 var sql = conn . CreateCommand ( ) ;
6580 sql . CommandText = q ;
6681 if ( ! non_query )
0 commit comments