📄 serverpreparedstatement.java
字号:
* @see java.sql.PreparedStatement#setNull(int, int, java.lang.String) */ public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException { checkClosed(); BindValue binding = getBinding(parameterIndex, false); // // Don't re-set types, but use something if this // parameter was never specified // if (binding.bufferType == 0) { setType(binding, MysqlDefs.FIELD_TYPE_NULL); } binding.value = null; binding.isNull = true; binding.isLongData = false; } /** * @see java.sql.PreparedStatement#setRef(int, java.sql.Ref) */ public void setRef(int i, Ref x) throws SQLException { throw new NotImplemented(); } /** * @see java.sql.PreparedStatement#setShort(int, short) */ public void setShort(int parameterIndex, short x) throws SQLException { checkClosed(); BindValue binding = getBinding(parameterIndex, false); setType(binding, MysqlDefs.FIELD_TYPE_SHORT); binding.value = null; binding.shortBinding = x; binding.isNull = false; binding.isLongData = false; } /** * @see java.sql.PreparedStatement#setString(int, java.lang.String) */ public void setString(int parameterIndex, String x) throws SQLException { checkClosed(); if (x == null) { setNull(parameterIndex, java.sql.Types.CHAR); } else { BindValue binding = getBinding(parameterIndex, false); setType(binding, this.stringTypeCode); binding.value = x; binding.isNull = false; binding.isLongData = false; } } /** * Set a parameter to a java.sql.Time value. * * @param parameterIndex * the first parameter is 1...)); * @param x * the parameter value * * @throws SQLException * if a database access error occurs */ public void setTime(int parameterIndex, java.sql.Time x) throws SQLException { setTimeInternal(parameterIndex, x, null, this.connection.getDefaultTimeZone(), false); } /** * Set a parameter to a java.sql.Time value. The driver converts this to a * SQL TIME value when it sends it to the database, using the given * timezone. * * @param parameterIndex * the first parameter is 1...)); * @param x * the parameter value * @param cal * the timezone to use * * @throws SQLException * if a database access error occurs */ public void setTime(int parameterIndex, java.sql.Time x, Calendar cal) throws SQLException { setTimeInternal(parameterIndex, x, cal, cal.getTimeZone(), true); } /** * Set a parameter to a java.sql.Time value. The driver converts this to a * SQL TIME value when it sends it to the database, using the given * timezone. * * @param parameterIndex * the first parameter is 1...)); * @param x * 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, Calendar targetCalendar, 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); Calendar sessionCalendar = getCalendarInstanceForSessionOrNew(); synchronized (sessionCalendar) { binding.value = TimeUtil.changeTimezone(this.connection, sessionCalendar, targetCalendar, 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, null, this.connection.getDefaultTimeZone(), 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, cal.getTimeZone(), true); } protected void setTimestampInternal(int parameterIndex, java.sql.Timestamp x, Calendar targetCalendar, 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); Calendar sessionCalendar = this.connection.getUseJDBCCompliantTimezoneShift() ? this.connection.getUtcCalendar() : getCalendarInstanceForSessionOrNew(); synchronized (sessionCalendar) { binding.value = TimeUtil.changeTimezone(this.connection, sessionCalendar, targetCalendar, 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: case MysqlDefs.FIELD_TYPE_DECIMAL: case MysqlDefs.FIELD_TYPE_NEW_DECIMAL: 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(), this.connection); } else { packet.writeLenBytes(((String) value).getBytes()); } return; } } catch (UnsupportedEncodingException uEE) { throw SQLError.createSQLException(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 { Calendar sessionCalendar = getCalendarInstanceForSessionOrNew(); synchronized (sessionCalendar) { java.util.Date oldTime = sessionCalendar.getTime(); try { intoBuf.ensureCapacity(8); intoBuf.writeByte((byte) 7); // length sessionCalendar.setTime(dt); int year = sessionCalendar.get(Calendar.YEAR); int month = sessionCalendar.get(Calendar.MONTH) + 1; int date = sessionCalendar.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) sessionCalendar .get(Calendar.HOUR_OF_DAY)); intoBuf.writeByte((byte) sessionCalendar .get(Calendar.MINUTE)); intoBuf.writeByte((byte) sessionCalendar .get(Calendar.SECOND)); } } finally { sessionCalendar.setTime(oldTime); } } } 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 { Calendar sessionCalendar = (dt instanceof Timestamp && this.connection.getUseJDBCCompliantTimezoneShift()) ? this.connection.getUtcCalendar() : getCalendarInstanceForSessionOrNew(); synchronized (sessionCalendar) { java.util.Date oldTime = sessionCalendar.getTime(); try { sessionCalendar.setTime(dt); if (dt instanceof java.sql.Date) { sessionCalendar.set(Calendar.HOUR_OF_DAY, 0); sessionCalendar.set(Calendar.MINUTE, 0); sessionCalendar.set(Calendar.SECOND, 0); } byte length = (byte) 7; if (dt instanceof java.sql.Timestamp) { length = (byte) 11; } intoBuf.ensureCapacity(length); intoBuf.writeByte(length); // length int year = sessionCalendar.get(Calendar.YEAR); int month = sessionCalendar.get(Calendar.MONTH) + 1; int date = sessionCalendar.get(Calendar.DAY_OF_MONTH); 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) sessionCalendar .get(Calendar.HOUR_OF_DAY)); intoBuf.writeByte((byte) sessionCalendar .get(Calendar.MINUTE)); intoBuf.writeByte((byte) sessionCalendar .get(Calendar.SECOND)); } if (length == 11) { // MySQL expects microseconds, not nanos intoBuf.writeLong(((java.sql.Timestamp) dt).getNanos() / 1000); } } finally { sessionCalendar.setTime(oldTime); } } } // // TO DO: Investigate using NIO to do this faster // private void storeReader(MysqlIO mysql, int parameterIndex, Buffer packet, Reader inStream) throws SQLException { String forcedEncoding = this.connection.getClobCharacterEncoding(); String clobEncoding = (forcedEncoding == null ? this.connection.getEncoding() : forcedEncoding); int maxBytesChar = 2; if (clobEncoding != null) { if (!clobEncoding.equals("UTF-16")) { maxBytesChar = this.connection.getMaxBytesPerChar(clobEncoding); if (maxBytesChar == 1) { maxBytesChar = 2; // for safety } } else { maxBytesChar = 4; } } 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -