Skip to content

Commit 60b78dc

Browse files
authored
Merge MySQL-Annotations branch into stable
* Requires more testing, but is considered stable.
2 parents 9b7efdb + 56f73dc commit 60b78dc

File tree

21 files changed

+898
-693
lines changed

21 files changed

+898
-693
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.

.idea/sqldialects.xml

Lines changed: 6 additions & 0 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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@ dependencies {
1111
testCompile group: 'junit', name: 'junit', version: '4.13.1'
1212

1313
implementation ('com.google.code.gson:gson:2.8.6')
14-
// compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.17'
15-
implementation 'org.mariadb.jdbc:mariadb-java-client:2.7.0'
14+
15+
api 'com.zaxxer:HikariCP:3.4.5'
16+
// compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.17'
17+
implementation 'org.mariadb.jdbc:mariadb-java-client:2.7.1'
18+
19+
// api "co.aikar:idb-core:1.0.0-SNAPSHOT"
20+
api 'com.github.Fernthedev:db:292c0bb'
21+
api 'com.github.Fernthedev:java-api-for-mysql:4c51dbe' // For MySQL annotations
22+
23+
1624
api 'com.github.Fernthedev:fern-configmanager:1.3.0' // Check github releases tag for the version.
1725

1826
// 2. Use Guava types in your public API:
@@ -22,6 +30,8 @@ dependencies {
2230
api group: 'org.slf4j', name: 'slf4j-jdk14', version: slf4j_ver
2331
compileOnly 'org.projectlombok:lombok:1.18.16'
2432
annotationProcessor 'org.projectlombok:lombok:1.18.16'
33+
34+
2535
}
2636

2737
compileKotlin {

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

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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;
78
import com.github.fernthedev.fernapi.universal.exceptions.FernRuntimeException;
89
import com.github.fernthedev.fernapi.universal.exceptions.setup.IncorrectSetupException;
910
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;
11+
import com.github.fernthedev.fernapi.universal.mysql.AikarFernDatabase;
1212
import com.github.fernthedev.fernapi.universal.util.UUIDFetcher;
1313
import com.github.fernthedev.fernapi.universal.util.UniversalContextResolvers;
1414
import com.github.fernthedev.fernapi.universal.util.VersionUtil;
@@ -44,9 +44,6 @@ private Universal() {}
4444
private static IChatHandler<?> ch;
4545
private static IPMessageHandler mh;
4646

47-
@Setter
48-
private static HikariDatabaseHandler databaseHandler = new HikariDatabaseHandler();
49-
5047
private static CommandManager comhand;
5148
private static NetworkHandler<? extends Object> nh;
5249
private static IScheduler<?,?> sh;
@@ -93,8 +90,7 @@ public void setup(@NonNull MethodInterface<?, ?> methodInterface, FernAPIPlugin
9390
comhand.getCommandContexts().registerContext(OfflineFPlayer.class, new UniversalContextResolvers.SingularIFPlayerContextResolver());
9491
comhand.getCommandContexts().registerContext(IFPlayer[].class, new UniversalContextResolvers.OnlineIFPlayerArrayCommandResolver());
9592

96-
HikariDatabaseHandler.registerSQLDriver(HikariSQLDriver.MARIADB_DRIVER);
97-
HikariDatabaseHandler.registerSQLDriver(HikariSQLDriver.MYSQL_DRIVER);
93+
9894
// comhand.getCommandCompletions().registerAsyncCompletion("fernPlayers", context ->
9995
// mi.getPlayers().parallelStream()
10096
// .filter(player -> !context.getIssuer().isPlayer() ||
@@ -128,9 +124,9 @@ protected static void checkNull() {
128124

129125

130126

131-
public static void debug(String message) {
127+
public static void debug(String message, Object... params) {
132128
if(debug) {
133-
getMethods().getAbstractLogger().info("[DEBUG] " + message);
129+
getMethods().getAbstractLogger().info("[DEBUG] " + message, params);
134130
}
135131
}
136132

@@ -149,11 +145,6 @@ public static IPMessageHandler getMessageHandler() {
149145
return mh;
150146
}
151147

152-
public static HikariDatabaseHandler getDatabaseHandler() {
153-
checkNull();
154-
return databaseHandler;
155-
}
156-
157148
public static NetworkHandler<? extends Object> getNetworkHandler() {
158149
checkNull();
159150
return nh;
@@ -174,10 +165,9 @@ public static FernAPIPlugin getPlugin() {
174165
return plugin;
175166
}
176167

168+
177169
public void onDisable() {
178-
new Thread(() -> {
179-
getDatabaseHandler().stopSchedule();
180-
getDatabaseHandler().closeConnection();
181-
}).start();
170+
new Thread(DB::close).start();
171+
new Thread(AikarFernDatabase::shutdownDatabases).start();
182172
}
183173
}
Lines changed: 105 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,135 @@
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 javax.annotation.Nullable;
9+
import java.io.Serializable;
10+
import java.lang.reflect.Field;
11+
12+
import static org.panteleyev.mysqlapi.DataTypes.*;
713

814
/**
9-
* The column is a piece of data.
15+
* The column is a piece of data. Immutable
1016
*/
1117
@ToString
12-
public class ColumnData {
18+
@AllArgsConstructor
19+
@Builder(toBuilder = true)
20+
@Immutable
21+
public class ColumnData implements Serializable {
1322

1423
@Getter
1524
@NonNull
16-
private String columnName;
25+
private final String columnName;
1726

1827
/**
1928
* Will return null if it is SQL NULL
2029
*/
2130
@Getter
22-
private String value;
31+
@Nullable
32+
private final String value;
2333

2434
@Getter
25-
@Setter
26-
private String type;
35+
private final String type;
2736

2837
@Getter
29-
private int length = -1;
38+
private final int length;
3039

3140
@Getter
32-
@Setter
33-
private boolean nullable = true;
41+
private final boolean nullable;
3442

3543
@Getter
36-
@Setter
37-
private boolean autoIncrement;
44+
private final boolean autoIncrement;
3845

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

43-
public ColumnData(@NonNull String columnName, String value) {
44-
this.columnName = columnName;
45-
this.value = value;
122+
return new ColumnData(columnName, value, mySQLType, length, nullable, autoIncrement, primaryKey);
46123
}
47124

48-
public ColumnData(@NonNull String columnName, String value, int length) {
49-
this(columnName,value);
50-
this.length = length;
125+
private static String stringSQL(Column column) {
126+
if (column.isJson()) {
127+
return "JSON";
128+
} else {
129+
return "VARCHAR("
130+
+ column.length()
131+
+ ")";
132+
}
51133
}
52134

53135
}
Lines changed: 0 additions & 31 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

@@ -26,18 +25,6 @@ public class DatabaseAuthInfo {
2625
@NonNull
2726
protected String database;
2827

29-
@Setter
30-
protected boolean cachePrepStmts = true;
31-
32-
@Setter
33-
protected int prepStmtCacheSize = 250;
34-
35-
@Setter
36-
protected int prepStmtCacheSqlLimit = 2048;
37-
38-
@Setter
39-
protected boolean useServerPrepStmts = true;
40-
4128
/**
4229
* Set if necessary
4330
*/
@@ -57,23 +44,5 @@ public DatabaseAuthInfo(@NonNull String username, @NonNull String password, @Non
5744
private DatabaseAuthInfo() {}
5845

5946

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-
7847

7948
}

0 commit comments

Comments
 (0)