-
Notifications
You must be signed in to change notification settings - Fork 7
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
Draft
NathanQingyangXu
wants to merge
8
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.
Draft
Changes from all commits
Commits
Show all changes
8 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 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
101 changes: 101 additions & 0 deletions
101
...Test/java/com/mongodb/hibernate/query/mutation/AbstractMutationQueryIntegrationTests.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,101 @@ | ||
/* | ||
* 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.mutation; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
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.hibernate.dialect.MongoDialect; | ||
import com.mongodb.hibernate.query.AbstractQueryIntegrationTests; | ||
import org.hibernate.dialect.Dialect; | ||
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo; | ||
import org.hibernate.engine.spi.SessionFactoryImplementor; | ||
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.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.Setting; | ||
import org.mockito.stubbing.Answer; | ||
|
||
@ServiceRegistry( | ||
settings = | ||
@Setting( | ||
name = DIALECT, | ||
value = | ||
"com.mongodb.hibernate.query.mutation.AbstractMutationQueryIntegrationTests$MutationTranslateResultAwareDialect")) | ||
public class AbstractMutationQueryIntegrationTests extends AbstractQueryIntegrationTests { | ||
|
||
protected void assertExpectedAffectedCollections(String... expectedAffectedfCollections) { | ||
assertThat(((MutationTranslateResultAwareDialect) getSessionFactoryScope() | ||
.getSessionFactory() | ||
.getJdbcServices() | ||
.getDialect()) | ||
.capturedTranslateResult.getAffectedTableNames()) | ||
.containsExactlyInAnyOrder(expectedAffectedfCollections); | ||
} | ||
|
||
public static final class MutationTranslateResultAwareDialect extends Dialect { | ||
private final Dialect delegate; | ||
private AbstractJdbcOperationQuery capturedTranslateResult; | ||
|
||
public MutationTranslateResultAwareDialect(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 delegate.getSqlAstTranslatorFactory().buildSelectTranslator(sessionFactory, statement); | ||
} | ||
|
||
@Override | ||
public SqlAstTranslator<? extends JdbcOperationQueryMutation> buildMutationTranslator( | ||
SessionFactoryImplementor sessionFactory, MutationStatement statement) { | ||
var originalTranslator = | ||
delegate.getSqlAstTranslatorFactory().buildMutationTranslator(sessionFactory, statement); | ||
var translatorSpy = spy(originalTranslator); | ||
doAnswer((Answer<AbstractJdbcOperationQuery>) invocation -> { | ||
capturedTranslateResult = (AbstractJdbcOperationQuery) invocation.callRealMethod(); | ||
return capturedTranslateResult; | ||
}) | ||
.when(translatorSpy) | ||
.translate(any(), any()); | ||
return translatorSpy; | ||
} | ||
|
||
@Override | ||
public <O extends JdbcMutationOperation> SqlAstTranslator<O> buildModelMutationTranslator( | ||
TableMutation<O> mutation, SessionFactoryImplementor sessionFactory) { | ||
return delegate.getSqlAstTranslatorFactory().buildModelMutationTranslator(mutation, sessionFactory); | ||
} | ||
}; | ||
} | ||
} | ||
} |
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.