From 65e3c7c6b024bacc6b98e5a11853363fb2ec8354 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Sat, 23 Jan 2021 11:48:23 +0200 Subject: [PATCH] Updated APIv5 Query API (markdown) --- APIv5-Query-API.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/APIv5-Query-API.md b/APIv5-Query-API.md index 03accce..c6b5564 100644 --- a/APIv5-Query-API.md +++ b/APIv5-Query-API.md @@ -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(
Example of update - insert if not exist procedure -``` +```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 (?, ?)";