Skip to content
Open
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 @@ -41,116 +41,116 @@ data class QueryEntry(
* Param values are wrapped in single quotes with internal backslashes
* doubled and single-quotes doubled; NULL params are emitted bare.
*/
val boundQuery: String?
get() {
if (params.isEmpty()) return null

val sb = StringBuilder(query.length + params.size * 8)
var paramIdx = 0
var i = 0

while (i < query.length) {
val ch = query[i]

// -- line comment (MySQL/MariaDB requires space/tab/newline after --)
if (ch == '-' && i + 1 < query.length && query[i + 1] == '-'
&& i + 2 < query.length && (query[i + 2] == ' ' || query[i + 2] == '\t' || query[i + 2] == '\n' || query[i + 2] == '\r')) {
val eol = query.indexOf('\n', i)
if (eol == -1) {
sb.append(query, i, query.length)
i = query.length
} else {
sb.append(query, i, eol + 1)
i = eol + 1
val boundQuery: String? by lazy {
if (params.isEmpty()) null
else {
val sb = StringBuilder(query.length + params.size * 8)
var paramIdx = 0
var i = 0

while (i < query.length) {
val ch = query[i]

// -- line comment (MySQL/MariaDB requires space/tab/newline after --)
if (ch == '-' && i + 1 < query.length && query[i + 1] == '-'
&& i + 2 < query.length && (query[i + 2] == ' ' || query[i + 2] == '\t' || query[i + 2] == '\n' || query[i + 2] == '\r')) {
val eol = query.indexOf('\n', i)
if (eol == -1) {
sb.append(query, i, query.length)
i = query.length
} else {
sb.append(query, i, eol + 1)
i = eol + 1
}
continue
}
Comment on lines +55 to 66

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

\r のみの行末記号で -- コメントの残りクエリ全体が誤って消費される

eol の検索に query.indexOf('\n', i) のみを使用しており、\r だけの行末(旧 Mac スタイル)の場合は eol == -1 となって、クエリの残り全体がコメントとして StringBuilder に追記されます。

例: "SELECT --\r WHERE id=?"eol = -1sb.append(query, i, query.length) → プレースホルダ ? が置換されずに残る。

同じ問題は # コメント(lines 69–79)にも存在します。実際の SQL では \r 単体の行末は稀ですが、念のため \r の検索も追加することを推奨します。

🐛 修正案(`--` コメントおよび `#` コメントの両方)
-                        val eol = query.indexOf('\n', i)
+                        var eol = query.indexOf('\n', i)
+                        if (eol == -1) eol = query.indexOf('\r', i)
                         if (eol == -1) {
                             sb.append(query, i, query.length)
                             i = query.length
                         } else {
-                            sb.append(query, i, eol + 1)
-                            i = eol + 1
+                            // \r\n の場合は \r も含めて読み飛ばす
+                            val end = if (eol + 1 < query.length && query[eol] == '\r' && query[eol + 1] == '\n') eol + 2 else eol + 1
+                            sb.append(query, i, end)
+                            i = end
                         }

# コメント(lines 70–77)も同様に修正してください。

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@jetbrains-plugin/src/main/kotlin/com/mariadbprofiler/plugin/model/QueryEntry.kt`
around lines 55 - 66, The logic in QueryEntry.kt that parses line comments is
only searching for '\n' (eol = query.indexOf('\n', i)), so a lone '\r' ends up
treated as "no eol" and the rest of the query is appended incorrectly; update
the comment-handling code (the block referencing sb, query, i, and eol for '--'
and the analogous '#' block) to locate the nearest line terminator by searching
for both '\n' and '\r' and taking the earliest non-negative index (handling -1s
appropriately), then use that index to append until eol+1 (or to end if none
found) so '\r'‑only line endings are handled correctly; apply the same change to
the '#' comment handling block.

continue
}

// # line comment (MySQL/MariaDB): copy through to end of line
if (ch == '#') {
val eol = query.indexOf('\n', i)
if (eol == -1) {
sb.append(query, i, query.length)
i = query.length
} else {
sb.append(query, i, eol + 1)
i = eol + 1
// # line comment (MySQL/MariaDB): copy through to end of line
if (ch == '#') {
val eol = query.indexOf('\n', i)
if (eol == -1) {
sb.append(query, i, query.length)
i = query.length
} else {
sb.append(query, i, eol + 1)
i = eol + 1
}
continue
}
continue
}

// /* block comment */: copy through to closing */
if (ch == '/' && i + 1 < query.length && query[i + 1] == '*') {
val close = query.indexOf("*/", i + 2)
if (close == -1) {
sb.append(query, i, query.length)
i = query.length
} else {
sb.append(query, i, close + 2)
i = close + 2
// /* block comment */: copy through to closing */
if (ch == '/' && i + 1 < query.length && query[i + 1] == '*') {
val close = query.indexOf("*/", i + 2)
if (close == -1) {
sb.append(query, i, query.length)
i = query.length
} else {
sb.append(query, i, close + 2)
i = close + 2
}
continue
}
continue
}

// Quoted context: single-quote, double-quote, or backtick
if (ch == '\'' || ch == '"' || ch == '`') {
val quote = ch
sb.append(ch)
i++
while (i < query.length) {
val qch = query[i]
// Backslash escape inside single- and double-quoted strings
if (qch == '\\' && (quote == '\'' || quote == '"')) {
sb.append(qch)
if (i + 1 < query.length) {
sb.append(query[i + 1])
i += 2
} else {
i++
// Quoted context: single-quote, double-quote, or backtick
if (ch == '\'' || ch == '"' || ch == '`') {
val quote = ch
sb.append(ch)
i++
while (i < query.length) {
val qch = query[i]
// Backslash escape inside single- and double-quoted strings
if (qch == '\\' && (quote == '\'' || quote == '"')) {
sb.append(qch)
if (i + 1 < query.length) {
sb.append(query[i + 1])
i += 2
} else {
i++
}
continue
}
continue
}
if (qch == quote) {
// doubled-quote escape ('' / "" inside their respective literals)
if (i + 1 < query.length && query[i + 1] == quote) {
sb.append(quote)
if (qch == quote) {
// doubled-quote escape ('' / "" inside their respective literals)
if (i + 1 < query.length && query[i + 1] == quote) {
sb.append(quote)
sb.append(quote)
i += 2
continue
}
// closing quote
sb.append(quote)
i += 2
continue
i++
break
}
// closing quote
sb.append(quote)
sb.append(qch)
i++
break
}
sb.append(qch)
i++
continue
}
continue
}

// Parameter placeholder
if (ch == '?') {
if (paramIdx < params.size) {
val v = params[paramIdx++]
if (v == null) {
sb.append("NULL")
// Parameter placeholder
if (ch == '?') {
if (paramIdx < params.size) {
val v = params[paramIdx++]
if (v == null) {
sb.append("NULL")
} else {
sb.append('\'')
sb.append(v.replace("\\", "\\\\").replace("'", "''"))
sb.append('\'')
}
} else {
sb.append('\'')
sb.append(v.replace("\\", "\\\\").replace("'", "''"))
sb.append('\'')
sb.append(ch) // more ?'s than params – keep as-is
}
} else {
sb.append(ch) // more ?'s than params – keep as-is
i++
continue
}

sb.append(ch)
i++
continue
}

sb.append(ch)
i++
sb.toString()
}
return sb.toString()
}
/** Tag as list for UI display compatibility */
val tags: List<String>
Expand Down