Package org.apache.calcite.sql.util
Class SqlBuilder
java.lang.Object
org.apache.calcite.sql.util.SqlBuilder
Extension to
StringBuilder
for the purposes of creating SQL queries
and expressions.
Using this class helps to prevent SQL injection attacks, incorrectly quoted identifiers and strings. These problems occur when you build SQL by concatenating strings, and you forget to treat identifers and string literals correctly. SqlBuilder has special methods for appending identifiers and literals.
-
Constructor Summary
ConstructorDescriptionSqlBuilder
(SqlDialect dialect) Creates a SqlBuilder.SqlBuilder
(SqlDialect dialect, String s) Creates a SqlBuilder with a given string. -
Method Summary
Modifier and TypeMethodDescriptionappend
(char c) Appends a character, without any quoting.append
(long n) Appends a number, perStringBuilder.append(long)
.Appends a string, without any quoting.Appends a hygienic SQL string.void
clear()
Clears the contents of the buffer.Returns the dialect.getSql()
Returns the SQL.Returns the SQL and clears the buffer.identifier
(String name) Appends an identifier to this buffer, quoting accordingly.identifier
(String... names) Appends one or more identifiers to this buffer, quoting accordingly.identifier
(List<String> names) Appends a compound identifier to this buffer, quoting accordingly.int
Returns the index within this string of the first occurrence of the specified substring.int
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.Inserts the string into this character sequence.int
length()
Returns the length (character count).Appends a string literal to this buffer.Appends a timestamp literal to this buffer.Returns the contents of this SQL buffer as a 'certified kocher' SQL string.toString()
-
Constructor Details
-
SqlBuilder
Creates a SqlBuilder.- Parameters:
dialect
- Dialect
-
SqlBuilder
Creates a SqlBuilder with a given string.- Parameters:
dialect
- Dialects
- Initial contents of the buffer
-
-
Method Details
-
getDialect
Returns the dialect.- Returns:
- dialect
-
length
public int length()Returns the length (character count).- Returns:
- the length of the sequence of characters currently represented by this object
-
clear
public void clear()Clears the contents of the buffer. -
toString
Returns the SQL string.
-
getSql
Returns the SQL. -
getSqlAndClear
Returns the SQL and clears the buffer.Convenient if you are reusing the same SQL builder in a loop.
-
append
Appends a hygienic SQL string.- Parameters:
s
- SQL string to append- Returns:
- This builder
-
append
Appends a string, without any quoting.Calls to this method are dubious.
- Parameters:
s
- String to append- Returns:
- This builder
-
append
Appends a character, without any quoting.- Parameters:
c
- Character to append- Returns:
- This builder
-
append
Appends a number, perStringBuilder.append(long)
. -
identifier
Appends an identifier to this buffer, quoting accordingly.- Parameters:
name
- Identifier- Returns:
- This builder
-
identifier
Appends one or more identifiers to this buffer, quoting accordingly.- Parameters:
names
- Varargs array of identifiers- Returns:
- This builder
-
identifier
Appends a compound identifier to this buffer, quoting accordingly.- Parameters:
names
- Parts of a compound identifier- Returns:
- This builder
-
toSqlString
Returns the contents of this SQL buffer as a 'certified kocher' SQL string.Use this method in preference to
toString()
. It indicates that the SQL string has been constructed using good hygiene, and is therefore less likely to contain SQL injection or badly quoted identifiers or strings.- Returns:
- Contents of this builder as a SQL string.
-
literal
Appends a string literal to this buffer.For example, calling
literal("can't")
would convert the buffer
toSELECT
SELECT 'can''t'
- Parameters:
s
- String to append- Returns:
- This buffer
-
literal
Appends a timestamp literal to this buffer.- Parameters:
timestamp
- Timestamp to append- Returns:
- This buffer
-
indexOf
Returns the index within this string of the first occurrence of the specified substring.- See Also:
-
indexOf
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.- See Also:
-
insert
Inserts the string into this character sequence.- See Also:
-