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.sql.PreparedStatement;
023import java.sql.SQLException;
024
025/**
026 * Enables {@link java.sql.PreparedStatement} parameter binding customization via {@link PreparedStatementBinder}.
027 *
028 * @author <a href="https://www.revetkn.com">Mark Allen</a>
029 * @since 3.0.0
030 */
031public interface CustomParameterBinder {
032        /**
033         * Performs custom binding of a {@link PreparedStatement} value given a {@code value} and its {@code index} when {@link #appliesTo(TargetType)} is {@code true}.
034         * <p>
035         * This function is only invoked when {@code parameter} is non-null.
036         * <p>
037         * Pyranid may invoke this method speculatively before falling back to another custom binder or to default
038         * {@link PreparedStatementBinder} behavior. Implementations that return {@link BindingResult#fallback()} should avoid
039         * mutating the {@code preparedStatement} or other externally-visible state before deciding to handle the value.
040         *
041         * @param statementContext  current SQL context
042         * @param preparedStatement the prepared statement to bind to
043         * @param parameterIndex    1-based parameter index at which to perform the binding
044         * @param parameter         the parameter to bind at the specified index
045         * @return {@link BindingResult#handled()} if the custom binding was performed, or {@link BindingResult#fallback()} to fall back to default {@link PreparedStatementBinder} behavior
046         * @throws SQLException if an error occurs during binding
047         */
048        @NonNull
049        BindingResult bind(@NonNull StatementContext<?> statementContext,
050                                                                                 @NonNull PreparedStatement preparedStatement,
051                                                                                 @NonNull Integer parameterIndex,
052                                                                                 @NonNull Object parameter) throws SQLException;
053
054        /**
055         * Performs custom binding of a null value given its {@code index} and {@code targetType}.
056         * <p>
057         * This function is only invoked for {@link TypedParameter} values that are {@code null}.
058         *
059         * @param statementContext  current SQL context
060         * @param preparedStatement the prepared statement to bind to
061         * @param parameterIndex    1-based parameter index at which to perform the binding
062         * @param targetType        explicit target type from {@link TypedParameter}
063         * @param sqlType           JDBC SQL type hint (may be {@link java.sql.Types#NULL})
064         * @return {@link BindingResult#handled()} if the custom binding was performed, or {@link BindingResult#fallback()} to fall back to default behavior
065         * @throws SQLException if an error occurs during binding
066         */
067        @NonNull
068        default BindingResult bindNull(@NonNull StatementContext<?> statementContext,
069                                                                                                                                 @NonNull PreparedStatement preparedStatement,
070                                                                                                                                 @NonNull Integer parameterIndex,
071                                                                                                                                 @NonNull TargetType targetType,
072                                                                                                                                 @NonNull Integer sqlType) throws SQLException {
073                return BindingResult.fallback();
074        }
075
076        /**
077         * Specifies which types this custom binder should handle.
078         * <p>
079         * For example, if this binder should apply when binding {@code MyCustomType}, this method could return {@code targetType.matchesClass(MyCustomType.class)}.
080         * <p>
081         * For parameterized types like {@code List<UUID>}, this method could return {@code targetType.matchesParameterizedType(List.class, UUID.class)}.
082         * Implicit enum parameters use their declaring enum class, even when a constant has its own class body.
083         * An explicit {@link TypedParameter} type takes precedence over this implicit derivation.
084         *
085         * @param targetType the target type to evaluate - should this custom binder handle it or not?
086         * @return {@code true} if this binder should handle the type, {@code false} otherwise.
087         */
088        @NonNull
089        Boolean appliesTo(@NonNull TargetType targetType);
090
091        /**
092         * Result of a custom parameter binding attempt.
093         * <p>
094         * Use {@link #handled()} to indicate a successfully-bound value or {@link #fallback()} to indicate "didn't bind; fall back to the registered {@link PreparedStatementBinder} behavior".</p>
095         */
096        @ThreadSafe
097        sealed abstract class BindingResult permits BindingResult.Handled, BindingResult.Fallback {
098                private BindingResult() {}
099
100                /**
101                 * Indicates that this mapper successfully bound a custom value.
102                 *
103                 * @return a result which indicates that this binder successfully bound a custom value
104                 */
105                @NonNull
106                public static BindingResult handled() {
107                        return Handled.INSTANCE;
108                }
109
110                /**
111                 * Indicates that this mapper did not bind a custom value and prefers to fall back to the behavior of the registered {@link PreparedStatementBinder}.
112                 *
113                 * @return a result which indicates that this binder did not bind a custom value
114                 */
115                @NonNull
116                public static BindingResult fallback() {
117                        return Fallback.INSTANCE;
118                }
119
120                @ThreadSafe
121                static final class Handled extends BindingResult {
122                        static final Handled INSTANCE = new Handled();
123
124                        private Handled() {}
125                }
126
127                @ThreadSafe
128                static final class Fallback extends BindingResult {
129                        static final Fallback INSTANCE = new Fallback();
130
131                        private Fallback() {}
132                }
133        }
134}