universe@47: /* universe@47: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@47: * universe@47: * Copyright 2018 Mike Becker. All rights reserved. universe@47: * universe@47: * Redistribution and use in source and binary forms, with or without universe@47: * modification, are permitted provided that the following conditions are met: universe@47: * universe@47: * 1. Redistributions of source code must retain the above copyright universe@47: * notice, this list of conditions and the following disclaimer. universe@47: * universe@47: * 2. Redistributions in binary form must reproduce the above copyright universe@47: * notice, this list of conditions and the following disclaimer in the universe@47: * documentation and/or other materials provided with the distribution. universe@47: * universe@47: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@47: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@47: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@47: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@47: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@47: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@47: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@47: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@47: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@47: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@47: * POSSIBILITY OF SUCH DAMAGE. universe@47: * universe@47: */ universe@47: package de.uapcore.lightpit.dao; universe@47: universe@62: import java.sql.Date; universe@47: import java.sql.PreparedStatement; universe@47: import java.sql.SQLException; universe@47: import java.sql.Types; universe@47: import java.util.Optional; universe@47: import java.util.function.Function; universe@47: universe@47: /** universe@47: * Some DAO utilities. universe@47: */ universe@47: public final class Functions { universe@47: universe@47: public static void setStringOrNull(PreparedStatement stmt, int index, String str) throws SQLException { universe@47: if (str == null || str.isBlank()) { universe@47: stmt.setNull(index, Types.VARCHAR); universe@47: } else { universe@47: stmt.setString(index, str); universe@47: } universe@47: } universe@47: universe@62: public static void setDateOrNull(PreparedStatement stmt, int index, Date date) throws SQLException { universe@62: if (date == null) { universe@62: stmt.setNull(index, Types.DATE); universe@62: } else { universe@62: stmt.setDate(index, date); universe@62: } universe@62: } universe@62: universe@47: public static void setForeignKeyOrNull(PreparedStatement stmt, int index, T instance, Function keyGetter) throws SQLException { universe@47: Integer key = Optional.ofNullable(instance).map(keyGetter).orElse(null); universe@47: if (key == null) { universe@47: stmt.setNull(index, Types.INTEGER); universe@47: } else { universe@47: stmt.setInt(index, key); universe@47: } universe@47: } universe@47: universe@47: private Functions() { universe@47: universe@47: } universe@47: }