Skip to content
This repository has been archived by the owner on Jul 4, 2022. It is now read-only.
Leonhard edited this page Sep 16, 2020 · 11 revisions

Bundled SQL library

SimplixCore offers you to use a customizable library for sql operations. It's extensible and offers support for connection pools like HikariCP.

Dependencies required

At first you need to add the following dependency to your pom file.

<dependency>  
  <groupId>dev.simplix.core</groupId>  
  <artifactId>simplixcore-database-sql</artifactId>  
  <version>1.0-SNAPSHOT</version>  
  <scope>provided</scope>  
</dependency>

On Spigot & BungeeCord you don't need to shade this into your jar or load this as a library. It's bundled within the SimplixCore binary. Otherwise for standalone applications since there is no SimplixCore binary to load.

Getting started

At first, you need to obtain an instance of SqlDatabaseConnection. You create such an instance using the following static factory method:

SqlDatabaseConnection.hikari(host, user, password, port, database);

This will create a pooled connection to the database using HikariCP. If you want to customize some options you can instantiate the SqlDatabaseConnection using its constructor:

new SqlDatabaseConnection(dataSource, host, user, password, port, database, connectionHandler);

Where dataSource needs to be an instance of javax.sql.DataSource and connectionHandler an instance of dev.simplix.core.database.sql.handlers.SqlConnectionHandler.

How to use

The SqlDatabaseConnection class provides multiple methods to interact with the database.

prepare()

The prepare-method is the easiest of them. It just executes a statement and does nothing return.

databaseConnection.prepare("UPDATE example SET name=? WHERE uuid=?", ps -> {  
  ps.setString(1, name);  
  ps.setString(2, uuid.toString());  
});
query()

The query method is used to retrieve data from the database.

Map<String, Object> data = databaseConnection.query("SELECT * FROM example WHERE name=?",   
 ps -> ps.setString(1, name), resultSet -> {  
  Map<String, Object> out = new HashMap<>();  
  out.put("uniqueId", resultSet.getString("uuid"));  
  out.put("lastJoin", resultSet.getTimestamp("lastJoin"));  
  out.put("firstJoin", resultSet.getTimestamp("firstJoin"));  
  return out;  
});

As you can see, this method returns the object you will return using the ResultSetTransformer. You can do this with every type.