⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 escapeprocessor.java

📁 mysql的jdbc驱动
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Copyright (C) 2002-2004 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as  published by the Free Software Foundation. There are special exceptions to the terms and conditions of the GPL  as it is applied to this software. View the full text of the  exception in file EXCEPTIONS-CONNECTOR-J in the directory of this  software distribution. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *//** * EscapeProcessor performs all escape code processing as outlined in the JDBC * spec by JavaSoft. */package com.mysql.jdbc;import java.sql.SQLException;import java.util.Collections;import java.util.HashMap;import java.util.Locale;import java.util.Map;import java.util.StringTokenizer;class EscapeProcessor {	private static Map JDBC_CONVERT_TO_MYSQL_TYPE_MAP;	private static Map JDBC_NO_CONVERT_TO_MYSQL_EXPRESSION_MAP;	static {		Map tempMap = new HashMap();		tempMap.put("BIGINT", "0 + ?");		tempMap.put("BINARY", "BINARY");		tempMap.put("BIT", "0 + ?");		tempMap.put("CHAR", "CHAR");		tempMap.put("DATE", "DATE");		tempMap.put("DECIMAL", "0.0 + ?");		tempMap.put("DOUBLE", "0.0 + ?");		tempMap.put("FLOAT", "0.0 + ?");		tempMap.put("INTEGER", "0 + ?");		tempMap.put("LONGVARBINARY", "BINARY");		tempMap.put("LONGVARCHAR", "CONCAT(?)");		tempMap.put("REAL", "0.0 + ?");		tempMap.put("SMALLINT", "CONCAT(?)");		tempMap.put("TIME", "TIME");		tempMap.put("TIMESTAMP", "DATETIME");		tempMap.put("TINYINT", "CONCAT(?)");		tempMap.put("VARBINARY", "BINARY");		tempMap.put("VARCHAR", "CONCAT(?)");		JDBC_CONVERT_TO_MYSQL_TYPE_MAP = Collections.unmodifiableMap(tempMap);		tempMap = new HashMap(JDBC_CONVERT_TO_MYSQL_TYPE_MAP);		tempMap.put("BINARY", "CONCAT(?)");		tempMap.put("CHAR", "CONCAT(?)");		tempMap.remove("DATE");		tempMap.put("LONGVARBINARY", "CONCAT(?)");		tempMap.remove("TIME");		tempMap.remove("TIMESTAMP");		tempMap.put("VARBINARY", "CONCAT(?)");		JDBC_NO_CONVERT_TO_MYSQL_EXPRESSION_MAP = Collections				.unmodifiableMap(tempMap);	}	/**	 * Escape process one string	 * 	 * @param sql	 *            the SQL to escape process.	 * 	 * @return the SQL after it has been escape processed.	 * 	 * @throws java.sql.SQLException	 *             DOCUMENT ME!	 * @throws SQLException	 *             DOCUMENT ME!	 */	public static final Object escapeSQL(String sql,			boolean serverSupportsConvertFn) throws java.sql.SQLException {		boolean replaceEscapeSequence = false;		String escapeSequence = null;		if (sql == null) {			return null;		}		/*		 * Short circuit this code if we don't have a matching pair of "{}". -		 * Suggested by Ryan Gustafason		 */		int beginBrace = sql.indexOf('{');		int nextEndBrace = (beginBrace == -1) ? (-1) : sql.indexOf('}',				beginBrace);		if (nextEndBrace == -1) {			return sql;		}		StringBuffer newSql = new StringBuffer();		EscapeTokenizer escapeTokenizer = new EscapeTokenizer(sql);		byte usesVariables = Statement.USES_VARIABLES_FALSE;		boolean callingStoredFunction = false;		while (escapeTokenizer.hasMoreTokens()) {			String token = escapeTokenizer.nextToken();			if (token.length() != 0) {				if (token.charAt(0) == '{') { // It's an escape code					if (!token.endsWith("}")) {						throw new SQLException("Not a valid escape sequence: "								+ token);					}					if (token.length() > 2) {						int nestedBrace = token.indexOf('{', 2);						if (nestedBrace != -1) {							StringBuffer buf = new StringBuffer(token									.substring(0, 1));							Object remainingResults = escapeSQL(token									.substring(1, token.length() - 1),									serverSupportsConvertFn);							String remaining = null;							if (remainingResults instanceof String) {								remaining = (String) remainingResults;							} else {								remaining = ((EscapeProcessorResult) remainingResults).escapedSql;								if (usesVariables != Statement.USES_VARIABLES_TRUE) {									usesVariables = ((EscapeProcessorResult) remainingResults).usesVariables;								}							}							buf.append(remaining);							buf.append('}');							token = buf.toString();						}					}					// nested escape code					// Compare to tokens with _no_ whitespace					String collapsedToken = removeWhitespace(token);					/*					 * Process the escape code					 */					if (StringUtils.startsWithIgnoreCase(collapsedToken,							"{escape")) {						try {							StringTokenizer st = new StringTokenizer(token,									" '");							st.nextToken(); // eat the "escape" token							escapeSequence = st.nextToken();							if (escapeSequence.length() < 3) {								throw new SQLException(										"Syntax error for escape sequence '"												+ token + "'", "42000");							}							escapeSequence = escapeSequence.substring(1,									escapeSequence.length() - 1);							replaceEscapeSequence = true;						} catch (java.util.NoSuchElementException e) {							throw new SQLException(									"Syntax error for escape sequence '"											+ token + "'", "42000");						}					} else if (StringUtils.startsWithIgnoreCase(collapsedToken,							"{fn")) {						int startPos = token.toLowerCase().indexOf("fn ") + 3;						int endPos = token.length() - 1; // no }						String fnToken = token.substring(startPos, endPos);						// We need to handle 'convert' by ourselves						if (StringUtils.startsWithIgnoreCaseAndWs(fnToken,								"convert")) {							newSql.append(processConvertToken(fnToken,									serverSupportsConvertFn));						} else {							// just pass functions right to the DB							newSql.append(fnToken);						}					} else if (StringUtils.startsWithIgnoreCase(collapsedToken,							"{d")) {						int startPos = token.indexOf('\'') + 1;						int endPos = token.lastIndexOf('\''); // no }						if ((startPos == -1) || (endPos == -1)) {							throw new SQLException(									"Syntax error for DATE escape sequence '"											+ token + "'", "42000");						}						String argument = token.substring(startPos, endPos);						try {							StringTokenizer st = new StringTokenizer(argument,									" -");							String year4 = st.nextToken();							String month2 = st.nextToken();							String day2 = st.nextToken();							String dateString = "'" + year4 + "-" + month2									+ "-" + day2 + "'";							newSql.append(dateString);						} catch (java.util.NoSuchElementException e) {							throw new SQLException(									"Syntax error for DATE escape sequence '"											+ argument + "'", "42000");						}					} else if (StringUtils.startsWithIgnoreCase(collapsedToken,							"{ts")) {						int startPos = token.indexOf('\'') + 1;						int endPos = token.lastIndexOf('\''); // no }						if ((startPos == -1) || (endPos == -1)) {							throw new SQLException(									"Syntax error for TIMESTAMP escape sequence '"											+ token + "'", "42000");						}						String argument = token.substring(startPos, endPos);						try {							StringTokenizer st = new StringTokenizer(argument,									" .-:");							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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -