All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kiwiproject.test.jdbi.Jdbi3GeneratedKeys Maven / Gradle / Ivy

There is a newer version: 3.7.0
Show newest version
package org.kiwiproject.test.jdbi;

import lombok.experimental.UtilityClass;
import org.jdbi.v3.core.statement.Update;

/**
 * Utilities for executing statements and extracting generated keys from JDBI 3 {@link Update} objects.
 */
@UtilityClass
public class Jdbi3GeneratedKeys {

    /**
     * Extract the value of the generated key named "id" as a Long.
     *
     * @param update the {@link Update} object
     * @return the Long value of the generated key
     * @see #executeAndGenerateKey(Update, String, Class)
     */
    public static Long executeAndGenerateId(Update update) {
        return executeAndGenerateId(update, "id");
    }

    /**
     * Extract the value of the generated key with the given {@code fieldName} as a Long.
     *
     * @param update    the {@link Update} object
     * @param fieldName the name of the generated key field
     * @return the Long value of the generated key
     * @see #executeAndGenerateKey(Update, String, Class)
     */
    public static Long executeAndGenerateId(Update update, String fieldName) {
        return executeAndGenerateKey(update, fieldName, Long.class);
    }

    /**
     * Extract the value of the generated key with the given {@code fieldName} as an instance of the given class.
     *
     * @param update    the {@link Update} object
     * @param fieldName the name of the generated key field
     * @param clazz     the target class of the generated key
     * @param        the key class
     * @return the value of the generated key
     */
    public static  T executeAndGenerateKey(Update update, String fieldName, Class clazz) {
        return update.executeAndReturnGeneratedKeys(fieldName)
                .mapTo(clazz)
                .one();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy