Class PostgresDialect

java.lang.Object
org.apache.juneau.beanquery.postgres.PostgresDialect
All Implemented Interfaces:
SqlDialect

public final class PostgresDialect extends Object implements SqlDialect
The PostgreSQL SqlDialect: the one dialect Juneau ships for the shared JDBC SqlBeanQueryContext.

It renders the built-in $-operators (design §5.1) into Postgres predicates that mirror the in-memory semantics: exact, case-sensitive $eq/$in/$ne on text/id/enum columns, with the case-insensitive $eqic (and the case-insensitive $contains/$prefix) via lower(...) and ILIKE; full-string $regex via ~/~* with the same i/m/s flag semantics as java.util.regex; dotted-tuple version comparison and $prefix; and typed binds for numeric/boolean/timestamp comparisons. A bare pattern maps * to % and ? to _ for ILIKE, unless the literal was quoted in the search string, in which case it is matched literally.

Values arrive already typed (design #4 §4: SearchType.parse(String) runs during query resolution, before this dialect ever sees a value), and are always bound as ? parameters, never concatenated into SQL text.

  • ENUM columns are compared as text: the column is cast (col::text) wherever this dialect applies a text function or operator to it, so a native Postgres enum column behaves like a text one (including bare */? wildcards, which match the whole constant name, case-insensitively).
  • VERSION columns are stored as dotted text (for example 10.0.1) and compared as a tuple of integers (10.9 sorts before 10.10), like the in-memory engine. A stored value that is not a dotted sequence of integers never matches. Known, deliberate differences from the in-memory parser (Integer.parseInt over split("\\.")): each part is limited to 9 digits here (so it always fits a Postgres int; longer parts never match, where in-memory accepts up to the int range), and a trailing empty part (10.) or a signed part (+1, -1) is rejected here but accepted in-memory.
  • Blank ($blank, and the blank-matching $ne) means NULL or only ASCII whitespace (space, tab, line breaks, vertical tab, form feed and the C0 separators), matching String.isBlank() for ASCII; non-ASCII Unicode spaces are not trimmed.
Example:

// Register Postgres as the dialect for a JDBC-backed context. SqlBeanQueryContext<Task> ctx = SqlBeanQueryContext.create(Task.class) .dialect(PostgresDialect.INSTANCE) .table("tasks") .column("name", SearchType.TEXT) .column("status", SearchType.ENUM) .connectionSupplier(supplier) .build(); // Render a bare value directly: the value is already typed, so a NUMERIC column gets a BigDecimal bind. SqlFragment frag = PostgresDialect.INSTANCE.renderBare("\"age\"", SearchType.NUMERIC, new BigDecimal("30"), false); // frag.sql() == "\"age\" = ?", frag.binds() == [30]

Since:
10.0.0
  • Field Details

  • Method Details

    • id

      public String id()
      Description copied from interface: SqlDialect
      The stable lowercase dialect id (for example "postgres").
      Specified by:
      id in interface SqlDialect
      Returns:
      The dialect id, never null or blank.
    • supportsVersionOrdering

      public boolean supportsVersionOrdering()
      Postgres compares versions as int[] tuples (see versionArray(java.lang.String)), matching the in-memory order.
      Specified by:
      supportsVersionOrdering in interface SqlDialect
      Returns:
      true if version range comparisons match the in-memory semantics.
    • quote

      public String quote(String identifier)
      Description copied from interface: SqlDialect
      Quotes a column or table identifier for this dialect (for example age → "age").
      Specified by:
      quote in interface SqlDialect
      Parameters:
      identifier - The raw identifier. Must not be null.
      Returns:
      The quoted identifier.
    • builtinRenderer

      public SearchSqlRenderer builtinRenderer(String operatorName)
      Description copied from interface: SqlDialect
      The renderer for a built-in leaf operator (for example "$eq"), or null if this dialect cannot express it.

      The context calls this only for built-in leaves; it handles the $and/$or/$not combinators itself. A null return makes the context fail with a clear error rather than emit wrong SQL.

      Specified by:
      builtinRenderer in interface SqlDialect
      Parameters:
      operatorName - The built-in operator name, including the leading $. Must not be null.
      Returns:
      The renderer, or null if unsupported.
    • renderLimitOffset

      public String renderLimitOffset(Long limit, Long offset)
      Description copied from interface: SqlDialect
      Renders the pagination clause appended to a SELECT (for example " LIMIT 10 OFFSET 20").

      The limit/offset are inlined as integer literals (never bind values), so the returned text carries no placeholders.

      Specified by:
      renderLimitOffset in interface SqlDialect
      Parameters:
      limit - The maximum row count, or null for no limit.
      offset - The zero-based start offset, or null/0 for none.
      Returns:
      The pagination clause (possibly empty, never null).
    • renderBare

      public SqlFragment renderBare(String columnSql, SearchType type, Object value, boolean quoted)
      Description copied from interface: SqlDialect
      Renders a bare pattern term (design §5.1) — text with optional */? wildcards, a version prefix, or an exact value for the other types — into a predicate.
      Specified by:
      renderBare in interface SqlDialect
      Parameters:
      columnSql - The dialect-quoted column reference. Must not be null.
      type - The column's value type. Must not be null.
      value - The literal's already-typed value (SearchExpression.typedValue()): a BigDecimal for NUMERIC, a Boolean for BOOLEAN, an OffsetDateTime, LocalDateTime or LocalDate for TIMESTAMP, and the already-$$-decoded String for TEXT, ID, ENUM and VERSION. Must not be null.
      quoted - Whether the literal was quoted in the search string; a quoted literal is never given wildcard interpretation, even if it contains * or ?.
      Returns:
      The predicate fragment.