-
Notifications
You must be signed in to change notification settings - Fork 363
GH-2020 Added SqlTypeResolver abstraction #2024
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
mipo256
wants to merge
1
commit into
spring-projects:main
Choose a base branch
from
mipo256:GH-2020
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
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
97 changes: 97 additions & 0 deletions
97
...jdbc/src/main/java/org/springframework/data/jdbc/core/dialect/DefaultSqlTypeResolver.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,97 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.data.jdbc.core.dialect; | ||
|
||
import java.sql.SQLType; | ||
|
||
import org.springframework.data.relational.repository.query.RelationalParameters; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Default implementation of {@link SqlTypeResolver}. Capable to resolve the {@link SqlType} annotation | ||
* on the {@link java.lang.annotation.ElementType#PARAMETER method parameters}, like this: | ||
* <p> | ||
* <pre class="code"> | ||
* List<User> findByAge(@SqlType(name = "TINYINT", vendorTypeNumber = Types.TINYINT) byte age); | ||
* </pre> | ||
* | ||
* Qualification of the actual {@link SQLType} (the sql type of the component), then the following needs to be done: | ||
* <pre class="code"> | ||
* List<User> findByAgeIn(@SqlType(name = "TINYINT", vendorTypeNumber = Types.TINYINT) Integer[] age); | ||
* </pre> | ||
* | ||
* @author Mikhail Polivakha | ||
*/ | ||
public class DefaultSqlTypeResolver implements SqlTypeResolver { | ||
|
||
public static DefaultSqlTypeResolver INSTANCE = new DefaultSqlTypeResolver(); | ||
|
||
@Override | ||
@Nullable | ||
public SQLType resolveSqlType(RelationalParameters.RelationalParameter relationalParameter) { | ||
return resolveInternally(relationalParameter); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public SQLType resolveActualSqlType(RelationalParameters.RelationalParameter relationalParameter) { | ||
return resolveInternally(relationalParameter); | ||
} | ||
|
||
private static AnnotationBasedSqlType resolveInternally( | ||
RelationalParameters.RelationalParameter relationalParameter) { | ||
SqlType parameterAnnotation = relationalParameter.getMethodParameter().getParameterAnnotation(SqlType.class); | ||
|
||
if (parameterAnnotation != null) { | ||
return new AnnotationBasedSqlType(parameterAnnotation); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* {@link SQLType} determined from the {@link SqlType} annotation. | ||
* | ||
* @author Mikhail Polivakha | ||
*/ | ||
protected static class AnnotationBasedSqlType implements SQLType { | ||
|
||
private final SqlType sqlType; | ||
|
||
public AnnotationBasedSqlType(SqlType sqlType) { | ||
Assert.notNull(sqlType, "sqlType must not be null"); | ||
|
||
this.sqlType = sqlType; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return sqlType.name(); | ||
} | ||
|
||
@Override | ||
public String getVendor() { | ||
return "Spring Data JDBC"; | ||
} | ||
|
||
@Override | ||
public Integer getVendorTypeNumber() { | ||
return sqlType.vendorTypeNumber(); | ||
} | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/dialect/SqlType.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,53 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.data.jdbc.core.dialect; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.sql.SQLType; | ||
|
||
/** | ||
* Serves as a hint to the {@link DefaultSqlTypeResolver}, that signals the {@link java.sql.SQLType} to be used. | ||
* The arguments of this annotation are identical to the methods on {@link java.sql.SQLType} interface, expect for | ||
* the {@link SQLType#getVendor()}, which is absent, because it typically does not matter as such for the underlying | ||
* JDBC drivers. The examples of usage, can be found in javadoc of {@link DefaultSqlTypeResolver}. | ||
* | ||
* @see DefaultSqlTypeResolver | ||
* @author Mikhail Polivakha | ||
*/ | ||
@Documented | ||
@Target({ElementType.PARAMETER}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface SqlType { | ||
|
||
/** | ||
* Returns the {@code SQLType} name that represents a SQL data type. | ||
* | ||
* @return The name of this {@code SQLType}. | ||
*/ | ||
String name(); | ||
|
||
/** | ||
* Returns the vendor specific type number for the data type. | ||
* | ||
* @return An Integer representing the vendor specific data type | ||
*/ | ||
int vendorTypeNumber(); | ||
} |
52 changes: 52 additions & 0 deletions
52
...g-data-jdbc/src/main/java/org/springframework/data/jdbc/core/dialect/SqlTypeResolver.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,52 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.data.jdbc.core.dialect; | ||
|
||
import java.sql.SQLType; | ||
|
||
import org.springframework.data.relational.repository.query.RelationalParameters.RelationalParameter; | ||
import org.springframework.data.util.TypeInformation; | ||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* Common interface for all objects capable to resolve the {@link SQLType} to be used for a give method parameter. | ||
* | ||
* @author Mikhail Polivakha | ||
*/ | ||
public interface SqlTypeResolver { | ||
|
||
/** | ||
* Resolving the {@link SQLType} from the given {@link RelationalParameter}. | ||
* | ||
* @param relationalParameter the parameter of the query method | ||
* @return {@code null} in case the given {@link SqlTypeResolver} cannot or do not want to determine the | ||
* {@link SQLType} of the given parameter | ||
*/ | ||
@Nullable | ||
SQLType resolveSqlType(RelationalParameter relationalParameter); | ||
|
||
/** | ||
* Resolving the {@link SQLType} from the given {@link RelationalParameter}. The definition of "actual" | ||
* type can be looked up in the {@link TypeInformation#getActualType()}. | ||
* | ||
* @param relationalParameter the parameter of the query method | ||
* @return {@code null} in case the given {@link SqlTypeResolver} cannot or do not want to determine the | ||
* actual {@link SQLType} of the given parameter | ||
*/ | ||
@Nullable | ||
SQLType resolveActualSqlType(RelationalParameter relationalParameter); | ||
} |
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.
Maybe we can move this in the separate commit? We have a chance to remove it in spring-data-jdbc 4.0 major release