📄 mysqlio.java
字号:
for (int i = 0; i < columnCount; i++) { int sw = this.mysqlInput.read() & 0xff; remaining--; if (firstTime) { if (sw == 255) { // error packet - we assemble it whole for "fidelity" // in case we ever need an entire packet in checkErrorPacket() // but we could've gotten away with just writing the error code // and message in it (for now). Buffer errorPacket = new Buffer(packetLength + HEADER_LENGTH); errorPacket.setPosition(0); errorPacket.writeByte(this.packetHeaderBuf[0]); errorPacket.writeByte(this.packetHeaderBuf[1]); errorPacket.writeByte(this.packetHeaderBuf[2]); errorPacket.writeByte((byte) 1); errorPacket.writeByte((byte)sw); readFully(this.mysqlInput, errorPacket.getByteBuffer(), 5, packetLength - 1); errorPacket.setPosition(4); checkErrorPacket(errorPacket); } if (sw == 254 && packetLength < 9) { if (this.use41Extensions) { this.warningCount = (this.mysqlInput.read() & 0xff) | ((this.mysqlInput.read() & 0xff) << 8); remaining -= 2; if (this.warningCount > 0) { this.hadWarnings = true; // this is a // 'latch', it's // reset by // sendCommand() } this.oldServerStatus = this.serverStatus; this.serverStatus = (this.mysqlInput.read() & 0xff) | ((this.mysqlInput.read() & 0xff) << 8); checkTransactionState(oldServerStatus); remaining -= 2; if (remaining > 0) { skipFully(this.mysqlInput, remaining); } } return null; // last data packet } rowData = new byte[columnCount][]; firstTime = false; } int len = 0; switch (sw) { case 251: len = NULL_LENGTH; break; case 252: len = (this.mysqlInput.read() & 0xff) | ((this.mysqlInput.read() & 0xff) << 8); remaining -= 2; break; case 253: len = (this.mysqlInput.read() & 0xff) | ((this.mysqlInput.read() & 0xff) << 8) | ((this.mysqlInput.read() & 0xff) << 16); remaining -= 3; break; case 254: len = (int) ((this.mysqlInput.read() & 0xff) | ((long) (this.mysqlInput.read() & 0xff) << 8) | ((long) (this.mysqlInput.read() & 0xff) << 16) | ((long) (this.mysqlInput.read() & 0xff) << 24) | ((long) (this.mysqlInput.read() & 0xff) << 32) | ((long) (this.mysqlInput.read() & 0xff) << 40) | ((long) (this.mysqlInput.read() & 0xff) << 48) | ((long) (this.mysqlInput.read() & 0xff) << 56)); remaining -= 8; break; default: len = sw; } if (len == NULL_LENGTH) { rowData[i] = null; } else if (len == 0) { rowData[i] = Constants.EMPTY_BYTE_ARRAY; } else { rowData[i] = new byte[len]; int bytesRead = readFully(this.mysqlInput, rowData[i], 0, len); if (bytesRead != len) { throw SQLError.createCommunicationsException(this.connection, this.lastPacketSentTimeMs, this.lastPacketReceivedTimeMs, new IOException(Messages.getString("MysqlIO.43"))); } remaining -= bytesRead; } } if (remaining > 0) { skipFully(this.mysqlInput, remaining); } return new ByteArrayRow(rowData); } catch (IOException ioEx) { throw SQLError.createCommunicationsException(this.connection, this.lastPacketSentTimeMs, this.lastPacketReceivedTimeMs, ioEx); } } /** * Log-off of the MySQL server and close the socket. * * @throws SQLException DOCUMENT ME! */ final void quit() throws SQLException { Buffer packet = new Buffer(6); this.packetSequence = -1; packet.writeByte((byte) MysqlDefs.QUIT); send(packet, packet.getPosition()); forceClose(); } /** * Returns the packet used for sending data (used by PreparedStatement) * Guarded by external synchronization on a mutex. * * @return A packet to send data with */ Buffer getSharedSendPacket() { if (this.sharedSendPacket == null) { this.sharedSendPacket = new Buffer(INITIAL_PACKET_SIZE); } return this.sharedSendPacket; } void closeStreamer(RowData streamer) throws SQLException { if (this.streamingData == null) { throw SQLError.createSQLException(Messages.getString("MysqlIO.17") //$NON-NLS-1$ +streamer + Messages.getString("MysqlIO.18")); //$NON-NLS-1$ } if (streamer != this.streamingData) { throw SQLError.createSQLException(Messages.getString("MysqlIO.19") //$NON-NLS-1$ +streamer + Messages.getString("MysqlIO.20") //$NON-NLS-1$ +Messages.getString("MysqlIO.21") //$NON-NLS-1$ +Messages.getString("MysqlIO.22")); //$NON-NLS-1$ } this.streamingData = null; } boolean tackOnMoreStreamingResults(ResultSetImpl addingTo) throws SQLException { if ((this.serverStatus & SERVER_MORE_RESULTS_EXISTS) != 0) { boolean moreRowSetsExist = true; ResultSetImpl currentResultSet = addingTo; boolean firstTime = true; while (moreRowSetsExist) { if (!firstTime && currentResultSet.reallyResult()) { break; } firstTime = false; Buffer fieldPacket = checkErrorPacket(); fieldPacket.setPosition(0); java.sql.Statement owningStatement = addingTo.getStatement(); int maxRows = owningStatement.getMaxRows(); // fixme for catalog, isBinary ResultSetImpl newResultSet = readResultsForQueryOrUpdate( (StatementImpl)owningStatement, maxRows, owningStatement.getResultSetType(), owningStatement.getResultSetConcurrency(), true, owningStatement.getConnection().getCatalog(), fieldPacket, addingTo.isBinaryEncoded, -1L, null); currentResultSet.setNextResultSet(newResultSet); currentResultSet = newResultSet; moreRowSetsExist = (this.serverStatus & MysqlIO.SERVER_MORE_RESULTS_EXISTS) != 0; if (!currentResultSet.reallyResult() && !moreRowSetsExist) { // special case, we can stop "streaming" return false; } } return true; } return false; } ResultSetImpl readAllResults(StatementImpl callingStatement, int maxRows, int resultSetType, int resultSetConcurrency, boolean streamResults, String catalog, Buffer resultPacket, boolean isBinaryEncoded, long preSentColumnCount, Field[] metadataFromCache) throws SQLException { resultPacket.setPosition(resultPacket.getPosition() - 1); ResultSetImpl topLevelResultSet = readResultsForQueryOrUpdate(callingStatement, maxRows, resultSetType, resultSetConcurrency, streamResults, catalog, resultPacket, isBinaryEncoded, preSentColumnCount, metadataFromCache); ResultSetImpl currentResultSet = topLevelResultSet; boolean checkForMoreResults = ((this.clientParam & CLIENT_MULTI_RESULTS) != 0); boolean serverHasMoreResults = (this.serverStatus & SERVER_MORE_RESULTS_EXISTS) != 0; // // TODO: We need to support streaming of multiple result sets // if (serverHasMoreResults && streamResults) { //clearInputStream();// //throw SQLError.createSQLException(Messages.getString("MysqlIO.23"), //$NON-NLS-1$ //SQLError.SQL_STATE_DRIVER_NOT_CAPABLE); if (topLevelResultSet.getUpdateCount() != -1) { tackOnMoreStreamingResults(topLevelResultSet); } reclaimLargeReusablePacket(); return topLevelResultSet; } boolean moreRowSetsExist = checkForMoreResults & serverHasMoreResults; while (moreRowSetsExist) { Buffer fieldPacket = checkErrorPacket(); fieldPacket.setPosition(0); ResultSetImpl newResultSet = readResultsForQueryOrUpdate(callingStatement, maxRows, resultSetType, resultSetConcurrency, streamResults, catalog, fieldPacket, isBinaryEncoded, preSentColumnCount, metadataFromCache); currentResultSet.setNextResultSet(newResultSet); currentResultSet = newResultSet; moreRowSetsExist = (this.serverStatus & SERVER_MORE_RESULTS_EXISTS) != 0; } if (!streamResults) { clearInputStream(); } reclaimLargeReusablePacket(); return topLevelResultSet; } /** * Sets the buffer size to max-buf */ void resetMaxBuf() { this.maxAllowedPacket = this.connection.getMaxAllowedPacket(); } /** * Send a command to the MySQL server If data is to be sent with command, * it should be put in extraData. * * Raw packets can be sent by setting queryPacket to something other * than null. * * @param command the MySQL protocol 'command' from MysqlDefs * @param extraData any 'string' data for the command * @param queryPacket a packet pre-loaded with data for the protocol (i.e. * from a client-side prepared statement). * @param skipCheck do not call checkErrorPacket() if true * @param extraDataCharEncoding the character encoding of the extraData * parameter. * * @return the response packet from the server * * @throws SQLException if an I/O error or SQL error occurs */ final Buffer sendCommand(int command, String extraData, Buffer queryPacket, boolean skipCheck, String extraDataCharEncoding, int timeoutMillis) throws SQLException { this.commandCount++; // // We cache these locally, per-command, as the checks // for them are in very 'hot' sections of the I/O code // and we save 10-15% in overall performance by doing this... // this.enablePacketDebug = this.connection.getEnablePacketDebug(); this.traceProtocol = this.connection.getTraceProtocol(); this.readPacketSequence = 0; int oldTimeout = 0; if (timeoutMillis != 0) { try { oldTimeout = this.mysqlConnection.getSoTimeout(); this.mysqlConnection.setSoTimeout(timeoutMillis); } catch (SocketException e) { throw SQLError.createCommunicationsException(this.connection, lastPacketSentTimeMs, lastPacketReceivedTimeMs, e); } } try { checkForOutstandingStreamingData(); // Clear serverStatus...this value is guarded by an // external mutex, as you can only ever be processing // one command at a time this.oldServerStatus = this.serverStatus; this.serverStatus = 0; this.hadWarnings = false; this.warningCount = 0; this.queryNoIndexUsed = false; this.queryBadIndexUsed = false; // // Compressed input stream needs cleared at beginning // of each command execution... // if (this.useCompression) { int bytesLeft = this.mysqlInput.available(); if (bytesLeft > 0) { this.mysqlInput.skip(bytesLeft); } } try { clearInputStream(); // // PreparedStatements construct their own packets, // for efficiency's sake. // // If this is a generic query, we need to re-use // the sending packet. // if (queryPacket == null) { int packLength = HEADER_LENGTH + COMP_HEADER_LENGTH + 1 + ((extraData != null) ? extraData.length() : 0) + 2; if (this.sendPacket == null) { this.sendPacket = new Buffer(packLength); } this.packetSequence = -1; this.read
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -