src/main/java/de/uapcore/lightpit/dao/Functions.java

Thu, 14 May 2020 22:48:01 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 14 May 2020 22:48:01 +0200
changeset 47
57cfb94ab99f
child 62
833e0385572a
permissions
-rw-r--r--

projects can now be added and updated

universe@47 1 /*
universe@47 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@47 3 *
universe@47 4 * Copyright 2018 Mike Becker. All rights reserved.
universe@47 5 *
universe@47 6 * Redistribution and use in source and binary forms, with or without
universe@47 7 * modification, are permitted provided that the following conditions are met:
universe@47 8 *
universe@47 9 * 1. Redistributions of source code must retain the above copyright
universe@47 10 * notice, this list of conditions and the following disclaimer.
universe@47 11 *
universe@47 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@47 13 * notice, this list of conditions and the following disclaimer in the
universe@47 14 * documentation and/or other materials provided with the distribution.
universe@47 15 *
universe@47 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@47 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@47 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@47 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@47 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@47 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@47 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@47 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@47 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@47 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@47 26 * POSSIBILITY OF SUCH DAMAGE.
universe@47 27 *
universe@47 28 */
universe@47 29 package de.uapcore.lightpit.dao;
universe@47 30
universe@47 31 import java.sql.PreparedStatement;
universe@47 32 import java.sql.SQLException;
universe@47 33 import java.sql.Types;
universe@47 34 import java.util.Optional;
universe@47 35 import java.util.function.Function;
universe@47 36
universe@47 37 /**
universe@47 38 * Some DAO utilities.
universe@47 39 */
universe@47 40 public final class Functions {
universe@47 41
universe@47 42 public static void setStringOrNull(PreparedStatement stmt, int index, String str) throws SQLException {
universe@47 43 if (str == null || str.isBlank()) {
universe@47 44 stmt.setNull(index, Types.VARCHAR);
universe@47 45 } else {
universe@47 46 stmt.setString(index, str);
universe@47 47 }
universe@47 48 }
universe@47 49
universe@47 50 public static <T> void setForeignKeyOrNull(PreparedStatement stmt, int index, T instance, Function<? super T, Integer> keyGetter) throws SQLException {
universe@47 51 Integer key = Optional.ofNullable(instance).map(keyGetter).orElse(null);
universe@47 52 if (key == null) {
universe@47 53 stmt.setNull(index, Types.INTEGER);
universe@47 54 } else {
universe@47 55 stmt.setInt(index, key);
universe@47 56 }
universe@47 57 }
universe@47 58
universe@47 59 private Functions() {
universe@47 60
universe@47 61 }
universe@47 62 }

mercurial