Skip to content

Commit 99cac3e

Browse files
committed
Small addons
1 parent 8db3184 commit 99cac3e

6 files changed

Lines changed: 132 additions & 4 deletions

File tree

core/src/main/java/me/zort/sqllib/SQLDatabaseConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public SQLDatabaseConnection(SQLConnectionFactory connectionFactory) {
3434
}
3535

3636
@ApiStatus.Experimental
37-
public abstract <T> T createMapping(Class<T> mappingInterface);
37+
public abstract <T> T createGate(Class<T> mappingInterface);
3838

3939
/**
4040
* Saves this mapping object into database using upsert query.

core/src/main/java/me/zort/sqllib/SQLDatabaseConnectionImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void setObjectMapper(@NotNull ObjectMapper objectMapper) {
130130
*/
131131
@SuppressWarnings("unchecked")
132132
@ApiStatus.Experimental
133-
public <T> T createMapping(Class<T> mappingInterface) {
133+
public <T> T createGate(Class<T> mappingInterface) {
134134
StatementMappingStrategy<T> statementMapping = mappingFactory.create(mappingInterface, this);
135135
return (T) Proxy.newProxyInstance(mappingInterface.getClassLoader(),
136136
new Class[]{mappingInterface}, (proxy, method, args) -> {

core/src/main/java/me/zort/sqllib/internal/query/QueryNodeRequest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,20 @@ public Optional<Row> obtainOne() {
2525
: Optional.ofNullable(resultList.get(0));
2626
}
2727

28+
public <T> Optional<T> obtainOne(Class<T> mapTo) {
29+
QueryRowsResult<T> resultList = obtainAll(mapTo);
30+
31+
return resultList.isEmpty()
32+
? Optional.empty()
33+
: Optional.ofNullable(resultList.get(0));
34+
}
35+
2836
public QueryRowsResult<Row> obtainAll() {
2937
return invokeToConnection(connection -> connection.query(getAncestor()));
3038
}
3139

40+
public <T> QueryRowsResult<T> obtainAll(Class<T> mapTo) {
41+
return invokeToConnection(connection -> connection.query(getAncestor(), mapTo));
42+
}
43+
3244
}

core/src/main/java/me/zort/sqllib/mapping/QueryAnnotation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* Represents a query annotation wrapper used in default statement
2323
* mapping proxy. Query that is wrapped appears on abstract methods
2424
* of the proxy instance created using:
25-
* {@link me.zort.sqllib.SQLDatabaseConnection#createMapping(Class)}
25+
* {@link me.zort.sqllib.SQLDatabaseConnection#createGate(Class)}
2626
*
2727
* @author ZorTik
2828
*/
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package me.zort.sqllib;
2+
3+
import me.zort.sqllib.api.data.QueryResult;
4+
import me.zort.sqllib.api.data.QueryRowsResult;
5+
import me.zort.sqllib.api.data.Row;
6+
import me.zort.sqllib.internal.annotation.PrimaryKey;
7+
import me.zort.sqllib.mapping.annotation.*;
8+
9+
import java.util.List;
10+
import java.util.Optional;
11+
12+
public class QuickStart {
13+
14+
private SQLDatabaseConnection connection;
15+
16+
public void quickStart() {
17+
18+
String address = "localhost";
19+
int port = 3306;
20+
String database = "database";
21+
String username = "username";
22+
String password = "password";
23+
24+
connection = new SQLConnectionBuilder(address, port, database, username, password).build();
25+
26+
if (!connection.connect()) {
27+
System.out.println("Failed to connect to the database!");
28+
System.exit(1);
29+
}
30+
31+
QueryResult result = connection.exec(() ->
32+
"CREATE TABLE IF NOT EXISTS users(" +
33+
"firstname VARCHAR(32) PRIMARY KEY NOT NULL," +
34+
"lastname VARCHAR(32) NOT NULL);");
35+
36+
if (!result.isSuccessful()) {
37+
System.out.println("Failed to create the table!");
38+
System.out.println(result.getRejectMessage());
39+
System.exit(1);
40+
}
41+
42+
QueryResult result2 = connection.insert()
43+
.into("users", "firstname", "lastname")
44+
.values("John", "Doe")
45+
.execute();
46+
47+
Optional<Row> result3 = connection.select()
48+
.from("users")
49+
.where().isEqual("firstname", "John")
50+
.obtainOne();
51+
52+
if (!result3.isPresent()) {
53+
System.out.println("Where did John go?");
54+
System.exit(1);
55+
}
56+
57+
connection.select();
58+
connection.update();
59+
connection.delete();
60+
connection.upsert();
61+
62+
connection.disconnect();
63+
}
64+
65+
class User {
66+
@PrimaryKey
67+
private String firstname;
68+
private String lastname;
69+
70+
public User(String firstname, String lastname) {
71+
this.firstname = firstname;
72+
this.lastname = lastname;
73+
}
74+
}
75+
76+
public void saveUser() {
77+
User user = new User("John", "Doe");
78+
QueryResult result = connection.save("users", user);
79+
}
80+
81+
public void loadUser() {
82+
User user = connection.select()
83+
.from("users")
84+
.where().isEqual("firstname", "John")
85+
.obtainOne(User.class)
86+
.orElse(null);
87+
}
88+
89+
90+
@Table("users")
91+
interface UsersGate {
92+
93+
@Save
94+
QueryResult saveUser(User user);
95+
96+
@Select
97+
@Where(@Where.Condition(column = "firstname", value = "{First Name}"))
98+
Optional<User> getUser(@Placeholder("First Name") String firstName);
99+
100+
@Select
101+
@Limit(10)
102+
List<User> selectFirstTen();
103+
104+
@Delete
105+
void deleteAll();
106+
}
107+
108+
public void gateExample() {
109+
UsersGate gate = connection.createGate(UsersGate.class);
110+
111+
QueryResult result = gate.saveUser(new User("John", "Doe"));
112+
113+
User user = gate.getUser("John").orElse(null);
114+
}
115+
116+
}

src/test/java/me/zort/sqllib/test/TestCase2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void prepare() {
5050
public void test1_Mapping() {
5151
assertNull(connection.save("users", new User("User1", 1000)).getRejectMessage());
5252

53-
DatabaseRepository repository = connection.createMapping(DatabaseRepository.class);
53+
DatabaseRepository repository = connection.createGate(DatabaseRepository.class);
5454
assertTrue(repository.selectOne("User1").isPresent());
5555
assertFalse(repository.selectAll().isEmpty());
5656
assertNull(repository.deleteAll().getRejectMessage());

0 commit comments

Comments
 (0)