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 * 037 * @param statementContext current SQL context 038 * @param preparedStatement the prepared statement to bind to 039 * @param parameterIndex 1-based parameter index at which to perform the binding 040 * @param parameter the parameter to bind at the specified index 041 * @return {@link BindingResult#handled()} if the custom binding was performed, or {@link BindingResult#fallback()} to fall back to default {@link PreparedStatementBinder} behavior 042 * @throws SQLException if an error occurs during binding 043 */ 044 @NonNull 045 BindingResult bind(@NonNull StatementContext<?> statementContext, 046 @NonNull PreparedStatement preparedStatement, 047 @NonNull Integer parameterIndex, 048 @NonNull Object parameter) throws SQLException; 049 050 /** 051 * Performs custom binding of a null value given its {@code index} and {@code targetType}. 052 * <p> 053 * This function is only invoked for {@link TypedParameter} values that are {@code null}. 054 * 055 * @param statementContext current SQL context 056 * @param preparedStatement the prepared statement to bind to 057 * @param parameterIndex 1-based parameter index at which to perform the binding 058 * @param targetType explicit target type from {@link TypedParameter} 059 * @param sqlType JDBC SQL type hint (may be {@link java.sql.Types#NULL}) 060 * @return {@link BindingResult#handled()} if the custom binding was performed, or {@link BindingResult#fallback()} to fall back to default behavior 061 * @throws SQLException if an error occurs during binding 062 */ 063 @NonNull 064 default BindingResult bindNull(@NonNull StatementContext<?> statementContext, 065 @NonNull PreparedStatement preparedStatement, 066 @NonNull Integer parameterIndex, 067 @NonNull TargetType targetType, 068 @NonNull Integer sqlType) throws SQLException { 069 return BindingResult.fallback(); 070 } 071 072 /** 073 * Specifies which types this custom binder should handle. 074 * <p> 075 * For example, if this binder should apply when binding {@code MyCustomType}, this method could return {@code targetType.matchesClass(MyCustomType.class)}. 076 * <p> 077 * For parameterized types like {@code List<UUID>}, this method could return {@code targetType.matchesParameterizedType(List.class, UUID.class)}. 078 * 079 * @param targetType the target type to evaluate - should this custom binder handle it or not? 080 * @return {@code true} if this binder should handle the type, {@code false} otherwise. 081 */ 082 @NonNull 083 Boolean appliesTo(@NonNull TargetType targetType); 084 085 /** 086 * Result of a custom parameter binding attempt. 087 * <p> 088 * 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> 089 */ 090 @ThreadSafe 091 sealed abstract class BindingResult permits BindingResult.Handled, BindingResult.Fallback { 092 private BindingResult() {} 093 094 /** 095 * Indicates that this mapper successfully bound a custom value. 096 * 097 * @return a result which indicates that this binder successfully bound a custom value 098 */ 099 @NonNull 100 public static BindingResult handled() { 101 return Handled.INSTANCE; 102 } 103 104 /** 105 * Indicates that this mapper did not bind a custom value and prefers to fall back to the behavior of the registered {@link PreparedStatementBinder}. 106 * 107 * @return a result which indicates that this binder did not bind a custom value 108 */ 109 @NonNull 110 public static BindingResult fallback() { 111 return Fallback.INSTANCE; 112 } 113 114 @ThreadSafe 115 static final class Handled extends BindingResult { 116 static final Handled INSTANCE = new Handled(); 117 118 private Handled() {} 119 } 120 121 @ThreadSafe 122 static final class Fallback extends BindingResult { 123 static final Fallback INSTANCE = new Fallback(); 124 125 private Fallback() {} 126 } 127 } 128}