Skip to content

Add query to InsufficientParametersException #418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ package com.github.jasync.sql.db.exceptions
* @param given the collection given
*/
@Suppress("RedundantVisibilityModifier")
public class InsufficientParametersException(expected: Int, given: List<Any?>) : DatabaseException(
"The query contains %s parameters but you gave it %s (%s)".format(
public class InsufficientParametersException(query: String, expected: Int, given: List<Any?>) : DatabaseException(
"The query contains %s parameters but you gave it %s (%s):${System.lineSeparator()}%s".format(
expected,
given.size,
given.joinToString(",")
given.joinToString(","),
query
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ class MySQLConnection @JvmOverloads constructor(
this.validateIsReadyForQuery()
val totalParameters = params.query.count { it == '?' }
if (params.values.length != totalParameters) {
throw InsufficientParametersException(totalParameters, params.values)
throw InsufficientParametersException(params.query, totalParameters, params.values)
}
val promise = CompletableFuture<QueryResult>()
this.setQueryPromise(promise)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,11 @@ class QuerySpec : ConnectionHelper() {
@Test
fun `connection should fail if number of args required is different than the number of provided parameters`() {
withConnection { connection ->
verifyException(InsufficientParametersException::class.java) {
val query = "select * from some_table where c = ? and b = ?"
verifyException(InsufficientParametersException::class.java, containedInMessage = query) {
executePreparedStatement(
connection,
"select * from some_table where c = ? and b = ?",
query,
listOf("one", "two", "three")
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.assertj.core.api.Assertions
fun verifyException(
exType: Class<out java.lang.Exception>,
causeType: Class<out java.lang.Exception>? = null,
containedInMessage: String? = null,
body: () -> Unit
): Throwable {
try {
Expand All @@ -13,6 +14,7 @@ fun verifyException(
} catch (e: Exception) {
// e.printStackTrace()
Assertions.assertThat(e::class.java).isEqualTo(exType)
containedInMessage?.let { Assertions.assertThat(e.message).contains(it) }
causeType?.let { Assertions.assertThat(e.cause!!::class.java).isEqualTo(it) }
return e.cause ?: e
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class PostgreSQLConnection @JvmOverloads constructor(

if (holder.paramsCount != params.values.length) {
this.clearQueryPromise()
throw InsufficientParametersException(holder.paramsCount, params.values)
throw InsufficientParametersException(params.query, holder.paramsCount, params.values)
}

this.currentPreparedStatement = Optional.of(holder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import org.assertj.core.api.Assertions
fun verifyException(
exType: Class<out java.lang.Exception>,
causeType: Class<out java.lang.Exception>? = null,
containedInMessage: String? = null,
body: () -> Unit
): Throwable {
try {
body()
throw Exception("Expected exception was not thrown: ${exType.simpleName}->${causeType?.simpleName}")
} catch (e: Exception) {
Assertions.assertThat(e::class.java).isEqualTo(exType)
containedInMessage?.let { Assertions.assertThat(e.message).contains(it) }
causeType?.let { Assertions.assertThat(e.cause!!::class.java).isEqualTo(it) }
return e.cause ?: e
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class PreparedStatementSpec : DatabaseTestHelper() {
fun `prepared statements should raise an exception if the parameter count is different from the given parameters count`() {
withHandler { handler ->
executeDdl(handler, this.messagesCreate)
verifyException(InsufficientParametersException::class.java) {
verifyException(InsufficientParametersException::class.java, containedInMessage = this.messagesSelectOne) {
executePreparedStatement(handler, this.messagesSelectOne)
}
}
Expand Down Expand Up @@ -268,9 +268,10 @@ class PreparedStatementSpec : DatabaseTestHelper() {
fun `prepared statements should fail if prepared statement has more variables than it was given`() {
withHandler { handler ->
executeDdl(handler, messagesCreate)
verifyException(InsufficientParametersException::class.java) {
val query = "SELECT * FROM messages WHERE content = ? AND moment = ?"
verifyException(InsufficientParametersException::class.java, containedInMessage = query) {
handler.sendPreparedStatement(
"SELECT * FROM messages WHERE content = ? AND moment = ?",
query,
listOf("some content")
)
}
Expand Down