001package com.nimbusds.infinispan.persistence.sql.transformers; 002 003 004import java.sql.SQLException; 005import java.sql.SQLFeatureNotSupportedException; 006import java.sql.Types; 007 008import org.jooq.*; 009import org.jooq.conf.ParamType; 010import org.jooq.impl.DSL; 011 012 013/** 014 * Microsoft SQL server NVARCHAR binding, prefixed 'N' to inline string values. 015 * 016 * <p>Used https://github.com/jOOQ/jOOQ/issues/7825 017 */ 018public class SQLServerNVarcharBinding implements Binding<String,String> { 019 020 021 @Override 022 public Converter<String, String> converter() { 023 /* 024 * String-to-string converter for the data type values. 025 */ 026 return new Converter<>() { 027 @Override 028 public String from(final String databaseObject) { 029 return databaseObject; 030 } 031 032 033 @Override 034 public String to(final String userObject) { 035 return userObject; 036 } 037 038 039 @Override 040 public Class<String> fromType() { 041 return String.class; 042 } 043 044 045 @Override 046 public Class<String> toType() { 047 return String.class; 048 } 049 }; 050 } 051 052 053 @Override 054 public void sql(final BindingSQLContext<String> ctx) { 055 if (ctx.render().paramType() == ParamType.INLINED) { 056 // Prefix value with 'N' for UTF16 057 // https://github.com/jOOQ/jOOQ/issues/9548 058 ctx.render().sql((ctx.value() != null ? "N" : "")).visit(DSL.inline(ctx.value())); 059 } else { 060 ctx.render().sql("?"); 061 } 062 } 063 064 065 // Registering NVARCHAR types for JDBC CallableStatement OUT parameters 066 @Override 067 public void register(final BindingRegisterContext<String> ctx) throws SQLException { 068 ctx.statement().registerOutParameter(ctx.index(), Types.NVARCHAR); 069 } 070 071 072 // Converting the String value and setting that on a JDBC PreparedStatement 073 @Override 074 public void set(final BindingSetStatementContext<String> ctx) throws SQLException { 075 ctx.statement().setNString(ctx.index(), ctx.value()); 076 } 077 078 079 // Getting a String value from a JDBC ResultSet and converting that to a JsonElement 080 @Override 081 public void get(final BindingGetResultSetContext<String> ctx) throws SQLException { 082 ctx.convert(converter()).value(ctx.resultSet().getNString(ctx.index())); 083 } 084 085 086 // Getting a String value from a JDBC CallableStatement 087 @Override 088 public void get(final BindingGetStatementContext<String> ctx) throws SQLException { 089 ctx.convert(converter()).value(ctx.statement().getNString(ctx.index())); 090 } 091 092 093 // Getting a value from a JDBC SQLInput (useful for Oracle OBJECT types) 094 @Override 095 public void get(final BindingGetSQLInputContext<String> ctx) throws SQLException { 096 throw new SQLFeatureNotSupportedException(); 097 } 098 099 100 // Setting a value on a JDBC SQLOutput (useful for Oracle OBJECT types) 101 @Override 102 public void set(final BindingSetSQLOutputContext<String> ctx) throws SQLException { 103 throw new SQLFeatureNotSupportedException(); 104 } 105}