Nullable types

This commit is contained in:
Andrew Golovashevich 2024-12-04 06:07:53 +03:00
parent 042bf20444
commit ac7df3393f
11 changed files with 36 additions and 42 deletions

View File

@ -2,9 +2,9 @@ package ru.landgrafhomyak.serdha.api.v0
import ru.landgrafhomyak.serdha.api.v0.ddl.ColumnType
public interface Expression<RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>, OwnerBuilderUserExtension : Any> {
public interface Expression<RuntimeType, DatabaseType : ColumnType<RuntimeType>, OwnerBuilderUserExtension : Any> {
public interface Builder<OwnerBuilderUserExtension : Any> {
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> equals(
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> equals(
left: Expression<RuntimeType, DatabaseType, OwnerBuilderUserExtension>,
right: Expression<RuntimeType, DatabaseType, OwnerBuilderUserExtension>
): Expression<Boolean, ColumnType.BOOLEAN, OwnerBuilderUserExtension>

View File

@ -1,6 +1,6 @@
package ru.landgrafhomyak.serdha.api.v0.ddl
public interface Column<RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>, TableUserExtension : Any> {
public interface Column<RuntimeType, DatabaseType : ColumnType<RuntimeType>, TableUserExtension : Any> {
public val name: String
public val type: DatabaseType

View File

@ -7,17 +7,26 @@ import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.LocalTime
@Suppress("ClassName")
public abstract class ColumnType<RuntimeType : Any> {
public abstract class ColumnType<RuntimeType> {
public abstract val typeName: String
abstract override fun toString(): String
public sealed class _StandardType<R : Any> : ColumnType<R>() {
public sealed class _StandardType<R> : ColumnType<R>() {
override fun toString(): String = "<serdha | column type '${this.typeName}'>"
}
public abstract class _VirtualType<R : Any>(public val wraps: ColumnType<R>) {
override fun toString(): String = "<serdha | virtual column type 'ROW_ID'>"
public abstract class _VirtualType<R>(@Suppress("MemberVisibilityCanBePrivate") public val wraps: ColumnType<R>) {
override fun toString(): String = "<serdha | virtual column type '${this.wraps.typeName}'>"
}
public class NULLABLE<RuntimeType : Any>(@Suppress("MemberVisibilityCanBePrivate") public val notNullType: ColumnType<RuntimeType>) : _StandardType<RuntimeType?>() {
init {
if (this.notNullType is NULLABLE<*>)
throw IllegalArgumentException("Type can't be nullable twice")
}
override val typeName: String = this.notNullType.typeName + "?"
}
public object ROW_ID : _StandardType<RowId<*>>() {

View File

@ -1,3 +1,4 @@
package ru.landgrafhomyak.serdha.api.v0.ddl
public interface RowId<OwnerTableUserExtension: Any>
public interface RowId<@Suppress("unused") OwnerTableUserExtension: Any>

View File

@ -13,22 +13,8 @@ public interface TableCreator<TableUserExtension : Any> {
*/
public fun <RuntimeType : Any, DatabaseType : ColumnType<RuntimeType>> column(name: String, type: DatabaseType): Column<RuntimeType, DatabaseType, TableUserExtension>
@Suppress("INAPPLICABLE_JVM_NAME")
@JvmName("nullableColumn\$notNull")
@Deprecated("This column can be not-null", replaceWith = ReplaceWith("this.column"))
public fun <RuntimeType : Any, DatabaseType : ColumnType<RuntimeType>> nullableColumn(name: String, type: DatabaseType): Column<RuntimeType?, DatabaseType, TableUserExtension> =
this.nullableColumn<RuntimeType?, DatabaseType>(name, type)
/**
* Offers column of type [D][type] named [name] and runtime type [R?][RuntimeType].
*
* @return Descriptor of offered column for future operations.
*/
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> nullableColumn(name: String, type: DatabaseType): Column<RuntimeType?, DatabaseType, TableUserExtension>
public fun index(name: String, vararg columns: Column<*, *, TableUserExtension>): Index<TableUserExtension>
// todo not-null column uniqueness
public fun unique(name: String, distinctNulls: Boolean, vararg columns: Column<*, *, TableUserExtension>): UniqueConstraint<TableUserExtension>
public fun check(name: String, constraint: Expression<Boolean, ColumnType.BOOLEAN, TableUserExtension>): CheckConstraint<TableUserExtension>

View File

@ -4,10 +4,10 @@ import ru.landgrafhomyak.serdha.api.v0.Expression
public interface TableUpdater<TableNewUserExtension : Any, TableOldUserExtension : Any> : TableCreator<TableNewUserExtension> {
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> keepColumn(c: Column<RuntimeType, DatabaseType, TableOldUserExtension>): Column<RuntimeType, DatabaseType, TableNewUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> renameColumn(c: Column<RuntimeType, DatabaseType, TableOldUserExtension>, newName: String): Column<RuntimeType, DatabaseType, TableNewUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> mapColumn(c: Column<RuntimeType, DatabaseType, TableNewUserExtension>, newValue: Expression<RuntimeType, DatabaseType, TableNewUserExtension>, where: Expression<Boolean, ColumnType.BOOLEAN, TableNewUserExtension>?)
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> mapColumn(c: Column<RuntimeType, DatabaseType, TableNewUserExtension>, newValue: Expression<RuntimeType, DatabaseType, TableNewUserExtension>): Unit = this.mapColumn(c, newValue, null)
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> keepColumn(c: Column<RuntimeType, DatabaseType, TableOldUserExtension>): Column<RuntimeType, DatabaseType, TableNewUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> renameColumn(c: Column<RuntimeType, DatabaseType, TableOldUserExtension>, newName: String): Column<RuntimeType, DatabaseType, TableNewUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> mapColumn(c: Column<RuntimeType, DatabaseType, TableNewUserExtension>, newValue: Expression<RuntimeType, DatabaseType, TableNewUserExtension>, where: Expression<Boolean, ColumnType.BOOLEAN, TableNewUserExtension>?)
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> mapColumn(c: Column<RuntimeType, DatabaseType, TableNewUserExtension>, newValue: Expression<RuntimeType, DatabaseType, TableNewUserExtension>): Unit = this.mapColumn(c, newValue, null)
public fun deleteColumn(c: Column<*, *, TableOldUserExtension>)
public fun keepIndex(i: Index<TableOldUserExtension>): Index<TableNewUserExtension>

View File

@ -3,7 +3,7 @@ package ru.landgrafhomyak.serdha.api.v0.dml
import ru.landgrafhomyak.serdha.api.v0.Expression
import ru.landgrafhomyak.serdha.api.v0.ddl.ColumnType
public interface InputParam<RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>, OwnerQueryUserExtension : Any> : Expression<RuntimeType, DatabaseType, OwnerQueryUserExtension> {
public interface InputParam<RuntimeType, DatabaseType : ColumnType<RuntimeType>, OwnerQueryUserExtension : Any> : Expression<RuntimeType, DatabaseType, OwnerQueryUserExtension> {
public val name: String
public val userWrapper: OwnerQueryUserExtension
}

View File

@ -10,9 +10,9 @@ public interface InsertCreator<TargetTableUserExtension : Any, QueryUserExtensio
@Suppress("ClassName")
public interface _UpsertCreator<TargetTableUserExtension : Any, QueryUserExtension : Any> {
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> oldColumnValue(c: Column<RuntimeType, DatabaseType, TargetTableUserExtension>): Expression<RuntimeType, DatabaseType, QueryUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> oldColumnValue(c: Column<RuntimeType, DatabaseType, TargetTableUserExtension>): Expression<RuntimeType, DatabaseType, QueryUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> updateColumn(c: Column<RuntimeType, DatabaseType, TargetTableUserExtension>, e: Expression<RuntimeType, DatabaseType, QueryUserExtension>)
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> updateColumn(c: Column<RuntimeType, DatabaseType, TargetTableUserExtension>, e: Expression<RuntimeType, DatabaseType, QueryUserExtension>)
}
public fun onConflictUpdate(u: UniqueConstraint<TargetTableUserExtension>, c: (_UpsertCreator<TargetTableUserExtension, QueryUserExtension>) -> Unit)
@ -25,7 +25,7 @@ public interface InsertCreator<TargetTableUserExtension : Any, QueryUserExtensio
@Suppress("ClassName")
public interface _ReturningUpdated<TargetTableUserExtension : Any, QueryUserExtension : Any> {
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> oldColumnValue(c: Column<RuntimeType, DatabaseType, TargetTableUserExtension>): Expression<RuntimeType, DatabaseType, QueryUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> oldColumnValue(c: Column<RuntimeType, DatabaseType, TargetTableUserExtension>): Expression<RuntimeType, DatabaseType, QueryUserExtension>
}
public fun returningUpdated(s: (_ReturningUpdated<TargetTableUserExtension, QueryUserExtension>) -> Unit)
@ -35,19 +35,18 @@ public interface InsertCreator<TargetTableUserExtension : Any, QueryUserExtensio
public val dataExpressionBuilder: Expression.Builder<DataParam<QueryUserExtension>>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> dataParam(name: String, type: DatabaseType): InputParam<RuntimeType, DatabaseType, DataParam<QueryUserExtension>>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> nullableDataParam(name: String, type: DatabaseType): InputParam<RuntimeType?, DatabaseType, DataParam<QueryUserExtension>>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> dataParam(name: String, type: DatabaseType): InputParam<RuntimeType, DatabaseType, DataParam<QueryUserExtension>>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> insertParam(
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> insertParam(
column: Column<RuntimeType, DatabaseType, TargetTableUserExtension>,
paramName: String = column.name
): InputParam<RuntimeType, DatabaseType, DataParam<QueryUserExtension>>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> insert(column: Column<RuntimeType, DatabaseType, TargetTableUserExtension>, expression: Expression<RuntimeType, DatabaseType, DataParam<QueryUserExtension>>)
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> insert(column: Column<RuntimeType, DatabaseType, TargetTableUserExtension>, expression: Expression<RuntimeType, DatabaseType, DataParam<QueryUserExtension>>)
}
public interface InsertFromQuery<TargetTableUserExtension : Any, QueryUserExtension : Any> : InsertCreator<TargetTableUserExtension, QueryUserExtension> {
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> insert(column: Column<RuntimeType, DatabaseType, TargetTableUserExtension>, expression: Expression<RuntimeType, DatabaseType, QueryUserExtension>)
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> insert(column: Column<RuntimeType, DatabaseType, TargetTableUserExtension>, expression: Expression<RuntimeType, DatabaseType, QueryUserExtension>)
}
}

View File

@ -5,5 +5,5 @@ import ru.landgrafhomyak.serdha.api.v0.ddl.Column
import ru.landgrafhomyak.serdha.api.v0.ddl.ColumnType
public interface SelectedTable<SelectedTableUserExtension : Any, QueryUserExtension : Any> {
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> selectColumn(column: Column<RuntimeType, DatabaseType, SelectedTableUserExtension>): Expression<RuntimeType, DatabaseType, QueryUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> selectColumn(column: Column<RuntimeType, DatabaseType, SelectedTableUserExtension>): Expression<RuntimeType, DatabaseType, QueryUserExtension>
}

View File

@ -8,9 +8,9 @@ import ru.landgrafhomyak.serdha.api.v0.ddl.Table
public interface UpdateCreator<TargetTableUserExtension : Any, QueryUserExtension : Any> : _CommonQueryMethods<QueryUserExtension> {
public val targetTable: SelectedTable<TargetTableUserExtension, QueryUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> selectOldColumnValue(c: Column<RuntimeType, DatabaseType, TargetTableUserExtension>): Expression<RuntimeType, DatabaseType, QueryUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> selectOldColumnValue(c: Column<RuntimeType, DatabaseType, TargetTableUserExtension>): Expression<RuntimeType, DatabaseType, QueryUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> updateColumn(c: Column<RuntimeType, DatabaseType, TargetTableUserExtension>, e: Expression<RuntimeType, DatabaseType, QueryUserExtension>)
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> updateColumn(c: Column<RuntimeType, DatabaseType, TargetTableUserExtension>, e: Expression<RuntimeType, DatabaseType, QueryUserExtension>)
public fun where(expression: Expression<Boolean, ColumnType.BOOLEAN, QueryUserExtension>)

View File

@ -13,18 +13,17 @@ public interface _CommonQueryMethods<QueryUserExtension : Any> {
public interface CanBeSubquery<@Suppress("unused") SelectedQueryUserExtension : Any>
public interface SubqueryParametersSetter<SubqueryUserExtension : Any, QueryUserExtension : Any> : ParametersSetter<SubqueryUserExtension> {
public operator fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> set(c: InputParam<RuntimeType, DatabaseType, QueryUserExtension>, value: Expression<RuntimeType, DatabaseType, QueryUserExtension>)
public operator fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> set(c: InputParam<RuntimeType, DatabaseType, QueryUserExtension>, value: Expression<RuntimeType, DatabaseType, QueryUserExtension>)
}
public fun <SubqueryUserExtension : Any> selectingQuery(q: CanBeSubquery<SubqueryUserExtension>, p: (SubqueryParametersSetter<SubqueryUserExtension, QueryUserExtension>) -> Unit): SelectedTable<SubqueryUserExtension, QueryUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> param(name: String, type: DatabaseType): InputParam<RuntimeType, DatabaseType, QueryUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> nullableParam(name: String, type: DatabaseType): InputParam<RuntimeType?, DatabaseType, QueryUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> param(name: String, type: DatabaseType): InputParam<RuntimeType, DatabaseType, QueryUserExtension>
public val expressionBuilder: Expression.Builder<QueryUserExtension>
public interface _Returning<QueryUserExtension : Any> {
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType & Any>> returnExpression(expression: Expression<RuntimeType, DatabaseType, QueryUserExtension>): Column<RuntimeType, DatabaseType, QueryUserExtension>
public fun <RuntimeType, DatabaseType : ColumnType<RuntimeType>> returnExpression(expression: Expression<RuntimeType, DatabaseType, QueryUserExtension>): Column<RuntimeType, DatabaseType, QueryUserExtension>
public val isDistinct: Boolean