forked from databricks/databricks-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWildcardUtil.java
More file actions
76 lines (69 loc) · 2.13 KB
/
WildcardUtil.java
File metadata and controls
76 lines (69 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.databricks.jdbc.common.util;
/**
* This class consists of utility functions with respect to wildcard strings that are required in
* building SQL queries
*/
public class WildcardUtil {
private static final String ASTERISK = "*";
/**
* This function checks if the input string is a "match anything" string i.e. "*"
*
* @param s the input string
* @return true if the input string is "*"
*/
public static boolean isMatchAnything(String s) {
return ASTERISK.equals(s);
}
public static boolean isNullOrEmpty(String s) {
return s == null || s.trim().isEmpty();
}
public static boolean isNullOrWildcard(String s) {
return s == null || isWildcard(s);
}
/**
* This function checks if the input string is a wildcard string
*
* @param s the input string
* @return true if the input string is wildcard
*/
public static boolean isWildcard(String s) {
return s != null && s.contains(ASTERISK);
}
public static String jdbcPatternToHive(String pattern) {
if (pattern == null) {
return null;
}
StringBuilder builder = new StringBuilder();
boolean escapeNext = false; // Flag to check if the next character is escaped
for (int i = 0; i < pattern.length(); i++) {
char ch = pattern.charAt(i);
if (ch == '\\') {
// Check if it's an escaped backslash
if (i + 1 < pattern.length() && pattern.charAt(i + 1) == '\\') {
builder.append("\\\\");
i++; // Skip the next backslash since it's part of the escape sequence
} else {
escapeNext = !escapeNext; // Toggle escape state for next character
}
} else if (escapeNext) {
// If the current character is escaped, add it directly
builder.append(ch);
escapeNext = false; // Reset escape state
} else {
// Handle unescaped wildcards
switch (ch) {
case '%':
builder.append("*");
break;
case '_':
builder.append(".");
break;
default:
builder.append(ch);
break;
}
}
}
return builder.toString();
}
}