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

com.github.t9t.jooq.json.JsonBinding 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 json} fields as {@link Json}. When selecting fields, the data is returned as * {@code Json}.

* *

Note that {@code null} values result in a {@code null} object as well, not a {@code Json} * object with {@code null} value!

* *

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 ::json} to the placeholder).

* *

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

*
{@code
 * 
 *     
 *         com.github.t9t.jooq.json.Json
 *         JsonBinding
 *         json
 *     
 * 
 * }
*/ public class JsonBinding implements Binding { private static final Converter CONVERTER = new JsonConverter(); @Override public Converter converter() { return CONVERTER; } @Override public void sql(BindingSQLContext ctx) { ctx.render().sql("?::json"); } @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