All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.t9t.jooq.json.JsonbStringBinding Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
package com.github.t9t.jooq.json;

import org.jooq.*;

import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Types;
import java.util.Objects;

/**
 * 

* jOOQ {@link Binding} to use {@code jsonb} fields as {@code String}. When selecting fields, the data * is returned as {@code String}. When inputting data (eg. on insert and update), the data is sent as text to the * server, and converted to json there (by adding {@code ::jsonb} to the placeholder). *

*

* To use this with the jOOQ code generator, use configuration like this: *

*
{@code
 * 
 *     
 *         java.lang.String
 *         JsonbStringBinding
 *         jsonb
 *     
 * 
 * }
* * @see JsonStringBinding */ public class JsonbStringBinding implements Binding { private static final Converter CONVERTER = new StringConverter(); @Override public Converter converter() { return CONVERTER; } @Override public void sql(BindingSQLContext ctx) { ctx.render().sql("?::jsonb"); } @Override public void register(BindingRegisterContext ctx) throws SQLException { ctx.statement().registerOutParameter(ctx.index(), Types.VARCHAR); } @Override public void set(BindingSetStatementContext ctx) throws SQLException { ctx.statement().setString(ctx.index(), Objects.toString(ctx.convert(converter()).value(), null)); } @Override public void get(BindingGetResultSetContext ctx) throws SQLException { ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index())); } @Override public void get(BindingGetStatementContext ctx) throws SQLException { ctx.convert(converter()).value(ctx.statement().getString(ctx.index())); } @Override public void set(BindingSetSQLOutputContext ctx) throws SQLException { throw new SQLFeatureNotSupportedException(); } @Override public void get(BindingGetSQLInputContext ctx) throws SQLException { throw new SQLFeatureNotSupportedException(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy