Skip to content

Commit ecb5613

Browse files
committed
Small addons
1 parent 99cac3e commit ecb5613

4 files changed

Lines changed: 122 additions & 8 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ public SQLConnectionBuilder withDriver(String driver) {
7878
return this;
7979
}
8080

81-
public SQLDatabaseConnectionImpl build() {
81+
public SQLDatabaseConnection build() {
8282
return build(null);
8383
}
8484

85-
public SQLDatabaseConnectionImpl build(@Nullable SQLDatabaseOptions options) {
85+
public SQLDatabaseConnection build(@Nullable SQLDatabaseOptions options) {
8686
return build(driver, options);
8787
}
8888

89-
public SQLDatabaseConnectionImpl build(@Nullable String driver, @Nullable SQLDatabaseOptions options) {
89+
public SQLDatabaseConnection build(@Nullable String driver, @Nullable SQLDatabaseOptions options) {
9090
Objects.requireNonNull(endpoint, "Endpoint must be set!");
9191
Objects.requireNonNull(jdbc);
9292
if(driver == null) {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,37 @@ public SQLDatabaseConnection(SQLConnectionFactory connectionFactory) {
3333
SQLConnectionPool.register(this);
3434
}
3535

36+
/**
37+
* Constructs a mapping repository based on provided interface.
38+
* The interface should follow rules for creating mapping repositories
39+
* in this library.
40+
* <p>
41+
* Example:
42+
* <pre>
43+
* &#64;Table("users")
44+
* public interface MyRepository {
45+
* &#64;Select("*")
46+
* &#64;Where(&#64;Where.Condition(column = "firstname", value = "{First Name}"))
47+
* &#64;Limit(1)
48+
* Optional&lt;User&gt; getUser(&#64;Placeholder("First Name") String firstName);
49+
*
50+
* &#64;Select
51+
* List&lt;User&gt; getUsers();
52+
*
53+
* &#64;Delete
54+
* QueryResult deleteUsers();
55+
* }
56+
*
57+
* SQLDatabaseConnection connection = ...;
58+
* MyRepository repository = connection.createGate(MyRepository.class);
59+
*
60+
* Optional&lt;User&gt; user = repository.getUser("John");
61+
* </pre>
62+
*
63+
* @param mappingInterface Interface to create mapping repository for.
64+
* @return Mapping repository.
65+
* @param <T> Type of mapping repository.
66+
*/
3667
@ApiStatus.Experimental
3768
public abstract <T> T createGate(Class<T> mappingInterface);
3869

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

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
*
4040
* @author ZorTik
4141
*/
42+
@SuppressWarnings("unused")
4243
public class SQLDatabaseConnectionImpl extends SQLDatabaseConnection {
4344

4445
// --***-- Default Constants --***--
@@ -123,14 +124,38 @@ public void setObjectMapper(@NotNull ObjectMapper objectMapper) {
123124
* Constructs a mapping repository based on provided interface.
124125
* The interface should follow rules for creating mapping repositories
125126
* in this library.
127+
* <p>
128+
* Example:
129+
* <pre>
130+
* &#64;Table("users")
131+
* public interface MyRepository {
132+
* &#64;Select("*")
133+
* &#64;Where(&#64;Where.Condition(column = "firstname", value = "{First Name}"))
134+
* &#64;Limit(1)
135+
* Optional&lt;User&gt; getUser(&#64;Placeholder("First Name") String firstName);
136+
*
137+
* &#64;Select
138+
* List&lt;User&gt; getUsers();
139+
*
140+
* &#64;Delete
141+
* QueryResult deleteUsers();
142+
* }
143+
*
144+
* SQLDatabaseConnection connection = ...;
145+
* MyRepository repository = connection.createGate(MyRepository.class);
146+
*
147+
* Optional&lt;User&gt; user = repository.getUser("John");
148+
* </pre>
126149
*
127150
* @param mappingInterface Interface to create mapping repository for.
128151
* @return Mapping repository.
129152
* @param <T> Type of mapping repository.
130153
*/
131154
@SuppressWarnings("unchecked")
132155
@ApiStatus.Experimental
133-
public <T> T createGate(Class<T> mappingInterface) {
156+
public final <T> T createGate(Class<T> mappingInterface) {
157+
Objects.requireNonNull(mappingInterface, "Mapping interface cannot be null!");
158+
134159
StatementMappingStrategy<T> statementMapping = mappingFactory.create(mappingInterface, this);
135160
return (T) Proxy.newProxyInstance(mappingInterface.getClassLoader(),
136161
new Class[]{mappingInterface}, (proxy, method, args) -> {
@@ -150,10 +175,30 @@ public <T> T createGate(Class<T> mappingInterface) {
150175
}
151176

152177
/**
153-
* @see SQLDatabaseConnection#query(Query, Class)
178+
* Performs new query and returns the result. This result is never null.
179+
* See: {@link QueryRowsResult#isSuccessful()}
180+
*
181+
* Examples:
182+
* <p>
183+
* query(Select.of().from("players"), Player.class)
184+
* .stream()
185+
* .map(Player::getNickname)
186+
* .forEach(System.out::println);
187+
* <p>
188+
* query(() -> "SELECT * FROM players;");
189+
*
190+
* @param query The query to use while constructing query string.
191+
* @param typeClass Type class of object which will be instantiated and
192+
* populated with column values.
193+
* @param <T> Type of objects in result.
194+
*
195+
* @return Collection of row objects.
154196
*/
155197
@Override
156198
public <T> QueryRowsResult<T> query(Query query, Class<T> typeClass) {
199+
Objects.requireNonNull(query);
200+
Objects.requireNonNull(typeClass);
201+
157202
QueryRowsResult<Row> resultRows = query(query.getAncestor());
158203
QueryRowsResult<T> result = new QueryRowsResult<>(resultRows.isSuccessful());
159204

@@ -165,7 +210,9 @@ public <T> QueryRowsResult<T> query(Query query, Class<T> typeClass) {
165210
}
166211

167212
/**
168-
* @see SQLDatabaseConnectionImpl#query(Query, Class)
213+
* Performs new query and returns the result. This result is never null.
214+
*
215+
* @see SQLDatabaseConnection#query(Query, Class)
169216
*/
170217
@Override
171218
public QueryRowsResult<Row> query(Query query) {
@@ -200,7 +247,14 @@ public QueryRowsResult<Row> query(Query query) {
200247
}
201248

202249
/**
203-
* @see SQLDatabaseConnection#exec(Query)
250+
* Executes given query and returns execution result.
251+
* This result does not contain any rows. If you want to
252+
* execute query return result of rows, see method
253+
* {@link SQLDatabaseConnection#query(Query)}
254+
*
255+
* @param query Query to use for building query string.
256+
* @return Blank rows result that only informs
257+
* about success state of the request.
204258
*/
205259
public QueryResult exec(Query query) {
206260
if(!handleAutoReconnect()) {
@@ -216,7 +270,14 @@ public QueryResult exec(Query query) {
216270
}
217271

218272
/**
219-
* @see SQLDatabaseConnection#save(String, Object)
273+
* Saves this mapping object into database using upsert query.
274+
* <p>
275+
* All mapping strategies are described in:
276+
* {@link SQLDatabaseConnection#query(Query, Class)}.
277+
*
278+
* @param table Table to save into.
279+
* @param obj The object to save.
280+
* @return Result of the query.
220281
*/
221282
@Override
222283
public QueryResult save(String table, Object obj) { // by default, it creates and upsert request.
@@ -285,6 +346,7 @@ protected Pair<String[], UnknownValueWrapper[]> buildDefsVals(Object obj) {
285346
return new Pair<>(defs, vals);
286347
}
287348

349+
@SuppressWarnings("all")
288350
private boolean handleAutoReconnect() {
289351
if(options.isAutoReconnect() && !isConnected()) {
290352
debug("Trying to make a new connection with the database!");
@@ -296,6 +358,8 @@ private boolean handleAutoReconnect() {
296358
return true;
297359
}
298360

361+
// --***-- Query builders --***--
362+
299363
public SelectQuery select(String... cols) {
300364
return new SelectQuery(this, cols);
301365
}
@@ -367,6 +431,16 @@ public boolean isDebug() {
367431
return options.isDebug();
368432
}
369433

434+
public final SQLDatabaseOptions cloneOptions() {
435+
SQLDatabaseOptions cloned = new SQLDatabaseOptions();
436+
cloned.setDebug(options.isDebug());
437+
cloned.setLogSqlErrors(options.isLogSqlErrors());
438+
cloned.setNamingStrategy(options.getNamingStrategy());
439+
cloned.setGson(options.getGson());
440+
cloned.setAutoReconnect(options.isAutoReconnect());
441+
return cloned;
442+
}
443+
370444
@SuppressWarnings("unchecked")
371445
private PreparedStatement buildStatement(Query query) throws SQLException {
372446
StatementFactory<PreparedStatement> factory = new DefaultStatementFactory(query);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ public QueryNode(@Nullable P parent, List<QueryNode<?>> initial, int priority) {
3636
this.details = new ConcurrentHashMap<>();
3737
}
3838

39+
/**
40+
* Builds the query string with placeholders containing values
41+
* for passing into PreparedStatement.
42+
* <p>
43+
* Query example: SELECT * FROM table WHERE id = &lt;id&gt;;
44+
* Values example: [AnyId]
45+
*
46+
* @return QueryDetails object.
47+
*/
3948
public abstract QueryDetails buildQueryDetails();
4049

4150
@Override

0 commit comments

Comments
 (0)