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; 020import org.jspecify.annotations.Nullable; 021 022import javax.annotation.concurrent.ThreadSafe; 023 024import static java.util.Objects.requireNonNull; 025 026/** 027 * Statement row-count information observed by Pyranid. 028 * <p> 029 * {@code rowsReturned} counts rows materialized through a JDBC {@link java.sql.ResultSet}. {@code rowsAffected} counts 030 * rows changed by a JDBC update-count operation. Some statements have neither value, and some database features can 031 * logically have both even if Pyranid can only observe one of them through the current JDBC execution path. 032 * 033 * @param rowsReturned rows returned through a result set, or {@code null} if unavailable 034 * @param rowsAffected rows affected by a DML/update operation, or {@code null} if unavailable 035 * @author <a href="https://www.revetkn.com">Mark Allen</a> 036 * @since 4.2.0 037 */ 038@ThreadSafe 039public record StatementResult(@Nullable Long rowsReturned, 040 @Nullable Long rowsAffected) { 041 @NonNull 042 private static final StatementResult EMPTY; 043 044 static { 045 EMPTY = new StatementResult(null, null); 046 } 047 048 public StatementResult { 049 if (rowsReturned != null && rowsReturned < 0L) 050 throw new IllegalArgumentException("rowsReturned must be nonnegative or null, got: " + rowsReturned); 051 052 if (rowsAffected != null && rowsAffected < 0L) 053 throw new IllegalArgumentException("rowsAffected must be nonnegative or null, got: " + rowsAffected); 054 } 055 056 /** 057 * A singleton result for statements with no row-count information. 058 * 059 * @return an empty result 060 */ 061 @NonNull 062 public static StatementResult empty() { 063 return EMPTY; 064 } 065 066 /** 067 * Creates a result with returned-row information. 068 * 069 * @param rowsReturned returned-row count 070 * @return a statement result 071 */ 072 @NonNull 073 public static StatementResult ofRowsReturned(@NonNull Long rowsReturned) { 074 requireNonNull(rowsReturned); 075 return new StatementResult(rowsReturned, null); 076 } 077 078 /** 079 * Creates a result with affected-row information. 080 * 081 * @param rowsAffected affected-row count 082 * @return a statement result 083 */ 084 @NonNull 085 public static StatementResult ofRowsAffected(@NonNull Long rowsAffected) { 086 requireNonNull(rowsAffected); 087 return new StatementResult(null, rowsAffected); 088 } 089}