|
| 1 | +/* |
| 2 | + * Copyright 2019-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jdbc.core.mapping; |
| 17 | + |
| 18 | +import java.sql.JDBCType; |
| 19 | +import java.util.Objects; |
| 20 | + |
| 21 | +import org.springframework.lang.Nullable; |
| 22 | + |
| 23 | +/** |
| 24 | + * Wraps a value with the JDBCType that should be used to pass it as a bind parameter to a |
| 25 | + * {@link java.sql.PreparedStatement}. Register a converter from any type to {@link JdbcValue} in order to control the |
| 26 | + * value and the {@link JDBCType} as which a value should get passed to the JDBC driver. |
| 27 | + * |
| 28 | + * @author Jens Schauder |
| 29 | + * @since 2.4 |
| 30 | + */ |
| 31 | +public class JdbcValue { |
| 32 | + |
| 33 | + private final Object value; |
| 34 | + private final JDBCType jdbcType; |
| 35 | + |
| 36 | + protected JdbcValue(@Nullable Object value, @Nullable JDBCType jdbcType) { |
| 37 | + |
| 38 | + this.value = value; |
| 39 | + this.jdbcType = jdbcType; |
| 40 | + } |
| 41 | + |
| 42 | + public static JdbcValue of(@Nullable Object value, @Nullable JDBCType jdbcType) { |
| 43 | + return new JdbcValue(value, jdbcType); |
| 44 | + } |
| 45 | + |
| 46 | + @Nullable |
| 47 | + public Object getValue() { |
| 48 | + return this.value; |
| 49 | + } |
| 50 | + |
| 51 | + @Nullable |
| 52 | + public JDBCType getJdbcType() { |
| 53 | + return this.jdbcType; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean equals(Object o) { |
| 58 | + |
| 59 | + if (this == o) |
| 60 | + return true; |
| 61 | + if (o == null || getClass() != o.getClass()) |
| 62 | + return false; |
| 63 | + JdbcValue jdbcValue = (JdbcValue) o; |
| 64 | + return Objects.equals(value, jdbcValue.value) && jdbcType == jdbcValue.jdbcType; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public int hashCode() { |
| 69 | + return Objects.hash(value, jdbcType); |
| 70 | + } |
| 71 | +} |
0 commit comments