#DataPub
GitLab/GitHub | Build | Repo | Docs
Hello! My name is @FerusGrim, and I’ve recently developed DataPub, what I hope to be one of the better libraries (currently, only, AFAIK) for managing SQL in a variety of formats.
Currently Supported:
- SQLite (Local)
- MySQL (Remote)
- Postgre (Remote)
Features:
- API is Implementation Agnostic.
- Connections automatically close.
- No more
Exception
handling. FauxStatement
Planned Features:
- DataPub4Sponge
- DataPub4Bukkit
- More Database support
Now, I’m sure you’re wondering. What is FauxStatement
? Well, keeping security in mind, I wanted to make sure that PreparedStatement
was always being employed. Why? Namely, during compilation of the Statement
, a PreparedStatement
will automatically escape any illegal characters. This is automatic protection against potential injection attacks, on a database.
So, where does FauxStatement
come in? Well, it contains a method which is used in the API to prepare a statement, while allowing you to manage a query quite easily. Here’s an example:
private final FauxStatement fauxBanPlayer = new FauxStatement("INSERT INTO banned (uuid, reason, banner) VALUES(?, ?, ?)");
private void banPlayer(UUID uuid, String reason, String whoDunIt) {
// Some stuff to handle this server-side.
fauxBanPlayer.setReplacements(uuid, reason, whoDunIt);
this.database.updateDatabase(fauxBanPlayer);
}
That’s it. It’s literally that simple.
Now, maybe you’re wondering how you retrieve data? Now, remember: I automatically close all connections. This includes Statement
and ResultSet
. So… how do you read the results of a query, if I’m closing the ResultSet
on you? Easy. A map. Too simple, you say? It’s really not.
private final FauxStatement fauxGetBanned = new FauxStatement("SELECT * FROM banned");
private List<UUID> getBannedPlayers() {
final List<UUID> bannedPlayers = new ArrayList<UUID>();
final List<Map<String, Object> results = this.database.getResultSet(fauxGetBanned, "uuid");
for (Map<String, Object> result : results) {
bannedPlayers.add(UUID.fromString((String) result.get("uuid"));
}
return bannedPlayers;
}
So, essentially, the result of Database#getResultSet()
acts exactly like a normal ResultSet
, but allows me to ensure that the actual ResultSet
is closed.
Now, I really can’t go over everything in this topic. Links to more information are at the top. Feel free to check them out.
Please, tell me what you think, if you might use this, and how I might improve it.
EDIT: How to work with DataPub.
<repository>
<id>FerusGrim-Public</id>
<url>https://repo.ferusgrim.me/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
[...]
<dependency>
<groupId>me.ferusgrim.datapub</groupId>
<artifactId>datapub-api</artifactId>
<scope>provided</scope>
</dependency>