Skip to content

Commit 78b09bf

Browse files
committed
Using Aikar's Hikari IDB API and MySQL annotations hopefully to make major improvements in SQL operations. Requires lots of testing, not ready yet.
1 parent 9b7efdb commit 78b09bf

File tree

19 files changed

+519
-607
lines changed

19 files changed

+519
-607
lines changed

.idea/checkstyle-idea.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ allprojects {
175175

176176
api "net.kyori:adventure-api:$adventureAPI"
177177
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
178-
api 'com.zaxxer:HikariCP:3.4.5'
178+
179179
compile group: 'com.google.code.findbugs', name: 'annotations', version: '3.0.1u2'
180180

181181
// compileOnly 'org.projectlombok:lombok:1.18.12'
@@ -199,7 +199,7 @@ allprojects {
199199
minimize()
200200
relocate 'co.aikar.commands', "${project.group}.acf"
201201
relocate 'co.aikar.locales', "${project.group}.locales"
202-
202+
relocate 'co.aikar.idb', "${project.group}.idb"
203203
}
204204

205205
compileJava.options.encoding = 'UTF-8'

core/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ dependencies {
1212

1313
implementation ('com.google.code.gson:gson:2.8.6')
1414
// compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.17'
15+
api 'com.zaxxer:HikariCP:3.4.5'
1516
implementation 'org.mariadb.jdbc:mariadb-java-client:2.7.0'
17+
api "co.aikar:idb-core:1.0.0-SNAPSHOT"
18+
api 'com.github.Fernthedev:java-api-for-mysql:1.4.2' // For MySQL annotations
19+
1620
api 'com.github.Fernthedev:fern-configmanager:1.3.0' // Check github releases tag for the version.
1721

1822
// 2. Use Guava types in your public API:
@@ -22,6 +26,8 @@ dependencies {
2226
api group: 'org.slf4j', name: 'slf4j-jdk14', version: slf4j_ver
2327
compileOnly 'org.projectlombok:lombok:1.18.16'
2428
annotationProcessor 'org.projectlombok:lombok:1.18.16'
29+
30+
2531
}
2632

2733
compileKotlin {

core/src/main/java/com/github/fernthedev/fernapi/universal/Universal.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.github.fernthedev.fernapi.universal;
22

33
import co.aikar.commands.CommandManager;
4+
import co.aikar.idb.DB;
45
import com.github.fernthedev.fernapi.universal.api.*;
56
import com.github.fernthedev.fernapi.universal.data.network.IPMessageHandler;
67
import com.github.fernthedev.fernapi.universal.data.network.PluginMessageData;
8+
import com.github.fernthedev.fernapi.universal.examples.mysql.DBCommand;
79
import com.github.fernthedev.fernapi.universal.exceptions.FernRuntimeException;
810
import com.github.fernthedev.fernapi.universal.exceptions.setup.IncorrectSetupException;
911
import com.github.fernthedev.fernapi.universal.handlers.*;
10-
import com.github.fernthedev.fernapi.universal.mysql.HikariDatabaseHandler;
11-
import com.github.fernthedev.fernapi.universal.mysql.HikariSQLDriver;
12+
import com.github.fernthedev.fernapi.universal.mysql.AikarFernDatabase;
1213
import com.github.fernthedev.fernapi.universal.util.UUIDFetcher;
1314
import com.github.fernthedev.fernapi.universal.util.UniversalContextResolvers;
1415
import com.github.fernthedev.fernapi.universal.util.VersionUtil;
@@ -44,9 +45,6 @@ private Universal() {}
4445
private static IChatHandler<?> ch;
4546
private static IPMessageHandler mh;
4647

47-
@Setter
48-
private static HikariDatabaseHandler databaseHandler = new HikariDatabaseHandler();
49-
5048
private static CommandManager comhand;
5149
private static NetworkHandler<? extends Object> nh;
5250
private static IScheduler<?,?> sh;
@@ -93,8 +91,7 @@ public void setup(@NonNull MethodInterface<?, ?> methodInterface, FernAPIPlugin
9391
comhand.getCommandContexts().registerContext(OfflineFPlayer.class, new UniversalContextResolvers.SingularIFPlayerContextResolver());
9492
comhand.getCommandContexts().registerContext(IFPlayer[].class, new UniversalContextResolvers.OnlineIFPlayerArrayCommandResolver());
9593

96-
HikariDatabaseHandler.registerSQLDriver(HikariSQLDriver.MARIADB_DRIVER);
97-
HikariDatabaseHandler.registerSQLDriver(HikariSQLDriver.MYSQL_DRIVER);
94+
9895
// comhand.getCommandCompletions().registerAsyncCompletion("fernPlayers", context ->
9996
// mi.getPlayers().parallelStream()
10097
// .filter(player -> !context.getIssuer().isPlayer() ||
@@ -105,6 +102,9 @@ public void setup(@NonNull MethodInterface<?, ?> methodInterface, FernAPIPlugin
105102
// ).map(IFPlayer::getName)
106103
// .collect(Collectors.toList()));
107104

105+
// TODO: Remove!
106+
comhand.registerCommand(new DBCommand());
107+
108108
comhand.getCommandCompletions().setDefaultCompletion("players", IFPlayer.class, IFPlayer[].class, OfflineFPlayer.class, FernCommandIssuer.class);
109109

110110
messageHandler.registerPacketParser("fernapi", json -> new Gson().fromJson(json, PluginMessageData.class));
@@ -149,11 +149,6 @@ public static IPMessageHandler getMessageHandler() {
149149
return mh;
150150
}
151151

152-
public static HikariDatabaseHandler getDatabaseHandler() {
153-
checkNull();
154-
return databaseHandler;
155-
}
156-
157152
public static NetworkHandler<? extends Object> getNetworkHandler() {
158153
checkNull();
159154
return nh;
@@ -174,10 +169,9 @@ public static FernAPIPlugin getPlugin() {
174169
return plugin;
175170
}
176171

172+
177173
public void onDisable() {
178-
new Thread(() -> {
179-
getDatabaseHandler().stopSchedule();
180-
getDatabaseHandler().closeConnection();
181-
}).start();
174+
new Thread(DB::close).start();
175+
new Thread(AikarFernDatabase::shutdownDatabases).start();
182176
}
183177
}
Lines changed: 98 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,127 @@
11
package com.github.fernthedev.fernapi.universal.data.database;
22

3-
import lombok.Getter;
4-
import lombok.NonNull;
5-
import lombok.Setter;
6-
import lombok.ToString;
3+
import com.google.errorprone.annotations.Immutable;
4+
import lombok.*;
5+
import org.panteleyev.mysqlapi.annotations.Column;
6+
import org.panteleyev.mysqlapi.annotations.PrimaryKey;
7+
8+
import java.io.Serializable;
9+
import java.lang.reflect.Field;
10+
11+
import static org.panteleyev.mysqlapi.DataTypes.*;
712

813
/**
9-
* The column is a piece of data.
14+
* The column is a piece of data. Immutable
1015
*/
1116
@ToString
12-
public class ColumnData {
17+
@AllArgsConstructor
18+
@Builder(toBuilder = true)
19+
@Immutable
20+
public class ColumnData implements Serializable {
1321

1422
@Getter
1523
@NonNull
16-
private String columnName;
24+
private final String columnName;
1725

1826
/**
1927
* Will return null if it is SQL NULL
2028
*/
2129
@Getter
22-
private String value;
30+
private final String value;
2331

2432
@Getter
25-
@Setter
26-
private String type;
33+
private final String type;
2734

2835
@Getter
29-
private int length = -1;
36+
private final int length;
3037

3138
@Getter
32-
@Setter
33-
private boolean nullable = true;
39+
private final boolean nullable;
3440

3541
@Getter
36-
@Setter
37-
private boolean autoIncrement;
42+
private final boolean autoIncrement;
3843

3944
@Getter
40-
@Setter
41-
private boolean primaryKey;
45+
private final boolean primaryKey;
4246

43-
public ColumnData(@NonNull String columnName, String value) {
44-
this.columnName = columnName;
45-
this.value = value;
46-
}
4747

48-
public ColumnData(@NonNull String columnName, String value, int length) {
49-
this(columnName,value);
50-
this.length = length;
48+
public static ColumnData fromField(Field field, String value) throws IllegalAccessException {
49+
if (!field.isAnnotationPresent(Column.class))
50+
throw new IllegalArgumentException("Field " + field.toString() + " does not contain " + Column.class.toString() + " annotation");
51+
52+
Column column = field.getAnnotation(Column.class);
53+
54+
String columnName = column.value();
55+
Class<?> type = field.getType();
56+
String mySQLType;
57+
58+
int length = column.length();
59+
60+
boolean nullable = column.nullable();
61+
boolean primaryKey = field.isAnnotationPresent(PrimaryKey.class);
62+
boolean autoIncrement = primaryKey && field.getAnnotation(PrimaryKey.class).isAutoIncrement();
63+
64+
String typeName = type.isEnum() ?
65+
TYPE_ENUM : type.getTypeName();
66+
67+
// Code snatched from Petr Panteleyev's MySQL annotation API
68+
switch (typeName) {
69+
case TYPE_STRING:
70+
if (column.isJson()) {
71+
mySQLType = "JSON";
72+
} else {
73+
mySQLType = "VARCHAR("
74+
+ column.length()
75+
+ ")";
76+
}
77+
break;
78+
case TYPE_ENUM:
79+
mySQLType = "VARCHAR("
80+
+ column.length()
81+
+ ")";
82+
break;
83+
case TYPE_BOOL:
84+
case TYPE_BOOLEAN:
85+
mySQLType = "BOOLEAN";
86+
break;
87+
case TYPE_INTEGER:
88+
case TYPE_INT:
89+
mySQLType = "INTEGER";
90+
break;
91+
case TYPE_LONG:
92+
case TYPE_LONG_PRIM:
93+
case TYPE_DATE:
94+
case TYPE_LOCAL_DATE:
95+
mySQLType = "BIGINT";
96+
break;
97+
case TYPE_BIG_DECIMAL:
98+
mySQLType = "DECIMAL(" +
99+
column.precision() +
100+
"," +
101+
column.scale() +
102+
")";
103+
break;
104+
case TYPE_BYTE_ARRAY:
105+
mySQLType = "VARBINARY(" +
106+
column.length() +
107+
")";
108+
break;
109+
case TYPE_UUID:
110+
if (column.storeUuidAsBinary()) {
111+
mySQLType = "BINARY(16)";
112+
} else {
113+
mySQLType = "VARCHAR(36)";
114+
}
115+
break;
116+
default:
117+
throw new IllegalStateException(BAD_FIELD_TYPE + typeName);
118+
}
119+
120+
121+
122+
123+
return new ColumnData(columnName, value, mySQLType, length, nullable, autoIncrement, primaryKey);
51124
}
52125

126+
53127
}

core/src/main/java/com/github/fernthedev/fernapi/universal/data/database/DatabaseAuthInfo.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.fernthedev.fernapi.universal.data.database;
22

33
import com.github.fernthedev.fernapi.universal.mysql.AbstractSQLDriver;
4-
import com.github.fernthedev.fernapi.universal.mysql.HikariDatabaseHandler;
54
import com.github.fernthedev.fernapi.universal.mysql.HikariSQLDriver;
65
import lombok.*;
76

@@ -57,23 +56,5 @@ public DatabaseAuthInfo(@NonNull String username, @NonNull String password, @Non
5756
private DatabaseAuthInfo() {}
5857

5958

60-
public String getUrlToDB() {
61-
AbstractSQLDriver sqlDriver = HikariDatabaseHandler.getSqlDriver(mysqlDriver);
62-
if (sqlDriver == null) throw new IllegalStateException("Sql Driver " + mysqlDriver + " could not be found");
63-
64-
String jdbcUrl = sqlDriver.getJdbcUrl();
65-
66-
if (jdbcUrl == null) throw new IllegalStateException("JDBC Url for " + sqlDriver.getSqlIdentifierName() + " cannot be found");
67-
68-
// Universal.debug("This DB:" + toString());
69-
70-
return jdbcUrl
71-
.replace("%host%", urlHost)
72-
.replace("%port%", port)
73-
.replace("%database%", database)
74-
.replace("%sql%", sqlDriver.getSqlName());
75-
}
76-
77-
7859

7960
}

0 commit comments

Comments
 (0)