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

Sun, 21 Jun 2020 12:03:43 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 Jun 2020 12:03:43 +0200
changeset 91
c3a6c65b3729
parent 62
833e0385572a
child 138
e2aa673dd473
permissions
-rw-r--r--

adds Edit Version button that replaces the pencil icon

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@62 31 import java.sql.Date;
universe@47 32 import java.sql.PreparedStatement;
universe@47 33 import java.sql.SQLException;
universe@47 34 import java.sql.Types;
universe@47 35 import java.util.Optional;
universe@47 36 import java.util.function.Function;
universe@47 37
universe@47 38 /**
universe@47 39 * Some DAO utilities.
universe@47 40 */
universe@47 41 public final class Functions {
universe@47 42
universe@47 43 public static void setStringOrNull(PreparedStatement stmt, int index, String str) throws SQLException {
universe@47 44 if (str == null || str.isBlank()) {
universe@47 45 stmt.setNull(index, Types.VARCHAR);
universe@47 46 } else {
universe@47 47 stmt.setString(index, str);
universe@47 48 }
universe@47 49 }
universe@47 50
universe@62 51 public static void setDateOrNull(PreparedStatement stmt, int index, Date date) throws SQLException {
universe@62 52 if (date == null) {
universe@62 53 stmt.setNull(index, Types.DATE);
universe@62 54 } else {
universe@62 55 stmt.setDate(index, date);
universe@62 56 }
universe@62 57 }
universe@62 58
universe@47 59 public static <T> void setForeignKeyOrNull(PreparedStatement stmt, int index, T instance, Function<? super T, Integer> keyGetter) throws SQLException {
universe@47 60 Integer key = Optional.ofNullable(instance).map(keyGetter).orElse(null);
universe@47 61 if (key == null) {
universe@47 62 stmt.setNull(index, Types.INTEGER);
universe@47 63 } else {
universe@47 64 stmt.setInt(index, key);
universe@47 65 }
universe@47 66 }
universe@47 67
universe@47 68 private Functions() {
universe@47 69
universe@47 70 }
universe@47 71 }

mercurial