-
Notifications
You must be signed in to change notification settings - Fork 13
implement java.sql.PreparedStatement #24
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
Merged
NathanQingyangXu
merged 39 commits into
mongodb:main
from
NathanQingyangXu:HIBERNATE-13-preparedStatement
Jan 27, 2025
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
5c70ee8
implement java.sql.PreparedStatement
NathanQingyangXu 68bd2ac
disable errorprone scanning on test code (so we can get rid of annoyi…
NathanQingyangXu 88e9d0a
suppress errorprone warnings regarding the usage of java.util.Date in…
NathanQingyangXu c479bf1
improve MongoPreparedStatementIntegrationTests by making it more complex
NathanQingyangXu 4ea1aae
simplify logic after references to other JDBC implementation
NathanQingyangXu 5c63de4
method renaming to improve readability
NathanQingyangXu 6a1bf55
remove 'protected' visibility keyword usage on MongoStatement#execute…
NathanQingyangXu bc32a4c
made all JDBC adapters as interfaces
NathanQingyangXu 43e5a2b
add checkSqlTypeSupported() method to MongoPreparedStatement
NathanQingyangXu 62c76a0
add fail() in MongoPreparedStatement#parseParameters(BsonValue,List)
NathanQingyangXu a34dbf2
rename parameters to parameterValueSetters
NathanQingyangXu 82b6194
fix the wrong usag of fail() in MongoPreparedStatement
NathanQingyangXu b19d26d
get rid of stream usage in MongoPreparedStatement
NathanQingyangXu 203d05f
remove exception message format constant usage in MongoPreparedStatement
NathanQingyangXu b978c8e
use static import for VisibleForTesting in MongoPreparedStatement
NathanQingyangXu 75db3d9
move checkClosed to public methods instead of common private method
NathanQingyangXu 17b91ac
make null value setting go through centralized setNull() method
NathanQingyangXu 2e45866
make time setter invoke other overloaded methods accepting nullable C…
NathanQingyangXu 62862f5
Update src/integrationTest/java/com/mongodb/hibernate/jdbc/MongoPrepa…
NathanQingyangXu d4f691a
remove unnecessary @MockitoSettings(strictness = Strictness.WARN) in …
NathanQingyangXu 17dd554
remove unnecessary ParameterParsingTests in MongoPreparedStatementTests
NathanQingyangXu 46ecaaf
fix erroneous usage of fail in MongoPreparedStatement
NathanQingyangXu 5609f0f
rename `parameters` method arguments consistently in MongoPreparedSta…
NathanQingyangXu d3a10bd
add checking explicitly at the beginning of each public method regard…
NathanQingyangXu 9ed6736
implement set time methods accepting Calendar parameter
NathanQingyangXu d93dfdc
change setNull() for user-typed overloaded method to throw NotYetImpl…
NathanQingyangXu 08916c1
refactor setNull() as per Javadoc; improve checkParameterIndex() logic
NathanQingyangXu 96692d6
refactor setNull() as per Javadoc; improve checkParameterIndex() logic
NathanQingyangXu 939424b
use global configuration to simplify mock answer smart nullness config
NathanQingyangXu cc10c1b
remove Blob and Clob support
NathanQingyangXu 55b223d
add isWrapper() implementation to be consistent with metadata PR
NathanQingyangXu 412f38f
minor change to PreparedStatementAdapter's javadoc (removing 'by defa…
NathanQingyangXu d1c4006
make time related stuff to be implemented in their own ticket
NathanQingyangXu 1073bf0
improve fail() message in MongoPreparedStatement#parseParameters
NathanQingyangXu 89429ab
Update src/test/java/org/mockito/configuration/MockitoConfiguration.java
NathanQingyangXu c71b5d5
remove unnecessary RETURNS_SMART_NULLS from MongoPreparedStatementTes…
NathanQingyangXu 30004df
add isWrapperFor for MongoConnection and MongoStatement
NathanQingyangXu bdad28d
Update src/main/java/com/mongodb/hibernate/jdbc/MongoStatement.java
NathanQingyangXu 038ec75
Merge branch 'main' into HIBERNATE-13-preparedStatement
NathanQingyangXu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
215 changes: 215 additions & 0 deletions
215
...tegrationTest/java/com/mongodb/hibernate/jdbc/MongoPreparedStatementIntegrationTests.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| /* | ||
| * Copyright 2024-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.mongodb.hibernate.jdbc; | ||
|
|
||
| import static com.mongodb.hibernate.internal.MongoAssertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import java.util.function.Function; | ||
| import org.bson.BsonDocument; | ||
| import org.hibernate.Session; | ||
| import org.hibernate.SessionFactory; | ||
| import org.hibernate.cfg.Configuration; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| class MongoPreparedStatementIntegrationTests { | ||
|
|
||
| private static @Nullable SessionFactory sessionFactory; | ||
|
|
||
| private @Nullable Session session; | ||
|
|
||
| @BeforeAll | ||
| static void beforeAll() { | ||
| sessionFactory = new Configuration().buildSessionFactory(); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void afterAll() { | ||
| if (sessionFactory != null) { | ||
| sessionFactory.close(); | ||
| } | ||
| } | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| session = assertNotNull(sessionFactory).openSession(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| if (session != null) { | ||
| session.close(); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| class ExecuteUpdateTests { | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| assertNotNull(session).doWork(conn -> { | ||
| conn.createStatement() | ||
| .executeUpdate( | ||
| """ | ||
| { | ||
| delete: "books", | ||
| deletes: [ | ||
| { q: {}, limit: 0 } | ||
| ] | ||
| }"""); | ||
| }); | ||
| } | ||
|
|
||
| private static final String INSERT_MQL = | ||
| """ | ||
| { | ||
| insert: "books", | ||
| documents: [ | ||
| { | ||
| _id: 1, | ||
| title: "War and Peace", | ||
| author: "Leo Tolstoy", | ||
| outOfStock: false, | ||
| tags: [ "classic", "tolstoy" ] | ||
| }, | ||
| { | ||
| _id: 2, | ||
| title: "Anna Karenina", | ||
| author: "Leo Tolstoy", | ||
| outOfStock: false, | ||
| tags: [ "classic", "tolstoy" ] | ||
| }, | ||
| { | ||
| _id: 3, | ||
| title: "Crime and Punishment", | ||
| author: "Fyodor Dostoevsky", | ||
| outOfStock: false, | ||
| tags: [ "classic", "Dostoevsky", "literature" ] | ||
| } | ||
| ] | ||
| }"""; | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(booleans = {true, false}) | ||
| void testUpdate(boolean autoCommit) { | ||
| // given | ||
| prepareData(); | ||
|
|
||
| // when && then | ||
| var expectedDocs = Set.of( | ||
| BsonDocument.parse( | ||
| """ | ||
| { | ||
| _id: 1, | ||
| title: "War and Peace", | ||
| author: "Leo Tolstoy", | ||
| outOfStock: true, | ||
| tags: [ "classic", "tolstoy", "literature" ] | ||
| }"""), | ||
| BsonDocument.parse( | ||
| """ | ||
| { | ||
| _id: 2, | ||
| title: "Anna Karenina", | ||
| author: "Leo Tolstoy", | ||
| outOfStock: true, | ||
| tags: [ "classic", "tolstoy", "literature" ] | ||
| }"""), | ||
| BsonDocument.parse( | ||
| """ | ||
| { | ||
| _id: 3, | ||
| title: "Crime and Punishment", | ||
| author: "Fyodor Dostoevsky", | ||
| outOfStock: false, | ||
| tags: [ "classic", "Dostoevsky", "literature" ] | ||
| }""")); | ||
| Function<Connection, MongoPreparedStatement> pstmtProvider = connection -> { | ||
| try { | ||
| var pstmt = (MongoPreparedStatement) | ||
| connection.prepareStatement( | ||
| """ | ||
| { | ||
| update: "books", | ||
| updates: [ | ||
| { | ||
| q: { author: { $undefined: true } }, | ||
| u: { | ||
| $set: { | ||
| outOfStock: { $undefined: true } | ||
| }, | ||
| $push: { tags: { $undefined: true } } | ||
| }, | ||
| multi: true | ||
| } | ||
| ] | ||
| }"""); | ||
| pstmt.setString(1, "Leo Tolstoy"); | ||
| pstmt.setBoolean(2, true); | ||
| pstmt.setString(3, "literature"); | ||
| return pstmt; | ||
| } catch (SQLException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }; | ||
| assertExecuteUpdate(pstmtProvider, autoCommit, 2, expectedDocs); | ||
| } | ||
|
|
||
| private void prepareData() { | ||
| assertNotNull(session).doWork(connection -> { | ||
| connection.setAutoCommit(true); | ||
| var statement = connection.createStatement(); | ||
| statement.executeUpdate(INSERT_MQL); | ||
| }); | ||
| } | ||
|
|
||
| private void assertExecuteUpdate( | ||
| Function<Connection, MongoPreparedStatement> pstmtProvider, | ||
| boolean autoCommit, | ||
| int expectedUpdatedRowCount, | ||
| Set<? extends BsonDocument> expectedDocuments) { | ||
| assertNotNull(session).doWork(connection -> { | ||
| connection.setAutoCommit(autoCommit); | ||
| try (var pstmt = pstmtProvider.apply(connection)) { | ||
| try { | ||
| assertEquals(expectedUpdatedRowCount, pstmt.executeUpdate()); | ||
| } finally { | ||
| if (!autoCommit) { | ||
| connection.commit(); | ||
| } | ||
| } | ||
| var realDocuments = pstmt.getMongoDatabase() | ||
| .getCollection("books", BsonDocument.class) | ||
| .find() | ||
| .into(new HashSet<>()); | ||
| assertEquals(expectedDocuments, realDocuments); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.