Skip to content

Commit

Permalink
test more edge-cases on empty values on insert
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Coser committed Jun 27, 2023
1 parent 136cab9 commit 776a7db
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/jako/dsl/insert/Insert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import java.sql.Date
import java.time.LocalDate

class Insert: StatementBuilder() {
private var insertInto: InsertInto? = null
private var insertInto: InsertInto = InsertInto("")
private var insertRow: InsertRow = InsertRow()
private var returning: Returning = Returning()

override fun blocks() = listOf(insertIntoOrThrow(), rowOrThrow(), returning)

fun into(table: String): Insert {
this.insertInto = InsertInto(table)
insertInto = InsertInto(table)
return this
}

Expand All @@ -39,7 +39,7 @@ class Insert: StatementBuilder() {
}

private fun rowOrThrow() = if (insertRow.isPresent()) insertRow else throw RuntimeException("Cannot generate insert without values")
private fun insertIntoOrThrow() = insertInto ?: throw RuntimeException("Cannot generate insert without table name")
private fun insertIntoOrThrow() = if (insertInto.isPresent()) insertInto else throw RuntimeException("Cannot generate insert without table name")

companion object {
fun into(table: String): Insert {
Expand Down
12 changes: 7 additions & 5 deletions src/main/kotlin/jako/dsl/insert/InsertRow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ internal class InsertRow: StatementBlock {
return this
}

override fun toSQL(dialect: Dialect) = if(cols.isNotEmpty()) "(${columnNames(dialect)}) VALUES (${placeholders()})" else ""
override fun params(): List<Any?> = cols.map { it.value }
override fun isPresent() = cols.any { it.isPresent() }
override fun toSQL(dialect: Dialect) = "(${columnNames(dialect)}) VALUES (${placeholders()})"
override fun params(): List<Any?> = presentCols().map { it.value }
override fun isPresent() = presentCols().isNotEmpty()

private fun columnNames(dialect: Dialect) = cols.joinToString(prefix = "${dialect.columnSeparator}", separator = "${dialect.columnSeparator}, ${dialect.columnSeparator}", postfix = "${dialect.columnSeparator}") { it.name }
private fun columnNames(dialect: Dialect) = presentCols().joinToString(prefix = dialect.columnSeparator, separator = "${dialect.columnSeparator}, ${dialect.columnSeparator}", postfix = dialect.columnSeparator) { it.name }

private fun placeholders() = List(cols.size) { "?" }.joinToString(", ")
private fun placeholders() = List(presentCols().size) { "?" }.joinToString(", ")

private fun presentCols() = cols.filter { it.isPresent() }
}
17 changes: 17 additions & 0 deletions src/test/kotlin/jako/dsl/insert/InsertTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ class InsertTest {
assertEquals(listOf("1", 2, "3"), insert.params())
}

@Test
fun `insert into EMPTY param`() {
val insert = Insert.into("table").set("", "0").set("col1", "1")

assertEquals("INSERT INTO \"table\" (\"col1\") VALUES (?)", insert.toSQL(PSQL))
assertEquals(listOf("1"), insert.params())
}

@Test
fun `insert into with empty table`() {
val message = assertThrows(RuntimeException::class.java) {
Insert.into("").set("col1", "1").toSQL(PSQL)
}.message

assertThat(message).isEqualTo("Cannot generate insert without table name")
}

@Test
fun `insert without values`() {
val message = assertThrows(RuntimeException::class.java) {
Expand Down

0 comments on commit 776a7db

Please sign in to comment.