Class StringTemplateEngine

java.lang.Object
org.jdbi.v3.stringtemplate4.StringTemplateEngine
All Implemented Interfaces:
TemplateEngine, TemplateEngine.Parsing

public class StringTemplateEngine extends Object implements TemplateEngine.Parsing
Rewrites a StringTemplate template, using the attributes on the StatementContext as template parameters. For configuration, see StringTemplates.
  • Constructor Details

    • StringTemplateEngine

      public StringTemplateEngine()
  • Method Details

    • render

      public String render(String sql, StatementContext ctx)
      Non-cached render, for direct callers; the core uses parse(String, ConfigRegistry).
      Specified by:
      render in interface TemplateEngine
      Specified by:
      render in interface TemplateEngine.Parsing
      Parameters:
      sql - The SQL to rewrite
      ctx - The statement context for the statement being executed
      Returns:
      something which can provide the actual SQL to prepare a statement from and which can bind the correct arguments to that prepared statement
    • parse

      Caches compilation. StringTemplate is expensive to compile, and its STGroup and ST are not thread-safe, so a compiled template must not be rendered by two threads at once. Compiled prototypes are pooled rather than bound to a thread, so compilation is reused across platform and virtual threads alike: a render checks a prototype out of the pool (compiling one only if the pool is empty), renders a copy, and returns the prototype. The pool holds at most POOL_CAPACITY prototypes; a render that finds it empty compiles its own, and a return that finds it full discards.

      The cached function bypasses render(String, StatementContext). A subclass that overrides render() must also override this method so the two agree: either return Optional.empty() to keep the core on the render() path, or return a function with the subclass's semantics.

      Specified by:
      parse in interface TemplateEngine
      Specified by:
      parse in interface TemplateEngine.Parsing
      Parameters:
      sql - the sql template to parse
      config - the Jdbi configuration at prepare time
      Returns:
      a parsed representation, if available
    • equals

      public boolean equals(Object obj)
      The engine is stateless, so all instances of the same class are interchangeable. Equality by class lets the core statement cache reuse compiled templates across instances, e.g. one created per statement or by each @UseStringTemplateEngine annotation. A stateful subclass must override equals and hashCode to keep differently-configured instances apart in the cache.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object