Skip to content

Commit

Permalink
test more edge-cases on empty values on update
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Coser committed Jun 27, 2023
1 parent 776a7db commit be5e15b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/main/kotlin/jako/dsl/update/SetFields.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ internal class SetFields: StatementBlock {
return this
}

override fun toSQL(dialect: Dialect) = if (cols.isNotEmpty()) "SET " + cols.joinToString(", ") { it.toSQL(dialect)} else ""
override fun params() = cols.flatMap { it.params() }
override fun isPresent() = cols.any { it.isPresent() }
override fun toSQL(dialect: Dialect) = "SET " + presentCols().joinToString(", ") { it.toSQL(dialect)}
override fun params() = presentCols().flatMap { it.params() }
override fun isPresent() = presentCols().isNotEmpty()

private fun presentCols() = cols.filter { it.isPresent() }
}
4 changes: 2 additions & 2 deletions src/main/kotlin/jako/dsl/update/Update.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.sql.Date
import java.time.LocalDate

class Update: StatementBuilder() {
private var table: UpdateTable? = null
private var table: UpdateTable = UpdateTable("")
private val fields: SetFields = SetFields()
private var where: Where = NoWhere()

Expand Down Expand Up @@ -37,7 +37,7 @@ class Update: StatementBuilder() {

private fun fieldsOrThrow() = if(fields.isPresent()) fields else throw RuntimeException("Cannot generate update without values")

private fun updateTableOrThrow() = table ?: throw RuntimeException("Cannot generate update without table name")
private fun updateTableOrThrow() = if(table.isPresent()) table else throw RuntimeException("Cannot generate update without table name")

companion object {
fun table(name: String): Update {
Expand Down
22 changes: 19 additions & 3 deletions src/test/kotlin/jako/dsl/update/UpdateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,28 @@ class UpdateTest {

@Test
fun `update with where statement `() {
val insert = Update()
val update = Update()
.table("table")
.set("column1", 0)
.where("column1" GT 10)

assertEquals("""UPDATE "table" SET "column1" = ? WHERE "column1" > ?""", insert.toSQL(PSQL))
assertEquals(listOf(0, 10), insert.params())
assertEquals("""UPDATE "table" SET "column1" = ? WHERE "column1" > ?""", update.toSQL(PSQL))
assertEquals(listOf(0, 10), update.params())
}

@Test
fun `update with empty table`() {
val message = assertThrows(RuntimeException::class.java) {
Update().table("").set("column1", 0).toSQL(PSQL)
}.message

assertEquals(message, "Cannot generate update without table name")
}

@Test
fun `update with empty values`() {
val update = Update().table("table").set("", 0).set("column1", 1)

assertEquals("""UPDATE "table" SET "column1" = ?""", update.toSQL(PSQL))
}
}

0 comments on commit be5e15b

Please sign in to comment.