Fri, 23 Oct 2020 20:34:57 +0200
migrate entities package
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@138 | 31 | import java.sql.*; |
universe@138 | 32 | import java.util.ArrayList; |
universe@138 | 33 | import java.util.List; |
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@150 | 42 | public static String getSafeString(ResultSet rs, String column) throws SQLException { |
universe@150 | 43 | return Optional.ofNullable(rs.getString(column)).orElse(""); |
universe@150 | 44 | } |
universe@150 | 45 | |
universe@47 | 46 | public static void setStringOrNull(PreparedStatement stmt, int index, String str) throws SQLException { |
universe@47 | 47 | if (str == null || str.isBlank()) { |
universe@47 | 48 | stmt.setNull(index, Types.VARCHAR); |
universe@47 | 49 | } else { |
universe@47 | 50 | stmt.setString(index, str); |
universe@47 | 51 | } |
universe@47 | 52 | } |
universe@47 | 53 | |
universe@62 | 54 | public static void setDateOrNull(PreparedStatement stmt, int index, Date date) throws SQLException { |
universe@62 | 55 | if (date == null) { |
universe@62 | 56 | stmt.setNull(index, Types.DATE); |
universe@62 | 57 | } else { |
universe@62 | 58 | stmt.setDate(index, date); |
universe@62 | 59 | } |
universe@62 | 60 | } |
universe@62 | 61 | |
universe@47 | 62 | public static <T> void setForeignKeyOrNull(PreparedStatement stmt, int index, T instance, Function<? super T, Integer> keyGetter) throws SQLException { |
universe@47 | 63 | Integer key = Optional.ofNullable(instance).map(keyGetter).orElse(null); |
universe@47 | 64 | if (key == null) { |
universe@47 | 65 | stmt.setNull(index, Types.INTEGER); |
universe@47 | 66 | } else { |
universe@47 | 67 | stmt.setInt(index, key); |
universe@47 | 68 | } |
universe@47 | 69 | } |
universe@47 | 70 | |
universe@138 | 71 | @FunctionalInterface |
universe@138 | 72 | public interface ResultSetMapper<T> { |
universe@138 | 73 | T apply(ResultSet rs) throws SQLException; |
universe@138 | 74 | } |
universe@138 | 75 | |
universe@138 | 76 | public static <T> List<T> list(PreparedStatement stmt, ResultSetMapper<T> mapper) throws SQLException { |
universe@138 | 77 | List<T> results = new ArrayList<>(); |
universe@138 | 78 | try (var result = stmt.executeQuery()) { |
universe@138 | 79 | while (result.next()) { |
universe@138 | 80 | final var project = mapper.apply(result); |
universe@138 | 81 | results.add(project); |
universe@138 | 82 | } |
universe@138 | 83 | } |
universe@138 | 84 | return results; |
universe@138 | 85 | } |
universe@138 | 86 | |
universe@138 | 87 | public static <T> T find(PreparedStatement stmt, ResultSetMapper<T> mapper) throws SQLException { |
universe@138 | 88 | try (var result = stmt.executeQuery()) { |
universe@138 | 89 | if (result.next()) { |
universe@138 | 90 | final var ent = mapper.apply(result); |
universe@138 | 91 | return ent; |
universe@138 | 92 | } else { |
universe@138 | 93 | return null; |
universe@138 | 94 | } |
universe@138 | 95 | } |
universe@138 | 96 | } |
universe@138 | 97 | |
universe@47 | 98 | private Functions() { |
universe@47 | 99 | |
universe@47 | 100 | } |
universe@47 | 101 | } |