mirror of
https://github.com/jlengrand/exposed-wiki.git
synced 2026-03-10 08:11:18 +00:00
Custom functions description
This commit is contained in:
committed by
Andrey.Tarashevskiy
parent
b15ca02e44
commit
614d1c5f02
32
Functions.md
32
Functions.md
@@ -1,3 +1,7 @@
|
|||||||
|
* [How to use functions](#how-to-use-functions)
|
||||||
|
* [String functions](#string-functions)
|
||||||
|
* [Aggregating functions](#aggregating-functions)
|
||||||
|
* [Custom functions](#custom-functions)
|
||||||
|
|
||||||
## How to use functions
|
## How to use functions
|
||||||
In cases when you want to retrieve function result from a query you have to declare function as a variable like:
|
In cases when you want to retrieve function result from a query you have to declare function as a variable like:
|
||||||
@@ -36,3 +40,31 @@ val (min, max, avg) = FooTable.slice(minId, maxId, averageId).selecAll().map {
|
|||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 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:
|
||||||
|
1. Function without parameters:
|
||||||
|
```kotlin
|
||||||
|
val sqrt = FooTable.id.function("SQRT")
|
||||||
|
```
|
||||||
|
In SQL representation it will be `SQRT(FooTable.id)`
|
||||||
|
|
||||||
|
2. Function with additional parameters:
|
||||||
|
```kotlin
|
||||||
|
val replacedName = CustomFunction<String?>("REPLACE", VarCharColumnType(), FooTable.name, stringParam("foo"), stringParam("bar"))
|
||||||
|
|
||||||
|
```
|
||||||
|
`CustomFunction` class accept function name as a first parameter and resulting column type as second, after that you can provide any amount of parameters with will be separated by a comma.
|
||||||
|
|
||||||
|
There are also shortcuts for string, long and datetime functions:
|
||||||
|
* `CustomStringFunction`
|
||||||
|
* `CustomLongFunction`
|
||||||
|
* `CustomDateTimeFunction`
|
||||||
|
|
||||||
|
The code above could be simplified to:
|
||||||
|
```kotlin
|
||||||
|
val replacedName = CustomStringFunction("REPLACE", FooTable.name, stringParam("foo"), stringParam("bar"))
|
||||||
|
|
||||||
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user