Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * * Copyright 2019 github.com/ReflxctionDev
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.test;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.function.Function;
- /**
- * An interface which contains standard SQL queries and methods for interacting with them
- */
- public interface Query {
- /**
- * The GSON used to handle SQL responses
- */
- Gson SQL = new GsonBuilder().disableHtmlEscaping().create();
- /**
- * Query to update more than 1 entry all at once
- */
- String MULTI_UPDATE = "INSERT OR REPLACE INTO statistics(Player, Data) VALUES ";
- /**
- * Query to create a table
- */
- String CREATE = "create table Statistics (Player varchar not null constraint Statistics_pk primary key, Data varchar default '{}' ); \n" +
- "create unique index Statistics_Player_uindex on Statistics (Player);";
- /**
- * Select query for getting data
- */
- String SELECT = "SELECT Player, Data FROM statistics WHERE Player = ?";
- /**
- * A simple function for retrieving statistics for the specified player from the database
- * directly.
- */
- Function<String, Statistics> REQUEST_FROM_DATABASE = (player) -> {
- try {
- PreparedStatement statement = Testy.C.get().prepareStatement(SELECT);
- statement.setString(1, player);
- ResultSet set = statement.executeQuery();
- return SQL.fromJson(set.getString("Data"), Statistics.class);
- } catch (SQLException e) {
- throw new DataException(e);
- }
- };
- /**
- * Creates the SQL table
- */
- static void createTable() {
- try {
- PreparedStatement statement = Testy.C.get().prepareStatement(CREATE);
- statement.execute();
- } catch (SQLException e) {
- throw new DataException(e);
- }
- }
- /**
- * Adds the specified player to the cache
- *
- * @param player Player to add
- * @param statistics Statistics to add
- */
- static void add(String player, Statistics statistics) {
- Testy.DATA_CACHE.put(player, statistics);
- }
- /**
- * Removes the specified player from the cache
- *
- * @param player Player to remove
- */
- static void remove(String player) {
- Testy.DATA_CACHE.remove(player);
- }
- /**
- * Writes all the cache to the database in a single statement
- */
- static void addAll() {
- if (Testy.DATA_CACHE.isEmpty()) return; // No cache to write
- try {
- /* Create the query */
- StringBuilder builder = new StringBuilder(MULTI_UPDATE);
- Testy.DATA_CACHE.forEach((player, statistic) -> builder
- .append("('").append(player).append("', '").append(Query.SQL.toJson(statistic)).append("'), "));
- String query = builder.toString().substring(0, builder.length() - 2) + ";";
- /* Execute the statement */
- PreparedStatement statement = Testy.C.get().prepareStatement(query);
- statement.executeUpdate();
- } catch (SQLException e) {
- throw new DataException(e);
- }
- }
- /**
- * Retrieves the statistics of the specified player from the cache, or requests from
- * the database and caches the response
- *
- * @param player Player to retrieve for
- * @return The player's statistics
- */
- static Statistics get(String player) {
- return Testy.DATA_CACHE.computeIfAbsent(player, REQUEST_FROM_DATABASE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement