Skip to content

Commit 516611c

Browse files
committed
Removed StorageMap. Improved storage functions and comparators
1 parent 7d98e62 commit 516611c

31 files changed

+877
-602
lines changed

.editorconfig

+462
Large diffs are not rendered by default.

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,4 @@ Sledgehammer [Bungeecord]\.idea/sonarlint
343343

344344
\data/src/main/java/com/noahhusby/lib/data/test/
345345
/data/src/main/java/com/noahhusby/lib/data/test/
346+
/data/src/main/java/com/noahhusby/lib/data/test/

.idea/compiler.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

data/build/tmp/jar/MANIFEST.MF

-2
This file was deleted.

data/src/main/java/com/noahhusby/lib/data/JsonUtils.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.noahhusby.lib.data;
22

3-
import com.google.gson.*;
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonIOException;
5+
import com.google.gson.JsonObject;
6+
import com.google.gson.JsonParseException;
7+
import com.google.gson.JsonSyntaxException;
48
import com.google.gson.internal.Streams;
59
import com.google.gson.stream.JsonReader;
610
import com.google.gson.stream.JsonToken;
@@ -10,7 +14,6 @@
1014
import java.io.IOException;
1115
import java.io.Reader;
1216
import java.io.StringReader;
13-
import java.util.ArrayList;
1417
import java.util.HashSet;
1518
import java.util.Map;
1619
import java.util.Set;
@@ -51,7 +54,7 @@ public static JsonElement parseReader(Reader reader) throws JsonIOException, Jso
5154
* Returns the next value from the JSON stream as a parse tree.
5255
*
5356
* @throws JsonParseException if there is an IOException or if the specified
54-
* text is not valid JSON
57+
* text is not valid JSON
5558
*/
5659
public static JsonElement parseReader(JsonReader reader)
5760
throws JsonIOException, JsonSyntaxException {
@@ -68,8 +71,9 @@ public static JsonElement parseReader(JsonReader reader)
6871

6972
public static Set<String> keySet(JsonObject object) {
7073
Set<String> result = new HashSet<>();
71-
for(Map.Entry<String, JsonElement> es : object.entrySet())
74+
for (Map.Entry<String, JsonElement> es : object.entrySet()) {
7275
result.add(es.getKey());
76+
}
7377
return result;
7478
}
7579

@@ -89,8 +93,8 @@ public static boolean isJsonValid(final JsonReader jsonReader) throws IOExceptio
8993
try {
9094
JsonToken token;
9195
loop:
92-
while ( (token = jsonReader.peek()) != JsonToken.END_DOCUMENT && token != null ) {
93-
switch ( token ) {
96+
while ((token = jsonReader.peek()) != JsonToken.END_DOCUMENT && token != null) {
97+
switch (token) {
9498
case BEGIN_ARRAY:
9599
jsonReader.beginArray();
96100
break;
@@ -119,7 +123,7 @@ public static boolean isJsonValid(final JsonReader jsonReader) throws IOExceptio
119123
}
120124
}
121125
return true;
122-
} catch ( final MalformedJsonException ignored ) {
126+
} catch (final MalformedJsonException ignored) {
123127
return false;
124128
}
125129
}

data/src/main/java/com/noahhusby/lib/data/sql/Credentials.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ public class Credentials {
77
private String password = "";
88
private String database = "";
99

10-
public Credentials() {}
10+
public Credentials() {
11+
}
1112

12-
public Credentials(String host, int port, String user, String password, String database) {
13+
public Credentials(String host, int port, String user, String password, String database) {
1314
this.host = host;
1415
this.port = port;
1516
this.user = user;
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
package com.noahhusby.lib.data.sql;
22

3-
import com.noahhusby.lib.data.sql.actions.*;
3+
import com.noahhusby.lib.data.sql.actions.Query;
4+
import com.noahhusby.lib.data.sql.actions.Result;
5+
import com.noahhusby.lib.data.sql.actions.Select;
46

57
import java.sql.Connection;
8+
import java.util.concurrent.CompletableFuture;
69

710
public interface ISQLDatabase {
811
void setCredentials(Credentials credentials);
12+
913
Credentials getCredentials();
1014

1115
Connection getConnection();
1216

1317
boolean connect();
18+
1419
boolean isConnected();
20+
1521
boolean close();
1622

1723
boolean execute(Query query);
24+
1825
Result select(Select select);
26+
27+
CompletableFuture<Result> asyncSelect(Select select);
1928
}

data/src/main/java/com/noahhusby/lib/data/sql/MySQL.java

+31-11
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@
33
import com.google.gson.Gson;
44
import com.google.gson.JsonElement;
55
import com.mysql.cj.jdbc.MysqlDataSource;
6-
import com.mysql.cj.protocol.a.result.TextBufferRow;
76
import com.noahhusby.lib.data.JsonUtils;
8-
import com.noahhusby.lib.data.sql.actions.*;
7+
import com.noahhusby.lib.data.sql.actions.Query;
8+
import com.noahhusby.lib.data.sql.actions.Result;
9+
import com.noahhusby.lib.data.sql.actions.Row;
10+
import com.noahhusby.lib.data.sql.actions.Select;
911

1012
import java.io.IOException;
11-
import java.sql.*;
13+
import java.sql.Connection;
14+
import java.sql.ResultSet;
15+
import java.sql.ResultSetMetaData;
16+
import java.sql.SQLException;
17+
import java.sql.Statement;
18+
import java.util.concurrent.CompletableFuture;
1219

1320
public class MySQL extends SQLDatabase {
1421

1522
private Gson gson = new Gson();
1623

17-
public MySQL() {}
24+
public MySQL() {
25+
}
1826

1927
public MySQL(Credentials credentials) {
2028
super(credentials);
@@ -49,10 +57,14 @@ public boolean connect() {
4957

5058
@Override
5159
public boolean isConnected() {
52-
if(connection == null) return false;
60+
if (connection == null) {
61+
return false;
62+
}
5363

5464
try {
55-
if(data.getConnection().isClosed()) return false;
65+
if (data.getConnection().isClosed()) {
66+
return false;
67+
}
5668
} catch (SQLException e) {
5769
return false;
5870
}
@@ -81,7 +93,7 @@ public boolean execute(Query query) {
8193
} catch (SQLException e) {
8294
e.printStackTrace();
8395
} finally {
84-
if(stmt != null) {
96+
if (stmt != null) {
8597
try {
8698
stmt.close();
8799
return true;
@@ -104,22 +116,23 @@ public Result select(Select select) {
104116
ResultSetMetaData resmeta = res.getMetaData();
105117
Result result = new Result();
106118

107-
while(res.next()) {
119+
while (res.next()) {
108120
Row row = new Row();
109121
int i = 1;
110122
boolean bound = true;
111123
while (bound) {
112124
try {
113125
boolean addedJson = false;
114-
if(res.getObject(i) instanceof String) {
115-
if(JsonUtils.isJsonValid(res.getString(i))) {
126+
if (res.getObject(i) instanceof String) {
127+
if (JsonUtils.isJsonValid(res.getString(i))) {
116128
addedJson = true;
117129
row.addColumn(resmeta.getColumnName(i), gson.fromJson(res.getString(i), JsonElement.class));
118130
}
119131
}
120132

121-
if(!addedJson)
133+
if (!addedJson) {
122134
row.addColumn(resmeta.getColumnName(i), res.getObject(i));
135+
}
123136

124137
} catch (SQLException e) {
125138
bound = false;
@@ -143,4 +156,11 @@ public Result select(Select select) {
143156
return new Result();
144157
}
145158
}
159+
160+
@Override
161+
public CompletableFuture<Result> asyncSelect(Select select) {
162+
CompletableFuture<Result> future = new CompletableFuture<>();
163+
new Thread(() -> future.complete(this.select(select)));
164+
return future;
165+
}
146166
}

data/src/main/java/com/noahhusby/lib/data/sql/actions/Insert.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ public class Insert implements Query {
77
private String columns = "";
88
private String[] data = null;
99

10-
public Insert() {}
10+
public Insert() {
11+
}
1112

1213
public Insert(String table, String columns, String... data) {
1314
this.table = table;
@@ -49,7 +50,7 @@ public String query() {
4950
StringBuilder sqlData = new StringBuilder();
5051
for (int i = 0; i < data.length; i++) {
5152
sqlData.append("'").append(data[i]).append("'");
52-
if(i != data.length - 1) {
53+
if (i != data.length - 1) {
5354
sqlData.append(", ");
5455
}
5556
}

data/src/main/java/com/noahhusby/lib/data/sql/actions/Select.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ public class Select implements Query {
55
private String columns = "";
66
private String filter = "";
77

8-
public Select() {}
8+
public Select() {
9+
}
910

1011
public Select(String table, String columns, String filter) {
1112
this.table = table;
@@ -34,7 +35,7 @@ public String getTable() {
3435
}
3536

3637
public String getColumns() {
37-
if(columns == null) {
38+
if (columns == null) {
3839
return "*";
3940
} else {
4041
return columns;
@@ -44,10 +45,12 @@ public String getColumns() {
4445

4546
@Override
4647
public String query() {
47-
if(columns == null || columns.equals("")) columns = "*";
48+
if (columns == null || columns.equals("")) {
49+
columns = "*";
50+
}
4851

4952
String sql = String.format("SELECT %s FROM %s", columns, table);
50-
if(filter != null && !filter.equals("")) {
53+
if (filter != null && !filter.equals("")) {
5154
sql += " WHERE " + filter;
5255
}
5356

data/src/main/java/com/noahhusby/lib/data/sql/actions/Update.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ public class Update implements Query {
55
private UpdateValue value = null;
66
private String filter = "";
77

8-
public Update() {}
8+
public Update() {
9+
}
910

1011
public Update(String table, UpdateValue value, String filter) {
1112
this.table = table;
@@ -41,9 +42,11 @@ public UpdateValue getValue() {
4142
public String query() {
4243
StringBuilder change = new StringBuilder();
4344
int i = 1;
44-
for(String key : value.getKeys()) {
45+
for (String key : value.getKeys()) {
4546
change.append(key).append(" = '").append(value.get(key)).append("'");
46-
if(i != value.getKeys().size()) change.append(", ");
47+
if (i != value.getKeys().size()) {
48+
change.append(", ");
49+
}
4750
i++;
4851
}
4952

data/src/main/java/com/noahhusby/lib/data/sql/actions/UpdateValue.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
public class UpdateValue {
77
private HashMap<String, String> data = new HashMap<>();
88

9-
public UpdateValue(String val1, String val2) {
9+
public UpdateValue(String val1, String val2) {
1010
data.put(val1, val2);
1111
}
1212

13-
public void add (String val1, String val2) {
13+
public void add(String val1, String val2) {
1414
data.put(val1, val2);
1515
}
1616

data/src/main/java/com/noahhusby/lib/data/sql/structure/Structure.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.noahhusby.lib.data.sql.structure;
22

3-
import lombok.AllArgsConstructor;
43
import lombok.Getter;
54
import lombok.RequiredArgsConstructor;
65

@@ -13,14 +12,16 @@
1312
*/
1413
@RequiredArgsConstructor
1514
public class Structure {
16-
@Getter private final List<StructureElement> elements;
17-
@Getter private final boolean repair;
15+
@Getter
16+
private final List<StructureElement> elements;
17+
@Getter
18+
private final boolean repair;
1819
private List<String> columnNames;
1920

2021
public List<String> getColumnNames() {
21-
if(columnNames == null) {
22+
if (columnNames == null) {
2223
columnNames = new ArrayList<>();
23-
for(StructureElement s : elements) {
24+
for (StructureElement s : elements) {
2425
columnNames.add(s.getColumn());
2526
}
2627
}
@@ -36,7 +37,8 @@ public static class StructureBuilder {
3637
private List<StructureElement> elements = new ArrayList<>();
3738
private boolean repair;
3839

39-
StructureBuilder() {}
40+
StructureBuilder() {
41+
}
4042

4143
public StructureBuilder repair(boolean repair) {
4244
this.repair = repair;
@@ -45,7 +47,9 @@ public StructureBuilder repair(boolean repair) {
4547

4648
public StructureBuilder add(String column, Type type, String... keys) {
4749
List<String> keyList = new ArrayList<>(Arrays.asList(keys));
48-
if(keyList.size() == 0) keyList.add(column);
50+
if (keyList.size() == 0) {
51+
keyList.add(column);
52+
}
4953
elements.add(new StructureElement(column, keyList, type));
5054
return this;
5155
}

data/src/main/java/com/noahhusby/lib/data/sql/structure/StructureElement.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
*/
1111
@AllArgsConstructor
1212
public class StructureElement {
13-
@Getter private final String column;
14-
@Getter private final List<String> keys;
15-
@Getter private final Type type;
13+
@Getter
14+
private final String column;
15+
@Getter
16+
private final List<String> keys;
17+
@Getter
18+
private final Type type;
1619
}

data/src/main/java/com/noahhusby/lib/data/sql/structure/Type.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public enum Type {
1212

1313
TEXT("TEXT(255)");
1414

15-
@Getter private final String query;
15+
@Getter
16+
private final String query;
1617

1718
Type(String query) {
1819
this.query = query;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.noahhusby.lib.data.storage;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* @author Noah Husby
10+
*/
11+
@Target(ElementType.TYPE)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface Key {
14+
String value();
15+
}

0 commit comments

Comments
 (0)