I have a SQL statement with parameters eg.
SELECT *
FROM persons
WHERE id = @id
AND firstName = @firstname
OR surname = @surname
I would like to use c# to rewrite the statement based on the parameters supplied at run-time.
Example if at runtime, @firstname
is not provided, then my SQL should be re-written to
SELECT *
FROM persons
WHERE id = @id OR surname = @surname
This is what I have so far.
public class SqlRewriter
{
public string RewriteSql(List<string> passedRuntimeParameter )
{
var sql = @"SELECT * FROM persons
WHERE id=@id AND firstName=@firstname
or surname=@surname";
/**
* todo parse the sql and compare with passedRuntimeParameter.
* todo reconstruct the sql based on parsed parameters
*/
return sql;
}
}