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 java.sql.Connection;
022import java.util.Optional;
023
024/**
025 * Performs raw JDBC work with a callback-scoped {@link Connection} managed by {@link Database}.
026 * <p>
027 * The connection type and safeguards depend on the {@code useRawConnection(...)} overload. The overload that accepts only
028 * this operation supplies a guarded standard JDBC connection. The overload that also accepts a connection class supplies
029 * an unguarded driver connection of the requested type so driver-specific APIs can be used.
030 * <p>
031 * In either case, the connection is valid only for the duration of the callback. Do not close it, retain it, manage
032 * transaction lifecycle directly from it, or mutate connection-wide state such as schema, catalog, client info,
033 * holdability, type map, or network timeout. All statements, result sets, appenders, readers, and other objects obtained
034 * from the connection must be closed before the callback returns.
035 *
036 * @param <C> the connection type made available to the operation
037 * @param <R> the operation result type
038 * @author <a href="https://www.revetkn.com">Mark Allen</a>
039 * @since 4.2.0
040 */
041@FunctionalInterface
042public interface RawConnectionOperation<C extends Connection, R> {
043        /**
044         * Performs raw JDBC work with the provided connection.
045         *
046         * @param connection connection to use
047         * @return the operation result
048         * @throws Exception if the operation fails
049         */
050        @NonNull
051        Optional<R> perform(@NonNull C connection) throws Exception;
052}