Skip to content

Commit 471d14e

Browse files
committed
db: Only use pgjdbc's query representation for PostgreSQL query logs
It does not seem possible to reliably replace the values pgjdbc could not replace itself, as we can't know when the pgjdbc-replaced value will end, we risk seeking into it.
1 parent 2308e63 commit 471d14e

File tree

2 files changed

+2
-274
lines changed

2 files changed

+2
-274
lines changed

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/internal/core/db/query/PostgresParametrizedQueryFactory.kt

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import io.github.freya022.botcommands.api.core.db.query.AbstractParametrizedQuer
66
import io.github.freya022.botcommands.api.core.db.query.ParametrizedQueryFactory
77
import io.github.freya022.botcommands.api.core.service.annotations.BService
88
import io.github.freya022.botcommands.api.core.service.annotations.Lazy
9-
import io.github.freya022.botcommands.internal.core.exceptions.internalErrorMessage
10-
import io.github.oshai.kotlinlogging.KotlinLogging
119
import java.sql.Connection
1210
import java.sql.DatabaseMetaData
1311
import java.sql.PreparedStatement
1412

1513
/**
1614
* This lets pgjdbc fill out the parameters by itself,
17-
* then we replace those it hasn't replaced.
15+
* if a parameter is not replaced, it should be contributed to pgjdbc.
1816
*/
1917
@Lazy
2018
@BService
@@ -35,65 +33,7 @@ internal object PostgresParametrizedQueryFactory : ParametrizedQueryFactory<Post
3533
if (values.isEmpty)
3634
return removeCommentsAndInline(rawSql)
3735

38-
val queryParts = splitByQueryParameter(rawSql)
39-
val postgresQuery = preparedStatement.toString()
40-
41-
// Rebuild the query using the split parts,
42-
// but for each part, check if there is a '?' after it,
43-
// if absent, it means postgres replaced it
44-
return removeCommentsAndInline(buildString {
45-
var lastIndex = 0
46-
// Drop last part as it has no query parameter after it
47-
for ((i, part) in queryParts.dropLast(1).withIndex()) {
48-
val partIndex = postgresQuery.indexOf(part, lastIndex)
49-
if (partIndex == -1) {
50-
logger.error { internalErrorMessage("Could not find part #$i '${part}' in '$postgresQuery'") }
51-
return removeCommentsAndInline(postgresQuery)
52-
}
53-
54-
append(part)
55-
if (postgresQuery[partIndex + part.length] == '?') {
56-
// The driver did not replace the value, use our own replacement
57-
append(formatParameter(values[i + 1]))
58-
lastIndex = partIndex + part.length + 1
59-
} else {
60-
// The driver replaced the value, find where it stops based on the next part
61-
val nextPart = queryParts[i + 1]
62-
if (nextPart.isBlank()) { // When the query ends with a parameter, the last part is empty
63-
append(postgresQuery.substring(partIndex + part.length))
64-
break
65-
}
66-
val nextPartIndex = postgresQuery.indexOf(nextPart, partIndex)
67-
append(postgresQuery.substring(partIndex + part.length, nextPartIndex))
68-
lastIndex = nextPartIndex
69-
}
70-
}
71-
72-
append(queryParts.last())
73-
})
74-
}
75-
76-
private fun splitByQueryParameter(query: String): List<String> = buildList {
77-
val builder = StringBuilder()
78-
var inComment = false
79-
for ((index, char) in query.withIndex()) {
80-
if (char == '?' && !inComment) {
81-
add(builder.toString())
82-
builder.clear()
83-
} else {
84-
builder.append(char)
85-
if (char == '-' && query.getOrNull(index + 1) == '-') // -- comment
86-
inComment = true
87-
if (char == '\n')
88-
inComment = false
89-
}
90-
}
91-
add(builder.toString())
92-
}
93-
94-
private companion object {
95-
96-
private val logger = KotlinLogging.logger { }
36+
return removeCommentsAndInline(preparedStatement.toString())
9737
}
9838
}
9939

BotCommands-core/src/test/kotlin/io/github/freya022/botcommands/db/query/PostgresParametrizedQueryTests.kt

Lines changed: 0 additions & 212 deletions
This file was deleted.

0 commit comments

Comments
 (0)