📄 serverpreparedstatement.java
字号:
pStmtForSub = PreparedStatement.getInstance(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; } } } } return pStmtForSub.asSql(quoteStreamsAndUnknowns); } finally { if (pStmtForSub != null) { try { pStmtForSub.close(); } catch (SQLException sqlEx) { ; // ignore } } } } /* * (non-Javadoc) * * @see com.mysql.jdbc.Statement#checkClosed() */ protected void checkClosed() throws SQLException { if (this.invalid) { throw this.invalidationException; } super.checkClosed(); } /** * @see java.sql.PreparedStatement#clearParameters() */ public void clearParameters() throws SQLException { checkClosed(); clearParametersInternal(true); } private void clearParametersInternal(boolean clearServerParameters) throws SQLException { boolean hadLongData = false; if (this.parameterBindings != null) { for (int i = 0; i < this.parameterCount; i++) { if ((this.parameterBindings[i] != null) && this.parameterBindings[i].isLongData) { hadLongData = true; } this.parameterBindings[i].reset(); } } if (clearServerParameters && hadLongData) { serverResetStatement(); this.detectedLongParameterSwitch = false; } } protected boolean isCached = false; private boolean useAutoSlowLog; private Calendar serverTzCalendar; private Calendar defaultTzCalendar; protected void setClosed(boolean flag) { this.isClosed = flag; } /** * @see java.sql.Statement#close() */ public synchronized void close() throws SQLException { if (this.isCached && !this.isClosed) { clearParameters(); this.isClosed = true; this.connection.recachePreparedStatement(this); return; } realClose(true, true); } private void dumpCloseForTestcase() { StringBuffer buf = new StringBuffer(); this.connection.generateConnectionCommentBlock(buf); buf.append("DEALLOCATE PREPARE debug_stmt_"); buf.append(this.statementId); buf.append(";\n"); this.connection.dumpTestcaseQuery(buf.toString()); } private void dumpExecuteForTestcase() throws SQLException { StringBuffer buf = new StringBuffer(); for (int i = 0; i < this.parameterCount; i++) { this.connection.generateConnectionCommentBlock(buf); buf.append("SET @debug_stmt_param"); buf.append(this.statementId); buf.append("_"); buf.append(i); buf.append("="); if (this.parameterBindings[i].isNull) { buf.append("NULL"); } else { buf.append(this.parameterBindings[i].toString(true)); } buf.append(";\n"); } this.connection.generateConnectionCommentBlock(buf); buf.append("EXECUTE debug_stmt_"); buf.append(this.statementId); if (this.parameterCount > 0) { buf.append(" USING "); for (int i = 0; i < this.parameterCount; i++) { if (i > 0) { buf.append(", "); } buf.append("@debug_stmt_param"); buf.append(this.statementId); buf.append("_"); buf.append(i); } } buf.append(";\n"); this.connection.dumpTestcaseQuery(buf.toString()); } private void dumpPrepareForTestcase() throws SQLException { StringBuffer buf = new StringBuffer(this.originalSql.length() + 64); this.connection.generateConnectionCommentBlock(buf); buf.append("PREPARE debug_stmt_"); buf.append(this.statementId); buf.append(" FROM \""); buf.append(this.originalSql); buf.append("\";\n"); this.connection.dumpTestcaseQuery(buf.toString()); } protected int[] executeBatchSerially(int batchTimeout) throws SQLException { ConnectionImpl locallyScopedConn = this.connection; if (locallyScopedConn == null) { checkClosed(); } if (locallyScopedConn.isReadOnly()) { throw SQLError.createSQLException(Messages .getString("ServerPreparedStatement.2") //$NON-NLS-1$ + Messages.getString("ServerPreparedStatement.3"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } checkClosed(); synchronized (locallyScopedConn.getMutex()) { clearWarnings(); // Store this for later, we're going to 'swap' them out // as we execute each batched statement... BindValue[] oldBindValues = this.parameterBindings; try { int[] updateCounts = null; if (this.batchedArgs != null) { int nbrCommands = this.batchedArgs.size(); updateCounts = new int[nbrCommands]; if (this.retrieveGeneratedKeys) { this.batchedGeneratedKeys = new ArrayList(nbrCommands); } for (int i = 0; i < nbrCommands; i++) { updateCounts[i] = -3; } SQLException sqlEx = null; int commandIndex = 0; BindValue[] previousBindValuesForBatch = null; CancelTask timeoutTask = null; try { if (locallyScopedConn.getEnableQueryTimeouts() && batchTimeout != 0 && locallyScopedConn.versionMeetsMinimum(5, 0, 0)) { timeoutTask = new CancelTask(this); ConnectionImpl.getCancelTimer().schedule(timeoutTask, batchTimeout); } for (commandIndex = 0; commandIndex < nbrCommands; commandIndex++) { Object arg = this.batchedArgs.get(commandIndex); 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, true); } 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 ByteArrayRow(new byte[][] { rs .getBytes(1) })); } } finally { if (rs != null) { rs.close(); } } } } catch (SQLException ex) { updateCounts[commandIndex] = EXECUTE_FAILED; if (this.continueBatchOnError && !(ex instanceof MySQLTimeoutException) && !(ex instanceof MySQLStatementCancelledException) && !hasDeadlockOrTimeoutRolledBackTx(ex)) { 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); } } } } } finally { if (timeoutTask != null) { timeoutTask.cancel(); } resetCancelledState(); } 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 com.mysql.jdbc.PreparedStatement#executeInternal(int, * com.mysql.jdbc.Buffer, boolean, boolean) */ protected com.mysql.jdbc.ResultSetInternalMethods executeInternal(int maxRowsToRetrieve, Buffer sendPacket, boolean createStreamingResultSet, boolean queryIsSelectOnly, Field[] metadataFromCache, boolean isBatch) throws SQLException { this.numberOfExecutions++; // We defer to server-side execution try { return serverExecute(maxRowsToRetrieve, createStreamingResultSet, metadataFromCache); } 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 = ConnectionImpl.appendMessageToException(sqlEx, messageBuf .toString()); } throw sqlEx; } catch (Exception ex) { if (this.connection.getEnablePacketDebug()) { this.connection.getIO().dumpPacketRingBuffer(); } SQLException sqlEx = SQLError.createSQLException(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 = ConnectionImpl.appendMessageToException(sqlEx, messageBuf .toString()); } sqlEx.initCause(ex); 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 } /** * Returns the structure representing the value that (can be)/(is) * bound at the given parameter index. * * @param parameterIndex 1-based * @param forLongData is this for a stream? * @return * @throws SQLException */ protected BindValue getBinding(int parameterIndex, boolean forLongData) throws SQLException { checkClosed(); if (this.parameterBindings.length == 0) { throw SQLError.createSQLException(Messages .getString("ServerPreparedStatement.8"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } parameterIndex--; if ((parameterIndex < 0) || (parameterIndex >= this.parameterBindings.length)) { throw SQLError.createSQLException(Messages .getString("ServerPreparedStatement.9") //$NON-NLS-1$ + (parameterIndex + 1) + Messages.getString("ServerPreparedStatement.10") //$NON-NLS-1$ + this.parameterBindings.length, SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } if (this.parameterBindings[parameterIndex] == null) { this.parameterBindings[parameterIndex] = new BindValue(); } else { if (this.parameterBindings[parameterIndex].isLongData && !forLongData) { this.detectedLongParameterSwitch = true; } } this.parameterBindings[parameterIndex].isSet = true; this.parameterBindings[parameterIndex].boundBeforeExecutionNum = this.numberOfExecutions;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -