📄 serverpreparedstatement.java
字号:
begin = mysql.getCurrentTimeNanosOrMillis(); } String characterEncoding = null; String connectionEncoding = this.connection.getEncoding(); if (!this.isLoadDataQuery && this.connection.getUseUnicode() && (connectionEncoding != null)) { characterEncoding = connectionEncoding; } Buffer prepareResultPacket = mysql.sendCommand( MysqlDefs.COM_PREPARE, sql, null, false, characterEncoding); if (this.connection.versionMeetsMinimum(4, 1, 1)) { // 4.1.1 and newer use the first byte // as an 'ok' or 'error' flag, so move // the buffer pointer past it to // start reading the statement id. prepareResultPacket.setPosition(1); } else { // 4.1.0 doesn't use the first byte as an // 'ok' or 'error' flag prepareResultPacket.setPosition(0); } this.serverStatementId = prepareResultPacket.readLong(); this.fieldCount = prepareResultPacket.readInt(); this.parameterCount = prepareResultPacket.readInt(); this.parameterBindings = new BindValue[this.parameterCount]; for (int i = 0; i < this.parameterCount; i++) { this.parameterBindings[i] = new BindValue(); } this.connection.incrementNumberOfPrepares(); if (this.profileSQL) { this.eventSink.consumeEvent(new ProfilerEvent( ProfilerEvent.TYPE_PREPARE, "", this.currentCatalog, //$NON-NLS-1$ this.connectionId, this.statementId, -1, System.currentTimeMillis(), mysql.getCurrentTimeNanosOrMillis() - begin, mysql.getQueryTimingUnits(), null, new Throwable(), truncateQueryToLog(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 String truncateQueryToLog(String sql) { String query = null; if (sql.length() > this.connection.getMaxQuerySizeToLog()) { StringBuffer queryBuf = new StringBuffer( this.connection.getMaxQuerySizeToLog() + 12); queryBuf.append(sql.substring(0, this.connection.getMaxQuerySizeToLog())); queryBuf.append(Messages.getString("MysqlIO.25")); query = queryBuf.toString(); } else { query = sql; } return query; } 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 SQLError.createSQLException(ex.toString(), SQLError.SQL_STATE_GENERAL_ERROR); } finally { mysql.clearInputStream(); } } } /** * @see java.sql.PreparedStatement#setArray(int, java.sql.Array) */ public void setArray(int i, Array x) throws SQLException { throw new NotImplemented(); } /** * @see java.sql.PreparedStatement#setAsciiStream(int, java.io.InputStream, * int) */ public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { checkClosed(); if (x == null) { setNull(parameterIndex, java.sql.Types.BINARY); } else { BindValue binding = getBinding(parameterIndex, true); setType(binding, MysqlDefs.FIELD_TYPE_BLOB); binding.value = x; binding.isNull = false; binding.isLongData = true; if (this.connection.getUseStreamLengthsInPrepStmts()) { binding.bindLength = length; } else { binding.bindLength = -1; } } } /** * @see java.sql.PreparedStatement#setBigDecimal(int, java.math.BigDecimal) */ public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { checkClosed(); if (x == null) { setNull(parameterIndex, java.sql.Types.DECIMAL); } else { BindValue binding = getBinding(parameterIndex, false); if (this.connection.versionMeetsMinimum(5, 0, 3)) { setType(binding, MysqlDefs.FIELD_TYPE_NEW_DECIMAL); } else { setType(binding, this.stringTypeCode); } binding.value = StringUtils .fixDecimalExponent(StringUtils.consistentToString(x)); binding.isNull = false; binding.isLongData = false; } } /** * @see java.sql.PreparedStatement#setBinaryStream(int, java.io.InputStream, * int) */ public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { checkClosed(); if (x == null) { setNull(parameterIndex, java.sql.Types.BINARY); } else { BindValue binding = getBinding(parameterIndex, true); setType(binding, MysqlDefs.FIELD_TYPE_BLOB); binding.value = x; binding.isNull = false; binding.isLongData = true; if (this.connection.getUseStreamLengthsInPrepStmts()) { binding.bindLength = length; } else { binding.bindLength = -1; } } } /** * @see java.sql.PreparedStatement#setBlob(int, java.sql.Blob) */ public void setBlob(int parameterIndex, Blob x) throws SQLException { checkClosed(); if (x == null) { setNull(parameterIndex, java.sql.Types.BINARY); } else { BindValue binding = getBinding(parameterIndex, true); setType(binding, MysqlDefs.FIELD_TYPE_BLOB); binding.value = x; binding.isNull = false; binding.isLongData = true; if (this.connection.getUseStreamLengthsInPrepStmts()) { binding.bindLength = x.length(); } else { binding.bindLength = -1; } } } /** * @see java.sql.PreparedStatement#setBoolean(int, boolean) */ public void setBoolean(int parameterIndex, boolean x) throws SQLException { setByte(parameterIndex, (x ? (byte) 1 : (byte) 0)); } /** * @see java.sql.PreparedStatement#setByte(int, byte) */ public void setByte(int parameterIndex, byte x) throws SQLException { checkClosed(); BindValue binding = getBinding(parameterIndex, false); setType(binding, MysqlDefs.FIELD_TYPE_TINY); binding.value = null; binding.byteBinding = x; binding.isNull = false; binding.isLongData = false; } /** * @see java.sql.PreparedStatement#setBytes(int, byte) */ public void setBytes(int parameterIndex, byte[] x) throws SQLException { checkClosed(); if (x == null) { setNull(parameterIndex, java.sql.Types.BINARY); } else { BindValue binding = getBinding(parameterIndex, false); setType(binding, MysqlDefs.FIELD_TYPE_VAR_STRING); binding.value = x; binding.isNull = false; binding.isLongData = false; } } /** * @see java.sql.PreparedStatement#setCharacterStream(int, java.io.Reader, * int) */ public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException { checkClosed(); if (reader == null) { setNull(parameterIndex, java.sql.Types.BINARY); } else { BindValue binding = getBinding(parameterIndex, true); setType(binding, MysqlDefs.FIELD_TYPE_BLOB); binding.value = reader; binding.isNull = false; binding.isLongData = true; if (this.connection.getUseStreamLengthsInPrepStmts()) { binding.bindLength = length; } else { binding.bindLength = -1; } } } /** * @see java.sql.PreparedStatement#setClob(int, java.sql.Clob) */ public void setClob(int parameterIndex, Clob x) throws SQLException { checkClosed(); if (x == null) { setNull(parameterIndex, java.sql.Types.BINARY); } else { BindValue binding = getBinding(parameterIndex, true); setType(binding, MysqlDefs.FIELD_TYPE_BLOB); binding.value = x.getCharacterStream(); binding.isNull = false; binding.isLongData = true; if (this.connection.getUseStreamLengthsInPrepStmts()) { binding.bindLength = x.length(); } else { binding.bindLength = -1; } } } /** * Set a parameter to a java.sql.Date value. The driver converts this to a * SQL DATE value when it sends it to the database. * * @param parameterIndex * the first parameter is 1, the second is 2, ... * @param x * the parameter value * * @exception SQLException * if a database-access error occurs. */ public void setDate(int parameterIndex, Date x) throws SQLException { setDate(parameterIndex, x, null); } /** * Set a parameter to a java.sql.Date value. The driver converts this to a * SQL DATE 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 calendar to interpret the date with * * @exception SQLException * if a database-access error occurs. */ public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { if (x == null) { setNull(parameterIndex, java.sql.Types.DATE); } else { BindValue binding = getBinding(parameterIndex, false); setType(binding, MysqlDefs.FIELD_TYPE_DATE); binding.value = x; binding.isNull = false; binding.isLongData = false; } } /** * @see java.sql.PreparedStatement#setDouble(int, double) */ public void setDouble(int parameterIndex, double x) throws SQLException { checkClosed(); if (!this.connection.getAllowNanAndInf() && (x == Double.POSITIVE_INFINITY || x == Double.NEGATIVE_INFINITY || Double.isNaN(x))) { throw SQLError.createSQLException("'" + x + "' is not a valid numeric or approximate numeric value", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } BindValue binding = getBinding(parameterIndex, false); setType(binding, MysqlDefs.FIELD_TYPE_DOUBLE); binding.value = null; binding.doubleBinding = x; binding.isNull = false; binding.isLongData = false; } /** * @see java.sql.PreparedStatement#setFloat(int, float) */ public void setFloat(int parameterIndex, float x) throws SQLException { checkClosed(); BindValue binding = getBinding(parameterIndex, false); setType(binding, MysqlDefs.FIELD_TYPE_FLOAT); binding.value = null; binding.floatBinding = x; binding.isNull = false; binding.isLongData = false; } /** * @see java.sql.PreparedStatement#setInt(int, int) */ public void setInt(int parameterIndex, int x) throws SQLException { checkClosed(); BindValue binding = getBinding(parameterIndex, false); setType(binding, MysqlDefs.FIELD_TYPE_LONG); binding.value = null; binding.intBinding = x; binding.isNull = false; binding.isLongData = false; } /** * @see java.sql.PreparedStatement#setLong(int, long) */ public void setLong(int parameterIndex, long x) throws SQLException { checkClosed(); BindValue binding = getBinding(parameterIndex, false); setType(binding, MysqlDefs.FIELD_TYPE_LONGLONG); binding.value = null; binding.longBinding = x; binding.isNull = false; binding.isLongData = false; } /** * @see java.sql.PreparedStatement#setNull(int, int) */ public void setNull(int parameterIndex, int sqlType) 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; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -