001/*
002 * Copyright 2015-2022 Transmogrify LLC, 2022-2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.pyranid;
018
019import org.jspecify.annotations.NonNull;
020
021import javax.annotation.concurrent.ThreadSafe;
022import java.util.Optional;
023
024/**
025 * Encapsulates {@link java.sql.PreparedStatement} parameter data meant to be bound to a DBMS-specific type (for example, {@code JSON} or {@code JSONB} for PostgreSQL) by {@link PreparedStatementBinder}.
026 * <p>
027 * Stardard instances may be constructed via {@link Parameters#json(String)} and {@link Parameters#json(String, BindingPreference)}.
028 * <p>
029 * Implementations should be threadsafe.
030 *
031 * @author <a href="https://www.revetkn.com">Mark Allen</a>
032 * @since 3.0.0
033 */
034@ThreadSafe
035public interface JsonParameter {
036        /**
037         * Specifies how a {@link JsonParameter} should be bound: binary (for example, {@code JSONB} for PostgreSQL), or text.
038         *
039         * @author <a href="https://www.revetkn.com">Mark Allen</a>
040         * @since 3.0.0
041         */
042        enum BindingPreference {
043                /**
044                 * Prefer a binary/native JSON type when available (for example, {@code JSONB} for PostgreSQL), otherwise falls back to {@link #TEXT}.
045                 */
046                BINARY,
047                /**
048                 * Prefer to bind as text (for example, {@code VARCHAR}/{@code TEXT}/{@code NVARCHAR} or {@code JSON} for PostgreSQL).
049                 */
050                TEXT
051        }
052
053        /**
054         * Gets the "stringified" JSON.
055         *
056         * @return the "stringified" JSON
057         */
058        @NonNull
059        Optional<String> getJson();
060
061        /**
062         * Gets how the JSON should be bound (automatic, binary, text).
063         *
064         * @return how the JSON should be bound
065         */
066        @NonNull
067        BindingPreference getBindingPreference();
068}