Package st.orm

Record Class SqlFunction<E>

java.lang.Object
java.lang.Record
st.orm.SqlFunction<E>
Type Parameters:
E - the type of the column the function takes and returns.
Record Components:
name - the function's name, a SQL identifier optionally qualified by a schema.

public record SqlFunction<E>(String name) extends Record
A SQL function of one argument, applied to a column wherever a query takes a column. Applied to a path it returns a path that renders as the function's call, so the predicate operators, search, suggest, orderFirst, orderBy, groupBy and a template placeholder take it as they take the column.

Its use is an expression index: the database serves a query that names the expression the index is built on. An index on LOWER(fold_accents(last_name)) serves a search over foldAccents.apply(Owner_.lastName), which compares as LOWER(fold_accents(o.last_name)) LIKE LOWER(fold_accents(?)).


 SqlFunction<String> foldAccents = SqlFunction.of("fold_accents");

 owners.select()
         .where(it -> it.search(input, foldAccents.apply(Owner_.lastName)))
         .getResultList();
 

A comparison applies the function to both sides: to the column, and to the value the column is compared against, so the database computes both and the application computes neither. For the text-matching operators the value is the pattern, escaped and with its wildcards, so a function compared this way must leave the wildcards % and _, SQL Server's [, and the escape character ! as they are.

The path names a single column. A call is not a value the result holds, so a keyset scroll key and a scroll sort field refuse it, and it never equals the path it wraps.

Since:
1.16
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    The call of a function on a path.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an instance of a SqlFunction record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T extends Data>
    Metamodel<T,E>
    apply(Metamodel<T,E> path)
    Returns the call of this function on the given path, which a query takes where it takes the path.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    static boolean
    isCall(Metamodel<?,?> path)
    Returns true if the path is a function call, directly or as the key it is viewed as.
    Returns the value of the name record component.
    static <E> SqlFunction<E>
    of(String name)
    Returns the function of the given name.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • SqlFunction

      public SqlFunction(String name)
      Creates an instance of a SqlFunction record class.
      Parameters:
      name - the value for the name record component
  • Method Details

    • of

      public static <E> SqlFunction<E> of(String name)
      Returns the function of the given name.
      Type Parameters:
      E - the type of the column the function takes and returns.
      Parameters:
      name - the function's name, a SQL identifier optionally qualified by a schema.
      Returns:
      the function.
    • apply

      public <T extends Data> Metamodel<T,E> apply(Metamodel<T,E> path)
      Returns the call of this function on the given path, which a query takes where it takes the path.
      Type Parameters:
      T - the root table type of the path.
      Parameters:
      path - the column to apply the function to.
      Returns:
      the call.
    • isCall

      public static boolean isCall(Metamodel<?,?> path)
      Returns true if the path is a function call, directly or as the key it is viewed as.
      Parameters:
      path - the path to test.
      Returns:
      true for a function call.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • name

      public String name()
      Returns the value of the name record component.
      Returns:
      the value of the name record component