1 package com.vladmihalcea.hibernate.type.json.internal;
2
3 import org.hibernate.type.descriptor.ValueExtractor;
4 import org.hibernate.type.descriptor.WrapperOptions;
5 import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
6 import org.hibernate.type.descriptor.sql.BasicExtractor;
7 import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
8
9 import java.sql.CallableStatement;
10 import java.sql.ResultSet;
11 import java.sql.SQLException;
12 import java.sql.Types;
13
14 /**
15  * @author Vlad Mihalcea
16  */

17 public abstract class AbstractJsonSqlTypeDescriptor implements SqlTypeDescriptor {
18
19     @Override
20     public int getSqlType() {
21         return Types.OTHER;
22     }
23
24     @Override
25     public boolean canBeRemapped() {
26         return true;
27     }
28
29     @Override
30     public <X> ValueExtractor<X> getExtractor(final JavaTypeDescriptor<X> javaTypeDescriptor) {
31         return new BasicExtractor<X>(javaTypeDescriptor, this) {
32             @Override
33             protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
34                 return javaTypeDescriptor.wrap(extractJson(rs, name), options);
35             }
36
37             @Override
38             protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
39                 return javaTypeDescriptor.wrap(extractJson(statement, index), options);
40             }
41
42             @Override
43             protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
44                 return javaTypeDescriptor.wrap(extractJson(statement, name), options);
45             }
46         };
47     }
48
49     protected Object extractJson(ResultSet rs, String name) throws SQLException {
50         return rs.getObject(name);
51     }
52
53     protected Object extractJson(CallableStatement statement, int index) throws SQLException {
54         return statement.getObject(index);
55     }
56
57     protected Object extractJson(CallableStatement statement, String name) throws SQLException {
58         return statement.getObject(name);
59     }
60 }
61