-
-
Notifications
You must be signed in to change notification settings - Fork 154
Add case when support #413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
/* | ||
* Copyright 2018-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.ktorm.dsl | ||
|
||
import org.ktorm.expression.ArgumentExpression | ||
import org.ktorm.expression.CaseWhenExpression | ||
import org.ktorm.expression.ScalarExpression | ||
import org.ktorm.schema.BooleanSqlType | ||
import org.ktorm.schema.ColumnDeclaring | ||
import org.ktorm.schema.SqlType | ||
|
||
/** | ||
* case when of case Expression. | ||
* [caseValue] case value, may be null | ||
* [caseSqlType] case value sqlType | ||
* | ||
* if [caseValue] is null, then [caseSqlType] is [BooleanSqlType] | ||
*/ | ||
public data class CaseExpression<T : Any> internal constructor( | ||
internal val caseValue: ScalarExpression<T>? = null, | ||
internal val caseSqlType: SqlType<T>, | ||
) | ||
|
||
/** | ||
* case when of when Expression. | ||
* | ||
* [caseValue] case value, may be null | ||
* [condition] when condition | ||
* [caseWhenExpression], the [CaseWhenExpression] when build [WhenExpression] | ||
* from [CaseWhenExpression], may be bull | ||
*/ | ||
public data class WhenExpression<T : Any, V : Any> internal constructor( | ||
internal val caseValue: ScalarExpression<T>?, | ||
internal val condition: ScalarExpression<T>, | ||
internal val caseWhenExpression: CaseWhenExpression<T, V>? = null, | ||
) | ||
|
||
/** | ||
* DSL to build [CaseExpression] without [CaseExpression.caseValue]. | ||
*/ | ||
@Suppress("FunctionName") | ||
public fun CASE(): CaseExpression<Boolean> { | ||
return CaseExpression(null, BooleanSqlType) | ||
} | ||
|
||
/** | ||
* DSL to build [CaseExpression] with [CaseExpression.caseValue]. | ||
*/ | ||
@Suppress("FunctionName") | ||
public fun <T : Any> CASE(caseValue: ColumnDeclaring<T>): CaseExpression<T> { | ||
return CaseExpression(caseValue.asExpression(), caseValue.sqlType) | ||
} | ||
|
||
/** | ||
* DSL to build [WhenExpression] from [CaseExpression]. | ||
*/ | ||
@Suppress("FunctionName") | ||
public infix fun <T : Any> CaseExpression<T>.WHEN(condition: ColumnDeclaring<T>): WhenExpression<T, Nothing> { | ||
return WhenExpression( | ||
caseValue = this.caseValue, | ||
condition = condition.asExpression(), | ||
caseWhenExpression = null | ||
) | ||
} | ||
|
||
/** | ||
* DSL to build [WhenExpression] from [CaseExpression]. | ||
*/ | ||
@Suppress("FunctionName") | ||
public infix fun <T : Any> CaseExpression<T>.WHEN(condition: T): WhenExpression<T, Nothing> { | ||
return WhenExpression( | ||
this.caseValue, | ||
ArgumentExpression(condition, this.caseSqlType), | ||
caseWhenExpression = null | ||
) | ||
} | ||
|
||
/** | ||
* DSL to build [CaseWhenExpression] from [WhenExpression]. | ||
* | ||
* [value] the value of then branch | ||
*/ | ||
@JvmName("_THEN") | ||
@Suppress("FunctionName") | ||
public infix fun <T : Any, V : Any> WhenExpression<T, V>.THEN(value: V): CaseWhenExpression<T, V> { | ||
return this.caseWhenExpression!!.copy( | ||
whenThenConditions = this.caseWhenExpression.whenThenConditions + | ||
(this.condition to | ||
ArgumentExpression( | ||
value, | ||
this.caseWhenExpression.sqlType | ||
)) | ||
) | ||
} | ||
|
||
/** | ||
* DSL to build [CaseWhenExpression] from [WhenExpression]. | ||
* | ||
* [value] the value of then branch | ||
*/ | ||
@JvmName("_THEN") | ||
@Suppress("FunctionName") | ||
public infix fun <T : Any, V : Any> WhenExpression<T, V>.THEN(value: ColumnDeclaring<V>): CaseWhenExpression<T, V> { | ||
return this.caseWhenExpression!!.copy( | ||
whenThenConditions = this.caseWhenExpression.whenThenConditions + | ||
(this.condition to value.asExpression()) | ||
) | ||
} | ||
|
||
/** | ||
* DSL to build [CaseWhenExpression] from [WhenExpression]. | ||
* | ||
* [value] the value of then branch | ||
*/ | ||
@Suppress("FunctionName") | ||
public infix fun <T : Any, V : Any> WhenExpression<T, Nothing>.THEN( | ||
value: ColumnDeclaring<V>, | ||
): CaseWhenExpression<T, V> { | ||
return CaseWhenExpression( | ||
caseExpr = this.caseValue, | ||
whenThenConditions = listOf(this.condition to value.asExpression()), | ||
whenSqlType = this.condition.sqlType, | ||
sqlType = value.sqlType, | ||
) | ||
} | ||
|
||
/** | ||
* DSL to build [CaseWhenExpression] from [CaseWhenExpression] with else branch. | ||
* [value] the value of else branch | ||
*/ | ||
@Suppress("FunctionName") | ||
public infix fun <T : Any, V : Any> CaseWhenExpression<T, V>.ELSE(value: ColumnDeclaring<V>): CaseWhenExpression<T, V> { | ||
return this.copy(elseExpr = value.asExpression()) | ||
} | ||
|
||
/** | ||
* DSL to build [CaseWhenExpression] from [CaseWhenExpression] with else branch. | ||
* | ||
* [value] the value of else branch | ||
*/ | ||
@Suppress("FunctionName") | ||
public infix fun <T : Any, V : Any> CaseWhenExpression<T, V>.ELSE(value: V): CaseWhenExpression<T, V> { | ||
return this.copy(elseExpr = ArgumentExpression(value, this.sqlType)) | ||
} | ||
|
||
/** | ||
* DSL to build [WhenExpression] from [CaseWhenExpression]. | ||
* | ||
* [condition] the when condition | ||
*/ | ||
@Suppress("FunctionName") | ||
public infix fun <T : Any, V : Any> CaseWhenExpression<T, V>.WHEN(condition: ColumnDeclaring<T>): WhenExpression<T, V> { | ||
return WhenExpression( | ||
caseWhenExpression = this, | ||
condition = condition.asExpression(), | ||
caseValue = null | ||
) | ||
} | ||
|
||
/** | ||
* DSL to build [WhenExpression] from [CaseWhenExpression]. | ||
* | ||
* [condition] the when condition | ||
*/ | ||
@Suppress("FunctionName") | ||
public infix fun <T : Any, V : Any> CaseWhenExpression<T, V>.WHEN(condition: T): WhenExpression<T, V> { | ||
return WhenExpression( | ||
caseWhenExpression = this, | ||
condition = ArgumentExpression(condition, this.whenSqlType), | ||
caseValue = null, | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -537,6 +537,28 @@ public data class BetweenExpression<T : Any>( | |
override val extraProperties: Map<String, Any> = emptyMap() | ||
) : ScalarExpression<Boolean>() | ||
|
||
/** | ||
* The CASE statement goes through conditions and returns a value when the | ||
* first condition is met (like an if-then-else statement). So, once a condition | ||
* is true, it will stop reading and return the result. If no conditions are true, | ||
* it returns the value in the ELSE clause. | ||
* If there is no ELSE part and no conditions are true, it returns NULL. | ||
* | ||
* @property whenThenConditions "when conditions then value" statements. | ||
* @property elseExpr else statements. | ||
* @property caseExpr case statements, optional, may be null. | ||
* @property sqlType the argument's [SqlType]. | ||
*/ | ||
public data class CaseWhenExpression<V : Any, T : Any> constructor( | ||
val caseExpr: ScalarExpression<V>? = null, | ||
val whenThenConditions: List<Pair<ScalarExpression<V>, ScalarExpression<T>>>, | ||
val elseExpr: ScalarExpression<T>? = null, | ||
internal val whenSqlType: SqlType<V>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
override val sqlType: SqlType<T>, | ||
override val isLeafNode: Boolean = true, | ||
override val extraProperties: Map<String, Any> = emptyMap(), | ||
) : ScalarExpression<T>() | ||
|
||
/** | ||
* Argument expression, wraps an argument passed to the executed SQL. | ||
* | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.