Updated APIv5 Query API (markdown)

Risto Lahtela 2021-01-23 11:48:23 +02:00
parent 79015e5c34
commit 65e3c7c6b0

@ -22,7 +22,7 @@ See [APIv5](https://github.com/plan-player-analytics/Plan/wiki/APIv5#plan-api-ve
## Obtaining QueryService ## Obtaining QueryService
`QueryService` can be obtained like so: `QueryService` can be obtained like so:
``` ```java
try { try {
QueryService queryService = QueryService.getInstance(); QueryService queryService = QueryService.getInstance();
} catch (IllegalStateException planIsNotEnabled) { } catch (IllegalStateException planIsNotEnabled) {
@ -37,19 +37,19 @@ Database schema might change due to optimizations or additions so it might be ne
- [[Database Schema]] - [[Database Schema]]
You can check if a table exists like this: You can check if a table exists like this:
``` ```java
boolean hasTable = queryService.getCommonQueries().doesDBHaveTable(tableName); boolean hasTable = queryService.getCommonQueries().doesDBHaveTable(tableName);
``` ```
You can check if a table has a column like this: You can check if a table has a column like this:
``` ```java
boolean hasColumn = queryService.getCommonQueries().doesDBHaveTableColumn(tableName, columName); boolean hasColumn = queryService.getCommonQueries().doesDBHaveTableColumn(tableName, columName);
``` ```
Any method called in `CommonQueries` blocks the thread. Any method called in `CommonQueries` blocks the thread.
In order to perform queries in the correct SQL dialect, you might need the following code. Please note that H2 has MySQL compatibility mode engaged. In order to perform queries in the correct SQL dialect, you might need the following code. Please note that H2 has MySQL compatibility mode engaged.
``` ```java
String dbType = queryService.getDBType(); // H2, SQLITE, MYSQL String dbType = queryService.getDBType(); // H2, SQLITE, MYSQL
``` ```
@ -58,7 +58,7 @@ String dbType = queryService.getDBType(); // H2, SQLITE, MYSQL
Queries can be done with `QueryService#query` method. Queries block the thread. Queries can be done with `QueryService#query` method. Queries block the thread.
Here is an example of a completed query Here is an example of a completed query
``` ```java
String result = queryService.query( String result = queryService.query(
"SELECT * FROM plan_users WHERE uuid=?", "SELECT * FROM plan_users WHERE uuid=?",
(PreparedStatement) statement -> { (PreparedStatement) statement -> {
@ -83,7 +83,7 @@ Any method called in `CommonQueries` blocks the thread.
Statements can be executed with `QueryService#execute` method. Statements can be executed with `QueryService#execute` method.
Here is an example of a executed statement Here is an example of a executed statement
``` ```java
Future<?> done = queryService.execute( Future<?> done = queryService.execute(
"INSERT INTO custom_table (uuid, value) VALUES (?, ?)", "INSERT INTO custom_table (uuid, value) VALUES (?, ?)",
(PreparedStatement) statement -> { (PreparedStatement) statement -> {
@ -105,7 +105,7 @@ If you would like to wait for the statement to execute you can call `Future#get(
<details> <details>
<summary>Example of update - insert if not exist procedure</summary> <summary>Example of update - insert if not exist procedure</summary>
``` ```java
public void storeProtocolVersion(UUID uuid, int version) throws ExecutionException { public void storeProtocolVersion(UUID uuid, int version) throws ExecutionException {
String update = "UPDATE plan_version_protocol SET protocol_version=? WHERE uuid=?"; String update = "UPDATE plan_version_protocol SET protocol_version=? WHERE uuid=?";
String insert = "INSERT INTO plan_version_protocol (protocol_version, uuid) VALUES (?, ?)"; String insert = "INSERT INTO plan_version_protocol (protocol_version, uuid) VALUES (?, ?)";