3 import java.lang.reflect.InvocationTargetException;
4 import java.lang.reflect.Method;
5 import java.util.ArrayList;
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
17 ESC_FUNCTION(
"\\s*fn\\s+([^ ]*?)\\s*\\(",
"\\(\\s*(.*)\\s*\\)", null),
22 EscapeFunctions(String escapePattern, String argPattern, String replacementKeyword) {
23 this.escapePattern = Pattern.compile(escapePattern);
24 this.argPattern = Pattern.compile(argPattern);
25 this.replacementKeyword = replacementKeyword;
34 StringBuilder sb =
new StringBuilder();
35 List<CharSequence> parseArgs =
new ArrayList<CharSequence>(3);
37 StringBuilder args_sb =
new StringBuilder();
38 for (
int index = 0; index < all_arguments.length(); ++index) {
39 if (all_arguments.charAt(index) ==
',' && bracket_cnt == 0) {
40 parseArgs.add(args_sb.toString());
41 args_sb.delete(0, args_sb.length());
44 if (all_arguments.charAt(index) ==
'(') {
46 }
else if (all_arguments.charAt(index) ==
')') {
49 args_sb.append(all_arguments.charAt(index));
52 parseArgs.add(args_sb.toString());
53 method.invoke(null, sb, parseArgs);
55 }
catch (InvocationTargetException e) {
56 throw new RuntimeException(
"Invocation fn argument parse error" + e.getMessage());
57 }
catch (IllegalAccessException ilE) {
58 throw new RuntimeException(
"Access fn argument error" + ilE.getMessage());
63 Matcher matcher = escapePattern.matcher(sql);
67 matcher = argPattern.matcher(sql);
69 String new_sql = this.replacementKeyword + matcher.group(1);
73 String fn_name = matcher.group(1);
74 Method method = HeavyAIEscapeFunctions.getFunction(fn_name);
75 matcher = argPattern.matcher(sql);
78 String new_sql = fn_name +
'(' + matcher.group(1) +
')';
81 return call_escape_fn(matcher.group(1), method);
88 public static String
simple(String arg) {
92 public static String
function(String arg) {
97 private static class Pair {
106 String value = sql.substring(index.start, index.end);
108 boolean found_match =
false;
110 String newsql = xx.makeMatch(value);
111 if (newsql != null) {
112 sql = sql.substring(0, index.start) + newsql +
" "
113 + sql.substring(index.
end + 1, sql.length());
114 int x = newsql.length();
115 index.end = index.start + newsql.length();
122 sql = sql.substring(0, index.start) +
"{" + sql.substring(index.
start);
127 public static String
parse(String sql) {
130 throw new RuntimeException(
"Invalid java escape syntax - badly matched '}'");
141 boolean in_quote =
false;
144 if (sql.charAt(index) ==
'\'') {
145 in_quote = !in_quote;
146 }
else if (sql.charAt(index) ==
'{' && !in_quote) {
147 if (index + 1 == sql.length()) {
150 throw new RuntimeException(
"Invalid java escape syntax - badly matched '{'");
153 bracket_cnt = pR.bracket_cnt;
154 String sql_snippet = pR.sql_value;
155 sql = sql.substring(0, index) +
" " + sql_snippet;
157 }
else if (sql.charAt(index) ==
'}' && !in_quote) {
162 pR.bracket_cnt = --bracket_cnt;
163 pR.end_idx = ptr.end + 1;
167 }
while (index < sql.length());
169 throw new RuntimeException(
"Invalid java escape syntax - badly matched '''");
174 pR.bracket_cnt = bracket_cnt;
175 pR.end_idx = sql.length();
String call_escape_fn(String all_arguments, Method method)
String makeMatch(String sql)
EscapeFunctions(String escapePattern, String argPattern, String replacementKeyword)
static String simple(String arg)
static String parse(String sql)
final Pattern escapePattern
static String process_sql(String sql, Pair index)
static final char[] QUOTE_OR_ALPHABETIC_MARKER
static Parser_return parse(String sql, int bracket_cnt)
static final char[] SINGLE_QUOTE
final String replacementKeyword