📄 mysqlio.java
字号:
* Does the server send back extra column info?
*
* @return true if so
*/
protected boolean hasLongColumnInfo() {
return this.hasLongColumnInfo;
}
/**
* 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
*/
protected final Field unpackField(Buffer packet,
boolean extractDefaultValues) {
if (this.use41Extensions) {
// we only store the position of the string and
// materialize only if needed...
if (this.has41NewNewProt) {
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();
int charSetNumber = 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;
} else {
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.getBufferSource(),
nameStart, nameLength, tableNameStart, tableNameLength,
colLength, colType, colFlag, colDecimals);
return field;
}
}
/**
* Determines if the database charset is the same as the platform charset
*/
protected void checkForCharsetMismatch() {
if (this.connection.useUnicode()
&& (this.connection.getEncoding() != null)) {
String encodingToCheck = jvmPlatformCharset;
if (encodingToCheck == null) {
encodingToCheck = System.getProperty("file.encoding");
}
if (encodingToCheck == null) {
this.platformDbCharsetMatches = false;
} else {
this.platformDbCharsetMatches = encodingToCheck.equals(this.connection
.getEncoding());
}
}
}
static int getMaxBuf() {
return maxBufferSize;
}
/**
* Get the major version of the MySQL server we are talking to.
*
* @return DOCUMENT ME!
*/
final int getServerMajorVersion() {
return this.serverMajorVersion;
}
/**
* Get the minor version of the MySQL server we are talking to.
*
* @return DOCUMENT ME!
*/
final int getServerMinorVersion() {
return this.serverMinorVersion;
}
/**
* Get the sub-minor version of the MySQL server we are talking to.
*
* @return DOCUMENT ME!
*/
final int getServerSubMinorVersion() {
return this.serverSubMinorVersion;
}
/**
* Get the version string of the server we are talking to
*
* @return DOCUMENT ME!
*/
String getServerVersion() {
return this.serverVersion;
}
/**
* Initialize communications with the MySQL server. Handles logging on, and
* handling initial connection errors.
*
* @param user DOCUMENT ME!
* @param password DOCUMENT ME!
* @param database DOCUMENT ME!
*
* @throws java.sql.SQLException DOCUMENT ME!
* @throws SQLException DOCUMENT ME!
*/
void doHandshake(String user, String password, String database)
throws java.sql.SQLException {
// Read the first packet
Buffer buf = readPacket();
// Get the protocol version
this.protocolVersion = buf.readByte();
if (this.protocolVersion == -1) {
try {
this.mysqlConnection.close();
} catch (Exception e) {
; // ignore
}
int errno = 2000;
errno = buf.readInt();
String serverErrorMessage = buf.readString();
StringBuffer errorBuf = new StringBuffer(" message from server: \"");
errorBuf.append(serverErrorMessage);
errorBuf.append("\"");
String xOpen = SQLError.mysqlToXOpen(errno);
throw new SQLException(SQLError.get(xOpen) + ", "
+ errorBuf.toString(), xOpen, errno);
}
this.serverVersion = buf.readString();
// Parse the server version into major/minor/subminor
int point = this.serverVersion.indexOf(".");
if (point != -1) {
try {
int n = Integer.parseInt(this.serverVersion.substring(0, point));
this.serverMajorVersion = n;
} catch (NumberFormatException NFE1) {
;
}
String remaining = this.serverVersion.substring(point + 1,
this.serverVersion.length());
point = remaining.indexOf(".");
if (point != -1) {
try {
int n = Integer.parseInt(remaining.substring(0, point));
this.serverMinorVersion = n;
} catch (NumberFormatException nfe) {
;
}
remaining = remaining.substring(point + 1, remaining.length());
int pos = 0;
while (pos < remaining.length()) {
if ((remaining.charAt(pos) < '0')
|| (remaining.charAt(pos) > '9')) {
break;
}
pos++;
}
try {
int n = Integer.parseInt(remaining.substring(0, pos));
this.serverSubMinorVersion = n;
} catch (NumberFormatException nfe) {
;
}
}
}
if (versionMeetsMinimum(4, 0, 8)) {
this.maxThreeBytes = (256 * 256 * 256) - 1;
this.useNewLargePackets = true;
} else {
this.maxThreeBytes = 255 * 255 * 255;
this.useNewLargePackets = false;
}
this.colDecimalNeedsBump = versionMeetsMinimum(3, 23, 0);
this.colDecimalNeedsBump = !versionMeetsMinimum(3, 23, 15); // guess? Not noted in changelog
this.useNewUpdateCounts = versionMeetsMinimum(3, 22, 5);
long threadId = buf.readLong();
seed = buf.readString();
if (Driver.TRACE) {
Debug.msg(this, "Protocol Version: " + (int) this.protocolVersion);
Debug.msg(this, "Server Version: " + this.serverVersion);
Debug.msg(this, "Thread ID: " + threadId);
Debug.msg(this, "Crypt Seed: " + seed);
}
int serverCapabilities = 0;
if (buf.getPosition() < buf.getBufLength()) {
serverCapabilities = buf.readInt();
}
if (versionMeetsMinimum(4, 1, 1)) {
int position = buf.getPosition();
/* New protocol with 16 bytes to describe server characteristics */
int serverLanguage = buf.readInt(); // 2 bytes
buf.readInt();
buf.setPosition(position + 16);
String seedPart2 = buf.readString();
StringBuffer newSeed = new StringBuffer(20);
newSeed.append(seed);
newSeed.append(seedPart2);
this.seed = newSeed.toString();
}
if (((serverCapabilities & CLIENT_COMPRESS) != 0)
&& this.connection.useCompression()) {
clientParam |= CLIENT_COMPRESS;
}
if ((database != null) && (database.length() > 0)) {
clientParam |= CLIENT_CONNECT_WITH_DB;
}
if (((serverCapabilities & CLIENT_SSL) == 0)
&& this.connection.useSSL()) {
this.connection.setUseSSL(false);
}
if ((serverCapabilities & CLIENT_LONG_FLAG) != 0) {
// We understand other column flags, as well
clientParam |= CLIENT_LONG_FLAG;
this.hasLongColumnInfo = true;
}
// return FOUND rows
clientParam |= CLIENT_FOUND_ROWS;
if (this.connection.allowLoadLocalInfile()) {
clientParam |= CLIENT_LOCAL_FILES;
}
if (isInteractiveClient) {
clientParam |= CLIENT_INTERACTIVE;
}
// Authenticate
if (this.protocolVersion > 9) {
clientParam |= CLIENT_LONG_PASSWORD; // for long passwords
} else {
clientParam &= ~CLIENT_LONG_PASSWORD;
}
//
// 4.1 has some differences in the protocol
//
if (versionMeetsMinimum(4, 1, 0)) {
if (versionMeetsMinimum(4, 1, 1)) {
clientParam |= CLIENT_PROTOCOL_41;
this.has41NewNewProt = true;
} else {
clientParam |= CLIENT_RESERVED;
this.has41NewNewProt = false;
}
this.use41Extensions = true;
}
int passwordLength = 16;
int userLength = 0;
int databaseLength = 0;
if (user != null) {
userLength = user.length();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -