mirror of
https://github.com/SimplexDevelopment/FreedomNetworkSuite.git
synced 2025-06-28 20:16:42 +00:00
Updates 💯
This commit is contained in:
89
Datura/src/main/java/me/totalfreedom/datura/sql/DBBan.java
Normal file
89
Datura/src/main/java/me/totalfreedom/datura/sql/DBBan.java
Normal file
@ -0,0 +1,89 @@
|
||||
package me.totalfreedom.datura.sql;
|
||||
|
||||
import me.totalfreedom.base.CommonsBase;
|
||||
import me.totalfreedom.datura.banning.SimpleBan;
|
||||
import me.totalfreedom.security.ban.Ban;
|
||||
import me.totalfreedom.security.ban.BanID;
|
||||
import me.totalfreedom.sql.SQL;
|
||||
import me.totalfreedom.utils.FreedomLogger;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class DBBan
|
||||
{
|
||||
private final SQL sql;
|
||||
|
||||
public DBBan(SQL sql)
|
||||
{
|
||||
this.sql = sql;
|
||||
}
|
||||
|
||||
public CompletableFuture<Ban> fromSQL(BanID id)
|
||||
{
|
||||
return sql.executeQuery("SELECT * FROM bans WHERE id = ?", id.getID())
|
||||
.thenApplyAsync(result ->
|
||||
{
|
||||
try
|
||||
{
|
||||
if (result.next())
|
||||
{
|
||||
UUID uuid = UUID.fromString(result.getString("uuid"));
|
||||
Instant timestamp = Instant.parse(result.getString("timestamp"));
|
||||
|
||||
final Instant expiry;
|
||||
String ex = result.getString("expiry");
|
||||
if (ex.equals("-1"))
|
||||
{
|
||||
expiry = null;
|
||||
} else
|
||||
{
|
||||
expiry = Instant.parse(ex);
|
||||
}
|
||||
|
||||
return new SimpleBan(uuid,
|
||||
result.getString("reason"),
|
||||
result.getString("issuer"),
|
||||
timestamp,
|
||||
expiry);
|
||||
}
|
||||
} catch (SQLException e)
|
||||
{
|
||||
FreedomLogger.getLogger("Datura")
|
||||
.error(e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}, CommonsBase.getInstance().getExecutor().getAsync());
|
||||
}
|
||||
|
||||
public void addBan(Ban ban)
|
||||
{
|
||||
sql.executeUpdate("INSERT INTO bans (id, uuid, reason, issuer, timestamp, expiry) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
ban.getBanID().getID(),
|
||||
ban.getOffenderID().toString(),
|
||||
ban.getReason(),
|
||||
ban.getBanIssuer(),
|
||||
ban.getCreationTime().toString(),
|
||||
(ban.getExpiry() != null ? ban.getExpiry().toString() : "-1")
|
||||
);
|
||||
}
|
||||
|
||||
public boolean hasEntry(UUID uuid) {
|
||||
return sql.executeQuery("SELECT * FROM bans WHERE uuid = ?", uuid.toString())
|
||||
.thenApplyAsync(result ->
|
||||
{
|
||||
try
|
||||
{
|
||||
return result.next();
|
||||
} catch (SQLException e)
|
||||
{
|
||||
FreedomLogger.getLogger("Datura")
|
||||
.error(e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}, CommonsBase.getInstance().getExecutor().getAsync())
|
||||
.join();
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package me.totalfreedom.datura.sql;
|
||||
|
||||
import me.totalfreedom.base.CommonsBase;
|
||||
import me.totalfreedom.sql.SQL;
|
||||
|
||||
import java.sql.*;
|
||||
@ -39,7 +40,7 @@ public class MySQL implements SQL
|
||||
throw new CompletionException("Failed to connect to the database: "
|
||||
+ url + "\n", ex);
|
||||
}
|
||||
});
|
||||
}, CommonsBase.getInstance().getExecutor().getAsync());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -57,7 +58,7 @@ public class MySQL implements SQL
|
||||
throw new CompletionException("Failed to prepare statement: "
|
||||
+ query + "\n", ex);
|
||||
}
|
||||
});
|
||||
}, CommonsBase.getInstance().getExecutor().getAsync());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -71,7 +72,7 @@ public class MySQL implements SQL
|
||||
throw new CompletionException("Failed to retrieve a result set from query: "
|
||||
+ query + "\n", ex);
|
||||
}
|
||||
});
|
||||
}, CommonsBase.getInstance().getExecutor().getAsync());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -85,7 +86,7 @@ public class MySQL implements SQL
|
||||
throw new CompletionException("Failed to execute update: "
|
||||
+ query + "\n", ex);
|
||||
}
|
||||
});
|
||||
}, CommonsBase.getInstance().getExecutor().getAsync());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -99,7 +100,7 @@ public class MySQL implements SQL
|
||||
throw new CompletionException("Failed to execute statement: "
|
||||
+ query + "\n", ex);
|
||||
}
|
||||
});
|
||||
}, CommonsBase.getInstance().getExecutor().getAsync());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user