Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. /*
  2. * * Copyright 2019 github.com/ReflxctionDev
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.test;
  17.  
  18. import com.google.gson.Gson;
  19. import com.google.gson.GsonBuilder;
  20.  
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.util.function.Function;
  25.  
  26. /**
  27. * An interface which contains standard SQL queries and methods for interacting with them
  28. */
  29. public interface Query {
  30.  
  31. /**
  32. * The GSON used to handle SQL responses
  33. */
  34. Gson SQL = new GsonBuilder().disableHtmlEscaping().create();
  35.  
  36. /**
  37. * Query to update more than 1 entry all at once
  38. */
  39. String MULTI_UPDATE = "INSERT OR REPLACE INTO statistics(Player, Data) VALUES ";
  40.  
  41. /**
  42. * Query to create a table
  43. */
  44. String CREATE = "create table Statistics (Player varchar not null constraint Statistics_pk primary key, Data varchar default '{}' ); \n" +
  45. "create unique index Statistics_Player_uindex on Statistics (Player);";
  46. /**
  47. * Select query for getting data
  48. */
  49. String SELECT = "SELECT Player, Data FROM statistics WHERE Player = ?";
  50.  
  51. /**
  52. * A simple function for retrieving statistics for the specified player from the database
  53. * directly.
  54. */
  55. Function<String, Statistics> REQUEST_FROM_DATABASE = (player) -> {
  56. try {
  57. PreparedStatement statement = Testy.C.get().prepareStatement(SELECT);
  58. statement.setString(1, player);
  59. ResultSet set = statement.executeQuery();
  60. return SQL.fromJson(set.getString("Data"), Statistics.class);
  61. } catch (SQLException e) {
  62. throw new DataException(e);
  63. }
  64. };
  65.  
  66. /**
  67. * Creates the SQL table
  68. */
  69. static void createTable() {
  70. try {
  71. PreparedStatement statement = Testy.C.get().prepareStatement(CREATE);
  72. statement.execute();
  73. } catch (SQLException e) {
  74. throw new DataException(e);
  75. }
  76. }
  77.  
  78. /**
  79. * Adds the specified player to the cache
  80. *
  81. * @param player Player to add
  82. * @param statistics Statistics to add
  83. */
  84. static void add(String player, Statistics statistics) {
  85. Testy.DATA_CACHE.put(player, statistics);
  86. }
  87.  
  88. /**
  89. * Removes the specified player from the cache
  90. *
  91. * @param player Player to remove
  92. */
  93. static void remove(String player) {
  94. Testy.DATA_CACHE.remove(player);
  95. }
  96.  
  97. /**
  98. * Writes all the cache to the database in a single statement
  99. */
  100. static void addAll() {
  101. if (Testy.DATA_CACHE.isEmpty()) return; // No cache to write
  102. try {
  103. /* Create the query */
  104. StringBuilder builder = new StringBuilder(MULTI_UPDATE);
  105. Testy.DATA_CACHE.forEach((player, statistic) -> builder
  106. .append("('").append(player).append("', '").append(Query.SQL.toJson(statistic)).append("'), "));
  107. String query = builder.toString().substring(0, builder.length() - 2) + ";";
  108.  
  109. /* Execute the statement */
  110. PreparedStatement statement = Testy.C.get().prepareStatement(query);
  111. statement.executeUpdate();
  112. } catch (SQLException e) {
  113. throw new DataException(e);
  114. }
  115. }
  116.  
  117. /**
  118. * Retrieves the statistics of the specified player from the cache, or requests from
  119. * the database and caches the response
  120. *
  121. * @param player Player to retrieve for
  122. * @return The player's statistics
  123. */
  124. static Statistics get(String player) {
  125. return Testy.DATA_CACHE.computeIfAbsent(player, REQUEST_FROM_DATABASE);
  126. }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement