1 24 25 package com.mckoi.database.sql; 26 27 import com.mckoi.database.Expression; 28 import com.mckoi.database.Operator; 29 import com.mckoi.database.FunctionDef; 30 import com.mckoi.database.Variable; 31 import com.mckoi.database.TableName; 32 import com.mckoi.database.TObject; 33 import com.mckoi.database.TType; 34 import com.mckoi.database.global.*; 35 import com.mckoi.util.BigNumber; 36 37 42 43 public class Util { 44 45 private static TObject ZERO_NUMBER = TObject.intVal(0); 46 47 54 public static String asNonQuotedRef(Token token) { 55 if (token.kind == SQLConstants.QUOTED_VARIABLE) { 56 return token.image.substring(1, token.image.length() - 1); 58 } 59 else if (token.kind == SQLConstants.QUOTED_DELIMINATED_REF || 60 token.kind == SQLConstants.QUOTEDGLOBVARIABLE) { 61 String image = token.image; 63 StringBuffer b = new StringBuffer (); 64 int sz = image.length(); 65 for (int i = 0; i < sz; ++i) { 66 char c = image.charAt(i); 67 if (c != '\"') { 68 b.append(c); 69 } 70 } 71 return new String (b); 72 } 73 else { 74 return token.image; 75 } 76 } 77 78 84 public static Object toParamObject(Token token, boolean upper_identifiers) { 85 if (token.kind == SQLConstants.STRING_LITERAL) { 86 String raw_string = token.image.substring(1, token.image.length() - 1); 87 return TObject.stringVal(escapeTranslated(raw_string)); 88 } 89 else if (token.kind == SQLConstants.BOOLEAN_LITERAL) { 93 return TObject.booleanVal(token.image.equalsIgnoreCase("true")); 94 } 95 else if (token.kind == SQLConstants.NULL_LITERAL) { 96 return TObject.nullVal(); 97 } 98 else if (token.kind == SQLConstants.REGEX_LITERAL) { 99 String str = token.image.substring(5).trim(); 102 return TObject.stringVal(str); 103 } 104 else if (token.kind == SQLConstants.QUOTED_VARIABLE || 105 token.kind == SQLConstants.GLOBVARIABLE || token.kind == SQLConstants.IDENTIFIER || 107 token.kind == SQLConstants.DOT_DELIMINATED_REF || 108 token.kind == SQLConstants.QUOTED_DELIMINATED_REF) { 109 String name = asNonQuotedRef(token); 110 if (upper_identifiers) { 117 name = name.toUpperCase(); 118 } 119 Variable v; 120 int div = name.lastIndexOf("."); 121 if (div != -1) { 122 String column_name = name.substring(div + 1); 125 TableName table_name = TableName.resolve(name.substring(0, div)); 127 128 v = new Variable(table_name, column_name); 130 } 131 else { 132 v = new Variable(name); 134 } 135 return v; 136 } 137 else { String name = token.image; 140 if (upper_identifiers) { 141 name = name.toUpperCase(); 142 } 143 return new Variable(token.image); 144 } 145 } 146 147 150 public static TObject zeroNumber() { 151 return ZERO_NUMBER; 152 } 153 154 157 public static TObject parseNumberToken(Token token, boolean negative) { 158 if (negative) { 159 return TObject.bigNumberVal(BigNumber.fromString("-" + token.image)); 160 } 161 else { 162 return TObject.bigNumberVal(BigNumber.fromString(token.image)); 163 } 164 } 165 166 170 public static TObject toArrayParamObject(Expression[] arr) { 171 return new TObject(TType.ARRAY_TYPE, arr); 172 } 173 174 177 public static String expressionListToString(Expression[] list) { 178 StringBuffer buf = new StringBuffer (); 179 for (int i = 0; i < list.length; ++i) { 180 buf.append(list[i].text().toString()); 181 if (i < list.length - 1) { 182 buf.append(", "); 183 } 184 } 185 return new String (buf); 186 } 187 188 195 public static Expression normalize(final Expression exp) { 196 if (exp.containsNotOperator()) { 198 return normalize(exp, false); 199 } 200 return exp; 201 } 202 203 210 private static Expression normalize(final Expression exp, 211 final boolean inverse) { 212 if (exp.size() <= 1) { 213 if (inverse) { 214 return standardInverse(exp); 215 } 216 else { 217 return exp; 218 } 219 } 220 final Operator op = (Operator) exp.last(); 221 final Expression[] exps = exp.split(); 222 223 if (op.isNot()) { 224 return normalize(exps[0], !inverse); 227 } 228 else if (op.isNotInversible()) { 229 Expression resolved_expr = 232 new Expression(normalize(exps[0], false), op, 233 normalize(exps[1], false)); 234 if (inverse) { 235 return standardInverse(resolved_expr); 236 } 237 else { 238 return resolved_expr; 239 } 240 } 241 else if (op.isLogical()) { 242 if (inverse) { 245 return new Expression(normalize(exps[0], inverse), op.inverse(), 246 normalize(exps[1], inverse)); 247 } 248 else { 249 return new Expression(normalize(exps[0], inverse), op, 250 normalize(exps[1], inverse)); 251 252 } 253 } 254 else { 255 if (inverse) { 257 return new Expression(normalize(exps[0], false), op.inverse(), 258 normalize(exps[1], false)); 259 } 260 else { 261 return new Expression(normalize(exps[0], false), op, 262 normalize(exps[1], false)); 263 } 264 } 265 266 } 267 268 273 private static Expression standardInverse(Expression exp) { 274 return new Expression(exp, Operator.get("="), 275 new Expression(TObject.booleanVal(false))); 276 } 277 278 283 public static FunctionDef resolveFunctionName(String name, 284 Expression[] exp_list) { 285 return new FunctionDef(name, exp_list); 286 } 287 288 292 private static String escapeTranslated(String input) { 293 StringBuffer result = new StringBuffer (); 294 int size = input.length(); 295 boolean last_char_escape = false; 296 boolean last_char_quote = false; 297 for (int i = 0; i < size; ++i) { 298 char c = input.charAt(i); 299 if (last_char_quote) { 300 last_char_quote = false; 301 if (c != '\'') { 302 result.append(c); 303 } 304 } 305 else if (last_char_escape) { 306 if (c == '\\') { 307 result.append('\\'); 308 } 309 else if (c == '\'') { 310 result.append('\''); 311 } 312 else if (c == 't') { 313 result.append('\t'); 314 } 315 else if (c == 'n') { 316 result.append('\n'); 317 } 318 else if (c == 'r') { 319 result.append('\r'); 320 } 321 else { 322 result.append('\\'); 323 result.append(c); 324 } 325 last_char_escape = false; 326 } 327 else if (c == '\\') { 328 last_char_escape = true; 329 } 330 else if (c == '\'') { 331 last_char_quote = true; 332 result.append(c); 333 } 334 else { 335 result.append(c); 336 } 337 } 338 return new String (result); 339 } 340 341 } 342 | Popular Tags |