From 5a49c878ede9df81417c79e98d226c941551f3c0 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Wed, 22 Jan 2025 01:42:48 +0100 Subject: refactor: Allow accessing typed operands more easily --- .../kotlin/moe/nea/ledger/database/sql/ClauseBuilder.kt | 5 ++++- .../kotlin/moe/nea/ledger/database/sql/ColumnOperand.kt | 6 +++++- .../kotlin/moe/nea/ledger/database/sql/StringOperand.kt | 3 +++ .../kotlin/moe/nea/ledger/database/sql/TypedOperand.kt | 7 +++++++ .../kotlin/moe/nea/ledger/database/sql/ValuedOperand.kt | 15 +++++++++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 database/core/src/main/kotlin/moe/nea/ledger/database/sql/TypedOperand.kt create mode 100644 database/core/src/main/kotlin/moe/nea/ledger/database/sql/ValuedOperand.kt (limited to 'database') diff --git a/database/core/src/main/kotlin/moe/nea/ledger/database/sql/ClauseBuilder.kt b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/ClauseBuilder.kt index f4fc478..aae8456 100644 --- a/database/core/src/main/kotlin/moe/nea/ledger/database/sql/ClauseBuilder.kt +++ b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/ClauseBuilder.kt @@ -1,11 +1,14 @@ package moe.nea.ledger.database.sql import moe.nea.ledger.database.Column +import moe.nea.ledger.database.DBType class ClauseBuilder { - fun column(column: Column): Operand = ColumnOperand(column) + fun column(column: Column): ColumnOperand = ColumnOperand(column) fun string(string: String): StringOperand = StringOperand(string) + fun value(dbType: DBType, value: T): Operand = ValuedOperand(dbType, value) infix fun Operand<*, T>.eq(operand: Operand<*, T>): Clause = EqualsClause(this, operand) + infix fun TypedOperand.eq(value: T): Clause = EqualsClause(this, value(dbType, value)) infix fun Operand<*, String>.like(op: StringOperand): Clause = LikeClause(this, op) infix fun Operand<*, String>.like(op: String): Clause = LikeClause(this, string(op)) infix fun Operand<*, T>.lt(op: Operand<*, T>): BooleanExpression = LessThanExpression(this, op) diff --git a/database/core/src/main/kotlin/moe/nea/ledger/database/sql/ColumnOperand.kt b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/ColumnOperand.kt index d06d834..710c577 100644 --- a/database/core/src/main/kotlin/moe/nea/ledger/database/sql/ColumnOperand.kt +++ b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/ColumnOperand.kt @@ -1,9 +1,13 @@ package moe.nea.ledger.database.sql import moe.nea.ledger.database.Column +import moe.nea.ledger.database.DBType import java.sql.PreparedStatement -data class ColumnOperand(val column: Column) : Operand { +data class ColumnOperand(val column: Column) : TypedOperand() { + override val dbType: DBType + get() = column.type + override fun asSql(): String { return column.qualifiedSqlName } diff --git a/database/core/src/main/kotlin/moe/nea/ledger/database/sql/StringOperand.kt b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/StringOperand.kt index e7ede6a..b8d3690 100644 --- a/database/core/src/main/kotlin/moe/nea/ledger/database/sql/StringOperand.kt +++ b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/StringOperand.kt @@ -2,6 +2,9 @@ package moe.nea.ledger.database.sql import java.sql.PreparedStatement +/** + * As opposed to just any [Operand<*, String>][Operand], this string operand represents a string operand that is part of the query, as opposed to potentially the state of a column. + */ data class StringOperand(val value: String) : Operand { override fun asSql(): String { return "?" diff --git a/database/core/src/main/kotlin/moe/nea/ledger/database/sql/TypedOperand.kt b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/TypedOperand.kt new file mode 100644 index 0000000..57efe58 --- /dev/null +++ b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/TypedOperand.kt @@ -0,0 +1,7 @@ +package moe.nea.ledger.database.sql + +import moe.nea.ledger.database.DBType + +abstract class TypedOperand : Operand { + abstract val dbType: DBType +} diff --git a/database/core/src/main/kotlin/moe/nea/ledger/database/sql/ValuedOperand.kt b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/ValuedOperand.kt new file mode 100644 index 0000000..714b4b5 --- /dev/null +++ b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/ValuedOperand.kt @@ -0,0 +1,15 @@ +package moe.nea.ledger.database.sql + +import moe.nea.ledger.database.DBType +import java.sql.PreparedStatement + +class ValuedOperand(val dbType: DBType, val value: T) : Operand { + override fun asSql(): String { + return "?" + } + + override fun appendToStatement(stmt: PreparedStatement, startIndex: Int): Int { + dbType.set(stmt, startIndex, value) + return startIndex + 1 + } +} -- cgit