diff options
Diffstat (limited to 'database/core')
5 files changed, 34 insertions, 2 deletions
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 <T, R> column(column: Column<T, R>): Operand<T, R> = ColumnOperand(column) + fun <T, R> column(column: Column<T, R>): ColumnOperand<T, R> = ColumnOperand(column) fun string(string: String): StringOperand = StringOperand(string) + fun <T, R> value(dbType: DBType<T, R>, value: T): Operand<T, R> = ValuedOperand(dbType, value) infix fun <T> Operand<*, T>.eq(operand: Operand<*, T>): Clause = EqualsClause(this, operand) + infix fun <T, R> TypedOperand<T, R>.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 <T> 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<T, Raw>(val column: Column<T, Raw>) : Operand<T, Raw> { +data class ColumnOperand<T, Raw>(val column: Column<T, Raw>) : TypedOperand<T, Raw>() { + override val dbType: DBType<T, Raw> + 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<String, String> { 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<T, Raw> : Operand<T, Raw> { + abstract val dbType: DBType<T, Raw> +} 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<T, R>(val dbType: DBType<T, R>, val value: T) : Operand<T, R> { + override fun asSql(): String { + return "?" + } + + override fun appendToStatement(stmt: PreparedStatement, startIndex: Int): Int { + dbType.set(stmt, startIndex, value) + return startIndex + 1 + } +} |