-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMySqlDataSource.java
More file actions
226 lines (185 loc) · 6.76 KB
/
MySqlDataSource.java
File metadata and controls
226 lines (185 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package nl.martenm.servertutorialplus.data;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import nl.martenm.servertutorialplus.ServerTutorialPlus;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* @author MartenM
* @since 24-12-2017.
*/
public class MySqlDataSource implements DataSource {
private ServerTutorialPlus plugin;
private HikariConfig config = new HikariConfig();
private HikariDataSource mySql;
public MySqlDataSource(ServerTutorialPlus plugin){
this.plugin = plugin;
setup();
}
public boolean setup() {
String host = plugin.getConfig().getString("datasource.mysql.host");
String database = plugin.getConfig().getString("datasource.mysql.database");
int port = plugin.getConfig().getInt("datasource.mysql.port");
config = new HikariConfig();
config.setUsername(plugin.getConfig().getString("datasource.mysql.username"));
config.setPassword(plugin.getConfig().getString("datasource.mysql.password"));
config.setJdbcUrl(String.format("jdbc:mysql://%s:%s/%s", host, port, database));
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
mySql = new HikariDataSource(config);
plugin.getLogger().info("Creating Tutorial_Players table.");
if(!simpleSqlUpdate("CREATE TABLE IF NOT EXISTS Tutorial_Players " +
"(uuid VARCHAR(64) not NULL, " +
" tutorial VARCHAR(255), " +
" PRIMARY KEY ( uuid, tutorial))" )){
return false;
}
return simpleSqlUpdate("CREATE TABLE IF NOT EXISTS Quit_Tutorials " +
"(uuid VARCHAR(64) PRIMARY KEY, " +
" tutorial VARCHAR(255) NOT NULL);");
}
public boolean simpleSqlUpdate(String sql)
{
Connection connection = null;
Statement statement = null;
try{
connection = mySql.getConnection();
statement = connection.createStatement();
statement.executeUpdate(sql);
} catch (Exception ex){
plugin.getLogger().warning("[!!!] Error while performing an SQL query!");
ex.printStackTrace();
return false;
} finally {
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return true;
}
@Override
public List<String> getPlayedTutorials(UUID uuid) {
List<String> tutorials = new ArrayList<>();
Connection connection = null;
Statement statement = null;
ResultSet result = null;
try{
connection = mySql.getConnection();
statement = connection.createStatement();
result = statement.executeQuery("select distinct tutorial from Tutorial_Players where uuid='" + uuid + "'");
while (result.next()){
tutorials.add(result.getString(1));
}
} catch (Exception ex){
plugin.getLogger().warning("[!!!] An error occurred while to get a players played tutorials...");
ex.printStackTrace();
return null;
} finally {
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(result != null){
try {
result.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return tutorials;
}
@Override
public String getQuitTutorial(UUID uuid) {
Connection connection = null;
Statement statement = null;
ResultSet result = null;
try{
connection = mySql.getConnection();
statement = connection.createStatement();
result = statement.executeQuery("select tutorial from Quit_Tutorials where uuid='" + uuid + "'");
if (result.next()){
return result.getString("tutorial");
}
} catch (Exception ex){
plugin.getLogger().warning("[!!!] An error occurred while to get a players played tutorials...");
ex.printStackTrace();
return null;
} finally {
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(result != null){
try {
result.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
public boolean addPlayedTutorial(UUID uuid, String id) {
return simpleSqlUpdate("insert into Tutorial_Players (uuid, tutorial) VALUES " + String.format("('%s', '%s')", uuid, id));
}
@Override
public void setQuitTutorial(UUID uuid, String id) {
simpleSqlUpdate("insert into Quit_Tutorials (uuid, tutorial) VALUES " + String.format("('%s', '%s')", uuid, id));
}
@Override
public boolean removePlayedTutorial(UUID uuid, String id) {
return simpleSqlUpdate("delete from tutorial_players where uuid='" + uuid + "' AND tutorial='" + id + "'");
}
@Override
public boolean removeQuitTutorial(UUID uuid) {
return simpleSqlUpdate("delete from Quit_Tutorials where uuid='" + uuid + "';");
}
@Override
public boolean hasPlayedTutorial(UUID uuid, String id) {
List<String> tutorials = getPlayedTutorials(uuid);
if(tutorials == null){
//Replay later for the rewards!
return true;
}
return tutorials.contains(id);
}
}