Skip to content

Commit a9909e1

Browse files
authored
Merge pull request #11 from ZorTik/development
Development
2 parents e9f13f1 + 99cac3e commit a9909e1

40 files changed

Lines changed: 1208 additions & 100 deletions

api/src/main/java/me/zort/sqllib/api/StatementFactory.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,22 @@
44
import java.sql.SQLException;
55
import java.sql.Statement;
66

7+
/**
8+
* The StatementFactory is responsible for creating new Statements for
9+
* the given connection.
10+
*
11+
* @param <T> The Statement type.
12+
* @author ZorTik
13+
*/
714
public interface StatementFactory<T extends Statement> {
815

16+
/**
17+
* Prepares the statement for executing in {@link SQLConnection}.
18+
*
19+
* @param connection The connection to use.
20+
* @return The prepared statement.
21+
* @throws SQLException If an error occurs while preparing.
22+
*/
923
T prepare(Connection connection) throws SQLException;
1024

1125
}

api/src/main/java/me/zort/sqllib/api/data/QueryResult.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import org.jetbrains.annotations.Nullable;
44

5+
/**
6+
* Represents a query result.
7+
*
8+
* @author ZorTik
9+
*/
510
public interface QueryResult {
611

712
boolean isSuccessful();

api/src/main/java/me/zort/sqllib/api/data/Row.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,36 @@
44

55
public class Row extends HashMap<String, Object> {
66

7-
// TODO
7+
public String getString(String key) {
8+
return (String) get(key);
9+
}
10+
11+
public int getInt(String key) {
12+
return (int) get(key);
13+
}
14+
15+
public long getLong(String key) {
16+
return (long) get(key);
17+
}
18+
19+
public double getDouble(String key) {
20+
return (double) get(key);
21+
}
22+
23+
public float getFloat(String key) {
24+
return (float) get(key);
25+
}
26+
27+
public boolean getBoolean(String key) {
28+
return (boolean) get(key);
29+
}
30+
31+
public byte getByte(String key) {
32+
return (byte) get(key);
33+
}
34+
35+
public short getShort(String key) {
36+
return (short) get(key);
37+
}
838

939
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package me.zort.sqllib.api.mapping;
2+
3+
import me.zort.sqllib.api.SQLConnection;
4+
5+
/**
6+
* The StatementMappingFactory is responsible for creating new {@link StatementMappingStrategy}
7+
* for defined interfaces.
8+
*
9+
* @author ZorTik
10+
*/
11+
public interface StatementMappingFactory {
12+
13+
/**
14+
* Creates a new StatementMapping for the given interface class that
15+
* is responsible for handling that specific interface.
16+
*
17+
* @param interfaceClass The interface class
18+
* @param connection The connection to use.
19+
* @return The StatementMapping.
20+
* @param <T> The interface class type.
21+
*/
22+
<T> StatementMappingStrategy<T> create(Class<T> interfaceClass, SQLConnection connection);
23+
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package me.zort.sqllib.api.mapping;
2+
3+
import me.zort.sqllib.api.SQLConnection;
4+
import me.zort.sqllib.api.data.QueryResult;
5+
6+
import java.lang.reflect.Method;
7+
8+
/**
9+
* Result adapter used for handling operations between {@link SQLConnection}
10+
* and {@link StatementMappingStrategy}.
11+
*
12+
* @author ZorTik
13+
*/
14+
public interface StatementMappingResultAdapter {
15+
16+
/**
17+
* Adapts invoked {@link StatementMappingStrategy} method QueryResult to
18+
* the final result that can be passed to proxy instance.
19+
*
20+
* @param method The invoked proxy method.
21+
* @param result The QueryResult of the invoked method.
22+
* @return The adapted result.
23+
*/
24+
Object adaptResult(Method method, QueryResult result);
25+
26+
/**
27+
* Retrieves type of entity that needs to be mapped in the request
28+
* to be passed in adaptResult as type in QueryResult.
29+
*
30+
* @param method The invoked proxy method.
31+
* @return The type of entity that needs to be mapped.
32+
*/
33+
Class<?> retrieveResultType(Method method);
34+
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package me.zort.sqllib.api.mapping;
2+
3+
import me.zort.sqllib.api.data.QueryResult;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import java.lang.reflect.Method;
7+
8+
/**
9+
* Represents an abstract proxy mapping that is used for execution of
10+
* queries using mapped methods in a proxy instance.
11+
*
12+
* @param <T> The type of the proxy instance.
13+
* @author ZorTik
14+
*/
15+
public interface StatementMappingStrategy<T> {
16+
17+
/**
18+
* Executes query based on invoked method with invoked args from
19+
* proxy instance. MapTo is used as type of entity to be mapped in the
20+
* request. If there is no mapping required, MapTo can be null.
21+
*
22+
* @param method The method that was executed in the proxy.
23+
* @param args The arguments passed.
24+
* @param mapTo The type of entity that needs to be mapped.
25+
* @return The QueryResult of the executed query.
26+
*/
27+
QueryResult executeQuery(Method method, Object[] args, @Nullable Class<?> mapTo);
28+
29+
/**
30+
* Checks if the method is eligible for mapping. If this returns false,
31+
* the method is executed in proxy mapping as normal method, eg. does not
32+
* return any mapping results.
33+
*
34+
* @param method The method that was executed in the proxy.
35+
* @return True if the method is eligible for mapping, or false.
36+
*/
37+
boolean isMappingMethod(Method method);
38+
39+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@
1818

1919
public class SQLConnectionBuilder {
2020

21-
public static SQLConnectionBuilder of(String address,
22-
int port,
23-
String database,
24-
String username,
25-
String password) {
21+
public static SQLConnectionBuilder of(String address, int port, String database, String username, String password) {
2622
return of(new DefaultSQLEndpoint(address + ":" + port, database, username, password));
2723
}
2824

@@ -47,6 +43,10 @@ public static SQLConnectionBuilder of(SQLEndpoint endpoint) {
4743
private String jdbc;
4844
private String driver = null;
4945

46+
public SQLConnectionBuilder(String address, int port, String database, String username, String password) {
47+
this(new DefaultSQLEndpoint(address + ":" + port, database, username, password));
48+
}
49+
5050
public SQLConnectionBuilder() {
5151
this(null);
5252
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import me.zort.sqllib.api.data.Row;
99
import me.zort.sqllib.internal.factory.SQLConnectionFactory;
1010
import me.zort.sqllib.internal.query.*;
11+
import org.jetbrains.annotations.ApiStatus;
1112
import org.jetbrains.annotations.Nullable;
1213

1314
import java.sql.Connection;
@@ -32,6 +33,9 @@ public SQLDatabaseConnection(SQLConnectionFactory connectionFactory) {
3233
SQLConnectionPool.register(this);
3334
}
3435

36+
@ApiStatus.Experimental
37+
public abstract <T> T createGate(Class<T> mappingInterface);
38+
3539
/**
3640
* Saves this mapping object into database using upsert query.
3741
* <p>

0 commit comments

Comments
 (0)