From 614d1c5f023ccc8d0278b81e69b19401d51fb199 Mon Sep 17 00:00:00 2001 From: Tapac Date: Wed, 19 Jun 2019 00:27:22 +0300 Subject: [PATCH] Custom functions description --- Functions.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/Functions.md b/Functions.md index c0ef220..dd0f53c 100644 --- a/Functions.md +++ b/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 In cases when you want to retrieve function result from a query you have to declare function as a variable like: @@ -35,4 +39,32 @@ val (min, max, avg) = FooTable.slice(minId, maxId, averageId).selecAll().map { Triple(it[minId], it[maxId], it[averageId]) } -``` \ No newline at end of file +``` + +## 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("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")) + +```