📄 mysqlio.java
字号:
* * @return a result set * * @throws SQLException if a database access error occurs */ protected ResultSet getResultSet(Statement callingStatement, long columnCount, int maxRows, int resultSetType, int resultSetConcurrency, boolean streamResults, String catalog, boolean isBinaryEncoded, boolean unpackFieldInfo) throws SQLException { Buffer packet; // The packet from the server Field[] fields = null; if (unpackFieldInfo) { fields = new Field[(int) columnCount]; } // Read in the column information for (int i = 0; i < columnCount; i++) { Buffer fieldPacket = null; if (this.useNewIo) { // allocations of NIO buffers are _very_, _very_ // slow...(this code used to run 10x slower than // it does now) so it is actually much faster to re-use, // extract the bytes, and wrap it as a plain-old // byte array packet. packet = reuseAndReadPacket(this.reusablePacket); if (unpackFieldInfo) { fieldPacket = new ByteArrayBuffer(packet.getByteBuffer()); } } else { fieldPacket = readPacket(); } if (unpackFieldInfo) { fields[i] = unpackField(fieldPacket, false); } } packet = reuseAndReadPacket(this.reusablePacket); RowData rowData = null; if (!streamResults) { rowData = readSingleRowSet(columnCount, maxRows, resultSetConcurrency, isBinaryEncoded, fields); } else { rowData = new RowDataDynamic(this, (int) columnCount, fields, isBinaryEncoded); this.streamingData = rowData; } ResultSet rs = buildResultSetWithRows(callingStatement, catalog, fields, rowData, resultSetType, resultSetConcurrency, isBinaryEncoded); return rs; } /** * Forcibly closes the underlying socket to MySQL. */ protected final void forceClose() { try { if (this.mysqlInput != null) { this.mysqlInput.close(); } } catch (IOException ioEx) { // we can't do anything constructive about this // Let the JVM clean it up later this.mysqlInput = null; } try { if (this.mysqlOutput != null) { this.mysqlOutput.close(); } } catch (IOException ioEx) { // we can't do anything constructive about this // Let the JVM clean it up later this.mysqlOutput = null; } try { if (this.mysqlConnection != null) { this.mysqlConnection.close(); } } catch (IOException ioEx) { // we can't do anything constructive about this // Let the JVM clean it up later this.mysqlConnection = null; } } /** * Read one packet from the MySQL server * * @return the packet from the server. * * @throws SQLException DOCUMENT ME! * @throws CommunicationsException DOCUMENT ME! */ protected final Buffer readPacket() throws SQLException { try { if (!this.useNewIo) { int lengthRead = readFully(this.mysqlInput, this.packetHeaderBuf, 0, 4); if (lengthRead < 4) { forceClose(); throw new IOException(Messages.getString("MysqlIO.1")); //$NON-NLS-1$ } int packetLength = (this.packetHeaderBuf[0] & 0xff) + ((this.packetHeaderBuf[1] & 0xff) << 8) + ((this.packetHeaderBuf[2] & 0xff) << 16); if (this.traceProtocol) { StringBuffer traceMessageBuf = new StringBuffer(); traceMessageBuf.append(Messages.getString("MysqlIO.2")); //$NON-NLS-1$ traceMessageBuf.append(packetLength); traceMessageBuf.append(Messages.getString("MysqlIO.3")); //$NON-NLS-1$ traceMessageBuf.append(StringUtils.dumpAsHex( this.packetHeaderBuf, 4)); this.connection.getLog().logTrace(traceMessageBuf.toString()); } byte multiPacketSeq = this.packetHeaderBuf[3]; if (!this.packetSequenceReset) { if (this.enablePacketDebug && this.checkPacketSequence) { checkPacketSequencing(multiPacketSeq); } } else { this.packetSequenceReset = false; } this.readPacketSequence = multiPacketSeq; // Read data byte[] buffer = new byte[packetLength + 1]; int numBytesRead = readFully(this.mysqlInput, buffer, 0, packetLength); if (numBytesRead != packetLength) { throw new IOException("Short read, expected " + packetLength + " bytes, only read " + numBytesRead); } buffer[packetLength] = 0; Buffer packet = Buffer.allocateNew(buffer, this.useNewIo); packet.setBufLength(packetLength + 1); if (this.traceProtocol) { StringBuffer traceMessageBuf = new StringBuffer(); traceMessageBuf.append(Messages.getString("MysqlIO.4")); //$NON-NLS-1$ traceMessageBuf.append(getPacketDumpToLog(packet, packetLength)); this.connection.getLog().logTrace(traceMessageBuf.toString()); } if (this.enablePacketDebug) { enqueuePacketForDebugging(false, false, 0, this.packetHeaderBuf, packet); } return packet; } return readViaChannel(); } catch (IOException ioEx) { throw new CommunicationsException(this.connection, this.lastPacketSentTimeMs, ioEx); } } /** * Unpacks the Field information from the given packet. Understands pre 4.1 * and post 4.1 server version field packet structures. * * @param packet the packet containing the field information * @param extractDefaultValues should default values be extracted? * * @return the unpacked field * * @throws SQLException DOCUMENT ME! */ protected final Field unpackField(Buffer packet, boolean extractDefaultValues) throws SQLException { if (this.use41Extensions) { // we only store the position of the string and // materialize only if needed... if (this.has41NewNewProt) { // Not used yet, 5.0? int catalogNameStart = packet.getPosition() + 1; int catalogNameLength = packet.fastSkipLenString(); } int databaseNameStart = packet.getPosition() + 1; int databaseNameLength = packet.fastSkipLenString(); int tableNameStart = packet.getPosition() + 1; int tableNameLength = packet.fastSkipLenString(); // orgTableName is never used so skip int originalTableNameStart = packet.getPosition() + 1; int originalTableNameLength = packet.fastSkipLenString(); // we only store the position again... int nameStart = packet.getPosition() + 1; int nameLength = packet.fastSkipLenString(); // orgColName is not required so skip... int originalColumnNameStart = packet.getPosition() + 1; int originalColumnNameLength = packet.fastSkipLenString(); packet.readByte(); short charSetNumber = (short) packet.readInt(); int colLength = 0; if (this.has41NewNewProt) { // fixme colLength = (int) packet.readLong(); } else { colLength = packet.readLongInt(); } int colType = packet.readByte() & 0xff; short colFlag = 0; if (this.hasLongColumnInfo) { colFlag = (short) packet.readInt(); } else { colFlag = (short) (packet.readByte() & 0xff); } int colDecimals = packet.readByte() & 0xff; int defaultValueStart = -1; int defaultValueLength = -1; if (extractDefaultValues) { defaultValueStart = packet.getPosition() + 1; defaultValueLength = packet.fastSkipLenString(); } Field field = new Field(this.connection, packet.getByteBuffer(), databaseNameStart, databaseNameLength, tableNameStart, tableNameLength, originalTableNameStart, originalTableNameLength, nameStart, nameLength, originalColumnNameStart, originalColumnNameLength, colLength, colType, colFlag, colDecimals, defaultValueStart, defaultValueLength, charSetNumber); return field; } int tableNameStart = packet.getPosition() + 1; int tableNameLength = packet.fastSkipLenString(); int nameStart = packet.getPosition() + 1; int nameLength = packet.fastSkipLenString(); int colLength = packet.readnBytes(); int colType = packet.readnBytes(); packet.readByte(); // We know it's currently 2 short colFlag = 0; if (this.hasLongColumnInfo) { colFlag = (short) (packet.readInt()); } else { colFlag = (short) (packet.readByte() & 0xff); } int colDecimals = (packet.readByte() & 0xff); if (this.colDecimalNeedsBump) { colDecimals++; } Field field = new Field(this.connection, packet.getByteBuffer(), nameStart, nameLength, tableNameStart, tableNameLength, colLength, colType, colFlag, colDecimals); return field; } protected boolean isSetNeededForAutoCommitMode(boolean autoCommitFlag) { if (this.use41Extensions && this.connection.getElideSetAutoCommits()) { boolean autoCommitModeOnServer = ((this.serverStatus & SERVER_STATUS_AUTOCOMMIT) != 0); if (!autoCommitFlag) { // Just to be safe, check if a transaction is in progress on the server.... // if so, then we must be in autoCommit == false // therefore return the opposite of transaction status boolean inTransactionOnServer = ((this.serverStatus & SERVER_STATUS_IN_TRANS) != 0); return !inTransactionOnServer; } return !autoCommitModeOnServer; } return true; } /** * Re-authenticates as the given user and password * * @param userName DOCUMENT ME! * @param password DOCUMENT ME! * @param database DOCUMENT ME! * * @throws SQLException DOCUMENT ME! */ protected void changeUser(String userName, String password, String database) throws SQLException { this.packetSequence = -1; int passwordLength = 16; int userLength = 0; if (userName != null) { userLength = userName.length(); } int packLength = (userLength + passwordLength) + 7 + HEADER_LENGTH; if ((this.serverCapabilities & CLIENT_SECURE_CONNECTION) != 0) { Buffer changeUserPacket = Buffer.allocateNew(packLength + 1, this.useNewIo); changeUserPacket.writeByte((byte) MysqlDefs.COM_CHANGE_USER); if (versionMeetsMinimum(4, 1, 1)) { secureAuth411(changeUserPacket, packLength, userName, password, database, false); } else { secureAuth(changeUserPacket, packLength, userName, password, database, false); } } else { // Passwords can be 16 chars long Buffer packet = Buffer.allocateNew(packLength, this.useNewIo); packet.writeByte((byte) MysqlDefs.COM_CHANGE_USER); // User/Password data packet.writeString(userName); if (this.protocolVersion > 9) { packet.writeString(Util.newCrypt(password, this.seed)); } else { packet.writeString(Util.oldCrypt(password, this.seed)); } if (((this.serverCapabilities & CLIENT_CONNECT_WITH_DB) != 0) && (database != null) && (database.length() > 0)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -