Skip to content

Commit

Permalink
fix: Preserve custom query name even if statement can't be parsed. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr authored Aug 23, 2024
1 parent 3b13591 commit 3ab0cb4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ private static ParseStatement CreateCompoundStatementParser(params ParseStatemen
}
}
return _nullParsedStatementStore.GetOrAdd(datastoreVendor, x => new ParsedSqlStatement(datastoreVendor, null, null));
// return a null statement with the model name if found, otherwise cache and return a null statement with no model name
return !string.IsNullOrEmpty(explicitModel) ? new ParsedSqlStatement(datastoreVendor, explicitModel, null) : _nullParsedStatementStore.GetOrAdd(datastoreVendor, x => new ParsedSqlStatement(datastoreVendor, null, null));
};
}

Expand Down Expand Up @@ -295,7 +296,7 @@ public virtual ParsedSqlStatement ParseStatement(DatastoreVendor vendor, Command
model = "ParseError";
}
}

return CreateParsedDatabaseStatement(vendor, GetFullModel(model, explicitModel));
}

Expand Down Expand Up @@ -323,7 +324,7 @@ public ParsedSqlStatement ParseStatement(DatastoreVendor vendor, CommandType com
string model = FromMatcher.Match(statement).Success
? "(subquery)"
: "VARIABLE";

return new ParsedSqlStatement(vendor, GetFullModel(model, explicitModel), "select");
}
return null;
Expand Down Expand Up @@ -414,7 +415,7 @@ public static bool FixParameterizedSql(IDbCommand command)
Log.Debug("Not executing explain plan since DbType is Object.");
return false;
}

// Parameter names can be supplied with the prefix @ or without
// if is supplied, remove the @ to the beginning of the param name
// This is to deal with regex issues, see below
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,41 @@ public void SqlParserTest_PullNameFromComment_ReplaceSlashes()
Assert.That(parsedDatabaseStatement.ToString(), Is.EqualTo("dude - [dudeservice|getalldudes]/select"));
});
}

[Test]
public void SqlParserTest_PullNameFromComment_IfStatementCannotBeParsed()
{
var parsedDatabaseStatement = SqlParser.GetParsedDatabaseStatement(DatastoreVendor.MSSQL, CommandType.Text,
"""
/* NewRelicQueryName: MyCustomQueryName */

-- This is a full-line comment
SELECT
(
SELECT COUNT(*)
FROM
SomeTable
WHERE
SomeColumn = @SomeValue
) +
(
SELECT COUNT(*)
FROM
SomeOtherTable
WHERE
SomeOtherColumn = @SomeOtherValue
);
""");

Assert.That(parsedDatabaseStatement, Is.Not.Null);

Assert.Multiple(() =>
{
Assert.That(parsedDatabaseStatement.Model, Is.EqualTo("MyCustomQueryName"));
Assert.That(parsedDatabaseStatement.ToString(), Is.EqualTo("MyCustomQueryName/other"));
});
}


[Test]
Expand Down

0 comments on commit 3ab0cb4

Please sign in to comment.