Class SQLBuilder

java.lang.Object
org.apache.sis.internal.metadata.sql.Syntax
org.apache.sis.internal.metadata.sql.SQLBuilder
Direct Known Subclasses:
SelectionClause

public class SQLBuilder extends Syntax
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 Details

    • SELECT

      public static final String 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

      protected final StringBuilder buffer
      The buffer where the SQL query is created.
  • Constructor Details

    • SQLBuilder

      public SQLBuilder(DatabaseMetaData metadata, boolean quoteSchema) throws SQLException
      Creates a new SQLBuilder 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

      public SQLBuilder(Syntax other)
      Creates a new SQLBuilder 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()
      Returns true if the builder is currently empty.
      Returns:
      true if the builder is empty.
    • clear

      public final SQLBuilder clear()
      Clears this builder and make it ready for creating a new SQL statement.
      Returns:
      this builder, for method call chaining.
    • append

      public final SQLBuilder append(int n)
      Appends the given integer.
      Parameters:
      n - the integer to append.
      Returns:
      this builder, for method call chaining.
    • append

      public final SQLBuilder append(long n)
      Appends the given long integer.
      Parameters:
      n - the long to append.
      Returns:
      this builder, for method call chaining.
    • append

      public final SQLBuilder append(char c)
      Appends the given character.
      Parameters:
      c - the character to append.
      Returns:
      this builder, for method call chaining.
    • append

      public final SQLBuilder append(String keyword)
      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

      public final SQLBuilder appendIdentifier(String identifier)
      Appends an identifier between quote characters.
      Parameters:
      identifier - the identifier to append.
      Returns:
      this builder, for method call chaining.
    • appendIdentifier

      public final SQLBuilder appendIdentifier(String schema, String identifier)
      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 is true.
      • The given identifier is always quoted.
      Parameters:
      schema - the schema, or null or empty if none.
      identifier - the identifier to append.
      Returns:
      this builder, for method call chaining.
    • appendIdentifier

      public final SQLBuilder appendIdentifier(String catalog, String schema, String identifier)
      Appends an identifier for an element in the given schema and catalog.
      Parameters:
      catalog - the catalog, or null or empty if none.
      schema - the schema, or null or empty if none.
      identifier - the identifier to append.
      Returns:
      this builder, for method call chaining.
    • appendEqualsValue

      public final SQLBuilder appendEqualsValue(Object value)
      Appends a "= <value>" string in a SELECT statement. The value is written between quotes, except if it is a number or a boolean.
      Parameters:
      value - the value to append, or null.
      Returns:
      this builder, for method call chaining.
    • appendValue

      public final SQLBuilder appendValue(String value)
      Appends a value in a SELECT or INSERT statement. The value is written between quotes.
      Parameters:
      value - the value to append, or null.
      Returns:
      this builder, for method call chaining.
    • appendValue

      public final SQLBuilder appendValue(Object value)
      Appends a value in a SELECT or INSERT statement. If the given value is a character string, then it is written between quotes.
      Parameters:
      value - the value to append, or null.
      Returns:
      this builder, for method call chaining.
    • appendWildcardEscaped

      public final SQLBuilder appendWildcardEscaped(String value)
      Appends a string as an escaped LIKE 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, then appendValue(String) should be invoked.

      Parameters:
      value - the value to append.
      Returns:
      this builder, for method call chaining.
    • appendFetchPage

      public final SQLBuilder appendFetchPage(long offset, long count)
      Appends OFFSET and FETCH 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

      public final SQLBuilder insertDistinctAfterSelect()
      Inserts the DISTINCT keyword after SELECT. An AssertionError 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: where type is some SQL keyword like INTEGER or VARCHAR depending on the type 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, or null for VARCHAR.
      maxLength - the maximal length (used for VARCHAR 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

      public final String toString()
      Returns the SQL statement.
      Overrides:
      toString in class Object
      Returns:
      the SQL statement.