3
3
import com .google .gson .Gson ;
4
4
import com .google .gson .JsonElement ;
5
5
import com .mysql .cj .jdbc .MysqlDataSource ;
6
- import com .mysql .cj .protocol .a .result .TextBufferRow ;
7
6
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 ;
9
11
10
12
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 ;
12
19
13
20
public class MySQL extends SQLDatabase {
14
21
15
22
private Gson gson = new Gson ();
16
23
17
- public MySQL () {}
24
+ public MySQL () {
25
+ }
18
26
19
27
public MySQL (Credentials credentials ) {
20
28
super (credentials );
@@ -49,10 +57,14 @@ public boolean connect() {
49
57
50
58
@ Override
51
59
public boolean isConnected () {
52
- if (connection == null ) return false ;
60
+ if (connection == null ) {
61
+ return false ;
62
+ }
53
63
54
64
try {
55
- if (data .getConnection ().isClosed ()) return false ;
65
+ if (data .getConnection ().isClosed ()) {
66
+ return false ;
67
+ }
56
68
} catch (SQLException e ) {
57
69
return false ;
58
70
}
@@ -81,7 +93,7 @@ public boolean execute(Query query) {
81
93
} catch (SQLException e ) {
82
94
e .printStackTrace ();
83
95
} finally {
84
- if (stmt != null ) {
96
+ if (stmt != null ) {
85
97
try {
86
98
stmt .close ();
87
99
return true ;
@@ -104,22 +116,23 @@ public Result select(Select select) {
104
116
ResultSetMetaData resmeta = res .getMetaData ();
105
117
Result result = new Result ();
106
118
107
- while (res .next ()) {
119
+ while (res .next ()) {
108
120
Row row = new Row ();
109
121
int i = 1 ;
110
122
boolean bound = true ;
111
123
while (bound ) {
112
124
try {
113
125
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 ))) {
116
128
addedJson = true ;
117
129
row .addColumn (resmeta .getColumnName (i ), gson .fromJson (res .getString (i ), JsonElement .class ));
118
130
}
119
131
}
120
132
121
- if (!addedJson )
133
+ if (!addedJson ) {
122
134
row .addColumn (resmeta .getColumnName (i ), res .getObject (i ));
135
+ }
123
136
124
137
} catch (SQLException e ) {
125
138
bound = false ;
@@ -143,4 +156,11 @@ public Result select(Select select) {
143
156
return new Result ();
144
157
}
145
158
}
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
+ }
146
166
}
0 commit comments