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

📄 mysqlio.java

📁 SearchPathServer
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			//
			// If this is a generic query, we need to re-use
			// the sending packet.
			//

			if (QueryPacket == null) {
				int pack_length =
					HEADER_LENGTH
						+ COMP_HEADER_LENGTH
						+ 1
						+ (ExtraData != null ? ExtraData.length() : 0)
						+ 2;

				if (_sendPacket == null) {
					_sendPacket = new Buffer(pack_length, _connection.getMaxAllowedPacket());
				}

				_packetSequence = -1;
				_sendPacket.clear();

				// Offset different for compression
				if (_useCompression) {
					_sendPacket._pos += COMP_HEADER_LENGTH;
				}

				_sendPacket.writeByte((byte) command);

				if (command == MysqlDefs.INIT_DB
					|| command == MysqlDefs.CREATE_DB
					|| command == MysqlDefs.DROP_DB
					|| command == MysqlDefs.QUERY) {

					_sendPacket.writeStringNoNull(ExtraData);
				}
				else
					if (command == MysqlDefs.PROCESS_KILL) {
						long id = new Long(ExtraData).longValue();
						_sendPacket.writeLong(id);
					}
					else
						if (command == MysqlDefs.RELOAD && _protocolVersion > 9) {
							Debug.msg(this, "Reload");
							//Packet.writeByte(reloadParam);
						}

				send(_sendPacket);
			}
			else {
				_packetSequence = -1;

				send(QueryPacket); // packet passed by PreparedStatement
			}

		}
		catch (Exception Ex) {

			throw new java.sql.SQLException(
				SQLError.get("08S01") + ": " + Ex.getClass().getName(),
				"08S01",
				0);
		}

		try {

			// Check return value, if we get a java.io.EOFException,
			// the server has gone away. We'll pass it on up the 
			// exception chain and let someone higher up decide
			// what to do (barf, reconnect, etc).

			Ret = readPacket();

			statusCode = Ret.readByte();
		}
		catch (java.io.EOFException EOFE) {
			throw EOFE;
		}
		catch (Exception FallThru) {
			throw new java.sql.SQLException(
				SQLError.get("08S01") + ": " + FallThru.getClass().getName(),
				"08S01",
				0);
		}

		try {

			// Error handling
			if (statusCode == (byte) 0xff) {
				String ErrorMessage;
				int errno = 2000;

				if (_protocolVersion > 9) {
					errno = Ret.readInt();
					ErrorMessage = Ret.readString();
					clearReceive();
					String XOpen = SQLError.mysqlToXOpen(errno);

					throw new java.sql.SQLException(
						SQLError.get(XOpen) + ": " + ErrorMessage,
						XOpen,
						errno);
				}
				else {
					ErrorMessage = Ret.readString();
					clearReceive();

					if (ErrorMessage.indexOf("Unknown column") != -1) {
						throw new java.sql.SQLException(
							SQLError.get("S0022") + ": " + ErrorMessage,
							"S0022",
							-1);
					}
					else {
						throw new java.sql.SQLException(
							SQLError.get("S1000") + ": " + ErrorMessage,
							"S1000",
							-1);
					}
				}
			}
			else
				if (statusCode == 0x00) {
					if (command == MysqlDefs.CREATE_DB || command == MysqlDefs.DROP_DB) {
						java.sql.SQLWarning NW = new java.sql.SQLWarning("Command=" + command + ": ");
						if (_warningChain != null)
							NW.setNextException(_warningChain);
						_warningChain = NW;
					}
				}
				else
					if (Ret.isLastDataPacket()) {
						java.sql.SQLWarning NW = new java.sql.SQLWarning("Command=" + command + ": ");
						if (_warningChain != null)
							NW.setNextException(_warningChain);
						_warningChain = NW;
					}
			return Ret;
		}
		catch (IOException E) {
			throw new java.sql.SQLException(
				SQLError.get("08S01") + ": " + E.getClass().getName(),
				"08S01",
				0);
		}
	}

	/**
	 * Send a query stored in a packet directly to the server.
	 */

	final ResultSet sqlQueryDirect(
		Buffer QueryPacket,
		int max_rows,
		Connection Conn)
		throws Exception {
		long updateCount = -1;
		long updateID = -1;


		StringBuffer profileMsgBuf = null; // used if profiling
		long queryStartTime = 0;
		
		if (_profileSql)
		{
			profileMsgBuf = new StringBuffer();
			queryStartTime = System.currentTimeMillis();
			byte[] queryBuf = QueryPacket._buf;
			
			// Extract the actual query from the network packet
			String query = new String(queryBuf, 5, (QueryPacket._pos - 5));
			
			profileMsgBuf.append("Query\t\"");
			profileMsgBuf.append(query);
			profileMsgBuf.append("\"\texecution time:\t");	
		}
		
		// Send query command and sql query string
		clearAllReceive();

		Buffer Packet = sendCommand(MysqlDefs.QUERY, null, QueryPacket);

		if (_profileSql)
		{
			long executionTime = System.currentTimeMillis() - queryStartTime;
			
			profileMsgBuf.append(executionTime);
			profileMsgBuf.append("\t");
		}		
		
		Packet._pos--;

		long columnCount = Packet.readLength();

		if (Driver.trace) {
			Debug.msg(this, "Column count: " + columnCount);
		}

		
			
		if (columnCount == 0) {
			try {

				if (versionMeetsMinimum(3, 22, 5)) {
					updateCount = Packet.newReadLength();
					updateID = Packet.newReadLength();

					//
					// Everyone else expects what MySQL calls
					// "rows matched" for an update count
					//

					//String ExtraInfo = Packet.readString();

					//if (ExtraInfo.length() > 0) {
					//long newUpdateCount = getMatchedRows(ExtraInfo);

					//if (newUpdateCount > -1) {
					//    updateCount = newUpdateCount;
					//}
					// }
				}
				else {
					updateCount = (long) Packet.readLength();
					updateID = (long) Packet.readLength();
				}
			}
			catch (Exception E) {
				throw new java.sql.SQLException(
					SQLError.get("S1000") + ": " + E.getClass().getName(),
					"S1000",
					-1);
			}

			if (Driver.trace) {
				Debug.msg(this, "Update Count = " + updateCount);
			}

			if (_profileSql)
			{
				System.err.println(profileMsgBuf.toString());
			}
			
			return buildResultSetWithUpdates(updateCount, updateID, Conn);
		}
		else {
			
			long fetchStartTime = 0;
			
			if (_profileSql)
			{
				fetchStartTime = System.currentTimeMillis();
			}
			
			com.mysql.jdbc.ResultSet results = getResultSet(columnCount, max_rows);
			
			if (_profileSql)
			{
				long fetchElapsedTime = System.currentTimeMillis() - fetchStartTime;
				
				profileMsgBuf.append("result set fetch time:\t");
				profileMsgBuf.append(fetchElapsedTime);
			}
			
			return results;
		}
	}

	/**
	 * Send a query specified in the String "Query" to the MySQL server.
	 *
	 * This method uses the specified character encoding to get the
	 * bytes from the query string.
	 */

	final ResultSet sqlQuery(
		String Query,
		int max_rows,
		String Encoding,
		Connection Conn)
		throws Exception {
			
		
		
		// We don't know exactly how many bytes we're going to get 
		// from the query. Since we're dealing with Unicode, the 
		// max is 2, so pad it (2 * query) + space for headers 

		int pack_length = HEADER_LENGTH + 1 + (Query.length() * 2) + 2;

		if (_sendPacket == null) {
			_sendPacket = new Buffer(pack_length, _connection.getMaxAllowedPacket());
		}
		else {
			_sendPacket.clear();
		}

		_sendPacket.writeByte((byte) MysqlDefs.QUERY);

		if (Encoding != null) {
			_sendPacket.writeStringNoNull(Query, Encoding);
		}
		else {
			_sendPacket.writeStringNoNull(Query);
		}

		return sqlQueryDirect(_sendPacket, max_rows, Conn);
	}

	final ResultSet sqlQuery(String Query, int max_rows, String Encoding)
		throws Exception {
		return sqlQuery(Query, max_rows, Encoding, null);
	}

	final ResultSet sqlQuery(String Query, int max_rows) throws Exception {
		
		StringBuffer profileMsgBuf = null; // used if profiling
		long queryStartTime = 0;
		
		if (_profileSql)
		{
			profileMsgBuf = new StringBuffer();
			queryStartTime = System.currentTimeMillis();
		
			profileMsgBuf.append("Query\t\"");
			profileMsgBuf.append(Query);
			profileMsgBuf.append("\"\texecution time:\t");	
		}
		


		// Send query command and sql query string
		clearAllReceive();
		Buffer Packet = sendCommand(MysqlDefs.QUERY, Query, null); //, (byte)0);
		
		if (_profileSql)
		{
			long executionTime = System.currentTimeMillis() - queryStartTime;
			
			profileMsgBuf.append(executionTime);
			profileMsgBuf.append("\t");
		}		
		
		Packet._pos--;

		long columnCount = Packet.readLength();

		if (Driver.trace) {
			Debug.msg(this, "Column count: " + columnCount);
		}

		if (columnCount == 0) {
			
			long updateCount = -1;
			long updateID = -1;
			
			try {

				if (versionMeetsMinimum(3, 22, 5)) {
					updateCount = Packet.newReadLength();
					updateID = Packet.newReadLength();

					//
					// Everyone else expects what MySQL calls
					// "rows matched" for an update count
					//

					String ExtraInfo = Packet.readString();

					if (ExtraInfo.length() > 0) {
						long newUpdateCount = getMatchedRows(ExtraInfo);

						if (newUpdateCount > -1) {
							updateCount = newUpdateCount;
						}
					}
				}
				else {
					updateCount = (long) Packet.readLength();
					updateID = (long) Packet.readLength();
				}
			}
			catch (Exception E) {
				throw new java.sql.SQLException(
					SQLError.get("S1000") + ": " + E.getClass().getName(),
					"S1000",
					-1);
			}

			if (Driver.trace) {
				Debug.msg(this, "Update Count = " + updateCount);
			}

			if (_profileSql)
			{
				System.err.println(profileMsgBuf.toString());
			}
			
			return buildResultSetWithUpdates(updateCount, updateID, null);
		}
		else {
			long fetchStartTime = 0;
			
			if (_profileSql)
			{
				fetchStartTime = System.currentTimeMillis();
			}
			
			com.mysql.jdbc.ResultSet results = getResultSet(columnCount, max_rows);
			
			if (_profileSql)
			{
				long fetchElapsedTime = System.currentTimeMillis() - fetchStartTime;
				
				profileMsgBuf.append("result set fetch time:\t");
				profileMsgBuf.append(fetchElapsedTime);
			}
			
			return results;
		}
	}

	/**
	 * Build a result set. Delegates to buildResultSetWithRows() to
	 * build a JDBC-version-specific ResultSet, given rows as byte
	 * data, and field information.
	 */

	protected ResultSet getResultSet(long columnCount, int max_rows)
		throws Exception {
		Buffer Packet; // The packet from the server

		Field[] Fields = new Field[(int) columnCount];

		// Read in the column information
		for (int i = 0; i < columnCount; i++) {

⌨️ 快捷键说明

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