📄 serverpreparedstatement.java
字号:
if (arg instanceof String) { updateCounts[commandIndex] = executeUpdate((String) arg); } else { this.parameterBindings = ((BatchedBindValues) arg).batchedParameterValues; try { // We need to check types each time, as // the user might have bound different // types in each addBatch() if (previousBindValuesForBatch != null) { for (int j = 0; j < this.parameterBindings.length; j++) { if (this.parameterBindings[j].bufferType != previousBindValuesForBatch[j].bufferType) { this.sendTypesToServer = true; break; } } } try { updateCounts[commandIndex] = executeUpdate(false); } finally { previousBindValuesForBatch = this.parameterBindings; } if (this.retrieveGeneratedKeys) { java.sql.ResultSet rs = null; try { // we don't want to use our version, // because we've altered the behavior of ours to support batch updates // (catch-22) // Ideally, what we need here is super.super.getGeneratedKeys() // but that construct doesn't exist in Java, so that's why there's // this kludge. rs = getGeneratedKeysInternal(); while (rs.next()) { this.batchedGeneratedKeys.add(new byte[][] { rs.getBytes(1) }); } } finally { if (rs != null) { rs.close(); } } } } catch (SQLException ex) { updateCounts[commandIndex] = EXECUTE_FAILED; if (this.connection.getContinueBatchOnError()) { sqlEx = ex; } else { int[] newUpdateCounts = new int[commandIndex]; System.arraycopy(updateCounts, 0, newUpdateCounts, 0, commandIndex); throw new java.sql.BatchUpdateException(ex.getMessage(), ex.getSQLState(), ex.getErrorCode(), newUpdateCounts); } } } } if (sqlEx != null) { throw new java.sql.BatchUpdateException(sqlEx.getMessage(), sqlEx.getSQLState(), sqlEx.getErrorCode(), updateCounts); } } return (updateCounts != null) ? updateCounts : new int[0]; } finally { this.parameterBindings = oldBindValues; this.sendTypesToServer = true; clearBatch(); } } } /** * @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$ PreparedStatement pStmtForSub = null; try { pStmtForSub = new PreparedStatement(this.connection, this.originalSql, this.currentCatalog); int numParameters = pStmtForSub.parameterCount; int ourNumParameters = this.parameterCount; for (int i = 0; (i < numParameters) && (i < ourNumParameters); i++) { if (this.parameterBindings[i] != null) { if (this.parameterBindings[i].isNull) { pStmtForSub.setNull(i + 1, Types.NULL); } else { BindValue bindValue = this.parameterBindings[i]; // // Handle primitives first // switch (bindValue.bufferType) { case MysqlDefs.FIELD_TYPE_TINY: pStmtForSub.setByte(i + 1,bindValue.byteBinding); break; case MysqlDefs.FIELD_TYPE_SHORT: pStmtForSub.setShort(i + 1,bindValue.shortBinding); break; case MysqlDefs.FIELD_TYPE_LONG: pStmtForSub.setInt(i + 1,bindValue.intBinding); break; case MysqlDefs.FIELD_TYPE_LONGLONG: pStmtForSub.setLong(i + 1,bindValue.longBinding); break; case MysqlDefs.FIELD_TYPE_FLOAT: pStmtForSub.setFloat(i + 1,bindValue.floatBinding); break; case MysqlDefs.FIELD_TYPE_DOUBLE: pStmtForSub.setDouble(i + 1,bindValue.doubleBinding); break; default: pStmtForSub.setObject(i + 1, this.parameterBindings[i].value); break; } } } } toStringBuf.append(pStmtForSub.asSql()); } catch (SQLException sqlEx) { toStringBuf.append(Messages.getString("ServerPreparedStatement.6")); //$NON-NLS-1$ toStringBuf.append(sqlEx); } finally { if (pStmtForSub != null) { try { pStmtForSub.close(); } catch (SQLException sqlEx) { ; // ignore } } } return toStringBuf.toString(); } 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; } } /* (non-Javadoc) * @see com.mysql.jdbc.Statement#checkClosed() */ protected void checkClosed() throws SQLException { if (this.invalid) { throw this.invalidationException; } super.checkClosed(); } /** * @see com.mysql.jdbc.PreparedStatement#executeInternal(int, * com.mysql.jdbc.Buffer, boolean, boolean) */ protected com.mysql.jdbc.ResultSet executeInternal(int maxRowsToRetrieve, Buffer sendPacket, boolean createStreamingResultSet, boolean queryIsSelectOnly, boolean unpackFields) throws SQLException { this.numberOfExecutions++; // We defer to server-side execution try { return serverExecute(maxRowsToRetrieve, createStreamingResultSet); } catch (SQLException sqlEx) { // don't wrap SQLExceptions if (this.connection.getEnablePacketDebug()) { this.connection.getIO().dumpPacketRingBuffer(); } if (this.connection.getDumpQueriesOnException()) { String extractedSql = toString(); StringBuffer messageBuf = new StringBuffer(extractedSql.length() + 32); messageBuf.append( "\n\nQuery being executed when exception was thrown:\n\n"); messageBuf.append(extractedSql); sqlEx = Connection.appendMessageToException(sqlEx, messageBuf.toString()); } if (this.connection.getDumpQueriesOnException()) { String extractedSql = toString(); StringBuffer messageBuf = new StringBuffer(extractedSql.length() + 32); messageBuf.append( "\n\nQuery being executed when exception was thrown:\n\n"); messageBuf.append(extractedSql); sqlEx = Connection.appendMessageToException(sqlEx, messageBuf.toString()); } throw sqlEx; } catch (Exception ex) { if (this.connection.getEnablePacketDebug()) { this.connection.getIO().dumpPacketRingBuffer(); } SQLException sqlEx = new SQLException(ex.toString(), SQLError.SQL_STATE_GENERAL_ERROR); if (this.connection.getDumpQueriesOnException()) { String extractedSql = toString(); StringBuffer messageBuf = new StringBuffer(extractedSql.length() + 32); messageBuf.append( "\n\nQuery being executed when exception was thrown:\n\n"); messageBuf.append(extractedSql); sqlEx = Connection.appendMessageToException(sqlEx, messageBuf.toString()); } throw sqlEx; } } /** * @see com.mysql.jdbc.PreparedStatement#fillSendPacket() */ protected Buffer fillSendPacket() throws SQLException { return null; // we don't use this type of packet } /** * @see com.mysql.jdbc.PreparedStatement#fillSendPacket(byte, * java.io.InputStream, boolean, int) */ protected Buffer fillSendPacket(byte[][] batchedParameterStrings, InputStream[] batchedParameterStreams, boolean[] batchedIsStream, int[] batchedStreamLengths) throws SQLException { return null; // we don't use this type of packet } /** * Used by Connection when auto-reconnecting to retrieve 'lost' prepared * statements. * * @throws SQLException if an error occurs. */ protected void rePrepare() throws SQLException { this.invalidationException = null; try { serverPrepare(this.originalSql); } catch (SQLException sqlEx) { // don't wrap SQLExceptions this.invalidationException = sqlEx; } catch (Exception ex) { this.invalidationException = new SQLException(ex.toString(), SQLError.SQL_STATE_GENERAL_ERROR); } if (this.invalidationException != null) { this.invalid = true; this.parameterBindings = null; this.parameterFields = null; this.resultFields = null; if (this.results != null) { try { this.results.close(); } catch (Exception ex) { ; } } if (this.connection != null) { if (this.maxRowsChanged) { this.connection.unsetMaxRows(this); } if (!this.connection.getDontTrackOpenResources()) { this.connection.unregisterStatement(this); } } } } /** * Closes this connection and frees all resources. * * @param calledExplicitly was this called from close()? * * @throws SQLException if an error occurs */ protected void realClose(boolean calledExplicitly) throws SQLException { if (this.isClosed) { return; } SQLException exceptionDuringClose = null; try { synchronized (this.connection.getMutex()) { MysqlIO mysql = this.connection.getIO(); Buffer packet = mysql.getSharedSendPacket(); packet.writeByte((byte) MysqlDefs.COM_CLOSE_STATEMENT); packet.writeLong(this.serverStatementId); mysql.sendCommand(MysqlDefs.COM_CLOSE_STATEMENT, null, packet, true, null); } } catch (SQLException sqlEx) { exceptionDuringClose = sqlEx; } clearParametersInternal(false); this.parameterBindings = null; this.parameterFields = null; this.resultFields = null; super.realClose(calledExplicitly); if (exceptionDuringClose != null) { throw exceptionDuringClose; } } /** * @see com.mysql.jdbc.PreparedStatement#getBytes(int) */ synchronized byte[] getBytes(int parameterIndex) throws SQLException { BindValue bindValue = getBinding(parameterIndex, false); if (bindValue.isNull) { return null; } else if (bindValue.isLongData) { throw new NotImplemented(); } else { if (this.outByteBuffer == null) { this.outByteBuffer = Buffer.allocateNew(this.connection.getNetBufferLength(), false); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -