diff options
Diffstat (limited to 'database')
4 files changed, 40 insertions, 5 deletions
diff --git a/database/core/src/main/kotlin/moe/nea/ledger/database/sql/Clause.kt b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/Clause.kt index 2921d80..f2bf193 100644 --- a/database/core/src/main/kotlin/moe/nea/ledger/database/sql/Clause.kt +++ b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/Clause.kt @@ -2,9 +2,8 @@ package moe.nea.ledger.database.sql interface Clause : BooleanExpression { companion object { - operator fun invoke(builder: ClauseBuilder.() -> Clause): Clause { + operator fun <T> invoke(builder: ClauseBuilder.() -> T): T { return builder(ClauseBuilder()) } } - }
\ No newline at end of file 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 2b141f0..bd16b58 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 @@ -5,7 +5,13 @@ import moe.nea.ledger.database.Column class ClauseBuilder { fun <T> column(column: Column<T>): Operand<T> = ColumnOperand(column) fun string(string: String): StringOperand = StringOperand(string) - infix fun Operand<*>.eq(operand: Operand<*>) = EqualsClause(this, operand) - infix fun Operand<*>.like(op: StringOperand) = LikeClause(this, op) - infix fun Operand<*>.like(op: String) = LikeClause(this, string(op)) + infix fun Operand<*>.eq(operand: Operand<*>): Clause = EqualsClause(this, operand) + infix fun Operand<*>.like(op: StringOperand): Clause = LikeClause(this, op) + infix fun Operand<*>.like(op: String): Clause = LikeClause(this, string(op)) + infix fun Operand<*>.lt(op: Operand<*>): BooleanExpression = LessThanExpression(this, op) + infix fun Operand<*>.le(op: Operand<*>): BooleanExpression = LessThanEqualsExpression(this, op) + infix fun Operand<*>.gt(op: Operand<*>): BooleanExpression = op lt this + infix fun Operand<*>.ge(op: Operand<*>): BooleanExpression = op le this + infix fun BooleanExpression.and(clause: BooleanExpression): BooleanExpression = ANDExpression(listOf(this, clause)) + infix fun BooleanExpression.or(clause: BooleanExpression): BooleanExpression = ORExpression(listOf(this, clause)) }
\ No newline at end of file diff --git a/database/core/src/main/kotlin/moe/nea/ledger/database/sql/LessThanEqualsExpression.kt b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/LessThanEqualsExpression.kt new file mode 100644 index 0000000..db7aa17 --- /dev/null +++ b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/LessThanEqualsExpression.kt @@ -0,0 +1,15 @@ +package moe.nea.ledger.database.sql + +import java.sql.PreparedStatement + +class LessThanEqualsExpression(val lhs: Operand<*>, val rhs: Operand<*>) : + BooleanExpression { + override fun asSql(): String { + return "${lhs.asSql()} <= ${rhs.asSql()}" + } + + override fun appendToStatement(stmt: PreparedStatement, startIndex: Int): Int { + val next = lhs.appendToStatement(stmt, startIndex) + return rhs.appendToStatement(stmt, next) + } +} diff --git a/database/core/src/main/kotlin/moe/nea/ledger/database/sql/LessThanExpression.kt b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/LessThanExpression.kt new file mode 100644 index 0000000..c36a1e8 --- /dev/null +++ b/database/core/src/main/kotlin/moe/nea/ledger/database/sql/LessThanExpression.kt @@ -0,0 +1,15 @@ +package moe.nea.ledger.database.sql + +import java.sql.PreparedStatement + +class LessThanExpression(val lhs: Operand<*>, val rhs: Operand<*>) : + BooleanExpression { + override fun asSql(): String { + return "${lhs.asSql()} < ${rhs.asSql()}" + } + + override fun appendToStatement(stmt: PreparedStatement, startIndex: Int): Int { + val next = lhs.appendToStatement(stmt, startIndex) + return rhs.appendToStatement(stmt, next) + } +} |