?? serverpreparedstatement.java
字號:
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; protected void setClosed(boolean flag) { this.isClosed = flag; } /** * @see java.sql.Statement#close() */ public void close() throws SQLException { if (this.isCached) { 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()); } /** * @see java.sql.Statement#executeBatch() */ public synchronized int[] executeBatchSerially() throws SQLException { if (this.connection.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 (this.connection.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; 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 byte[][] { rs .getBytes(1) }); } } finally { if (rs != null) { rs.close(); } } } } catch (SQLException ex) { updateCounts[commandIndex] = EXECUTE_FAILED; if (this.continueBatchOnError) { 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 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, Field[] metadataFromCache, boolean isBatch) throws SQLException { this.numberOfExecutions++; // We defer to server-side execution try { return serverExecute(maxRowsToRetrieve, createStreamingResultSet, unpackFields, 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 = Connection.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 = 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 } /** * 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 */ private 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; return this.parameterBindings[parameterIndex]; } /** * @see com.mysql.jdbc.PreparedStatement#getBytes(int) */ 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 = new Buffer(this.connection .getNetBufferLength()); } this.outByteBuffer.clear(); int originalPosition = this.outByteBuffer.getPosition(); storeBinding(this.outByteBuffer, bindValue, this.connection.getIO()); int newPosition = this.outByteBuffer.getPosition(); int length = newPosition - originalPosition; byte[] valueAsBytes = new byte[length]; System.arraycopy(this.outByteBuffer.getByteBuffer(), originalPosition, valueAsBytes, 0, length); return valueAsBytes; } } /** * @see java.sql.PreparedStatement#getMetaData() */ public java.sql.ResultSetMetaData getMetaData() throws SQLException { checkClosed(); if (this.resultFields == null) { return null; } return new ResultSetMetaData(this.resultFields, this.connection.getUseOldAliasMetadataBehavior()); } /** * @see java.sql.PreparedStatement#getParameterMetaData() */ public ParameterMetaData getParameterMetaData() throws SQLException { checkClosed(); if (this.parameterMetaData == null) { this.parameterMetaData = new MysqlParameterMetadata( this.parameterFields, this.parameterCount); } return this.parameterMetaData; } /** * @see com.mysql.jdbc.PreparedStatement#isNull(int) */ boolean isNull(int paramIndex) { throw new IllegalArgumentException(Messages .getString("ServerPreparedStatement.7")); //$NON-NLS-1$ } /** * 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, boolean closeOpenResults) throws SQLException { if (this.isClosed) { return; } if (this.connection != null) { if (this.connection.getAutoGenerateTestcaseScript()) { dumpCloseForTestcase(); } synchronized (this.connection.getMutex()) { // // Don't communicate with the server if we're being // called from the finalizer... // // This will leak server resources, but if we don't do this, // we'll deadlock (potentially, because there's no guarantee // when, what order, and what concurrency finalizers will be // called with). Well-behaved programs won't rely on finalizers // to clean up their statements. // SQLException exceptionDuringClose = null; if (calledExplicitly) { try { MysqlIO mysql = this.connection.getIO(); Buffer packet = mysql.getSharedSendPacket();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -