Class Sql

java.lang.Object
org.jdbi.v3.core.Sql
All Implemented Interfaces:
CharSequence

@Beta public final class Sql extends Object implements CharSequence

An immutable sql statement string created from multiple tokens in order to write inline sql statements in an easy-to-read fashion spread out over multiple lines of code.

The class implements CharSequence and thus can be used as a drop-in alternative wherever API supports CharSequence rather than String.

Please note that the validity of the statement is never checked, and that null or empty inputs are permitted (no run-time exceptions).
The input of multiple tokens is formatted into a single String by removing leading and trailing whitespace and concatenating non-empty tokens by a single space character. Further, any trailing semicolons are removed from the resulting sql string.

Example:

     String tblName = "table";
     Sql.of("SELECT COUNT(*)",
            "FROM", tblName,
            " WHERE cond1 = :cond1",
            "   AND cond2 = :cond2");
 
  • Field Details

    • EMPTY_SQL

      private static final Sql EMPTY_SQL
    • str

      private final String str
      The internal sql string. Cannot be null.
  • Constructor Details

    • Sql

      private Sql(String sql)
  • Method Details

    • of

      public static Sql of(CharSequence... tokens)
    • of

      public static Sql of(Iterable<? extends CharSequence> tokens)
    • format

      static String format(Iterable<? extends CharSequence> tokens)
      Formats an sql statement from multiple tokens.
      Leading and trailing whitespace is removed from each token and empty tokens ignored.
      The tokens are joined using a single blank character to create the sql string.
      Finally, any trailing semicolons are removed from the resulting sql.
      Parameters:
      tokens - collection of tokens
      Returns:
      formatted sql string
    • length

      public int length()
      Specified by:
      length in interface CharSequence
    • charAt

      public char charAt(int index)
      Specified by:
      charAt in interface CharSequence
    • subSequence

      @Nonnull public CharSequence subSequence(int start, int end)
      Specified by:
      subSequence in interface CharSequence
    • hashCode

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

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

      public String toString()
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Object