@@ -39,17 +39,17 @@ impl SqlCommandBuilder {
3939 }
4040
4141 pub fn with_schema_pattern ( mut self , pattern : Option < & str > ) -> Self {
42- self . schema_pattern = pattern. map ( Self :: jdbc_pattern_to_hive ) ;
42+ self . schema_pattern = pattern. map ( str :: to_owned ) ;
4343 self
4444 }
4545
4646 pub fn with_table_pattern ( mut self , pattern : Option < & str > ) -> Self {
47- self . table_pattern = pattern. map ( Self :: jdbc_pattern_to_hive ) ;
47+ self . table_pattern = pattern. map ( str :: to_owned ) ;
4848 self
4949 }
5050
5151 pub fn with_column_pattern ( mut self , pattern : Option < & str > ) -> Self {
52- self . column_pattern = pattern. map ( Self :: jdbc_pattern_to_hive ) ;
52+ self . column_pattern = pattern. map ( str :: to_owned ) ;
5353 self
5454 }
5555
@@ -63,9 +63,10 @@ impl SqlCommandBuilder {
6363 /// - `_` → `.` (single-char wildcard)
6464 /// - `\%` → `%` (escaped literal percent)
6565 /// - `\_` → `_` (escaped literal underscore)
66- /// - `\\` → `\\ ` (escaped backslash)
66+ /// - `\\` → `\` (escaped backslash)
6767 ///
68- /// Also escapes single quotes for safe SQL embedding.
68+ /// This only converts wildcards; callers must separately escape the
69+ /// converted value before embedding it in a SQL string literal.
6970 /// Matches JDBC driver's `WildcardUtil.jdbcPatternToHive`.
7071 fn jdbc_pattern_to_hive ( pattern : & str ) -> String {
7172 let mut result = String :: with_capacity ( pattern. len ( ) ) ;
@@ -77,24 +78,26 @@ impl SqlCommandBuilder {
7778 }
7879 if escape_next {
7980 // Escaped character: emit literally
80- if ch == '\\' {
81- result. push_str ( "\\ \\ " ) ;
82- } else {
83- result. push ( ch) ;
84- }
81+ result. push ( ch) ;
8582 escape_next = false ;
8683 } else {
8784 match ch {
8885 '%' => result. push ( '*' ) ,
8986 '_' => result. push ( '.' ) ,
90- '\'' => result. push_str ( "''" ) ,
9187 _ => result. push ( ch) ,
9288 }
9389 }
9490 }
91+ if escape_next {
92+ result. push ( '\\' ) ;
93+ }
9594 result
9695 }
9796
97+ fn escape_show_pattern ( pattern : & str ) -> String {
98+ Self :: escape_sql_string ( & Self :: jdbc_pattern_to_hive ( pattern) )
99+ }
100+
98101 /// Whether an ODBC/JDBC-style pattern contains an *active* wildcard
99102 /// (`%` or `_`) — i.e. one that is not backslash-escaped.
100103 ///
@@ -213,7 +216,7 @@ impl SqlCommandBuilder {
213216 } ;
214217
215218 if let Some ( ref pattern) = self . schema_pattern {
216- sql. push_str ( & format ! ( " LIKE '{}'" , pattern) ) ;
219+ sql. push_str ( & format ! ( " LIKE '{}'" , Self :: escape_show_pattern ( pattern) ) ) ;
217220 }
218221
219222 sql
@@ -235,11 +238,14 @@ impl SqlCommandBuilder {
235238 } ;
236239
237240 if let Some ( ref pattern) = self . schema_pattern {
238- sql. push_str ( & format ! ( " SCHEMA LIKE '{}'" , pattern) ) ;
241+ sql. push_str ( & format ! (
242+ " SCHEMA LIKE '{}'" ,
243+ Self :: escape_show_pattern( pattern)
244+ ) ) ;
239245 }
240246
241247 if let Some ( ref pattern) = self . table_pattern {
242- sql. push_str ( & format ! ( " LIKE '{}'" , pattern) ) ;
248+ sql. push_str ( & format ! ( " LIKE '{}'" , Self :: escape_show_pattern ( pattern) ) ) ;
243249 }
244250
245251 sql
@@ -260,15 +266,21 @@ impl SqlCommandBuilder {
260266 } ;
261267
262268 if let Some ( ref pattern) = self . schema_pattern {
263- sql. push_str ( & format ! ( " SCHEMA LIKE '{}'" , pattern) ) ;
269+ sql. push_str ( & format ! (
270+ " SCHEMA LIKE '{}'" ,
271+ Self :: escape_show_pattern( pattern)
272+ ) ) ;
264273 }
265274
266275 if let Some ( ref pattern) = self . table_pattern {
267- sql. push_str ( & format ! ( " TABLE LIKE '{}'" , pattern) ) ;
276+ sql. push_str ( & format ! (
277+ " TABLE LIKE '{}'" ,
278+ Self :: escape_show_pattern( pattern)
279+ ) ) ;
268280 }
269281
270282 if let Some ( ref pattern) = self . column_pattern {
271- sql. push_str ( & format ! ( " LIKE '{}'" , pattern) ) ;
283+ sql. push_str ( & format ! ( " LIKE '{}'" , Self :: escape_show_pattern ( pattern) ) ) ;
272284 }
273285
274286 sql
@@ -278,7 +290,7 @@ impl SqlCommandBuilder {
278290 ///
279291 /// Use for `LIKE` pattern values in `information_schema` queries.
280292 /// For identifiers (catalog/schema/table names), use [`escape_identifier`] instead.
281- /// For `SHOW` command patterns, use [`jdbc_pattern_to_hive `] instead.
293+ /// For `SHOW` command patterns, use [`escape_show_pattern `] instead.
282294 ///
283295 /// Databricks SQL treats backslash as an escape character in string literals
284296 /// by default, so both single quotes and backslashes must be escaped.
@@ -358,12 +370,12 @@ impl SqlCommandBuilder {
358370 if let Some ( pattern) = schema_pattern {
359371 sql. push_str ( & format ! (
360372 " SCHEMA LIKE '{}'" ,
361- Self :: jdbc_pattern_to_hive ( pattern)
373+ Self :: escape_show_pattern ( pattern)
362374 ) ) ;
363375 }
364376
365377 let pattern = function_pattern. unwrap_or ( "%" ) ;
366- sql. push_str ( & format ! ( " LIKE '{}'" , Self :: jdbc_pattern_to_hive ( pattern) ) ) ;
378+ sql. push_str ( & format ! ( " LIKE '{}'" , Self :: escape_show_pattern ( pattern) ) ) ;
367379
368380 sql
369381 }
@@ -963,6 +975,16 @@ mod tests {
963975 ) ;
964976 }
965977
978+ #[ test]
979+ fn test_get_functions_escaped_quotes_are_sql_escaped_once ( ) {
980+ let sql =
981+ SqlCommandBuilder :: build_show_functions ( "main" , Some ( r"sch\'ema" ) , Some ( r"fn\'name" ) ) ;
982+ assert_eq ! (
983+ sql,
984+ "SHOW FUNCTIONS IN CATALOG `main` SCHEMA LIKE 'sch''ema' LIKE 'fn''name'"
985+ ) ;
986+ }
987+
966988 // --- foreign keys with special characters ---
967989
968990 #[ test]
@@ -1001,7 +1023,7 @@ mod tests {
10011023
10021024 #[ test]
10031025 fn test_pattern_escaped_backslash ( ) {
1004- assert_eq ! ( SqlCommandBuilder :: jdbc_pattern_to_hive( r"abc\\" ) , r"abc\\ " ) ;
1026+ assert_eq ! ( SqlCommandBuilder :: jdbc_pattern_to_hive( r"abc\\" ) , r"abc\" ) ;
10051027 }
10061028
10071029 #[ test]
@@ -1010,8 +1032,13 @@ mod tests {
10101032 }
10111033
10121034 #[ test]
1013- fn test_pattern_single_quote_escaped ( ) {
1014- assert_eq ! ( SqlCommandBuilder :: jdbc_pattern_to_hive( "it's" ) , "it''s" ) ;
1035+ fn test_pattern_single_quote_unchanged ( ) {
1036+ assert_eq ! ( SqlCommandBuilder :: jdbc_pattern_to_hive( "it's" ) , "it's" ) ;
1037+ }
1038+
1039+ #[ test]
1040+ fn test_pattern_trailing_backslash_preserved ( ) {
1041+ assert_eq ! ( SqlCommandBuilder :: jdbc_pattern_to_hive( r"abc\" ) , r"abc\" ) ;
10151042 }
10161043
10171044 // --- catalog wildcard helpers (list_tables client-side path) ---
0 commit comments