Class SQLBuilder
java.lang.Object
org.apache.sis.internal.metadata.sql.Syntax
org.apache.sis.internal.metadata.sql.SQLBuilder
- Direct Known Subclasses:
SelectionClause
Utility methods for building SQL statements.
This class is for internal purpose only and may change or be removed in any future SIS version.
- Since:
- 0.8
- Version:
- 1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final StringBuilder
The buffer where the SQL query is created.static final String
The "SELECT " keyword (with a trailing space).Fields inherited from class org.apache.sis.internal.metadata.sql.Syntax
dialect, escape, quote, quoteSchema
-
Constructor Summary
ConstructorsConstructorDescriptionSQLBuilder
(DatabaseMetaData metadata, boolean quoteSchema) Creates a newSQLBuilder
initialized from the given database metadata.SQLBuilder
(Syntax other) Creates a newSQLBuilder
initialized to the same metadata than the given template. -
Method Summary
Modifier and TypeMethodDescriptionfinal SQLBuilder
append
(char c) Appends the given character.final SQLBuilder
append
(int n) Appends the given integer.final SQLBuilder
append
(long n) Appends the given long integer.final SQLBuilder
Appends the given text verbatim.final SQLBuilder
appendEqualsValue
(Object value) Appends a"= <value>"
string in aSELECT
statement.final SQLBuilder
appendFetchPage
(long offset, long count) AppendsOFFSET
andFETCH
clauses for fetching only a page of data.final SQLBuilder
appendIdentifier
(String identifier) Appends an identifier between quote characters.final SQLBuilder
appendIdentifier
(String schema, String identifier) Appends an identifier for an element in the given schema.final SQLBuilder
appendIdentifier
(String catalog, String schema, String identifier) Appends an identifier for an element in the given schema and catalog.final SQLBuilder
appendValue
(Object value) Appends a value in aSELECT
orINSERT
statement.final SQLBuilder
appendValue
(String value) Appends a value in aSELECT
orINSERT
statement.final SQLBuilder
appendWildcardEscaped
(String value) Appends a string as an escapedLIKE
argument.final SQLBuilder
clear()
Clears this builder and make it ready for creating a new SQL statement.final String
createColumn
(String schema, String table, String column, Class<?> type, int maxLength) Returns a SQL statement for adding a column in a table.final String
createForeignKey
(String schema, String table, String column, String target, String primaryKey, boolean cascade) Returns a SQL statement for creating a foreigner key constraint.final SQLBuilder
Inserts theDISTINCT
keyword afterSELECT
.final boolean
isEmpty()
Returnstrue
if the builder is currently empty.final String
toString()
Returns the SQL statement.
-
Field Details
-
SELECT
The "SELECT " keyword (with a trailing space). Defined as a convenience for identifying locations in the Java code where we start to write a SQL statement using a builder.- See Also:
-
buffer
The buffer where the SQL query is created.
-
-
Constructor Details
-
SQLBuilder
Creates a newSQLBuilder
initialized from the given database metadata.- Parameters:
metadata
- the database metadata.quoteSchema
- whether the schema name should be written between quotes.- Throws:
SQLException
- if an error occurred while fetching the database metadata.
-
SQLBuilder
Creates a newSQLBuilder
initialized to the same metadata than the given template.- Parameters:
other
- the template from which to copy metadata.
-
-
Method Details
-
isEmpty
public final boolean isEmpty()Returnstrue
if the builder is currently empty.- Returns:
true
if the builder is empty.
-
clear
Clears this builder and make it ready for creating a new SQL statement.- Returns:
- this builder, for method call chaining.
-
append
Appends the given integer.- Parameters:
n
- the integer to append.- Returns:
- this builder, for method call chaining.
-
append
Appends the given long integer.- Parameters:
n
- the long to append.- Returns:
- this builder, for method call chaining.
-
append
Appends the given character.- Parameters:
c
- the character to append.- Returns:
- this builder, for method call chaining.
-
append
Appends the given text verbatim. The text should be SQL keywords like"SELECT * FROM"
.- Parameters:
keyword
- the keyword to append verbatim.- Returns:
- this builder, for method call chaining.
-
appendIdentifier
Appends an identifier between quote characters.- Parameters:
identifier
- the identifier to append.- Returns:
- this builder, for method call chaining.
-
appendIdentifier
Appends an identifier for an element in the given schema.- The given schema will be written only if non-null.
- The given schema will be quoted only if
quoteSchema
istrue
. - The given identifier is always quoted.
- Parameters:
schema
- the schema, ornull
or empty if none.identifier
- the identifier to append.- Returns:
- this builder, for method call chaining.
-
appendIdentifier
Appends an identifier for an element in the given schema and catalog.- Parameters:
catalog
- the catalog, ornull
or empty if none.schema
- the schema, ornull
or empty if none.identifier
- the identifier to append.- Returns:
- this builder, for method call chaining.
-
appendEqualsValue
Appends a"= <value>"
string in aSELECT
statement. The value is written between quotes, except if it is a number or a boolean.- Parameters:
value
- the value to append, ornull
.- Returns:
- this builder, for method call chaining.
-
appendValue
Appends a value in aSELECT
orINSERT
statement. The value is written between quotes.- Parameters:
value
- the value to append, ornull
.- Returns:
- this builder, for method call chaining.
-
appendValue
Appends a value in aSELECT
orINSERT
statement. If the given value is a character string, then it is written between quotes.- Parameters:
value
- the value to append, ornull
.- Returns:
- this builder, for method call chaining.
-
appendWildcardEscaped
Appends a string as an escapedLIKE
argument. This method does not put any'
character, and does not accept null argument.This method does not double the simple quotes of the given string on intent, because it may be used in a
PreparedStatement
. If the simple quotes need to be doubled, thenappendValue(String)
should be invoked.- Parameters:
value
- the value to append.- Returns:
- this builder, for method call chaining.
-
appendFetchPage
AppendsOFFSET
andFETCH
clauses for fetching only a page of data. If a limit or an offset is appended, a space will be added before the clauses. This method uses ANSI notation for better compatibility with various drivers.- Parameters:
offset
- the offset to use. If zero or negative, no offset is written.count
- number of rows to fetch. If zero or negative, no count is written.- Returns:
- this builder, for method call chaining.
-
insertDistinctAfterSelect
Inserts theDISTINCT
keyword afterSELECT
. AnAssertionError
may be thrown if the buffer content does not starts with "SELECT ".- Returns:
- this builder, for method call chaining.
-
createColumn
public final String createColumn(String schema, String table, String column, Class<?> type, int maxLength) Returns a SQL statement for adding a column in a table. The returned statement is of the form: wheretype
is some SQL keyword likeINTEGER
orVARCHAR
depending on thetype
argument.- Parameters:
schema
- the schema for the table.table
- the table to alter with the new column.column
- the column to add.type
- the column type, ornull
forVARCHAR
.maxLength
- the maximal length (used forVARCHAR
only).- Returns:
- a SQL statement for creating the column.
-
createForeignKey
public final String createForeignKey(String schema, String table, String column, String target, String primaryKey, boolean cascade) Returns a SQL statement for creating a foreigner key constraint. The returned statement is of the form: Note that the primary key is not quoted on intent. If quoted are desired, then they must be added explicitly before to call this method.- Parameters:
schema
- the schema for both tables.table
- the table to alter with the new constraint.column
- the column to alter with the new constraint.target
- the table to reference.primaryKey
- the primary key in the target table.cascade
-true
if updates in primary key should be cascaded. this apply to updates only; delete is always restricted.- Returns:
- a SQL statement for creating the foreigner key constraint.
-
toString
Returns the SQL statement.
-