3939 *
4040 * @author ZorTik
4141 */
42+ @ SuppressWarnings ("unused" )
4243public 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+ * @Table("users")
131+ * public interface MyRepository {
132+ * @Select("*")
133+ * @Where(@Where.Condition(column = "firstname", value = "{First Name}"))
134+ * @Limit(1)
135+ * Optional<User> getUser(@Placeholder("First Name") String firstName);
136+ *
137+ * @Select
138+ * List<User> getUsers();
139+ *
140+ * @Delete
141+ * QueryResult deleteUsers();
142+ * }
143+ *
144+ * SQLDatabaseConnection connection = ...;
145+ * MyRepository repository = connection.createGate(MyRepository.class);
146+ *
147+ * Optional<User> 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 );
0 commit comments