-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJKueryTest.java
186 lines (125 loc) · 5.51 KB
/
JKueryTest.java
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
package com.sxtanna.database.tests;
import com.sxtanna.database.Kuery;
import com.sxtanna.database.config.KueryConfig;
import com.sxtanna.database.ext.Kext;
import com.sxtanna.database.ext.Krs;
import com.sxtanna.database.struct.base.Duo;
import com.sxtanna.database.task.builder.Create;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.sxtanna.database.ext.Kext.sorts;
import static com.sxtanna.database.ext.Kext.targets;
import static com.sxtanna.database.ext.Krs.getString;
import static com.sxtanna.database.ext.Krs.whileNext;
import static com.sxtanna.database.struct.base.Duo.co;
import static com.sxtanna.database.struct.base.Duo.valueColumns;
import static com.sxtanna.database.struct.obj.Duplicate.Update;
import static com.sxtanna.database.struct.obj.Sort.Order;
import static com.sxtanna.database.struct.obj.SqlType.VarChar;
import static com.sxtanna.database.struct.obj.Target.Where;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
// For those of us that "enjoy" verbosity and deprecation
public final class JKueryTest extends DatabaseTest<Kuery> {
// Configs can be loaded out of the scope of a database
protected static final KueryConfig CONFIG = Kuery.getConfig(new File("../SqlConfig.json"));
@NotNull
@Override
public Kuery create() {
return Kuery.get(CONFIG);
}
@Override
public void runTest() {
// None ORM with plain queries
database.execute(task -> {
task.execute("CREATE TABLE IF NOT EXISTS User(name VARCHAR(255) PRIMARY KEY NOT NULL)");
task.execute("INSERT INTO User (name) VALUES (?) ON DUPLICATE KEY UPDATE name=VALUES(name)", "Emiliee");
task.execute("INSERT INTO User (name) VALUES (?) ON DUPLICATE KEY UPDATE name=VALUES(name)", "Sxtanna");
final int[] users = {0};
task.query("SELECT * FROM User", rs -> {
try {
while (rs.next()) {
users[0]++;
System.out.println("Found user " + rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
});
assertEquals(users[0], 2, "Should have found 2 users, Sxtanna and Emiliee");
// Yes, I hate the Java array syntax too...
task.query("SELECT * FROM User WHERE name LIKE ?", new String[] {"S%"}, rs -> {
try {
while (rs.next()) {
final String name = rs.getString("name");
assertEquals("Sxtanna", name, "Name should have been Sxtanna");
System.out.println("Found user " + name);
}
} catch (SQLException e) {
e.printStackTrace();
}
});
task.execute("DELETE FROM User WHERE name='Emiliee'");
task.execute("DELETE FROM User WHERE name LIKE 'S%'");
});
// Non ORM with helper methods
database.execute(task -> {
task.execute(Create.table("User").co("name", new VarChar(255, true)));
task.insert("User", valueColumns(co("name", "Emiliee")), new Update("name"));
task.insert("User", valueColumns(co("name", "Sxtanna")), new Update("name"));
final int[] users = {0};
// Select all users
task.select("User", resultSet -> whileNext(resultSet, rs -> {
users[0]++;
System.out.println("Found user " + getString(rs, "name"));
}));
assertEquals(users[0], 2, "Should have found 2 users, Sxtanna and Emiliee");
// Select all users who name's start with S
task.select("User", targets(Where.starts("name", "S")), resultSet -> whileNext(resultSet, rs -> {
final String name = getString(rs, "name");
assertEquals("Sxtanna", name, "Name should have been Sxtanna");
System.out.println("Found user " + name);
}));
final List<String> names = new ArrayList<>();
// Select all users and order by name ascending
task.select("User", sorts(Order.ascend("name")), resultSet -> whileNext(resultSet, rs -> {
final String name = getString(rs, "name");
System.out.println("Found user " + name);
names.add(name);
}));
assertIterableEquals(names, Arrays.asList("Emiliee", "Sxtanna"));
task.delete("User", Where.starts("name", "S"));
task.delete("User", Where.equals("name", "Emiliee"));
});
// Version without static imports
database.execute(task -> {
task.execute(Create.table("User").co("name", new VarChar(255, true)));
task.insert("User", Duo.valueColumns(Duo.co("name", "Emiliee")), new Update("name"));
task.insert("User", Duo.valueColumns(Duo.co("name", "Sxtanna")), new Update("name"));
final int[] users = {0};
task.select("User", resultSet -> Krs.whileNext(resultSet, rs -> {
users[0]++;
System.out.println("Found user " + Krs.getString(rs, "name"));
}));
assertEquals(users[0], 2, "Should have found 2 users, Sxtanna and Emiliee");
task.select("User", Kext.targets(Where.starts("name", "S")), resultSet -> Krs.whileNext(resultSet, rs -> {
final String name = Krs.getString(rs, "name");
assertEquals("Sxtanna", name, "Name should have been Sxtanna");
System.out.println("Found user " + name);
}));
final List<String> names = new ArrayList<>();
task.select("User", Kext.sorts(Order.ascend("name")), resultSet -> whileNext(resultSet, rs -> {
final String name = getString(rs, "name");
System.out.println("Found user " + name);
names.add(name);
}));
assertIterableEquals(names, Arrays.asList("Emiliee", "Sxtanna"), "Results were in wrong order");
task.delete("User", Where.starts("name", "S"));
task.delete("User", Where.equals("name", "Emiliee"));
});
}
}