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 * 083 * @param targetType the target type to evaluate - should this custom binder handle it or not? 084 * @return {@code true} if this binder should handle the type, {@code false} otherwise. 085 */ 086 @NonNull 087 Boolean appliesTo(@NonNull TargetType targetType); 088 089 /** 090 * Result of a custom parameter binding attempt. 091 * <p> 092 * 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> 093 */ 094 @ThreadSafe 095 sealed abstract class BindingResult permits BindingResult.Handled, BindingResult.Fallback { 096 private BindingResult() {} 097 098 /** 099 * Indicates that this mapper successfully bound a custom value. 100 * 101 * @return a result which indicates that this binder successfully bound a custom value 102 */ 103 @NonNull 104 public static BindingResult handled() { 105 return Handled.INSTANCE; 106 } 107 108 /** 109 * Indicates that this mapper did not bind a custom value and prefers to fall back to the behavior of the registered {@link PreparedStatementBinder}. 110 * 111 * @return a result which indicates that this binder did not bind a custom value 112 */ 113 @NonNull 114 public static BindingResult fallback() { 115 return Fallback.INSTANCE; 116 } 117 118 @ThreadSafe 119 static final class Handled extends BindingResult { 120 static final Handled INSTANCE = new Handled(); 121 122 private Handled() {} 123 } 124 125 @ThreadSafe 126 static final class Fallback extends BindingResult { 127 static final Fallback INSTANCE = new Fallback(); 128 129 private Fallback() {} 130 } 131 } 132}