Package org.jdbi.v3.core
Class Sql
- java.lang.Object
-
- org.jdbi.v3.core.Sql
-
- All Implemented Interfaces:
java.lang.CharSequence
@Beta public final class Sql extends java.lang.Object implements java.lang.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
Please note that the validity of the statement is never checked, and thatCharSequenceand thus can be used as a drop-in alternative wherever API supportsCharSequencerather thanString.nullor 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");
-
-
Constructor Summary
Constructors Modifier Constructor Description privateSql(java.lang.String sql)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description charcharAt(int index)booleanequals(java.lang.Object obj)(package private) static java.lang.Stringformat(java.lang.Iterable<? extends java.lang.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.inthashCode()intlength()static Sqlof(java.lang.CharSequence... tokens)static Sqlof(java.lang.Iterable<? extends java.lang.CharSequence> tokens)java.lang.CharSequencesubSequence(int start, int end)java.lang.StringtoString()
-
-
-
Field Detail
-
EMPTY_SQL
private static final Sql EMPTY_SQL
-
str
private final java.lang.String str
The internal sql string. Cannot be null.
-
-
Method Detail
-
of
public static Sql of(java.lang.CharSequence... tokens)
-
of
public static Sql of(java.lang.Iterable<? extends java.lang.CharSequence> tokens)
-
format
static java.lang.String format(java.lang.Iterable<? extends java.lang.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:
lengthin interfacejava.lang.CharSequence
-
charAt
public char charAt(int index)
- Specified by:
charAtin interfacejava.lang.CharSequence
-
subSequence
@Nonnull public java.lang.CharSequence subSequence(int start, int end)- Specified by:
subSequencein interfacejava.lang.CharSequence
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()
- Specified by:
toStringin interfacejava.lang.CharSequence- Overrides:
toStringin classjava.lang.Object
-
-