Skip to content

Commit 6b2cc60

Browse files
mysql datediff function
1 parent 28e60eb commit 6b2cc60

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ buildscript {
1111

1212
allprojects {
1313
group = "me.liuwj.ktorm"
14-
version = "2.0"
14+
version = "2.1"
1515
}
1616

1717
subprojects { project ->

hexo-docs/source/en/dialects-and-native-sql.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Here is a list of features provided by module ktorm-support-mysql:
9595
- Add `naturalJoin` function for natural joining, based on `natural join` keyword.
9696
- Add `jsonContains` function to determine if the specific item exists in a json array, based on the `json_contains` function in MySQL.
9797
- Add `jsonExtract` function to obtain fields in a json, that's the `->` grammar in MySQL, based on `json_extract` function.
98-
- Add other functions such as `rand`, `ifnull`, `greatest`, `least`, etc, supporting the corresponding functions in MySQL.
98+
- Add other functions such as `rand`, `ifnull`, `greatest`, `least`, `dateDiff`, etc, supporting the corresponding functions in MySQL.
9999

100100
The features of ktorm-support-postgresql are listed below:
101101

hexo-docs/source/zh-cn/dialects-and-native-sql.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ on duplicate key update t_employee.salary = t_employee.salary + ?
9595
- 增加了 `naturalJoin` 函数,支持自然连接,基于 `natural join` 关键字
9696
- 增加了 `jsonContains` 函数,判断 json 数组中是否存在指定元素,基于 `json_contains` 函数
9797
- 增加了 `jsonExtract` 函数,支持从 json 中获取字段,即 MySQL 中的 -> 语法,基于 `json_extract` 函数
98-
- 增加了 `rand``ifnull``greatest``least` 等函数,支持 MySQL 中的同名函数
98+
- 增加了 `rand``ifnull``greatest``least``dateDiff` 等函数,支持 MySQL 中的同名函数
9999

100100
ktorm-support-postgresql 提供的功能有:
101101

ktorm-support-mysql/src/main/kotlin/me/liuwj/ktorm/support/mysql/Functions.kt

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package me.liuwj.ktorm.support.mysql
33
import me.liuwj.ktorm.expression.ArgumentExpression
44
import me.liuwj.ktorm.expression.FunctionExpression
55
import me.liuwj.ktorm.schema.*
6+
import java.time.LocalDate
67

78
fun <T : Any> Column<List<T>>.jsonContains(item: T, itemSqlType: SqlType<T>): FunctionExpression<Boolean> {
89
val listSqlType = this.sqlType
@@ -101,4 +102,21 @@ fun <T : Any> ColumnDeclaring<T>.ifNull(right: ColumnDeclaring<T>): FunctionExpr
101102

102103
fun <T : Any> ColumnDeclaring<T>.ifNull(right: T?): FunctionExpression<T> {
103104
return this.ifNull(wrapArgument(right))
105+
}
106+
107+
fun dateDiff(left: ColumnDeclaring<LocalDate>, right: ColumnDeclaring<LocalDate>): FunctionExpression<Int> {
108+
// datediff(left, right)
109+
return FunctionExpression(
110+
functionName = "datediff",
111+
arguments = listOf(left.asExpression(), right.asExpression()),
112+
sqlType = IntSqlType
113+
)
114+
}
115+
116+
fun dateDiff(left: ColumnDeclaring<LocalDate>, right: LocalDate): FunctionExpression<Int> {
117+
return dateDiff(left, left.wrapArgument(right))
118+
}
119+
120+
fun dateDiff(left: LocalDate, right: ColumnDeclaring<LocalDate>): FunctionExpression<Int> {
121+
return dateDiff(right.wrapArgument(left), right)
104122
}

ktorm-support-mysql/src/test/kotlin/me/liuwj/ktorm/support/mysql/MySqlTest.kt

-10
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import me.liuwj.ktorm.database.Database
55
import me.liuwj.ktorm.database.useConnection
66
import me.liuwj.ktorm.dsl.*
77
import me.liuwj.ktorm.entity.*
8-
import me.liuwj.ktorm.expression.FunctionExpression
9-
import me.liuwj.ktorm.schema.ColumnDeclaring
10-
import me.liuwj.ktorm.schema.IntSqlType
118
import org.junit.Test
129
import java.time.LocalDate
1310

@@ -183,13 +180,6 @@ class MySqlTest : BaseTest() {
183180

184181
@Test
185182
fun testMapColumns3() {
186-
// MySQL datediff function
187-
fun dateDiff(left: LocalDate, right: ColumnDeclaring<LocalDate>) = FunctionExpression(
188-
functionName = "datediff",
189-
arguments = listOf(right.wrapArgument(left), right.asExpression()),
190-
sqlType = IntSqlType
191-
)
192-
193183
Employees
194184
.asSequence()
195185
.filter { it.departmentId eq 1 }

0 commit comments

Comments
 (0)