SQL Functions
Exposed provides basic support for classic SQL functions. This topic consists of definitions for those functions, and their usage examples. It also explains how to define custom functions.
How to use functions
If you want to retrieve a function result from a query, you have to declare the function as a variable:
Also, functions could be chained and combined:
String functions
LowerCase/UpperCase
Returns a lower-cased/upper-cased string value.
Substring
Returns a substring value from the specified start and with the specified length.
Concat
Returns a string value that concatenates the text representations of all non-null input values, separated by an optional separator.
Locate
Returns the index of the first occurrence of a specified substring or 0.
CharLength
Returns the length, measured in characters, or null
if the String value is null.
Aggregating functions
These functions should be used in queries with groupBy.
Min/Max/Average
Returns minimum/maximum/average value and can be applied to any comparable expression:
Custom functions
If you can't find your most loved function used in your database (as Exposed provides only basic support for classic SQL functions), you can define your own functions.
Since Exposed 0.15.1 there multiple options to define custom functions:
Function without parameters:
In SQL representation it will be SQRT(FooTable.id)
Function with additional parameters:
CustomFunction
class accepts a function name as a first parameter and the resulting column type as second. After that, you can provide any amount of parameters separated by a comma.
There are also shortcuts for string, long, and datetime functions:
CustomStringFunction
CustomLongFunction
CustomDateTimeFunction
The code above could be simplified to:
For example, the following could be used in SQLite to mimic its date()
function:
Function that requires more complex query building:
All functions in Exposed extend the abstract class Function
, which takes a column type and allows overriding toQueryBuilder()
. This is what CustomFunction
actually does, which can be leveraged to create more complex queries.
For example, Exposed provides a trim()
function that removes leading and trailing whitespace from a String. In MySQL, this is just the default behavior as specifiers can be provided to limit the trim to either leading or trailing, as well as providing a specific substring other than spaces to remove. The custom function below supports this extended behavior:
Window Functions
Window functions allow calculations across a set of table rows that are related to the current row.
Existing aggregate functions (like sum()
, avg()
) can be used, as well as new rank and value functions:
cumeDist()
denseRank()
firstValue()
lag()
lastValue()
lead()
nthValue()
nTile()
percentRank()
rank()
rowNumber()
To use a window function, include the OVER
clause by chaining .over()
after the function call. A PARTITION BY
and ORDER BY
clause can be optionally chained using .partitionBy()
and .orderBy()
, which both take multiple arguments:
Frame clause functions (like rows()
, range()
, and groups()
) are also supported and take a WindowFrameBound
option depending on the expected result:
WindowFrameBound.currentRow()
WindowFrameBound.unboundedPreceding()
WindowFrameBound.unboundedFollowing()
WindowFrameBound.offsetPreceding()
WindowFrameBound.offsetFollowing()