📄 escapeprocessor.java
字号:
Time inServerTimezone = TimeUtil .changeTimezone( conn, sessionCalendar, null, toBeAdjusted, sessionCalendar .getTimeZone(), conn .getServerTimezoneTZ(), false); newSql.append("'"); newSql.append(inServerTimezone .toString()); newSql.append("'"); } } catch (NumberFormatException nfe) { throw SQLError .createSQLException( "Syntax error in TIMESTAMP escape sequence '" + token + "'.", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } } } catch (java.util.NoSuchElementException e) { throw SQLError.createSQLException( "Syntax error for escape sequence '" + argument + "'", "42000"); } } } private static void processTimestampToken(ConnectionImpl conn, StringBuffer newSql, String token) throws SQLException { int startPos = token.indexOf('\'') + 1; int endPos = token.lastIndexOf('\''); // no } if ((startPos == -1) || (endPos == -1)) { newSql.append(token); // it's just part of the // query, push possible // syntax errors onto // server's shoulders } else { String argument = token.substring(startPos, endPos); try { if (!conn.getUseLegacyDatetimeCode()) { Timestamp ts = Timestamp.valueOf(argument); SimpleDateFormat tsdf = new SimpleDateFormat( "''yyyy-MM-dd HH:mm:ss''", Locale.US); //$NON-NLS-1$ tsdf .setTimeZone(conn .getServerTimezoneTZ()); newSql.append(tsdf.format(ts)); } else { StringTokenizer st = new StringTokenizer( argument, " .-:"); try { String year4 = st.nextToken(); String month2 = st.nextToken(); String day2 = st.nextToken(); String hour = st.nextToken(); String minute = st.nextToken(); String second = st.nextToken(); /* * For now, we get the fractional * seconds part, but we don't use it, as * MySQL doesn't support it in it's * TIMESTAMP data type * * String fractionalSecond = ""; * * if (st.hasMoreTokens()) { * fractionalSecond = st.nextToken(); } */ /* * Use the full format because number * format will not work for "between" * clauses. * * Ref. Mysql Docs * * You can specify DATETIME, DATE and * TIMESTAMP values using any of a * common set of formats: * * As a string in either 'YYYY-MM-DD * HH:MM:SS' or 'YY-MM-DD HH:MM:SS' * format. * * Thanks to Craig Longman for pointing * out this bug */ if (!conn.getUseTimezone() && !conn .getUseJDBCCompliantTimezoneShift()) { newSql.append("'").append(year4) .append("-").append(month2) .append("-").append(day2) .append(" ").append(hour) .append(":").append(minute) .append(":").append(second) .append("'"); } else { Calendar sessionCalendar; if (conn != null) { sessionCalendar = conn .getCalendarInstanceForSessionOrNew(); } else { sessionCalendar = new GregorianCalendar(); sessionCalendar .setTimeZone(TimeZone .getTimeZone("GMT")); } try { int year4Int = Integer .parseInt(year4); int month2Int = Integer .parseInt(month2); int day2Int = Integer .parseInt(day2); int hourInt = Integer .parseInt(hour); int minuteInt = Integer .parseInt(minute); int secondInt = Integer .parseInt(second); synchronized (sessionCalendar) { boolean useGmtMillis = conn .getUseGmtMillisForDatetimes(); Timestamp toBeAdjusted = TimeUtil .fastTimestampCreate( useGmtMillis, useGmtMillis ? Calendar .getInstance(TimeZone .getTimeZone("GMT")) : null, sessionCalendar, year4Int, month2Int, day2Int, hourInt, minuteInt, secondInt, 0); Timestamp inServerTimezone = TimeUtil .changeTimezone( conn, sessionCalendar, null, toBeAdjusted, sessionCalendar .getTimeZone(), conn .getServerTimezoneTZ(), false); newSql.append("'"); String timezoneLiteral = inServerTimezone .toString(); int indexOfDot = timezoneLiteral .indexOf("."); if (indexOfDot != -1) { timezoneLiteral = timezoneLiteral .substring(0, indexOfDot); } newSql .append(timezoneLiteral); } newSql.append("'"); } catch (NumberFormatException nfe) { throw SQLError .createSQLException( "Syntax error in TIMESTAMP escape sequence '" + token + "'.", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } } } catch (java.util.NoSuchElementException e) { throw SQLError.createSQLException( "Syntax error for TIMESTAMP escape sequence '" + argument + "'", "42000"); } } } catch (IllegalArgumentException illegalArgumentException) { SQLException sqlEx = SQLError .createSQLException( "Syntax error for TIMESTAMP escape sequence '" + argument + "'", "42000"); sqlEx.initCause(illegalArgumentException); throw sqlEx; } } } /** * Re-writes {fn convert (expr, type)} as cast(expr AS type) * * @param functionToken * @return * @throws SQLException */ private static String processConvertToken(String functionToken, boolean serverSupportsConvertFn) throws SQLException { // The JDBC spec requires these types: // // BIGINT // BINARY // BIT // CHAR // DATE // DECIMAL // DOUBLE // FLOAT // INTEGER // LONGVARBINARY // LONGVARCHAR // REAL // SMALLINT // TIME // TIMESTAMP // TINYINT // VARBINARY // VARCHAR // MySQL supports these types: // // BINARY // CHAR // DATE // DATETIME // SIGNED (integer) // UNSIGNED (integer) // TIME int firstIndexOfParen = functionToken.indexOf("("); if (firstIndexOfParen == -1) { throw SQLError .createSQLException( "Syntax error while processing {fn convert (... , ...)} token, missing opening parenthesis in token '" + functionToken + "'.", SQLError.SQL_STATE_SYNTAX_ERROR); } int tokenLength = functionToken.length(); int indexOfComma = functionToken.lastIndexOf(","); if (indexOfComma == -1) { throw SQLError .createSQLException( "Syntax error while processing {fn convert (... , ...)} token, missing comma in token '" + functionToken + "'.", SQLError.SQL_STATE_SYNTAX_ERROR); } int indexOfCloseParen = functionToken.indexOf(')', indexOfComma); if (indexOfCloseParen == -1) { throw SQLError .createSQLException( "Syntax error while processing {fn convert (... , ...)} token, missing closing parenthesis in token '" + functionToken + "'.", SQLError.SQL_STATE_SYNTAX_ERROR); } String expression = functionToken.substring(firstIndexOfParen + 1, indexOfComma); String type = functionToken.substring(indexOfComma + 1, indexOfCloseParen); String newType = null; String trimmedType = type.trim(); if (StringUtils.startsWithIgnoreCase(trimmedType, "SQL_")) { trimmedType = trimmedType.substring(4, trimmedType.length()); } if (serverSupportsConvertFn) { newType = (String) JDBC_CONVERT_TO_MYSQL_TYPE_MAP.get(trimmedType .toUpperCase(Locale.ENGLISH)); } else { newType = (String) JDBC_NO_CONVERT_TO_MYSQL_EXPRESSION_MAP .get(trimmedType.toUpperCase(Locale.ENGLISH)); // We need a 'special' check here to give a better error message. If // we're in this // block, the version of MySQL we're connected to doesn't support // CAST/CONVERT, // so we can't re-write some data type conversions // (date,time,timestamp, datetime) if (newType == null) { throw SQLError .createSQLException( "Can't find conversion re-write for type '" + type + "' that is applicable for this server version while processing escape tokens.", SQLError.SQL_STATE_GENERAL_ERROR); } } if (newType == null) { throw SQLError.createSQLException("Unsupported conversion type '" + type.trim() + "' found while processing escape token.", SQLError.SQL_STATE_GENERAL_ERROR); } int replaceIndex = newType.indexOf("?"); if (replaceIndex != -1) { StringBuffer convertRewrite = new StringBuffer(newType.substring(0, replaceIndex)); convertRewrite.append(expression); convertRewrite.append(newType.substring(replaceIndex + 1, newType .length())); return convertRewrite.toString(); } else { StringBuffer castRewrite = new StringBuffer("CAST("); castRewrite.append(expression); castRewrite.append(" AS "); castRewrite.append(newType); castRewrite.append(")"); return castRewrite.toString(); } } /** * Removes all whitespace from the given String. We use this to make escape * token comparison white-space ignorant. * * @param toCollapse * the string to remove the whitespace from * * @return a string with _no_ whitespace. */ private static String removeWhitespace(String toCollapse) { if (toCollapse == null) { return null; } int length = toCollapse.length(); StringBuffer collapsed = new StringBuffer(length); for (int i = 0; i < length; i++) { char c = toCollapse.charAt(i); if (!Character.isWhitespace(c)) { collapsed.append(c); } } return collapsed.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -