2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.dao.model.jsontype;
26 import java.sql.CallableStatement;
27 import java.sql.PreparedStatement;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30 import java.sql.Types;
32 import org.hibernate.type.descriptor.ValueBinder;
33 import org.hibernate.type.descriptor.ValueExtractor;
34 import org.hibernate.type.descriptor.WrapperOptions;
35 import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
36 import org.hibernate.type.descriptor.sql.BasicBinder;
37 import org.hibernate.type.descriptor.sql.BasicExtractor;
38 import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
40 public class JsonStringSqlTypeDescriptor implements SqlTypeDescriptor {
43 * The serial version ID.
45 private static final long serialVersionUID = 1103168570216921981L;
47 public static final JsonStringSqlTypeDescriptor INSTANCE = new JsonStringSqlTypeDescriptor();
50 public int getSqlType() {
55 public boolean canBeRemapped() {
60 public <X> ValueBinder<X> getBinder(JavaTypeDescriptor<X> javaTypeDescriptor) {
61 return new BasicBinder<X>(javaTypeDescriptor, this) {
63 protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
65 st.setString(index, javaTypeDescriptor.unwrap(value, String.class, options));
69 protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
71 st.setString(name, javaTypeDescriptor.unwrap(value, String.class, options));
77 public <X> ValueExtractor<X> getExtractor(JavaTypeDescriptor<X> javaTypeDescriptor) {
78 return new BasicExtractor<X>(javaTypeDescriptor, this) {
80 protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
81 return javaTypeDescriptor.wrap(extractJson(rs, name), options);
85 protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
86 return javaTypeDescriptor.wrap(extractJson(statement, index), options);
90 protected X doExtract(CallableStatement statement, String name, WrapperOptions options)
92 return javaTypeDescriptor.wrap(extractJson(statement, name), options);
97 protected Object extractJson(ResultSet rs, String name) throws SQLException {
98 return rs.getObject(name);
101 protected Object extractJson(CallableStatement statement, int index) throws SQLException {
102 return statement.getObject(index);
105 protected Object extractJson(CallableStatement statement, String name) throws SQLException {
106 return statement.getObject(name);