| 1 | /* |
| 2 | Copyright (C) 2002-2004 MySQL AB |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of version 2 of the GNU General Public License as |
| 6 | published by the Free Software Foundation. |
| 7 | |
| 8 | There are special exceptions to the terms and conditions of the GPL |
| 9 | as it is applied to this software. View the full text of the |
| 10 | exception in file EXCEPTIONS-CONNECTOR-J in the directory of this |
| 11 | software distribution. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | |
| 22 | |
| 23 | |
| 24 | */ |
| 25 | |
| 26 | /** |
| 27 | * EscapeProcessor performs all escape code processing as outlined in the JDBC |
| 28 | * spec by JavaSoft. |
| 29 | */ |
| 30 | package com.mysql.jdbc; |
| 31 | |
| 32 | import java.sql.SQLException; |
| 33 | import java.sql.Time; |
| 34 | import java.sql.Timestamp; |
| 35 | |
| 36 | import java.util.Calendar; |
| 37 | import java.util.Collections; |
| 38 | import java.util.GregorianCalendar; |
| 39 | import java.util.HashMap; |
| 40 | import java.util.Locale; |
| 41 | import java.util.Map; |
| 42 | import java.util.StringTokenizer; |
| 43 | import java.util.TimeZone; |
| 44 | |
| 45 | class EscapeProcessor { |
| 46 | private static Map JDBC_CONVERT_TO_MYSQL_TYPE_MAP; |
| 47 | |
| 48 | private static Map JDBC_NO_CONVERT_TO_MYSQL_EXPRESSION_MAP; |
| 49 | |
| 50 | static { |
| 51 | Map tempMap = new HashMap(); |
| 52 | |
| 53 | tempMap.put("BIGINT", "0 + ?"); |
| 54 | tempMap.put("BINARY", "BINARY"); |
| 55 | tempMap.put("BIT", "0 + ?"); |
| 56 | tempMap.put("CHAR", "CHAR"); |
| 57 | tempMap.put("DATE", "DATE"); |
| 58 | tempMap.put("DECIMAL", "0.0 + ?"); |
| 59 | tempMap.put("DOUBLE", "0.0 + ?"); |
| 60 | tempMap.put("FLOAT", "0.0 + ?"); |
| 61 | tempMap.put("INTEGER", "0 + ?"); |
| 62 | tempMap.put("LONGVARBINARY", "BINARY"); |
| 63 | tempMap.put("LONGVARCHAR", "CONCAT(?)"); |
| 64 | tempMap.put("REAL", "0.0 + ?"); |
| 65 | tempMap.put("SMALLINT", "CONCAT(?)"); |
| 66 | tempMap.put("TIME", "TIME"); |
| 67 | tempMap.put("TIMESTAMP", "DATETIME"); |
| 68 | tempMap.put("TINYINT", "CONCAT(?)"); |
| 69 | tempMap.put("VARBINARY", "BINARY"); |
| 70 | tempMap.put("VARCHAR", "CONCAT(?)"); |
| 71 | |
| 72 | JDBC_CONVERT_TO_MYSQL_TYPE_MAP = Collections.unmodifiableMap(tempMap); |
| 73 | |
| 74 | tempMap = new HashMap(JDBC_CONVERT_TO_MYSQL_TYPE_MAP); |
| 75 | |
| 76 | tempMap.put("BINARY", "CONCAT(?)"); |
| 77 | tempMap.put("CHAR", "CONCAT(?)"); |
| 78 | tempMap.remove("DATE"); |
| 79 | tempMap.put("LONGVARBINARY", "CONCAT(?)"); |
| 80 | tempMap.remove("TIME"); |
| 81 | tempMap.remove("TIMESTAMP"); |
| 82 | tempMap.put("VARBINARY", "CONCAT(?)"); |
| 83 | |
| 84 | JDBC_NO_CONVERT_TO_MYSQL_EXPRESSION_MAP = Collections |
| 85 | .unmodifiableMap(tempMap); |
| 86 | |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Escape process one string |
| 91 | * |
| 92 | * @param sql |
| 93 | * the SQL to escape process. |
| 94 | * |
| 95 | * @return the SQL after it has been escape processed. |
| 96 | * |
| 97 | * @throws java.sql.SQLException |
| 98 | * DOCUMENT ME! |
| 99 | * @throws SQLException |
| 100 | * DOCUMENT ME! |
| 101 | */ |
| 102 | public static final Object escapeSQL(String sql, |
| 103 | boolean serverSupportsConvertFn, |
| 104 | Connection conn) throws java.sql.SQLException { |
| 105 | boolean replaceEscapeSequence = false; |
| 106 | String escapeSequence = null; |
| 107 | |
| 108 | if (sql == null) { |
| 109 | return null; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Short circuit this code if we don't have a matching pair of "{}". - |
| 114 | * Suggested by Ryan Gustafason |
| 115 | */ |
| 116 | int beginBrace = sql.indexOf('{'); |
| 117 | int nextEndBrace = (beginBrace == -1) ? (-1) : sql.indexOf('}', |
| 118 | beginBrace); |
| 119 | |
| 120 | if (nextEndBrace == -1) { |
| 121 | return sql; |
| 122 | } |
| 123 | |
| 124 | StringBuffer newSql = new StringBuffer(); |
| 125 | |
| 126 | EscapeTokenizer escapeTokenizer = new EscapeTokenizer(sql); |
| 127 | |
| 128 | byte usesVariables = Statement.USES_VARIABLES_FALSE; |
| 129 | boolean callingStoredFunction = false; |
| 130 | |
| 131 | while (escapeTokenizer.hasMoreTokens()) { |
| 132 | String token = escapeTokenizer.nextToken(); |
| 133 | |
| 134 | if (token.length() != 0) { |
| 135 | if (token.charAt(0) == '{') { // It's an escape code |
| 136 | |
| 137 | if (!token.endsWith("}")) { |
| 138 | throw SQLError.createSQLException("Not a valid escape sequence: " |
| 139 | + token); |
| 140 | } |
| 141 | |
| 142 | if (token.length() > 2) { |
| 143 | int nestedBrace = token.indexOf('{', 2); |
| 144 | |
| 145 | if (nestedBrace != -1) { |
| 146 | StringBuffer buf = new StringBuffer(token |
| 147 | .substring(0, 1)); |
| 148 | |
| 149 | Object remainingResults = escapeSQL(token |
| 150 | .substring(1, token.length() - 1), |
| 151 | serverSupportsConvertFn, conn); |
| 152 | |
| 153 | String remaining = null; |
| 154 | |
| 155 | if (remainingResults instanceof String) { |
| 156 | remaining = (String) remainingResults; |
| 157 | } else { |
| 158 | remaining = ((EscapeProcessorResult) remainingResults).escapedSql; |
| 159 | |
| 160 | if (usesVariables != Statement.USES_VARIABLES_TRUE) { |
| 161 | usesVariables = ((EscapeProcessorResult) remainingResults).usesVariables; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | buf.append(remaining); |
| 166 | |
| 167 | buf.append('}'); |
| 168 | |
| 169 | token = buf.toString(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // nested escape code |
| 174 | // Compare to tokens with _no_ whitespace |
| 175 | String collapsedToken = removeWhitespace(token); |
| 176 | |
| 177 | /* |
| 178 | * Process the escape code |
| 179 | */ |
| 180 | if (StringUtils.startsWithIgnoreCase(collapsedToken, |
| 181 | "{escape")) { |
| 182 | try { |
| 183 | StringTokenizer st = new StringTokenizer(token, |
| 184 | " '"); |
| 185 | st.nextToken(); // eat the "escape" token |
| 186 | escapeSequence = st.nextToken(); |
| 187 | |
| 188 | if (escapeSequence.length() < 3) { |
| 189 | throw SQLError.createSQLException( |
| 190 | "Syntax error for escape sequence '" |
| 191 | + token + "'", "42000"); |
| 192 | } |
| 193 | |
| 194 | escapeSequence = escapeSequence.substring(1, |
| 195 | escapeSequence.length() - 1); |
| 196 | replaceEscapeSequence = true; |
| 197 | } catch (java.util.NoSuchElementException e) { |
| 198 | throw SQLError.createSQLException( |
| 199 | "Syntax error for escape sequence '" |
| 200 | + token + "'", "42000"); |
| 201 | } |
| 202 | } else if (StringUtils.startsWithIgnoreCase(collapsedToken, |
| 203 | "{fn")) { |
| 204 | int startPos = token.toLowerCase().indexOf("fn ") + 3; |
| 205 | int endPos = token.length() - 1; // no } |
| 206 | |
| 207 | String fnToken = token.substring(startPos, endPos); |
| 208 | |
| 209 | // We need to handle 'convert' by ourselves |
| 210 | |
| 211 | if (StringUtils.startsWithIgnoreCaseAndWs(fnToken, |
| 212 | "convert")) { |
| 213 | newSql.append(processConvertToken(fnToken, |
| 214 | serverSupportsConvertFn)); |
| 215 | } else { |
| 216 | // just pass functions right to the DB |
| 217 | newSql.append(fnToken); |
| 218 | } |
| 219 | } else if (StringUtils.startsWithIgnoreCase(collapsedToken, |
| 220 | "{d")) { |
| 221 | int startPos = token.indexOf('\'') + 1; |
| 222 | int endPos = token.lastIndexOf('\''); // no } |
| 223 | |
| 224 | if ((startPos == -1) || (endPos == -1)) { |
| 225 | throw SQLError.createSQLException( |
| 226 | "Syntax error for DATE escape sequence '" |
| 227 | + token + "'", "42000"); |
| 228 | } |
| 229 | |
| 230 | String argument = token.substring(startPos, endPos); |
| 231 | |
| 232 | try { |
| 233 | StringTokenizer st = new StringTokenizer(argument, |
| 234 | " -"); |
| 235 | String year4 = st.nextToken(); |
| 236 | String month2 = st.nextToken(); |
| 237 | String day2 = st.nextToken(); |
| 238 | String dateString = "'" + year4 + "-" + month2 |
| 239 | + "-" + day2 + "'"; |
| 240 | newSql.append(dateString); |
| 241 | } catch (java.util.NoSuchElementException e) { |
| 242 | throw SQLError.createSQLException( |
| 243 | "Syntax error for DATE escape sequence '" |
| 244 | + argument + "'", "42000"); |
| 245 | } |
| 246 | } else if (StringUtils.startsWithIgnoreCase(collapsedToken, |
| 247 | "{ts")) { |
| 248 | int startPos = token.indexOf('\'') + 1; |
| 249 | int endPos = token.lastIndexOf('\''); // no } |
| 250 | |
| 251 | if ((startPos == -1) || (endPos == -1)) { |
| 252 | throw SQLError.createSQLException( |
| 253 | "Syntax error for TIMESTAMP escape sequence '" |
| 254 | + token + "'", "42000"); |
| 255 | } |
| 256 | |
| 257 | String argument = token.substring(startPos, endPos); |
| 258 | |
| 259 | try { |
| 260 | StringTokenizer st = new StringTokenizer(argument, |
| 261 | " .-:"); |
| 262 | String year4 = st.nextToken(); |
| 263 | String month2 = st.nextToken(); |
| 264 | String day2 = st.nextToken(); |
| 265 | String hour = st.nextToken(); |
| 266 | String minute = st.nextToken(); |
| 267 | String second = st.nextToken(); |
| 268 | |
| 269 | /* |
| 270 | * For now, we get the fractional seconds part, but |
| 271 | * we don't use it, as MySQL doesn't support it in |
| 272 | * it's TIMESTAMP data type |
| 273 | * |
| 274 | * String fractionalSecond = ""; |
| 275 | * |
| 276 | * if (st.hasMoreTokens()) { fractionalSecond = |
| 277 | * st.nextToken(); } |
| 278 | */ |
| 279 | /* |
| 280 | * Use the full format because number format will |
| 281 | * not work for "between" clauses. |
| 282 | * |
| 283 | * Ref. Mysql Docs |
| 284 | * |
| 285 | * You can specify DATETIME, DATE and TIMESTAMP |
| 286 | * values using any of a common set of formats: |
| 287 | * |
| 288 | * As a string in either 'YYYY-MM-DD HH:MM:SS' or |
| 289 | * 'YY-MM-DD HH:MM:SS' format. |
| 290 | * |
| 291 | * Thanks to Craig Longman for pointing out this bug |
| 292 | */ |
| 293 | if (!conn.getUseTimezone() && !conn.getUseJDBCCompliantTimezoneShift()) { |
| 294 | newSql.append("'").append(year4).append("-") |
| 295 | .append(month2).append("-").append(day2) |
| 296 | .append(" ").append(hour).append(":") |
| 297 | .append(minute).append(":").append(second) |
| 298 | .append("'"); |
| 299 | } else { |
| 300 | Calendar sessionCalendar; |
| 301 | |
| 302 | if (conn != null) { |
| 303 | sessionCalendar = conn.getCalendarInstanceForSessionOrNew(); |
| 304 | } else { |
| 305 | sessionCalendar = new GregorianCalendar(); |
| 306 | sessionCalendar.setTimeZone(TimeZone.getTimeZone("GMT")); |
| 307 | } |
| 308 | |
| 309 | try { |
| 310 | int year4Int = Integer.parseInt(year4); |
| 311 | int month2Int = Integer.parseInt(month2); |
| 312 | int day2Int = Integer.parseInt(day2); |
| 313 | int hourInt = Integer.parseInt(hour); |
| 314 | int minuteInt = Integer.parseInt(minute); |
| 315 | int secondInt = Integer.parseInt(second); |
| 316 | |
| 317 | synchronized (sessionCalendar) { |
| 318 | boolean useGmtMillis = conn.getUseGmtMillisForDatetimes(); |
| 319 | |
| 320 | Timestamp toBeAdjusted = TimeUtil.fastTimestampCreate(useGmtMillis, |
| 321 | useGmtMillis ? Calendar.getInstance(TimeZone.getTimeZone("GMT")): null, |
| 322 | sessionCalendar, |
| 323 | year4Int, |
| 324 | month2Int, |
| 325 | day2Int, |
| 326 | hourInt, |
| 327 | minuteInt, |
| 328 | secondInt, |
| 329 | 0); |
| 330 | |
| 331 | Timestamp inServerTimezone = TimeUtil.changeTimezone( |
| 332 | conn, |
| 333 | sessionCalendar, |
| 334 | null, |
| 335 | toBeAdjusted, |
| 336 | sessionCalendar.getTimeZone(), |
| 337 | conn.getServerTimezoneTZ(), |
| 338 | false); |
| 339 | |
| 340 | |
| 341 | newSql.append("'"); |
| 342 | |
| 343 | String timezoneLiteral = inServerTimezone.toString(); |
| 344 | |
| 345 | int indexOfDot = timezoneLiteral.indexOf("."); |
| 346 | |
| 347 | if (indexOfDot != -1) { |
| 348 | timezoneLiteral = timezoneLiteral.substring(0, indexOfDot); |
| 349 | } |
| 350 | |
| 351 | newSql.append(timezoneLiteral); |
| 352 | } |
| 353 | |
| 354 | newSql.append("'"); |
| 355 | |
| 356 | |
| 357 | } catch (NumberFormatException nfe) { |
| 358 | throw SQLError.createSQLException("Syntax error in TIMESTAMP escape sequence '" |
| 359 | + token + "'.", |
| 360 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 361 | } |
| 362 | } |
| 363 | } catch (java.util.NoSuchElementException e) { |
| 364 | throw SQLError.createSQLException( |
| 365 | "Syntax error for TIMESTAMP escape sequence '" |
| 366 | + argument + "'", "42000"); |
| 367 | } |
| 368 | } else if (StringUtils.startsWithIgnoreCase(collapsedToken, |
| 369 | "{t")) { |
| 370 | int startPos = token.indexOf('\'') + 1; |
| 371 | int endPos = token.lastIndexOf('\''); // no } |
| 372 | |
| 373 | if ((startPos == -1) || (endPos == -1)) { |
| 374 | throw SQLError.createSQLException( |
| 375 | "Syntax error for TIME escape sequence '" |
| 376 | + token + "'", "42000"); |
| 377 | } |
| 378 | |
| 379 | String argument = token.substring(startPos, endPos); |
| 380 | |
| 381 | try { |
| 382 | StringTokenizer st = new StringTokenizer(argument, |
| 383 | " :"); |
| 384 | String hour = st.nextToken(); |
| 385 | String minute = st.nextToken(); |
| 386 | String second = st.nextToken(); |
| 387 | |
| 388 | if (!conn.getUseTimezone()) { |
| 389 | String timeString = "'" + hour + ":" + minute + ":" |
| 390 | + second + "'"; |
| 391 | newSql.append(timeString); |
| 392 | } else { |
| 393 | Calendar sessionCalendar = null; |
| 394 | |
| 395 | if (conn != null) { |
| 396 | sessionCalendar = conn.getCalendarInstanceForSessionOrNew(); |
| 397 | } else { |
| 398 | sessionCalendar = new GregorianCalendar(); |
| 399 | } |
| 400 | |
| 401 | try { |
| 402 | int hourInt = Integer.parseInt(hour); |
| 403 | int minuteInt = Integer.parseInt(minute); |
| 404 | int secondInt = Integer.parseInt(second); |
| 405 | |
| 406 | synchronized (sessionCalendar) { |
| 407 | Time toBeAdjusted = TimeUtil.fastTimeCreate( |
| 408 | sessionCalendar, |
| 409 | hourInt, |
| 410 | minuteInt, |
| 411 | secondInt); |
| 412 | |
| 413 | Time inServerTimezone = TimeUtil.changeTimezone( |
| 414 | conn, |
| 415 | sessionCalendar, |
| 416 | null, |
| 417 | toBeAdjusted, |
| 418 | sessionCalendar.getTimeZone(), |
| 419 | conn.getServerTimezoneTZ(), |
| 420 | false); |
| 421 | |
| 422 | newSql.append("'"); |
| 423 | newSql.append(inServerTimezone.toString()); |
| 424 | newSql.append("'"); |
| 425 | } |
| 426 | |
| 427 | } catch (NumberFormatException nfe) { |
| 428 | throw SQLError.createSQLException("Syntax error in TIMESTAMP escape sequence '" |
| 429 | + token + "'.", |
| 430 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 431 | } |
| 432 | } |
| 433 | } catch (java.util.NoSuchElementException e) { |
| 434 | throw SQLError.createSQLException( |
| 435 | "Syntax error for escape sequence '" |
| 436 | + argument + "'", "42000"); |
| 437 | } |
| 438 | } else if (StringUtils.startsWithIgnoreCase(collapsedToken, |
| 439 | "{call") |
| 440 | || StringUtils.startsWithIgnoreCase(collapsedToken, |
| 441 | "{?=call")) { |
| 442 | |
| 443 | int startPos = StringUtils.indexOfIgnoreCase(token, |
| 444 | "CALL") + 5; |
| 445 | int endPos = token.length() - 1; |
| 446 | |
| 447 | if (StringUtils.startsWithIgnoreCase(collapsedToken, |
| 448 | "{?=call")) { |
| 449 | callingStoredFunction = true; |
| 450 | newSql.append("SELECT "); |
| 451 | newSql.append(token.substring(startPos, endPos)); |
| 452 | } else { |
| 453 | callingStoredFunction = false; |
| 454 | newSql.append("CALL "); |
| 455 | newSql.append(token.substring(startPos, endPos)); |
| 456 | } |
| 457 | } else if (StringUtils.startsWithIgnoreCase(collapsedToken, |
| 458 | "{oj")) { |
| 459 | // MySQL already handles this escape sequence |
| 460 | // because of ODBC. Cool. |
| 461 | newSql.append(token); |
| 462 | } |
| 463 | } else { |
| 464 | newSql.append(token); // it's just part of the query |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | String escapedSql = newSql.toString(); |
| 470 | |
| 471 | // |
| 472 | // FIXME: Let MySQL do this, however requires |
| 473 | // lightweight parsing of statement |
| 474 | // |
| 475 | if (replaceEscapeSequence) { |
| 476 | String currentSql = escapedSql; |
| 477 | |
| 478 | while (currentSql.indexOf(escapeSequence) != -1) { |
| 479 | int escapePos = currentSql.indexOf(escapeSequence); |
| 480 | String lhs = currentSql.substring(0, escapePos); |
| 481 | String rhs = currentSql.substring(escapePos + 1, currentSql |
| 482 | .length()); |
| 483 | currentSql = lhs + "\\" + rhs; |
| 484 | } |
| 485 | |
| 486 | escapedSql = currentSql; |
| 487 | } |
| 488 | |
| 489 | EscapeProcessorResult epr = new EscapeProcessorResult(); |
| 490 | epr.escapedSql = escapedSql; |
| 491 | epr.callingStoredFunction = callingStoredFunction; |
| 492 | |
| 493 | if (usesVariables != Statement.USES_VARIABLES_TRUE) { |
| 494 | if (escapeTokenizer.sawVariableUse()) { |
| 495 | epr.usesVariables = Statement.USES_VARIABLES_TRUE; |
| 496 | } else { |
| 497 | epr.usesVariables = Statement.USES_VARIABLES_FALSE; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | return epr; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Re-writes {fn convert (expr, type)} as cast(expr AS type) |
| 506 | * |
| 507 | * @param functionToken |
| 508 | * @return |
| 509 | * @throws SQLException |
| 510 | */ |
| 511 | private static String processConvertToken(String functionToken, |
| 512 | boolean serverSupportsConvertFn) throws SQLException { |
| 513 | // The JDBC spec requires these types: |
| 514 | // |
| 515 | // BIGINT |
| 516 | // BINARY |
| 517 | // BIT |
| 518 | // CHAR |
| 519 | // DATE |
| 520 | // DECIMAL |
| 521 | // DOUBLE |
| 522 | // FLOAT |
| 523 | // INTEGER |
| 524 | // LONGVARBINARY |
| 525 | // LONGVARCHAR |
| 526 | // REAL |
| 527 | // SMALLINT |
| 528 | // TIME |
| 529 | // TIMESTAMP |
| 530 | // TINYINT |
| 531 | // VARBINARY |
| 532 | // VARCHAR |
| 533 | |
| 534 | // MySQL supports these types: |
| 535 | // |
| 536 | // BINARY |
| 537 | // CHAR |
| 538 | // DATE |
| 539 | // DATETIME |
| 540 | // SIGNED (integer) |
| 541 | // UNSIGNED (integer) |
| 542 | // TIME |
| 543 | |
| 544 | int firstIndexOfParen = functionToken.indexOf("("); |
| 545 | |
| 546 | if (firstIndexOfParen == -1) { |
| 547 | throw SQLError.createSQLException( |
| 548 | "Syntax error while processing {fn convert (... , ...)} token, missing opening parenthesis in token '" |
| 549 | + functionToken + "'.", |
| 550 | SQLError.SQL_STATE_SYNTAX_ERROR); |
| 551 | } |
| 552 | |
| 553 | int tokenLength = functionToken.length(); |
| 554 | |
| 555 | int indexOfComma = functionToken.lastIndexOf(","); |
| 556 | |
| 557 | if (indexOfComma == -1) { |
| 558 | throw SQLError.createSQLException( |
| 559 | "Syntax error while processing {fn convert (... , ...)} token, missing comma in token '" |
| 560 | + functionToken + "'.", |
| 561 | SQLError.SQL_STATE_SYNTAX_ERROR); |
| 562 | } |
| 563 | |
| 564 | int indexOfCloseParen = functionToken.indexOf(')', indexOfComma); |
| 565 | |
| 566 | if (indexOfCloseParen == -1) { |
| 567 | throw SQLError.createSQLException( |
| 568 | "Syntax error while processing {fn convert (... , ...)} token, missing closing parenthesis in token '" |
| 569 | + functionToken + "'.", |
| 570 | SQLError.SQL_STATE_SYNTAX_ERROR); |
| 571 | |
| 572 | } |
| 573 | |
| 574 | String expression = functionToken.substring(firstIndexOfParen + 1, |
| 575 | indexOfComma); |
| 576 | String type = functionToken.substring(indexOfComma + 1, |
| 577 | indexOfCloseParen); |
| 578 | |
| 579 | String newType = null; |
| 580 | |
| 581 | String trimmedType = type.trim(); |
| 582 | |
| 583 | if (StringUtils.startsWithIgnoreCase(trimmedType, "SQL_")) { |
| 584 | trimmedType = trimmedType.substring(4, trimmedType.length()); |
| 585 | } |
| 586 | |
| 587 | if (serverSupportsConvertFn) { |
| 588 | newType = (String) JDBC_CONVERT_TO_MYSQL_TYPE_MAP.get(trimmedType |
| 589 | .toUpperCase(Locale.ENGLISH)); |
| 590 | } else { |
| 591 | newType = (String) JDBC_NO_CONVERT_TO_MYSQL_EXPRESSION_MAP |
| 592 | .get(trimmedType.toUpperCase(Locale.ENGLISH)); |
| 593 | |
| 594 | // We need a 'special' check here to give a better error message. If |
| 595 | // we're in this |
| 596 | // block, the version of MySQL we're connected to doesn't support |
| 597 | // CAST/CONVERT, |
| 598 | // so we can't re-write some data type conversions |
| 599 | // (date,time,timestamp, datetime) |
| 600 | |
| 601 | if (newType == null) { |
| 602 | throw SQLError.createSQLException( |
| 603 | "Can't find conversion re-write for type '" |
| 604 | + type |
| 605 | + "' that is applicable for this server version while processing escape tokens.", |
| 606 | SQLError.SQL_STATE_GENERAL_ERROR); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | if (newType == null) { |
| 611 | throw SQLError.createSQLException("Unsupported conversion type '" |
| 612 | + type.trim() + "' found while processing escape token.", |
| 613 | SQLError.SQL_STATE_GENERAL_ERROR); |
| 614 | } |
| 615 | |
| 616 | int replaceIndex = newType.indexOf("?"); |
| 617 | |
| 618 | if (replaceIndex != -1) { |
| 619 | StringBuffer convertRewrite = new StringBuffer(newType.substring(0, |
| 620 | replaceIndex)); |
| 621 | convertRewrite.append(expression); |
| 622 | convertRewrite.append(newType.substring(replaceIndex + 1, newType |
| 623 | .length())); |
| 624 | |
| 625 | return convertRewrite.toString(); |
| 626 | } else { |
| 627 | |
| 628 | StringBuffer castRewrite = new StringBuffer("CAST("); |
| 629 | castRewrite.append(expression); |
| 630 | castRewrite.append(" AS "); |
| 631 | castRewrite.append(newType); |
| 632 | castRewrite.append(")"); |
| 633 | |
| 634 | return castRewrite.toString(); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * Removes all whitespace from the given String. We use this to make escape |
| 640 | * token comparison white-space ignorant. |
| 641 | * |
| 642 | * @param toCollapse |
| 643 | * the string to remove the whitespace from |
| 644 | * |
| 645 | * @return a string with _no_ whitespace. |
| 646 | */ |
| 647 | private static String removeWhitespace(String toCollapse) { |
| 648 | if (toCollapse == null) { |
| 649 | return null; |
| 650 | } |
| 651 | |
| 652 | int length = toCollapse.length(); |
| 653 | |
| 654 | StringBuffer collapsed = new StringBuffer(length); |
| 655 | |
| 656 | for (int i = 0; i < length; i++) { |
| 657 | char c = toCollapse.charAt(i); |
| 658 | |
| 659 | if (!Character.isWhitespace(c)) { |
| 660 | collapsed.append(c); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | return collapsed.toString(); |
| 665 | } |
| 666 | } |