MergeActionClause object has wrong FirstTokenIndex value
ScriptDom version: 161.9142.1 Compatibility level: 150
After parsing a MERGE statement MergeActionClause instance has the same FirstTokenIndex as it's property - Action (for example, ValuesInsertSource instance); in case if there is a SearchCondition then the clause's FirstTokenIndex will equal to SearchCondition.FirstTokenIndex value. This seems to be inaccurate. MergeActionClause is the object containing Condition, SearchCondition and Action properties which makes me believe that the MergeActionClause instance is supposed to match the whole clause starting with WHEN keyword which occurs way earlier in a script than the merge action (or search condition).
Here is a sample script:
MERGE t WITH (HOLDLOCK) AS trg
USING (SELECT * FROM @src) AS src
ON trg.id = src.id
WHEN NOT MATCHED AND (1=1) THEN
INSERT (title)
VALUES (src.title)
WHEN NOT MATCHED BY SOURCE THEN
DELETE
WHEN MATCHED THEN
UPDATE SET
title = src.title,
lastmod = SYSDATETIME()
OUTPUT DELETED.title AS old_title, $action AS act
INTO @log(old_title, act);
for the first clause WHEN NOT MATCHED AND (1=1) THEN INSERT clause:
MergeActionClause.SearchCondition.FirstTokenIndex = 50
MergeActionClause.Action.FirstTokenIndex = 59
MergeActionClause.FirstTokenIndex = 50 (same as for SearchCondition)
for the second clause WHEN NOT MATCHED BY SOURCE THEN DELETE without SearchCondition:
MergeActionClause.Action.FirstTokenIndex = 87
MergeActionClause.FirstTokenIndex = 87 (same as for Action)
and this makes it hard to work with such clauses in code - the beginning of each WHEN has to be located manually by iterating tokens backwards.
MergeActionClause.FirstTokenIndex should always point to the WHEN keyword.
See also token-index related bug-reports: #90 , #91