-
Notifications
You must be signed in to change notification settings - Fork 11
simple DML translation #102
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
NathanQingyangXu
wants to merge
21
commits into
mongodb:main
Choose a base branch
from
NathanQingyangXu:HIBERNATE-46
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.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
55ab573
simple DML translation
NathanQingyangXu c4deaf9
Update src/main/java/com/mongodb/hibernate/internal/translate/mongoas…
NathanQingyangXu d71f568
minor improvement to AbstractMqlTranslator#visitInsertStatement()
NathanQingyangXu 1655c57
minor improvements to AbstractMqlTranslator
NathanQingyangXu 163fb45
improvements to naming
NathanQingyangXu f8edea4
Merge branch 'main' into HIBERNATE-46
NathanQingyangXu 6558ab1
some improvements
NathanQingyangXu 164f433
validate affectedTableNames via translation result as opposed to tran…
NathanQingyangXu ed8eb06
Update src/integrationTest/java/com/mongodb/hibernate/query/mutation/…
NathanQingyangXu 3cd06f7
Update src/integrationTest/java/com/mongodb/hibernate/query/Book.java
NathanQingyangXu 29b740f
fix compiling issues after the collection name constant in Book was c…
NathanQingyangXu 8e11fa9
Update src/integrationTest/java/com/mongodb/hibernate/query/mutation/…
NathanQingyangXu 41b1f10
Update src/integrationTest/java/com/mongodb/hibernate/query/mutation/…
NathanQingyangXu 5217cc9
fix compiling issue since last suggestion commit;
NathanQingyangXu f6bd353
add description for FeatureNotSupportedException for returning column…
NathanQingyangXu 36a7d26
add FeatureNotSupportedException when table joining is found
NathanQingyangXu 221ced3
add descriptive messages to unsupported features in insertion statement
NathanQingyangXu 826df65
add disabled testing case to track unique constraint violation except…
NathanQingyangXu 6eb6578
add affectedTableNames generic validation covering both new DML and t…
NathanQingyangXu 90f8509
Merge branch 'main' into HIBERNATE-46
NathanQingyangXu 9c35cf5
resolve conflict with latest main branch
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
283 changes: 283 additions & 0 deletions
283
src/integrationTest/java/com/mongodb/hibernate/query/AbstractQueryIntegrationTests.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,283 @@ | ||
/* | ||
* Copyright 2025-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.query; | ||
|
||
import static com.mongodb.hibernate.MongoTestAssertions.assertIterableEq; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.hibernate.cfg.JdbcSettings.DIALECT; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.spy; | ||
|
||
import com.mongodb.client.MongoCollection; | ||
import com.mongodb.hibernate.TestCommandListener; | ||
import com.mongodb.hibernate.dialect.MongoDialect; | ||
import com.mongodb.hibernate.junit.MongoExtension; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import org.assertj.core.api.InstanceOfAssertFactories; | ||
import org.bson.BsonDocument; | ||
import org.hibernate.dialect.Dialect; | ||
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo; | ||
import org.hibernate.engine.spi.SessionFactoryImplementor; | ||
import org.hibernate.query.MutationQuery; | ||
import org.hibernate.query.SelectionQuery; | ||
import org.hibernate.sql.ast.SqlAstTranslator; | ||
import org.hibernate.sql.ast.SqlAstTranslatorFactory; | ||
import org.hibernate.sql.ast.tree.MutationStatement; | ||
import org.hibernate.sql.ast.tree.select.SelectStatement; | ||
import org.hibernate.sql.exec.spi.AbstractJdbcOperationQuery; | ||
import org.hibernate.sql.exec.spi.JdbcOperation; | ||
import org.hibernate.sql.exec.spi.JdbcOperationQueryMutation; | ||
import org.hibernate.sql.exec.spi.JdbcOperationQuerySelect; | ||
import org.hibernate.sql.model.ast.TableMutation; | ||
import org.hibernate.sql.model.jdbc.JdbcMutationOperation; | ||
import org.hibernate.testing.orm.junit.ServiceRegistry; | ||
import org.hibernate.testing.orm.junit.ServiceRegistryScope; | ||
import org.hibernate.testing.orm.junit.ServiceRegistryScopeAware; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScopeAware; | ||
import org.hibernate.testing.orm.junit.Setting; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.stubbing.Answer; | ||
|
||
@SessionFactory(exportSchema = false) | ||
@ServiceRegistry( | ||
settings = | ||
@Setting( | ||
name = DIALECT, | ||
value = | ||
"com.mongodb.hibernate.query.AbstractQueryIntegrationTests$TranslateResultAwareDialect")) | ||
@ExtendWith(MongoExtension.class) | ||
public abstract class AbstractQueryIntegrationTests implements SessionFactoryScopeAware, ServiceRegistryScopeAware { | ||
|
||
private SessionFactoryScope sessionFactoryScope; | ||
|
||
private TestCommandListener testCommandListener; | ||
|
||
protected SessionFactoryScope getSessionFactoryScope() { | ||
return sessionFactoryScope; | ||
} | ||
|
||
protected TestCommandListener getTestCommandListener() { | ||
return testCommandListener; | ||
} | ||
|
||
@Override | ||
public void injectSessionFactoryScope(SessionFactoryScope sessionFactoryScope) { | ||
this.sessionFactoryScope = sessionFactoryScope; | ||
} | ||
|
||
@Override | ||
public void injectServiceRegistryScope(ServiceRegistryScope serviceRegistryScope) { | ||
this.testCommandListener = serviceRegistryScope.getRegistry().requireService(TestCommandListener.class); | ||
} | ||
|
||
protected <T> void assertSelectionQuery( | ||
String hql, | ||
Class<T> resultType, | ||
Consumer<SelectionQuery<T>> queryPostProcessor, | ||
String expectedMql, | ||
Iterable<T> expectedResultList, | ||
Set<String> expectedAffectedCollections) { | ||
assertSelectionQuery( | ||
hql, | ||
resultType, | ||
queryPostProcessor, | ||
expectedMql, | ||
resultList -> assertIterableEq(expectedResultList, resultList), | ||
expectedAffectedCollections); | ||
} | ||
|
||
protected <T> void assertSelectionQuery( | ||
String hql, | ||
Class<T> resultType, | ||
String expectedMql, | ||
Iterable<T> expectedResultList, | ||
Set<String> expectedAffectedCollections) { | ||
assertSelectionQuery(hql, resultType, null, expectedMql, expectedResultList, expectedAffectedCollections); | ||
} | ||
|
||
protected <T> void assertSelectionQuery( | ||
String hql, | ||
Class<T> resultType, | ||
Consumer<SelectionQuery<T>> queryPostProcessor, | ||
String expectedMql, | ||
Consumer<Iterable<? extends T>> resultListVerifier, | ||
Set<String> expectedAffectedCollections) { | ||
sessionFactoryScope.inTransaction(session -> { | ||
var selectionQuery = session.createSelectionQuery(hql, resultType); | ||
if (queryPostProcessor != null) { | ||
queryPostProcessor.accept(selectionQuery); | ||
} | ||
var resultList = selectionQuery.getResultList(); | ||
|
||
assertActualCommand(BsonDocument.parse(expectedMql)); | ||
|
||
resultListVerifier.accept(resultList); | ||
|
||
assertAffectedCollections(expectedAffectedCollections); | ||
}); | ||
} | ||
|
||
protected <T> void assertSelectionQuery( | ||
String hql, | ||
Class<T> resultType, | ||
String expectedMql, | ||
Consumer<Iterable<? extends T>> resultListVerifier, | ||
Set<String> expectedAffectedCollections) { | ||
assertSelectionQuery(hql, resultType, null, expectedMql, resultListVerifier, expectedAffectedCollections); | ||
} | ||
|
||
protected <T> void assertSelectQueryFailure( | ||
String hql, | ||
Class<T> resultType, | ||
Consumer<SelectionQuery<T>> queryPostProcessor, | ||
Class<? extends Exception> expectedExceptionType, | ||
String expectedExceptionMessage, | ||
Object... expectedExceptionMessageParameters) { | ||
sessionFactoryScope.inTransaction(session -> assertThatThrownBy(() -> { | ||
var selectionQuery = session.createSelectionQuery(hql, resultType); | ||
if (queryPostProcessor != null) { | ||
queryPostProcessor.accept(selectionQuery); | ||
} | ||
selectionQuery.getResultList(); | ||
}) | ||
.isInstanceOf(expectedExceptionType) | ||
.hasMessage(expectedExceptionMessage, expectedExceptionMessageParameters)); | ||
} | ||
|
||
protected <T> void assertSelectQueryFailure( | ||
String hql, | ||
Class<T> resultType, | ||
Class<? extends Exception> expectedExceptionType, | ||
String expectedExceptionMessage, | ||
Object... expectedExceptionMessageParameters) { | ||
assertSelectQueryFailure( | ||
hql, | ||
resultType, | ||
null, | ||
expectedExceptionType, | ||
expectedExceptionMessage, | ||
expectedExceptionMessageParameters); | ||
} | ||
|
||
protected void assertActualCommand(BsonDocument expectedCommand) { | ||
var capturedCommands = testCommandListener.getStartedCommands(); | ||
|
||
assertThat(capturedCommands) | ||
.singleElement() | ||
.asInstanceOf(InstanceOfAssertFactories.MAP) | ||
.containsAllEntriesOf(expectedCommand); | ||
} | ||
|
||
protected void assertMutationQuery( | ||
String hql, | ||
Consumer<MutationQuery> queryPostProcessor, | ||
int expectedMutationCount, | ||
String expectedMql, | ||
MongoCollection<BsonDocument> collection, | ||
Iterable<? extends BsonDocument> expectedDocuments, | ||
Set<String> expectedAffectedCollections) { | ||
sessionFactoryScope.inTransaction(session -> { | ||
var query = session.createMutationQuery(hql); | ||
if (queryPostProcessor != null) { | ||
queryPostProcessor.accept(query); | ||
} | ||
var mutationCount = query.executeUpdate(); | ||
assertActualCommand(BsonDocument.parse(expectedMql)); | ||
assertThat(mutationCount).isEqualTo(expectedMutationCount); | ||
}); | ||
assertThat(collection.find()).containsExactlyElementsOf(expectedDocuments); | ||
assertAffectedCollections(expectedAffectedCollections); | ||
} | ||
|
||
protected void assertMutationQueryFailure( | ||
String hql, | ||
Consumer<MutationQuery> queryPostProcessor, | ||
Class<? extends Exception> expectedExceptionType, | ||
String expectedExceptionMessage, | ||
Object... expectedExceptionMessageParameters) { | ||
sessionFactoryScope.inTransaction(session -> assertThatThrownBy(() -> { | ||
var query = session.createMutationQuery(hql); | ||
if (queryPostProcessor != null) { | ||
queryPostProcessor.accept(query); | ||
} | ||
query.executeUpdate(); | ||
}) | ||
.isInstanceOf(expectedExceptionType) | ||
.hasMessage(expectedExceptionMessage, expectedExceptionMessageParameters)); | ||
} | ||
|
||
private void assertAffectedCollections(Set<String> expectedAffectedCollections) { | ||
assertThat(((TranslateResultAwareDialect) getSessionFactoryScope() | ||
.getSessionFactory() | ||
.getJdbcServices() | ||
.getDialect()) | ||
.capturedTranslateResult.getAffectedTableNames()) | ||
.containsExactlyInAnyOrderElementsOf(expectedAffectedCollections); | ||
} | ||
|
||
protected static final class TranslateResultAwareDialect extends Dialect { | ||
private final Dialect delegate; | ||
private AbstractJdbcOperationQuery capturedTranslateResult; | ||
|
||
public TranslateResultAwareDialect(DialectResolutionInfo info) { | ||
super(info); | ||
delegate = new MongoDialect(info); | ||
} | ||
|
||
@Override | ||
public SqlAstTranslatorFactory getSqlAstTranslatorFactory() { | ||
return new SqlAstTranslatorFactory() { | ||
@Override | ||
public SqlAstTranslator<JdbcOperationQuerySelect> buildSelectTranslator( | ||
SessionFactoryImplementor sessionFactory, SelectStatement statement) { | ||
return createCapturingTranslator( | ||
delegate.getSqlAstTranslatorFactory().buildSelectTranslator(sessionFactory, statement)); | ||
} | ||
|
||
@Override | ||
public SqlAstTranslator<? extends JdbcOperationQueryMutation> buildMutationTranslator( | ||
SessionFactoryImplementor sessionFactory, MutationStatement statement) { | ||
return createCapturingTranslator( | ||
delegate.getSqlAstTranslatorFactory().buildMutationTranslator(sessionFactory, statement)); | ||
} | ||
|
||
@Override | ||
public <O extends JdbcMutationOperation> SqlAstTranslator<O> buildModelMutationTranslator( | ||
TableMutation<O> mutation, SessionFactoryImplementor sessionFactory) { | ||
return delegate.getSqlAstTranslatorFactory().buildModelMutationTranslator(mutation, sessionFactory); | ||
} | ||
|
||
private <T extends JdbcOperation> SqlAstTranslator<T> createCapturingTranslator( | ||
SqlAstTranslator<T> originalTranslator) { | ||
var translatorSpy = spy(originalTranslator); | ||
doAnswer((Answer<AbstractJdbcOperationQuery>) invocation -> { | ||
capturedTranslateResult = (AbstractJdbcOperationQuery) invocation.callRealMethod(); | ||
return capturedTranslateResult; | ||
}) | ||
.when(translatorSpy) | ||
.translate(any(), any()); | ||
return translatorSpy; | ||
} | ||
}; | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this class is reusable not only for
select
query but also formutate
query, so it is renamed and moved to more general location. TheBook
class was changed accordingly to go together with it hand in hand.