Skip to content

Commit

Permalink
Refactor insert/update/delete to prepare for other database types
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Mar 25, 2016
1 parent 14cafb8 commit 97cb3ab
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 150 deletions.
155 changes: 103 additions & 52 deletions src/main/java/com/speedment/db/DbmsHandler.java
Expand Up @@ -20,6 +20,8 @@
import com.speedment.config.db.Dbms; import com.speedment.config.db.Dbms;
import com.speedment.config.db.Project; import com.speedment.config.db.Project;
import com.speedment.config.db.Schema; import com.speedment.config.db.Schema;
import com.speedment.field.trait.FieldTrait;
import com.speedment.field.trait.ReferenceFieldTrait;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collections; import java.util.Collections;
Expand All @@ -35,9 +37,9 @@
* A DbmsHandler provides the interface between Speedment and an underlying * A DbmsHandler provides the interface between Speedment and an underlying
* {@link Dbms}. * {@link Dbms}.
* *
* @author Per Minborg * @author Per Minborg
* @author Emil Forslund * @author Emil Forslund
* @since 2.0 * @since 2.0
*/ */
@Api(version = "2.2") @Api(version = "2.2")
public interface DbmsHandler { public interface DbmsHandler {
Expand All @@ -57,7 +59,6 @@ public interface DbmsHandler {
// * @return the {@link Dbms} document to use as a prototype // * @return the {@link Dbms} document to use as a prototype
// */ // */
// Dbms getDbms(); // Dbms getDbms();

/** /**
* Reads the schema metadata with populated {@link Schema Schemas} that are * Reads the schema metadata with populated {@link Schema Schemas} that are
* available in this database. The schemas are populated by all their * available in this database. The schemas are populated by all their
Expand All @@ -67,9 +68,9 @@ public interface DbmsHandler {
* <p> * <p>
* This method can be used to read a complete inventory of the database * This method can be used to read a complete inventory of the database
* structure. * structure.
* *
* @param progressListener the progress listener * @param progressListener the progress listener
* @return the handle for this task * @return the handle for this task
*/ */
default CompletableFuture<Project> readSchemaMetadata( default CompletableFuture<Project> readSchemaMetadata(
ProgressMeasure progressListener) { ProgressMeasure progressListener) {
Expand All @@ -85,12 +86,12 @@ default CompletableFuture<Project> readSchemaMetadata(
* the model or that does not match the given filter will be excluded from * the model or that does not match the given filter will be excluded from
* the {@code Stream}. * the {@code Stream}.
* *
* @param progressListener the progress listener * @param progressListener the progress listener
* @param filterCriteria criteria that schema names must fulfill * @param filterCriteria criteria that schema names must fulfill
* @return the handle for this task * @return the handle for this task
*/ */
CompletableFuture<Project> readSchemaMetadata( CompletableFuture<Project> readSchemaMetadata(
ProgressMeasure progressListener, ProgressMeasure progressListener,
Predicate<String> filterCriteria Predicate<String> filterCriteria
); );


Expand All @@ -102,10 +103,10 @@ CompletableFuture<Project> readSchemaMetadata(
* are present or if an SQLException is thrown internally, an {@code empty} * are present or if an SQLException is thrown internally, an {@code empty}
* stream is returned. * stream is returned.
* *
* @param <T> the type of the objects in the stream to return * @param <T> the type of the objects in the stream to return
* @param sql the SQL command to execute * @param sql the SQL command to execute
* @param rsMapper the mapper to use when iterating over the ResultSet * @param rsMapper the mapper to use when iterating over the ResultSet
* @return a stream of the mapped objects * @return a stream of the mapped objects
*/ */
default <T> Stream<T> executeQuery(String sql, SqlFunction<ResultSet, T> rsMapper) { default <T> Stream<T> executeQuery(String sql, SqlFunction<ResultSet, T> rsMapper) {
return executeQuery(sql, Collections.emptyList(), rsMapper); return executeQuery(sql, Collections.emptyList(), rsMapper);
Expand All @@ -118,13 +119,13 @@ default <T> Stream<T> executeQuery(String sql, SqlFunction<ResultSet, T> rsMappe
* are present or if an {@link SQLException} is thrown internally, an * are present or if an {@link SQLException} is thrown internally, an
* {@code empty} stream is returned. * {@code empty} stream is returned.
* *
* @param <T> the type of the objects in the stream to return * @param <T> the type of the objects in the stream to return
* @param sql the non-null SQL command to execute * @param sql the non-null SQL command to execute
* @param values non-null values to use for "?" parameters in the sql * @param values non-null values to use for "?" parameters in the sql
* command * command
* @param rsMapper the non-null mapper to use when iterating over the * @param rsMapper the non-null mapper to use when iterating over the
* {@link ResultSet} * {@link ResultSet}
* @return a stream of the mapped objects * @return a stream of the mapped objects
*/ */
public <T> Stream<T> executeQuery(String sql, List<?> values, SqlFunction<ResultSet, T> rsMapper); public <T> Stream<T> executeQuery(String sql, List<?> values, SqlFunction<ResultSet, T> rsMapper);


Expand All @@ -135,45 +136,95 @@ default <T> Stream<T> executeQuery(String sql, SqlFunction<ResultSet, T> rsMappe
* stream will consume the {@code ResultSet} as the objects are consumed. If * stream will consume the {@code ResultSet} as the objects are consumed. If
* no objects are present, an {@code empty} stream is returned. * no objects are present, an {@code empty} stream is returned.
* *
* @param <T> the type of the objects in the Stream to return * @param <T> the type of the objects in the Stream to return
* @param sql the non-null SQL command to execute * @param sql the non-null SQL command to execute
* @param values non-null List of objects to use for "?" parameters in * @param values non-null List of objects to use for "?" parameters in the
* the SQL command * SQL command
* @param rsMapper the non-null mapper to use when iterating over the * @param rsMapper the non-null mapper to use when iterating over the
* {@link ResultSet} * {@link ResultSet}
* @return a stream of the mapped objects * @return a stream of the mapped objects
*/ */
public <T> AsynchronousQueryResult<T> executeQueryAsync( public <T> AsynchronousQueryResult<T> executeQueryAsync(
String sql, List<?> values, Function<ResultSet, T> rsMapper); String sql,
List<?> values,
Function<ResultSet, T> rsMapper
);


// /**
// * Executes an SQL update command. Generated key(s) following an insert
// * command (if any) will be feed to the provided {code Consumer}.
// *
// * @param sql the non-null SQL command to execute
// * @param generatedKeyConsumer the non-null key Consumer
// * @throws SQLException if an error occurs
// */
// default void executeInsert(String sql, Consumer<List<Long>> generatedKeyConsumer) throws SQLException {
// executeInsert(sql, Collections.emptyList(), generatedKeyConsumer);
// }
/** /**
* Executes a SQL update command. Generated key(s) following an insert * Executes an SQL update command. Generated key(s) following an insert
* command (if any) will be feed to the provided {code Consumer}. * command (if any) will be feed to the provided Consumer.
* *
* @param sql the non-null SQL command to execute * @param <F> dynamic type
* @param generatedKeyConsumer the non-null key Consumer * @param sql the non-null SQL command to execute
* @throws SQLException if an error occurs * @param values a non-null list
* @param generatedKeyFields list of the generated fields
* @param generatedKeyConsumer non-null List of objects to use for "?"
* parameters in the SQL command
* @throws SQLException if an error occurs
*/ */
default void executeUpdate( public <F extends FieldTrait & ReferenceFieldTrait> void executeInsert(
String sql, Consumer<List<Long>> generatedKeyConsumer final String sql,
) throws SQLException { final List<?> values,
final List<F> generatedKeyFields,
final Consumer<List<Long>> generatedKeyConsumer
) throws SQLException;


executeUpdate(sql, Collections.emptyList(), generatedKeyConsumer); // /**
} // * Executes an SQL update command. Generated key(s) following an insert
// * command (if any) will be feed to the provided {code Consumer}.
// *
// * @param sql the non-null SQL command to execute
// * @throws SQLException if an error occurs
// */
// default void executeUpdate(String sql) throws SQLException {
// executeUpdate(sql, Collections.emptyList());
// }
/**
* Executes an SQL update command. Generated key(s) following an insert
* command (if any) will be feed to the provided Consumer.
*
* @param sql the non-null SQL command to execute
* @param values a non-null list
* @throws SQLException if an error occurs
*/
public void executeUpdate(final String sql, final List<?> values) throws SQLException;


// /**
// * Executes an SQL delete command.
// *
// * @param sql the non-null SQL command to execute
// * @throws SQLException if an error occurs
// */
// default void executeDelete(String sql) throws SQLException {
// executeDelete(sql, Collections.emptyList());
// }
/** /**
* Executes a SQL update command. Generated key(s) following an insert * Executes an SQL delete command. Generated key(s) following an insert
* command (if any) will be feed to the provided Consumer. * command (if any) will be feed to the provided Consumer.
* *
* @param sql the non-null SQL command to execute * @param sql the non-null SQL command to execute
* @param values a non-null list * @param values a non-null list
* @param generatedKeyConsumer non-null List of objects to use for "?" * @throws SQLException if an error occurs
* parameters in the SQL command
* @throws SQLException if an error occurs
*/ */
public void executeUpdate( public void executeDelete(final String sql, final List<?> values) throws SQLException;
final String sql,
final List<?> values, /**
final Consumer<List<Long>> generatedKeyConsumer * Returns a string with information on the current dbms.
) throws SQLException; *
} * @return a string with information on the current dbms
* @throws SQLException if an error occurs
*/
public String getDbmsInfoString() throws SQLException;

}

0 comments on commit 97cb3ab

Please sign in to comment.