📄 serverpreparedstatement.java
字号:
* the parameter value * @param tz * the timezone to use * * @throws SQLException * if a database access error occurs */ public void setTimeInternal(int parameterIndex, java.sql.Time x, TimeZone tz, boolean rollForward) throws SQLException { if (x == null) { setNull(parameterIndex, java.sql.Types.TIME); } else { BindValue binding = getBinding(parameterIndex, false); setType(binding, MysqlDefs.FIELD_TYPE_TIME); binding.value = TimeUtil.changeTimezone(this.connection, x, tz, this.connection.getServerTimezoneTZ(), rollForward); binding.isNull = false; binding.isLongData = false; } } /** * Set a parameter to a java.sql.Timestamp value. The driver converts this * to a SQL TIMESTAMP value when it sends it to the database. * * @param parameterIndex * the first parameter is 1, the second is 2, ... * @param x * the parameter value * * @throws SQLException * if a database-access error occurs. */ public void setTimestamp(int parameterIndex, java.sql.Timestamp x) throws SQLException { setTimestampInternal(parameterIndex, x, TimeZone.getDefault(), false); } /** * Set a parameter to a java.sql.Timestamp value. The driver converts this * to a SQL TIMESTAMP value when it sends it to the database. * * @param parameterIndex * the first parameter is 1, the second is 2, ... * @param x * the parameter value * @param cal * the timezone to use * * @throws SQLException * if a database-access error occurs. */ public void setTimestamp(int parameterIndex, java.sql.Timestamp x, Calendar cal) throws SQLException { setTimestampInternal(parameterIndex, x, cal.getTimeZone(), true); } protected void setTimestampInternal(int parameterIndex, java.sql.Timestamp x, TimeZone tz, boolean rollForward) throws SQLException { if (x == null) { setNull(parameterIndex, java.sql.Types.TIMESTAMP); } else { BindValue binding = getBinding(parameterIndex, false); setType(binding, MysqlDefs.FIELD_TYPE_DATETIME); binding.value = TimeUtil.changeTimezone(this.connection, x, tz, this.connection.getServerTimezoneTZ(), rollForward); binding.isNull = false; binding.isLongData = false; } } private void setType(BindValue oldValue, int bufferType) { if (oldValue.bufferType != bufferType) { this.sendTypesToServer = true; } oldValue.bufferType = bufferType; } /** * DOCUMENT ME! * * @param parameterIndex * DOCUMENT ME! * @param x * DOCUMENT ME! * @param length * DOCUMENT ME! * * @throws SQLException * DOCUMENT ME! * @throws NotImplemented * DOCUMENT ME! * * @see java.sql.PreparedStatement#setUnicodeStream(int, * java.io.InputStream, int) * @deprecated */ public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { checkClosed(); throw new NotImplemented(); } /** * @see java.sql.PreparedStatement#setURL(int, java.net.URL) */ public void setURL(int parameterIndex, URL x) throws SQLException { checkClosed(); setString(parameterIndex, x.toString()); } /** * 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 (value instanceof byte[]) { packet.writeLenBytes((byte[]) value); } else if (!this.isLoadDataQuery) { packet.writeLenString((String) value, this.charEncoding, this.connection.getServerCharacterEncoding(), this.charConverter, this.connection .parserKnowsUnicode()); } else { packet.writeLenBytes(((String) value).getBytes()); } return; } } 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(MysqlIO mysql, int parameterIndex, Buffer packet, Reader inStream) throws SQLException { int maxBytesChar = 2; if (this.connection.getEncoding() != null) { maxBytesChar = this.connection.getMaxBytesPerChar( this.connection.getEncoding()); if (maxBytesChar == 1) { maxBytesChar = 2; // for safety } } char[] buf = new char[BLOB_STREAM_READ_BUF_SIZE / maxBytesChar]; int numRead = 0; int bytesInPacket = 0; int totalBytesRead = 0; int bytesReadAtLastSend = 0; int packetIsFullAt = this.connection.getBlobSendChunkSize(); try { packet.clear(); packet.writeByte((byte) MysqlDefs.COM_LONG_DATA); packet.writeLong(this.serverStatementId); packet.writeInt((parameterIndex)); boolean readAny = false; while ((numRead = inStream.read(buf)) != -1) { readAny = true; byte[] valueAsBytes = StringUtils.getBytes(buf, null, this.connection.getEncoding(), this.connection .getServerCharacterEncoding(), 0, numRead, this.connection.parserKnowsUnicode()); packet.writeBytesNoNull(valueAsBytes, 0, valueAsBytes.length); bytesInPacket += valueAsBytes.length; totalBytesRead += valueAsBytes.length; if (bytesInPacket >= packetIsFullAt) { bytesReadAtLastSend = totalBytesRead; mysql.sendCommand(MysqlDefs.COM_LONG_DATA, null, packet, true, null); bytesInPacket = 0; packet.clear(); packet.writeByte((byte) MysqlDefs.COM_LONG_DATA); packet.writeLong(this.serverStatementId); packet.writeInt((parameterIndex)); } } if (totalBytesRead != bytesReadAtLastSend) { mysql.sendCommand(MysqlDefs.COM_LONG_DATA, null, packet, true, null); } if (!readAny) { mysql.sendCommand(MysqlDefs.COM_LONG_DATA, null, packet, true, null); } } 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 } } } } private void storeStream(MysqlIO mysql, int parameterIndex, Buffer packet, InputStream inStream) throws SQLException { byte[] buf = new byte[BLOB_STREAM_READ_BUF_SIZE]; int numRead = 0; try { int bytesInPacket = 0; int totalBytesRead = 0; int bytesReadAtLastSend = 0; int packetIsFullAt = this.connection.getBlobSendChunkSize(); packet.clear(); packet.writeByte((byte) MysqlDefs.COM_LONG_DATA); packet.writeLong(this.serverStatementId); packet.writeInt((parameterIndex)); boolean readAny = false; while ((numRead = inStream.read(buf)) != -1) { readAny = true; packet.writeBytesNoNull(buf, 0, numRead); bytesInPacket += numRead; totalBytesRead += numRead; if (bytesInPacket >= packetIsFullAt) { bytesReadAtLastSend = totalBytesRead; mysql.sendCommand(MysqlDefs.COM_LONG_DATA, null, packet, true, null); bytesInPacket = 0; packet.clear(); packet.writeByte((byte) MysqlDefs.COM_LONG_DATA); packet.writeLong(this.serverStatementId); packet.writeInt((parameterIndex)); } } if (totalBytesRead != bytesReadAtLastSend) { mysql.sendCommand(MysqlDefs.COM_LONG_DATA, null, packet, true, null); } if (!readAny) { mysql.sendCommand(MysqlDefs.COM_LONG_DATA, null, packet, true, null); } } 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 } } } } /** * @see java.lang.Object#toString() */ public String toString() { StringBuffer toStringBuf = new StringBuffer(); toStringBuf.append("com.mysql.jdbc.ServerPreparedStatement["); //$NON-NLS-1$ toStringBuf.append(this.serverStatementId); toStringBuf.append("] - "); //$NON-NLS-1$ try { toStringBuf.append(asSql()); } catch (SQLException sqlEx) { toStringBuf.append(Messages.getString("ServerPreparedStatement.6")); //$NON-NLS-1$ toStringBuf.append(sqlEx); } return toStringBuf.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -