-
Notifications
You must be signed in to change notification settings - Fork 453
Introduce new prepareMethod exec for PreparedStatement #2844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
divang
wants to merge
31
commits into
main
Choose a base branch
from
dev/divang/sybase-migration-dynamic-prepare
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,734
−30
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Add EXEC enum value to PrepareMethod for direct execution without preparation
- Implement Sybase DYNAMIC_PREPARE=false equivalent functionality
- Update reuseCachedHandle to disable caching for exec method
- Modify doPrepExec to bypass statement preparation when using exec method
- Add comprehensive tests for temp table persistence and parameter binding
- Update resource strings for proper documentation
This enables seamless migration from Sybase applications by providing
direct statement execution without preparation, ensuring temp tables
persist across executions as expected by legacy Sybase applications.
Connection usage:
String url = "jdbc:sqlserver://server:1433;databaseName=mydb;prepareMethod=exec";
DataSource usage:
SQLServerDataSource ds = new SQLServerDataSource();
ds.setPrepareMethod("exec");
This reverts commit f8811ea.
- Implemented SqlServerPreparedStatementExpander utility class for expanding prepared statement placeholders with actual parameter values - Added support for prepareMethod=exec to enable direct execution mode similar to Sybase's DYNAMIC_PREPARE=false - Handles proper SQL syntax parsing to avoid replacing placeholders inside strings, comments, or delimited identifiers - Implements smart NULL handling with operator rewrites (= ? -> IS NULL, <> ? -> IS NOT NULL, != ? -> IS NOT NULL, IS ? -> IS NULL, IS NOT ? -> IS NOT NULL) - Formats various data types: strings (with single quote escaping), numbers, booleans (as 1/0), dates/times, byte arrays (as hex), CLOBs, BLOBs - Added comprehensive JUnit test suite (23 test cases) covering all scenarios - Added demo class for manual testing and verification - Updated SQLServerConnection to use the new expander when useDirectValues=true in replaceParameterMarkers
- Fix operator detection to check for two-char operators (!=, <>) before single-char
- Fix spacing issues when replacing operators with IS NULL/IS NOT NULL
- Fix string literal parsing to properly handle escaped quotes ('')
- All 23 tests now pass successfully
…ev/divang/sybase-migration-dynamic-prepare
* Add comprehensive test coverage for EXEC prepare method * commented testFourPartSyntaxCallEscapeSyntax due to linked server error
Contributor
Author
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
machavan
reviewed
Nov 26, 2025
src/main/java/com/microsoft/sqlserver/jdbc/SqlServerPreparedStatementExpander.java
Outdated
Show resolved
Hide resolved
.../java/com/microsoft/sqlserver/jdbc/preparedStatement/PreparedStatementExecMethodE2ETest.java
Outdated
Show resolved
Hide resolved
machavan
reviewed
Nov 26, 2025
src/main/java/com/microsoft/sqlserver/jdbc/SqlServerPreparedStatementExpander.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SqlServerPreparedStatementExpander.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SqlServerPreparedStatementExpander.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SqlServerPreparedStatementExpander.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SqlServerPreparedStatementExpander.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SqlServerPreparedStatementExpander.java
Outdated
Show resolved
Hide resolved
- Moved expandSQLWithParameters() and helper methods from SqlServerPreparedStatementExpander to SQLServerConnection - Converted from static utility class pattern to instance methods - Deleted obsolete SqlServerPreparedStatementExpander class and associated test files - Simplified replaceParameterMarkers() to call expandSQLWithParameters() directly - Reduced code duplication by ~280 lines
…improve numeric formatting - Check sendStringParametersAsUnicode config before adding N prefix to string literals - Improve Float/Double formatting using BigDecimal to avoid precision loss - Separate integer type handling from floating-point types for better precision control
…g formatting - Updated Clob value formatting to respect sendStringParametersAsUnicode setting - Updated fallback case (toString) to respect sendStringParametersAsUnicode setting
- Simplified replaceParameterMarkers when useDirectValues=true by directly iterating paramPositions array instead of complex SQL parsing - Removed expandSQLWithParameters() method which is no longer needed - Uses existing paramPositions array to replace ? markers with formatted values
…rely after encountering a single statement error, instead of marking the failed statement with EXECUTE_FAILED (-3) and continuing with remaining statements.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #2844 +/- ##
============================================
+ Coverage 56.42% 56.59% +0.16%
- Complexity 4555 4627 +72
============================================
Files 151 151
Lines 34560 34764 +204
Branches 5768 5825 +57
============================================
+ Hits 19501 19673 +172
- Misses 12418 12456 +38
+ Partials 2641 2635 -6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
Author
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
machavan
reviewed
Dec 5, 2025
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java
Outdated
Show resolved
Hide resolved
…hExecutionTest; fix batch update count handling for comma-separated SQL statements.
machavan
reviewed
Dec 15, 2025
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java
Outdated
Show resolved
Hide resolved
machavan
reviewed
Dec 16, 2025
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDriver.java
Outdated
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java
Show resolved
Hide resolved
src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java
Show resolved
Hide resolved
…on logic
- Renamed PrepareMethod from 'exec' to 'scopeTempTablesToConnection' for clarity
- Added containsTemporaryTableOperations() method with optimized regex pattern
- Method detects temp tables (#temp, ##global, [#temp_'chars]) in SQL statements
- Only applies scopeTempTablesToConnection when temp tables are present
- Falls back to normal execution when no temp tables detected
Batch execution improvements:
- Fixed batch combining to separate CREATE TABLE prefix from parameterized INSERT
- Uses lastIndexOf(';', firstParamPos) to find proper SQL statement boundary
- Handles whitespace offset calculation for accurate parameter positions
- Fixed exception hierarchy: BatchUpdateException wrapped in SQLServerException
Regex pattern enhancements:
- Supports both unescaped (#temp) and bracketed ([#temp_'special-chars]) identifiers
- Detects CREATE TABLE, INSERT, UPDATE, DELETE, SELECT INTO operations
- Performance optimized with pre-compiled patterns
Test coverage:
- Added comprehensive test cases for temp table batch execution
- Tests constraint violations with proper update counts validation
- Tests exception cause chain unwrapping
- All 5 tests in SQLServerPreparedStatementTempTableTest passing
…epare method in PrepareMethodExecTest and PreparedStatementTest.
…/github.com/microsoft/mssql-jdbc into dev/divang/sybase-migration-dynamic-prepare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
Introduce a new way to execute prepared statements using a single batch execution method.
Solution
Added a new prepareMethod option value: exec. When selected, this method substitutes parameters directly into the SQL and executes the statement as a single batch, rather than preparing and executing in separate steps.
Testing
Unit and integration tests have been added to validate the correct behavior of the exec prepare method. The tests ensure that statements execute successfully and that parameters are substituted as expected.