Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ public String toString() {


enum PrepareMethod {
NONE("none"),
PREPEXEC("prepexec"), // sp_prepexec, default prepare method
PREPARE("prepare"),
SCOPE_TEMP_TABLES_TO_CONNECTION("scopeTempTablesToConnection");
Expand Down Expand Up @@ -841,8 +842,8 @@ public final class SQLServerDriver implements java.sql.Driver {
SQLServerDriverStringProperty.SERVER_CERTIFICATE.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.PREPARE_METHOD.toString(),
SQLServerDriverStringProperty.PREPARE_METHOD.getDefaultValue(), false,
new String[] { PrepareMethod.PREPEXEC.toString(), PrepareMethod.PREPARE.toString(),
PrepareMethod.SCOPE_TEMP_TABLES_TO_CONNECTION.toString() }),
new String[] { PrepareMethod.NONE.toString(), PrepareMethod.PREPEXEC.toString(),
PrepareMethod.PREPARE.toString(), PrepareMethod.SCOPE_TEMP_TABLES_TO_CONNECTION.toString() }),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.FAILOVER_PARTNER.toString(),
SQLServerDriverStringProperty.FAILOVER_PARTNER.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.HOSTNAME_IN_CERTIFICATE.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,10 +685,13 @@ final void doExecutePreparedStatement(PrepStmtExecCmd command) throws SQLServerE
// continue using it after we return.
// Use PKT_QUERY for exec mode (direct execution), PKT_RPC for prepared
// statements
boolean isPrepareMethodNone = connection.getPrepareMethod()
.equals(PrepareMethod.NONE.toString());
boolean isScopeTempTablesToConnection = connection.getPrepareMethod()
.equals(PrepareMethod.SCOPE_TEMP_TABLES_TO_CONNECTION.toString())
&& containsTemporaryTableOperations(userSQL);
TDSWriter tdsWriter = command.startRequest(isScopeTempTablesToConnection ? TDS.PKT_QUERY : TDS.PKT_RPC);
boolean isDirectSqlRequest = isPrepareMethodNone || isScopeTempTablesToConnection;
TDSWriter tdsWriter = command.startRequest(isDirectSqlRequest ? TDS.PKT_QUERY : TDS.PKT_RPC);

needsPrepare = doPrepExec(tdsWriter, inOutParam, hasNewTypeDefinitions, hasExistingTypeDefinitions,
command);
Expand Down Expand Up @@ -1130,7 +1133,8 @@ private boolean reuseCachedHandle(boolean hasNewTypeDefinitions, boolean discard
return false;

// No caching for exec method as it always executes directly without preparation
if (connection.getPrepareMethod().equals(PrepareMethod.SCOPE_TEMP_TABLES_TO_CONNECTION.toString())) {
if (connection.getPrepareMethod().equals(PrepareMethod.SCOPE_TEMP_TABLES_TO_CONNECTION.toString())
|| connection.getPrepareMethod().equals(PrepareMethod.NONE.toString())) {
return false;
}

Expand Down Expand Up @@ -1230,16 +1234,16 @@ private boolean doPrepExec(TDSWriter tdsWriter, Parameter[] params, boolean hasN
boolean hasExistingTypeDefinitions, TDSCommand command) throws SQLServerException {

boolean needsPrepare = (hasNewTypeDefinitions && hasExistingTypeDefinitions) || !hasPreparedStatementHandle();
boolean isPrepareMethodNone = connection.getPrepareMethod().equals(PrepareMethod.NONE.toString());
boolean isPrepareMethodSpPrepExec = connection.getPrepareMethod().equals(PrepareMethod.PREPEXEC.toString());
boolean isScopeTempTablesToConnection = connection.getPrepareMethod()
.equals(PrepareMethod.SCOPE_TEMP_TABLES_TO_CONNECTION.toString());

// If using scopeTempTablesToConnection method, check if temp tables are present
// in the SQLisScopeTempTablesToConnectionns
if (isScopeTempTablesToConnection && containsTemporaryTableOperations(userSQL)) {
// Build direct SQL using enhanced replaceParameterMarkers with direct values
String directSQL = replaceParameterMarkersWithValues(userSQL, userSQLParamPositions, params,
false);
// For prepareMethod=none, always send direct SQL. For prepareMethod=scopeTempTablesToConnection,
// send direct SQL only when temporary table operations are detected in the user SQL.
if (isPrepareMethodNone || (isScopeTempTablesToConnection && containsTemporaryTableOperations(userSQL))) {
// Build direct SQL using enhanced replaceParameterMarkersWithValues with direct values
String directSQL = replaceParameterMarkersWithValues(userSQL, userSQLParamPositions, params, false);
tdsWriter.writeString(directSQL);

expectPrepStmtHandle = false;
Expand Down Expand Up @@ -3064,6 +3068,7 @@ final void doExecutePreparedStatementBatch(PrepStmtBatchExecCmd batchCommand)

int numBatchesPrepared = 0;
int numBatchesExecuted = 0;
boolean isPrepareMethodNone = connection.getPrepareMethod().equals(PrepareMethod.NONE.toString());
boolean isScopeTempTablesToConnectionMethod = connection.getPrepareMethod()
.equals(PrepareMethod.SCOPE_TEMP_TABLES_TO_CONNECTION.toString());
boolean isScopeTempTablesToConnection = isScopeTempTablesToConnectionMethod
Expand All @@ -3077,10 +3082,10 @@ final void doExecutePreparedStatementBatch(PrepStmtBatchExecCmd batchCommand)
// Make sure any previous maxRows limitation on the connection is removed.
connection.setMaxRows(0);

// For scopeTempTablesToConnection method, handle batch execution with literal
// parameter substitution
// only if temp tables are detected in the SQL
if (isScopeTempTablesToConnection) {
// For prepareMethod=NONE, always handle batch execution with literal parameter
// substitution (direct SQL). For scopeTempTablesToConnection, use this path with
// literal parameter substitution only if temp table operations are detected in the SQL.
if (isPrepareMethodNone || isScopeTempTablesToConnection) {
doExecuteExecMethodBatchCombined(batchCommand);
return;
} else if (isScopeTempTablesToConnectionMethod) {
Expand Down
Loading
Loading