Dieser Commit ist enthalten in:
Ursprung
a2bb52ab26
Commit
b4ce9493d1
90
src/de/steamwar/sql/Script.java
Normale Datei
90
src/de/steamwar/sql/Script.java
Normale Datei
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2023 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.sql;
|
||||
|
||||
import de.steamwar.sql.internal.Field;
|
||||
import de.steamwar.sql.internal.SelectStatement;
|
||||
import de.steamwar.sql.internal.Statement;
|
||||
import de.steamwar.sql.internal.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public class Script {
|
||||
|
||||
private static final Table<Script> table = new Table<>(Script.class);
|
||||
|
||||
private static final SelectStatement<Script> select = table.select(Table.PRIMARY);
|
||||
private static final SelectStatement<Script> selectNameUser = table.select("nameUser");
|
||||
|
||||
private static final Statement update = table.updateFields(new String[]{"name"}, Table.PRIMARY);
|
||||
private static final Statement insert = table.insertAll();
|
||||
private static final Statement delete = table.delete(Table.PRIMARY);
|
||||
|
||||
private static final Statement getScript = new Statement("SELECT Code FROM Script WHERE id = ?");
|
||||
private static final Statement updateScript = new Statement("UPDATE Script SET Code = ? WHERE id = ?");
|
||||
|
||||
public static Script get(int id) {
|
||||
return select.select(id);
|
||||
}
|
||||
|
||||
public static Script get(SteamwarUser user, String name) {
|
||||
return selectNameUser.select(user, name);
|
||||
}
|
||||
|
||||
public static Script create(SteamwarUser user, String name, String script) {
|
||||
int id = insert.insertGetKey(user, name, script);
|
||||
return get(id);
|
||||
}
|
||||
|
||||
@Field(keys = Table.PRIMARY, autoincrement = true)
|
||||
private final int id;
|
||||
@Field(keys = "nameUser")
|
||||
private final SteamwarUser user;
|
||||
@Field(keys = "nameUser")
|
||||
private String name;
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
update();
|
||||
}
|
||||
|
||||
public String getScript() {
|
||||
return getScript.select(rs -> {
|
||||
if(rs.next()) {
|
||||
return rs.getString("Code");
|
||||
}
|
||||
return null;
|
||||
}, id);
|
||||
}
|
||||
|
||||
public void setScript(String script) {
|
||||
updateScript.update(script, id);
|
||||
}
|
||||
|
||||
private void update() {
|
||||
update.update(name, id);
|
||||
}
|
||||
|
||||
public void delete() {
|
||||
delete.update(id);
|
||||
}
|
||||
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren