Class SqlTemplate<I,R>

java.lang.Object
io.vertx.mutiny.sqlclient.templates.SqlTemplate<I,R>
All Implemented Interfaces:
MutinyDelegate

public class SqlTemplate<I,R> extends Object implements MutinyDelegate
An SQL template.

SQL templates are useful for interacting with a relational database.

SQL templates execute queries using named instead of positional parameters. Query execution is parameterized by a map of string to objects instead of a Tuple. The default source of parameters is a simple map, a user defined mapping can be used instead given it maps the source to such a map.

SQL template default results are Row, a user defined mapping can be used instead, mapping the result set Row to a RowSet of the mapped type.

NOTE: This class has been automatically generated from the original non Mutiny-ified interface.

See Also:
  • SqlTemplate
  • Field Details

  • Constructor Details

    • SqlTemplate

      public SqlTemplate(io.vertx.sqlclient.templates.SqlTemplate<I,R> delegate)
      Create a new instance of SqlTemplate delegating to the given (non-null) instance of SqlTemplate.
    • SqlTemplate

      public SqlTemplate(io.vertx.sqlclient.templates.SqlTemplate<I,R> delegate, TypeArg<I> typeArg_0, TypeArg<R> typeArg_1)
    • SqlTemplate

      public SqlTemplate(Object delegate, TypeArg<I> typeArg_0, TypeArg<R> typeArg_1)
  • Method Details

    • getDelegate

      public io.vertx.sqlclient.templates.SqlTemplate<I,R> getDelegate()
      Get the delegate instance.

      This method returns the instance on which this shim is delegating the calls. And so, give you access to the bare API.

      Specified by:
      getDelegate in interface MutinyDelegate
      Returns:
      the delegate instance
    • execute

      @CheckReturnValue public io.smallrye.mutiny.Uni<R> execute(I params)
      Execute the query with the parameters

      Unlike the bare Vert.x variant, this method returns a Uni. The uni emits the result of the operation as item. If the operation fails, the uni emits the failure.

      Don't forget to subscribe on it to trigger the operation.

      Parameters:
      params - the query parameters
      Returns:
      A Uni representing the asynchronous result of this operation.
      See Also:
      • io.vertx.sqlclient.templates.SqlTemplate#execute(I)
    • executeAndAwait

      public R executeAndAwait(I params)
      Execute the query with the parameters

      Unlike the bare Vert.x variant, this method returns a SqlTemplate. This method awaits indefinitely for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

      Parameters:
      params - the query parameters
      Returns:
      The operation result
      See Also:
      • io.vertx.sqlclient.templates.SqlTemplate#execute(I)
    • executeAndForget

      public SqlTemplate<I,R> executeAndForget(I params)
      Execute the query with the parameters

      Unlike the bare Vert.x variant, this method ignores the SqlTemplate result or any failure.

      Parameters:
      params - the query parameters
      Returns:
      The current instance to chain operations if needed.
      See Also:
      • io.vertx.sqlclient.templates.SqlTemplate#execute(I)
    • executeBatch

      @CheckReturnValue public io.smallrye.mutiny.Uni<R> executeBatch(List<I> batch)
      Execute a batch query with the batch.

      Each item in the batch is mapped to a tuple.

      Unlike the bare Vert.x variant, this method returns a Uni. The uni emits the result of the operation as item. If the operation fails, the uni emits the failure.

      Don't forget to subscribe on it to trigger the operation.

      Parameters:
      batch - the batch
      Returns:
      A Uni representing the asynchronous result of this operation.
      See Also:
      • SqlTemplate.executeBatch(List)
    • executeBatchAndAwait

      public R executeBatchAndAwait(List<I> batch)
      Execute a batch query with the batch.

      Each item in the batch is mapped to a tuple.

      Unlike the bare Vert.x variant, this method returns a SqlTemplate. This method awaits indefinitely for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

      Parameters:
      batch - the batch
      Returns:
      The operation result
      See Also:
      • SqlTemplate.executeBatch(List)
    • executeBatchAndForget

      public SqlTemplate<I,R> executeBatchAndForget(List<I> batch)
      Execute a batch query with the batch.

      Each item in the batch is mapped to a tuple.

      Unlike the bare Vert.x variant, this method ignores the SqlTemplate result or any failure.

      Parameters:
      batch - the batch
      Returns:
      The current instance to chain operations if needed.
      See Also:
      • SqlTemplate.executeBatch(List)
    • forQuery

      public static SqlTemplate<Map<String,Object>,RowSet<Row>> forQuery(SqlClient client, String template)
      Create an SQL template for query purpose consuming map parameters and returning Row.
      Parameters:
      client - the wrapped SQL client
      template - the template query string
      Returns:
      the template
    • forUpdate

      public static SqlTemplate<Map<String,Object>,SqlResult<Void>> forUpdate(SqlClient client, String template)
      Create an SQL template for query purpose consuming map parameters and returning void.
      Parameters:
      client - the wrapped SQL client
      template - the template update string
      Returns:
      the template
    • sql

      public String sql()
      Returns:
      the computed SQL for this template
    • mapFrom

      public <T> SqlTemplate<T,R> mapFrom(TupleMapper<T> mapper)
      Set a parameters user defined mapping function.

      At query execution, the mapper is called to map the parameters object to a Tuple that configures the prepared query.

      Parameters:
      mapper - the mapping function
      Returns:
      a new template
    • mapFrom

      public <T> SqlTemplate<T,R> mapFrom(Class<T> type)
      Set a parameters user defined class mapping.

      At query execution, the parameters object is is mapped to a Map<String, Object> that configures the prepared query.

      This feature relies on JsonObject.mapFrom(java.lang.Object) feature. This likely requires to use Jackson databind in the project.

      Parameters:
      type - the mapping type
      Returns:
      a new template
    • mapTo

      public <U> SqlTemplate<I,RowSet<U>> mapTo(RowMapper<U> mapper)
      Set a row user defined mapping function.

      When the query execution completes, the mapper function is called to map the resulting rows to objects.

      Parameters:
      mapper - the mapping function
      Returns:
      a new template
    • mapTo

      public <U> SqlTemplate<I,RowSet<U>> mapTo(Class<U> type)
      Set a row user defined mapping function.

      When the query execution completes, resulting rows are mapped to type instances.

      This feature relies on JsonObject.mapFrom(java.lang.Object) feature. This likely requires to use Jackson databind in the project.

      Parameters:
      type - the mapping type
      Returns:
      a new template
    • withClient

      public SqlTemplate<I,R> withClient(SqlClient client)
      Returns a new template, using the specified client.

      This method does not compute the template query again, so it can be useful to execute a template on a specific SqlConnection. For example, after starting a transaction:

         // Typically stored as a verticle field
         // So that heavy computation of the template happens once
         SqlTemplate<Map<String, Object>, RowSet> template = SqlTemplate
           .forQuery(pool, "SELECT id, randomnumber FROM tmp_world")
           .mapTo(World.class);
      
         // Executing the template inside a transaction
         Future<RowSet> future = pool.withTransaction(conn -> template.withClient(conn).execute(Map.of()));
       
      Parameters:
      client - the client that will execute requests
      Returns:
      a new template
    • newInstance

      public static <I, R> SqlTemplate<I,R> newInstance(io.vertx.sqlclient.templates.SqlTemplate<I,R> delegate)
      Creates a new instance of the SqlTemplate.
    • newInstance

      public static <I, R> SqlTemplate<I,R> newInstance(io.vertx.sqlclient.templates.SqlTemplate<I,R> delegate, TypeArg<I> typeArg_0, TypeArg<R> typeArg_1)
      Creates a new instance of the SqlTemplate.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object