convertQuestionMark function improperly handles single quotes in comments #1954

Closed
opened 2021-06-14 10:32:32 +00:00 by antialiasis · 2 comments
Contributor

The convertQuestionMark function converts ? SQL parameters to $1, $2 and so on. It tries to ignore question marks inside single-quoted strings. However, it does this very simplisticially, simply alternating each time a single quote occurs in the SQL string. This is not always correct, as SQL comments may contain single quotes, and this makes the parsing of the rest of the string incorrect.

These tests both fail with the current implementation:

func TestSeqFilterLineComment(t *testing.T) {
	var kases = map[string]string{
		`SELECT *
		FROM TABLE1
		WHERE a=? -- a comment with a single quote'
		AND b=?`: `SELECT *
		FROM TABLE1
		WHERE a=$1 -- a comment with a single quote'
		AND b=$2`,
	}
	for sql, result := range kases {
		assert.EqualValues(t, result, convertQuestionMark(sql, "$", 1))
	}
}

func TestSeqFilterComment(t *testing.T) {
	var kases = map[string]string{
		`SELECT *
		FROM TABLE1
		WHERE a=? /* it's a comment */
		AND b=?`: `SELECT *
		FROM TABLE1
		WHERE a=$1 /* it's a comment */
		AND b=$2`,
	}
	for sql, result := range kases {
		assert.EqualValues(t, result, convertQuestionMark(sql, "$", 1))
	}
}
The `convertQuestionMark` function converts ? SQL parameters to $1, $2 and so on. It tries to ignore question marks inside single-quoted strings. However, it does this very simplisticially, simply alternating each time a single quote occurs in the SQL string. This is not always correct, as SQL comments may contain single quotes, and this makes the parsing of the rest of the string incorrect. These tests both fail with the current implementation: ``` func TestSeqFilterLineComment(t *testing.T) { var kases = map[string]string{ `SELECT * FROM TABLE1 WHERE a=? -- a comment with a single quote' AND b=?`: `SELECT * FROM TABLE1 WHERE a=$1 -- a comment with a single quote' AND b=$2`, } for sql, result := range kases { assert.EqualValues(t, result, convertQuestionMark(sql, "$", 1)) } } func TestSeqFilterComment(t *testing.T) { var kases = map[string]string{ `SELECT * FROM TABLE1 WHERE a=? /* it's a comment */ AND b=?`: `SELECT * FROM TABLE1 WHERE a=$1 /* it's a comment */ AND b=$2`, } for sql, result := range kases { assert.EqualValues(t, result, convertQuestionMark(sql, "$", 1)) } } ```
antialiasis changed title from convertQuestionMark function improperly handles single quotes to convertQuestionMark function improperly handles single quotes in comments 2021-06-14 11:17:04 +00:00
lunny added the
kind
bug
label 2021-06-14 14:13:26 +00:00
Owner

Could you send a PR to fix that?

Could you send a PR to fix that?
Owner

Closed by #1955

Closed by #1955
lunny closed this issue 2021-07-19 05:04:54 +00:00
lunny added this to the 1.2.0 milestone 2021-07-19 05:05:03 +00:00
lunny removed this from the 1.2.0 milestone 2021-07-19 05:05:14 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#1954
No description provided.