📄 serverpreparedstatement.java
字号:
this.eventSink.consumeEvent(new ProfilerEvent( ProfilerEvent.TYPE_PREPARE, "", this.currentCatalog, //$NON-NLS-1$ this.connection.getId(), this.statementId, -1, System.currentTimeMillis(), (int) (System.currentTimeMillis() - begin), null, new Throwable(), sql)); } if (this.parameterCount > 0) { if (this.connection.versionMeetsMinimum(4, 1, 2) && !mysql.isVersion(5, 0, 0)) { this.parameterFields = new Field[this.parameterCount]; Buffer metaDataPacket = mysql.readPacket(); int i = 0; while (!metaDataPacket.isLastDataPacket() && (i < this.parameterCount)) { this.parameterFields[i++] = mysql.unpackField(metaDataPacket, false); metaDataPacket = mysql.readPacket(); } } } if (this.fieldCount > 0) { this.resultFields = new Field[this.fieldCount]; Buffer fieldPacket = mysql.readPacket(); int i = 0; // Read in the result set column information while (!fieldPacket.isLastDataPacket() && (i < this.fieldCount)) { this.resultFields[i++] = mysql.unpackField(fieldPacket, false); fieldPacket = mysql.readPacket(); } } } catch (SQLException sqlEx) { if (this.connection.getDumpQueriesOnException()) { StringBuffer messageBuf = new StringBuffer(this.originalSql.length() + 32); messageBuf.append( "\n\nQuery being prepared when exception was thrown:\n\n"); messageBuf.append(this.originalSql); sqlEx = Connection.appendMessageToException(sqlEx, messageBuf.toString()); } throw sqlEx; } finally { // Leave the I/O channel in a known state...there might be packets out there // that we're not interested in this.connection.getIO().clearInputStream(); } } } private void serverResetStatement() throws SQLException { synchronized (this.connection.getMutex()) { MysqlIO mysql = this.connection.getIO(); Buffer packet = mysql.getSharedSendPacket(); packet.clear(); packet.writeByte((byte) MysqlDefs.COM_RESET_STMT); packet.writeLong(this.serverStatementId); try { mysql.sendCommand(MysqlDefs.COM_RESET_STMT, null, packet, !this.connection.versionMeetsMinimum(4, 1, 2), null); } catch (SQLException sqlEx) { throw sqlEx; } catch (Exception ex) { throw new SQLException(ex.toString(), SQLError.SQL_STATE_GENERAL_ERROR); } finally { mysql.clearInputStream(); } } } /** * Method storeBinding. * * @param packet * @param bindValue * @param mysql DOCUMENT ME! * * @throws SQLException DOCUMENT ME! */ private void storeBinding(Buffer packet, BindValue bindValue, MysqlIO mysql) throws SQLException { try { Object value = bindValue.value; // // Handle primitives first // switch (bindValue.bufferType) { case MysqlDefs.FIELD_TYPE_TINY: packet.writeByte(bindValue.byteBinding); return; case MysqlDefs.FIELD_TYPE_SHORT: packet.ensureCapacity(2); packet.writeInt(bindValue.shortBinding); return; case MysqlDefs.FIELD_TYPE_LONG: packet.ensureCapacity(4); packet.writeLong(bindValue.intBinding); return; case MysqlDefs.FIELD_TYPE_LONGLONG: packet.ensureCapacity(8); packet.writeLongLong(bindValue.longBinding); return; case MysqlDefs.FIELD_TYPE_FLOAT: packet.ensureCapacity(4); packet.writeFloat(bindValue.floatBinding); return; case MysqlDefs.FIELD_TYPE_DOUBLE: packet.ensureCapacity(8); packet.writeDouble(bindValue.doubleBinding); return; case MysqlDefs.FIELD_TYPE_TIME: storeTime(packet, (Time) value); return; case MysqlDefs.FIELD_TYPE_DATE: case MysqlDefs.FIELD_TYPE_DATETIME: case MysqlDefs.FIELD_TYPE_TIMESTAMP: storeDateTime(packet, (java.util.Date) value, mysql); return; case MysqlDefs.FIELD_TYPE_VAR_STRING: case MysqlDefs.FIELD_TYPE_STRING: case MysqlDefs.FIELD_TYPE_VARCHAR: if (!this.isLoadDataQuery) { packet.writeLenString((String) value, this.charEncoding, this.connection.getServerCharacterEncoding(), this.charConverter, this.connection.parserKnowsUnicode()); } else { packet.writeLenBytes(((String) value).getBytes()); } return; } if (value instanceof byte[]) { packet.writeLenBytes((byte[]) value); } } catch (UnsupportedEncodingException uEE) { throw new SQLException(Messages.getString( "ServerPreparedStatement.22") //$NON-NLS-1$ +this.connection.getEncoding() + "'", //$NON-NLS-1$ SQLError.SQL_STATE_GENERAL_ERROR); } } private void storeDataTime412AndOlder(Buffer intoBuf, java.util.Date dt) throws SQLException { // This is synchronized on the connection by callers, so it is // safe to lazily-instantiate this... if (this.dateTimeBindingCal == null) { this.dateTimeBindingCal = Calendar.getInstance(); } this.dateTimeBindingCal.setTime(dt); intoBuf.ensureCapacity(8); intoBuf.writeByte((byte) 7); // length int year = this.dateTimeBindingCal.get(Calendar.YEAR); int month = this.dateTimeBindingCal.get(Calendar.MONTH) + 1; int date = this.dateTimeBindingCal.get(Calendar.DATE); intoBuf.writeInt(year); intoBuf.writeByte((byte) month); intoBuf.writeByte((byte) date); if (dt instanceof java.sql.Date) { intoBuf.writeByte((byte) 0); intoBuf.writeByte((byte) 0); intoBuf.writeByte((byte) 0); } else { intoBuf.writeByte((byte) this.dateTimeBindingCal.get( Calendar.HOUR_OF_DAY)); intoBuf.writeByte((byte) this.dateTimeBindingCal.get(Calendar.MINUTE)); intoBuf.writeByte((byte) this.dateTimeBindingCal.get(Calendar.SECOND)); } } private void storeDateTime(Buffer intoBuf, java.util.Date dt, MysqlIO mysql) throws SQLException { if (this.connection.versionMeetsMinimum(4, 1, 3)) { storeDateTime413AndNewer(intoBuf, dt); } else { storeDataTime412AndOlder(intoBuf, dt); } } private void storeDateTime413AndNewer(Buffer intoBuf, java.util.Date dt) throws SQLException { // This is synchronized on the connection by callers, so it is // safe to lazily-instantiate this... if (this.dateTimeBindingCal == null) { this.dateTimeBindingCal = Calendar.getInstance(); } this.dateTimeBindingCal.setTime(dt); byte length = (byte) 7; intoBuf.ensureCapacity(length); if (dt instanceof java.sql.Timestamp) { length = (byte) 11; } intoBuf.writeByte(length); // length int year = this.dateTimeBindingCal.get(Calendar.YEAR); int month = this.dateTimeBindingCal.get(Calendar.MONTH) + 1; int date = this.dateTimeBindingCal.get(Calendar.DATE); intoBuf.writeInt(year); intoBuf.writeByte((byte) month); intoBuf.writeByte((byte) date); if (dt instanceof java.sql.Date) { intoBuf.writeByte((byte) 0); intoBuf.writeByte((byte) 0); intoBuf.writeByte((byte) 0); } else { intoBuf.writeByte((byte) this.dateTimeBindingCal.get( Calendar.HOUR_OF_DAY)); intoBuf.writeByte((byte) this.dateTimeBindingCal.get(Calendar.MINUTE)); intoBuf.writeByte((byte) this.dateTimeBindingCal.get(Calendar.SECOND)); } if (length == 11) { intoBuf.writeLong(((java.sql.Timestamp) dt).getNanos()); } } // // TO DO: Investigate using NIO to do this faster // private void storeReader(Buffer packet, Reader inStream) throws SQLException { char[] buf = new char[4096]; StringBuffer valueAsString = new StringBuffer(); int numRead = 0; try { while ((numRead = inStream.read(buf)) != -1) { valueAsString.append(buf, 0, numRead); } } catch (IOException ioEx) { throw new SQLException(Messages.getString( "ServerPreparedStatement.24") //$NON-NLS-1$ +ioEx.toString(), SQLError.SQL_STATE_GENERAL_ERROR); } finally { if (inStream != null) { try { inStream.close(); } catch (IOException ioEx) { ; // ignore } } } byte[] valueAsBytes = StringUtils.getBytes(valueAsString.toString(), this.connection.getEncoding(), this.connection.getServerCharacterEncoding(), this.connection.parserKnowsUnicode()); packet.writeBytesNoNull(valueAsBytes); } private void storeStream(Buffer packet, InputStream inStream) throws SQLException { byte[] buf = new byte[4096]; int numRead = 0; try { while ((numRead = inStream.read(buf)) != -1) { packet.writeBytesNoNull(buf, 0, numRead); } } catch (IOException ioEx) { throw new SQLException(Messages.getString( "ServerPreparedStatement.25") //$NON-NLS-1$ +ioEx.toString(), SQLError.SQL_STATE_GENERAL_ERROR); } finally { if (inStream != null) { try { inStream.close(); } catch (IOException ioEx) { ; // ignore } } } } private static void storeTime(Buffer intoBuf, Time tm) throws SQLException { intoBuf.ensureCapacity(9); intoBuf.writeByte((byte) 8); // length intoBuf.writeByte((byte) 0); // neg flag intoBuf.writeLong(0); // tm->day, not used Calendar cal = Calendar.getInstance(); cal.setTime(tm); intoBuf.writeByte((byte) cal.get(Calendar.HOUR_OF_DAY)); intoBuf.writeByte((byte) cal.get(Calendar.MINUTE)); intoBuf.writeByte((byte) cal.get(Calendar.SECOND)); //intoBuf.writeLongInt(0); // tm-second_part } static class BatchedBindValues { BindValue[] batchedParameterValues; BatchedBindValues(BindValue[] paramVals) { int numParams = paramVals.length; this.batchedParameterValues = new BindValue[numParams]; for (int i = 0; i < numParams; i++) { this.batchedParameterValues[i] = new BindValue(paramVals[i]); } } } static class BindValue { Object value; /* The value to store */ boolean isLongData; /* long data indicator */ boolean isNull; /* NULL indicator */ int bufferType; /* buffer type */ long bindLength; /* Default length of data */ byte byteBinding; short shortBinding; int intBinding; long longBinding; double doubleBinding; float floatBinding; boolean isSet = false; /* has this parameter been set? */ BindValue() { } void reset() { this.isSet = false; this.value = null; this.isLongData = false; this.byteBinding = 0; this.shortBinding = 0; this.intBinding = 0; this.longBinding = 0L; this.floatBinding = 0; this.doubleBinding = 0D; } BindValue(BindValue copyMe) { this.value = copyMe.value; this.isSet = copyMe.isSet; thi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -