001/* 002 * Copyright 2015-2022 Transmogrify LLC, 2022-2025 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 javax.annotation.Nonnull; 020import javax.annotation.concurrent.ThreadSafe; 021import java.sql.PreparedStatement; 022import java.sql.SQLException; 023 024/** 025 * Enables {@link java.sql.PreparedStatement} parameter binding customization via {@link PreparedStatementBinder}. 026 * 027 * @author <a href="https://www.revetkn.com">Mark Allen</a> 028 * @since 3.0.0 029 */ 030public interface CustomParameterBinder { 031 /** 032 * Performs custom binding of a {@link PreparedStatement} value given a {@code value} and its {@code index} when {@link #appliesTo(TargetType)} is {@code true}. 033 * <p> 034 * This function is only invoked when {@code parameter} is non-null. 035 * 036 * @param statementContext current SQL context 037 * @param preparedStatement the prepared statement to bind to 038 * @param parameterIndex 1-based parameter index at which to perform the binding 039 * @param parameter the parameter to bind at the specified index 040 * @return {@link BindingResult#handled()} if the custom binding was performed, or {@link BindingResult#fallback()} to fall back to default {@link PreparedStatementBinder} behavior 041 * @throws SQLException if an error occurs during binding 042 */ 043 @Nonnull 044 BindingResult bind(@Nonnull StatementContext<?> statementContext, 045 @Nonnull PreparedStatement preparedStatement, 046 @Nonnull Integer parameterIndex, 047 @Nonnull Object parameter) throws SQLException; 048 049 /** 050 * Specifies which types this custom binder should handle. 051 * <p> 052 * For example, if this binder should apply when binding {@code MyCustomType}, this method could return {@code targetType.matchesClass(MyCustomType.class)}. 053 * <p> 054 * For parameterized types like {@code List<UUID>}, this method could return {@code targetType.matchesParameterizedType(List.class, UUID.class)}. 055 * 056 * @param targetType the target type to evaluate - should this custom binder handle it or not? 057 * @return {@code true} if this binder should handle the type, {@code false} otherwise. 058 */ 059 @Nonnull 060 Boolean appliesTo(@Nonnull TargetType targetType); 061 062 /** 063 * Result of a custom parameter binding attempt. 064 * <p> 065 * 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> 066 */ 067 @ThreadSafe 068 sealed abstract class BindingResult permits BindingResult.Handled, BindingResult.Fallback { 069 private BindingResult() {} 070 071 /** 072 * Indicates that this mapper successfully bound a custom value. 073 * 074 * @return a result which indicates that this binder successfully bound a custom value 075 */ 076 @Nonnull 077 public static BindingResult handled() { 078 return Handled.INSTANCE; 079 } 080 081 /** 082 * Indicates that this mapper did not bind a custom value and prefers to fall back to the behavior of the registered {@link PreparedStatementBinder}. 083 * 084 * @return a result which indicates that this binder did not bind a custom value 085 */ 086 @Nonnull 087 public static BindingResult fallback() { 088 return Fallback.INSTANCE; 089 } 090 091 @ThreadSafe 092 static final class Handled extends BindingResult { 093 static final Handled INSTANCE = new Handled(); 094 095 private Handled() {} 096 } 097 098 @ThreadSafe 099 static final class Fallback extends BindingResult { 100 static final Fallback INSTANCE = new Fallback(); 101 102 private Fallback() {} 103 } 104 } 105}