mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-11-25 03:55:37 +01:00
Updated APIv5 Query API (markdown)
parent
79015e5c34
commit
65e3c7c6b0
@ -22,7 +22,7 @@ See [APIv5](https://github.com/plan-player-analytics/Plan/wiki/APIv5#plan-api-ve
|
||||
## Obtaining QueryService
|
||||
|
||||
`QueryService` can be obtained like so:
|
||||
```
|
||||
```java
|
||||
try {
|
||||
QueryService queryService = QueryService.getInstance();
|
||||
} catch (IllegalStateException planIsNotEnabled) {
|
||||
@ -37,19 +37,19 @@ Database schema might change due to optimizations or additions so it might be ne
|
||||
- [[Database Schema]]
|
||||
|
||||
You can check if a table exists like this:
|
||||
```
|
||||
```java
|
||||
boolean hasTable = queryService.getCommonQueries().doesDBHaveTable(tableName);
|
||||
```
|
||||
|
||||
You can check if a table has a column like this:
|
||||
```
|
||||
```java
|
||||
boolean hasColumn = queryService.getCommonQueries().doesDBHaveTableColumn(tableName, columName);
|
||||
```
|
||||
|
||||
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.
|
||||
```
|
||||
```java
|
||||
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.
|
||||
|
||||
Here is an example of a completed query
|
||||
```
|
||||
```java
|
||||
String result = queryService.query(
|
||||
"SELECT * FROM plan_users WHERE uuid=?",
|
||||
(PreparedStatement) statement -> {
|
||||
@ -83,7 +83,7 @@ Any method called in `CommonQueries` blocks the thread.
|
||||
Statements can be executed with `QueryService#execute` method.
|
||||
|
||||
Here is an example of a executed statement
|
||||
```
|
||||
```java
|
||||
Future<?> done = queryService.execute(
|
||||
"INSERT INTO custom_table (uuid, value) VALUES (?, ?)",
|
||||
(PreparedStatement) statement -> {
|
||||
@ -105,7 +105,7 @@ If you would like to wait for the statement to execute you can call `Future#get(
|
||||
<details>
|
||||
<summary>Example of update - insert if not exist procedure</summary>
|
||||
|
||||
```
|
||||
```java
|
||||
public void storeProtocolVersion(UUID uuid, int version) throws ExecutionException {
|
||||
String update = "UPDATE plan_version_protocol SET protocol_version=? WHERE uuid=?";
|
||||
String insert = "INSERT INTO plan_version_protocol (protocol_version, uuid) VALUES (?, ?)";
|
||||
|
Loading…
Reference in New Issue
Block a user