/* * This file is a part of the SteamWar software. * * Copyright (C) 2022 SteamWar.de-Serverteam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package de.steamwar.sql; import de.steamwar.sql.internal.*; import lombok.AllArgsConstructor; import lombok.Getter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.sql.SQLException; @AllArgsConstructor public class Replay { static { new SqlTypeMapper<>(File.class, "BLOB", (rs, identifier) -> { try { File file = File.createTempFile("replay", ".replay"); file.deleteOnExit(); Files.copy(rs.getBinaryStream(identifier), file.toPath(), StandardCopyOption.REPLACE_EXISTING); return file; } catch (IOException e) { throw new SQLException(e); } }, (st, index, value) -> { try { st.setBinaryStream(index, new FileInputStream(value)); } catch (FileNotFoundException e) { throw new SQLException(e); } }); } private static final Table table = new Table<>(Replay.class); private static final SelectStatement get = table.select(Table.PRIMARY); public static final Statement insert = table.insertAll(); public static Replay get(int fightID) { return get.select(fightID); } public static void save(int fightID, File file) { insert.update(fightID, file); } @Field(keys = {Table.PRIMARY}) private final int fightID; @Getter @Field private final File replay; }