8000 Add query to InsufficientParametersException (#418) · jasync-sql/jasync-sql@83029e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 83029e5

Browse files
kennethtillerkenneth-tiller
andauthored
Add query to InsufficientParametersException (#418)
* added query to InsufficientParametersException * added assertions for message to existing tests --------- Co-authored-by: kenneth-tiller <kenneth.tiller@finn.no>
1 parent 06bedd1 commit 83029e5

File tree

7 files changed

+17
-10
lines changed

7 files changed

+17
-10
lines changed

db-async-common/src/main/java/com/github/jasync/sql/db/exceptions/InsufficientParametersException.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ package com.github.jasync.sql.db.exceptions
1010
* @param given the collection given
1111
*/
1212
@Suppress("RedundantVisibilityModifier")
13-
public class InsufficientParametersException(expected: Int, given: List<Any?>) : DatabaseException(
14-
"The query contains %s parameters but you gave it %s (%s)".format(
13+
public class InsufficientParametersException(query: String, expected: Int, given: List<Any?>) : DatabaseException(
14+
"The query contains %s parameters but you gave it %s (%s):${System.lineSeparator()}%s".format(
1515
expected,
1616
given.size,
17-
given.joinToString(",")
17+
given.joinToString(","),
18+
query
1819
)
1920
)

mysql-async/src/main/java/com/github/jasync/sql/db/mysql/MySQLConnection.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ class MySQLConnection @JvmOverloads constructor(
468468
this.validateIsReadyForQuery()
469469
val totalParameters = params.query.count { it == '?' }
470470
if (params.values.length != totalParameters) {
471-
throw InsufficientParametersException(totalParameters, params.values)
471+
throw InsufficientParametersException(params.query, totalParameters, params.values)
472472
}
473473
val promise = CompletableFuture<QueryResult>()
474474
this.setQueryPromise(promise)

mysql-async/src/test/java/com/github/jasync/sql/db/mysql/QuerySpec.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,11 @@ class QuerySpec : ConnectionHelper() {
239239
@Test
240240
fun `connection should fail if number of args required is different than the number of provided parameters`() {
241241
withConnection { connection ->
242-
verifyException(InsufficientParametersException::class.java) {
242+
val query = "select * from some_table where c = ? and b = ?"
243+
verifyException(InsufficientParametersException::class.java, containedInMessage = query) {
243244
executePreparedStatement(
244245
connection,
245-
"select * from some_table where c = ? and b = ?",
246+
query,
246247
listOf("one", "two", "three")
247248
)
248249
}

mysql-async/src/test/java/com/github/jasync/sql/db/mysql/Utils.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.assertj.core.api.Assertions
55
fun verifyException(
66
exType: Class<out java.lang.Exception>,
77
causeType: Class<out java.lang.Exception>? = null,
8+
containedInMessage: String? = null,
89
body: () -> Unit
910
): Throwable {
1011
try {
@@ -13,6 +14,7 @@ fun verifyException(
1314
} catch (e: Exception) {
1415
// e.printStackTrace()
1516
Assertions.assertThat(e::class.java).isEqualTo(exType)
17+
containedInMessage?.let { Assertions.assertThat(e.message).contains(it) }
1618
causeType?.let { Assertions.assertThat(e.cause!!::class.java).isEqualTo(it) }
1719
return e.cause ?: e
1820
}

pos 10000 tgresql-async/src/main/java/com/github/jasync/sql/db/postgresql/PostgreSQLConnection.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class PostgreSQLConnection @JvmOverloads constructor(
190190

191191
if (holder.paramsCount != params.values.length) {
192192
this.clearQueryPromise()
193-
throw InsufficientParametersException(holder.paramsCount, params.values)
193+
throw InsufficientParametersException(params.query, holder.paramsCount, params.values)
194194
}
195195

196196
this.currentPreparedStatement = Optional.of(holder)

postgresql-async/src/test/java/com/github/aysnc/sql/db/Utils.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import org.assertj.core.api.Assertions
55
fun verifyException(
66
exType: Class<out java.lang.Exception>,
77
causeType: Class<out java.lang.Exception>? = null,
8+
containedInMessage: String? = null,
89
body: () -> Unit
910
): Throwable {
1011
try {
1112
body()
1213
throw Exception("Expected exception was not thrown: ${exType.simpleName}->${causeType?.simpleName}")
1314
} catch (e: Exception) {
1415
Assertions.assertThat(e::class.java).isEqualTo(exType)
16+
containedInMessage?.let { Assertions.assertThat(e.message).contains(it) }
1517
causeType?.let { Assertions.assertThat(e.cause!!::class.java).isEqualTo(it) }
1618
return e.cause ?: e
1719
}

postgresql-async/src/test/java/com/github/aysnc/sql/db/integration/PreparedStatementSpec.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class PreparedStatementSpec : DatabaseTestHelper() {
7777
fun `prepared statements should raise an exception if the parameter count is different from the given parameters count`() {
7878
withHandler { handler ->
7979
executeDdl(handler, this.messagesCreate)
80-
verifyException(InsufficientParametersException::class.java) {
80+
verifyException(InsufficientParametersException::class.java, containedInMessage = this.messagesSelectOne) {
8181
executePreparedStatement(handler, this.messagesSelectOne)
8282
}
8383
}
@@ -268,9 +268,10 @@ class PreparedStatementSpec : DatabaseTestHelper() {
268268
fun `prepared statements should fail if prepared statement has more variables than it was given`() {
269269
withHandler { handler ->
270270
executeDdl(handler, messagesCreate)
271-
verifyException(InsufficientParametersException::class.java) {
271+
val query = "SELECT * FROM messages WHERE content = ? AND moment = ?"
272+
verifyException(InsufficientParametersException::class.java, containedInMessage = query) {
272273
handler.sendPreparedStatement(
273-
"SELECT * FROM messages WHERE content = ? AND moment = ?",
274+
query,
274275
listOf("some content")
275276
)
276277
}

0 commit comments

Comments
 (0)
0