|
| 1 | +/* |
| 2 | + * Copyright 2025-present MongoDB, Inc. |
| 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 | + * http://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 | + |
| 17 | +package com.mongodb.hibernate.internal.type; |
| 18 | + |
| 19 | +import com.mongodb.hibernate.internal.FeatureNotSupportedException; |
| 20 | +import java.io.Serial; |
| 21 | +import java.sql.CallableStatement; |
| 22 | +import java.sql.PreparedStatement; |
| 23 | +import java.sql.ResultSet; |
| 24 | +import java.sql.SQLException; |
| 25 | +import java.sql.SQLFeatureNotSupportedException; |
| 26 | +import org.bson.types.ObjectId; |
| 27 | +import org.hibernate.type.descriptor.ValueBinder; |
| 28 | +import org.hibernate.type.descriptor.ValueExtractor; |
| 29 | +import org.hibernate.type.descriptor.WrapperOptions; |
| 30 | +import org.hibernate.type.descriptor.java.JavaType; |
| 31 | +import org.hibernate.type.descriptor.jdbc.BasicBinder; |
| 32 | +import org.hibernate.type.descriptor.jdbc.BasicExtractor; |
| 33 | +import org.hibernate.type.descriptor.jdbc.JdbcType; |
| 34 | + |
| 35 | +public final class ObjectIdJdbcType implements JdbcType { |
| 36 | + @Serial |
| 37 | + private static final long serialVersionUID = 1L; |
| 38 | + |
| 39 | + private static final MqlType MQL_TYPE = MqlType.OBJECT_ID; |
| 40 | + private static final ObjectIdJavaType JAVA_TYPE = ObjectIdJavaType.INSTANCE; |
| 41 | + public static final ObjectIdJdbcType INSTANCE = new ObjectIdJdbcType(); |
| 42 | + |
| 43 | + private ObjectIdJdbcType() {} |
| 44 | + |
| 45 | + @Override |
| 46 | + public int getJdbcTypeCode() { |
| 47 | + return MQL_TYPE.getVendorTypeNumber(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getFriendlyName() { |
| 52 | + return MQL_TYPE.getName(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public <X> ValueBinder<X> getBinder(JavaType<X> javaType) { |
| 57 | + if (!javaType.equals(JAVA_TYPE)) { |
| 58 | + throw new FeatureNotSupportedException(); |
| 59 | + } |
| 60 | + @SuppressWarnings("unchecked") |
| 61 | + var result = (ValueBinder<X>) Binder.INSTANCE; |
| 62 | + return result; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public <X> ValueExtractor<X> getExtractor(JavaType<X> javaType) { |
| 67 | + if (!javaType.equals(JAVA_TYPE)) { |
| 68 | + throw new FeatureNotSupportedException(); |
| 69 | + } |
| 70 | + @SuppressWarnings("unchecked") |
| 71 | + var result = (ValueExtractor<X>) Extractor.INSTANCE; |
| 72 | + return result; |
| 73 | + } |
| 74 | + |
| 75 | + private static final class Binder extends BasicBinder<ObjectId> { |
| 76 | + @Serial |
| 77 | + private static final long serialVersionUID = 1L; |
| 78 | + |
| 79 | + static Binder INSTANCE = new Binder(); |
| 80 | + |
| 81 | + private Binder() { |
| 82 | + super(JAVA_TYPE, ObjectIdJdbcType.INSTANCE); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected void doBind(PreparedStatement st, ObjectId value, int index, WrapperOptions options) |
| 87 | + throws SQLException { |
| 88 | + st.setObject(index, value, getJdbcType().getJdbcTypeCode()); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + protected void doBind(CallableStatement st, ObjectId value, String name, WrapperOptions options) |
| 93 | + throws SQLException { |
| 94 | + throw new SQLFeatureNotSupportedException(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static final class Extractor extends BasicExtractor<ObjectId> { |
| 99 | + @Serial |
| 100 | + private static final long serialVersionUID = 1L; |
| 101 | + |
| 102 | + static Extractor INSTANCE = new Extractor(); |
| 103 | + |
| 104 | + private Extractor() { |
| 105 | + super(JAVA_TYPE, ObjectIdJdbcType.INSTANCE); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + protected ObjectId doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException { |
| 110 | + return rs.getObject(paramIndex, getJavaType().getJavaTypeClass()); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + protected ObjectId doExtract(CallableStatement statement, int index, WrapperOptions options) |
| 115 | + throws SQLException { |
| 116 | + throw new SQLFeatureNotSupportedException(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + protected ObjectId doExtract(CallableStatement statement, String name, WrapperOptions options) |
| 121 | + throws SQLException { |
| 122 | + throw new SQLFeatureNotSupportedException(); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments