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

cn.javaer.jany.jooq.field.JsonbField Maven / Gradle / Ivy

package cn.javaer.jany.jooq.field;

import cn.javaer.jany.jackson.Json;
import org.jooq.Condition;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.JSONB;
import org.jooq.Record;
import org.jooq.SQLDialect;
import org.jooq.Support;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.impl.CustomField;
import org.jooq.impl.DSL;

import java.util.Collections;

/**
 * @author cn-src
 */
public class JsonbField extends CustomField implements TableField {
    private static final long serialVersionUID = -2128410511798819756L;
    private final Table table;
    private final Field[] arguments;

    public JsonbField(final String name, final DataType type, final Field... argument) {
        super(name, type);
        this.arguments = argument;
        this.table = null;
    }

    public JsonbField(final String name, final DataType type, final Table table) {
        super(name, type);
        this.arguments = null;
        this.table = table;
        CustomFieldUtils.addToFields(table, this);
    }

    @Override
    public void accept(final Context ctx) {
        if (null != this.arguments) {
            ctx.sql(this.getName());
            ctx.sql('(');
            int i = 1;
            for (final Field arg : this.arguments) {
                ctx.visit(arg);
                if (i != this.arguments.length) {
                    ctx.sql(',');
                }
                i++;
            }
            ctx.sql(')');
        }
        else {
            if (ctx.qualify()) {
                ctx.visit(this.table);
                ctx.sql('.');
            }
            ctx.visit(this.getUnqualifiedName());
        }
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Support(SQLDialect.POSTGRES)
    public Condition jsonbContains(final T object) {
        if (object instanceof Field) {
            return this.jsonbContains((Field) object);
        }
        final String val;
        if (object instanceof String) {
            val = (String) object;
        }
        else if (object instanceof JSONB) {
            val = ((JSONB) object).data();
        }
        else {
            val = object == null ? null : Json.DEFAULT.write(object);
        }
        return DSL.condition("{0} @> {1}::jsonb", this,
            DSL.val(val, this.getDataType()));
    }

    @Support(SQLDialect.POSTGRES)
    public Condition jsonbContains(final Field field) {
        return DSL.condition("{0} @> {1}", this, field);
    }

    @Support(SQLDialect.POSTGRES)
    public Condition jsonbContains(final String jsonKey, final Object jsonValue) {
        final String json = Json.DEFAULT.write(Collections.singletonMap(jsonKey, jsonValue));
        return DSL.condition("{0} @> {1}::jsonb", this, DSL.val(json, this.getDataType()));
    }

    @Override
    public Table getTable() {
        return this.table;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy