Afternoon all
I've been working on a dll extension for an application, it's a report on customer activity and the first section is a user search to lookup the customer.
The query itself works fine but when I tried to make it SQL injection proof by parameter passing, I was unable to get it to work.
I tried having the apostrophes and wildcards in the main query but when I did the q->Params->ParamByName("ID")->AsString = param function threw an exception saying it could not find the parameter which I've confirmed is the effect of the apostrophes. When I pass in the apostrophes and wildcards as part of the parameter like below, there's no error but the query returns nothing.
I have tried escaping both the apostrophe and the wildcards but it made no difference.
Please let me know if I need to provide any more info.
TStringList* SQLString = new TStringList;
SQLString->Add("select p.id, p.givenName, p.surname, p.dateOfBirth, vm.MaxRowDate ");
SQLString->Add("from ( ");
SQLString->Add(" select pid, customer_systemID, customer_dbID, max(datetime) as MaxRowDate ");
SQLString->Add(" from Visit ");
SQLString->Add(" group by pid, customer_systemID, customer_dbID");
SQLString->Add(" ) vm ");
SQLString->Add("inner join Customer p ");
SQLString->Add("ON vm.pid = p.pid ");
SQLString->Add("and vm.customer_systemID = p.systemID ");
SQLString->Add("and vm.customer_dbID = p.databaseID ");
AnsiString joiningTerm("where ");
if(id != "")
{
SQLString->Add(joiningTerm+"id like :ID ");
joiningTerm = "and ";
}
SQLString->Add("order by surname ");
TMSQuery* q;
try
{
try
{
q = new TMSQuery(NULL);
q->Connection = _db;
q->SQL->Assign(SQLString);
if(id != "")
{
AnsiString param("'%"+id+"%'");
q->Params->ParamByName("ID")->AsString = param;
}
q->Open();
Here is my raw SQL query:
SELECT p.id, p.givenName, p.surname, p.dateOfBirth, vm.MaxRowDate
FROM
(
SELECT pid, customer_systemID, customer_dbID, MAX(DATETIME) AS MaxRowDate
FROM Visit
GROUP BY pid, customer_systemID, customer_dbID
) vm
INNER JOIN Customer p
ON vm.pid = p.pid AND vm.customer_systemID = p.systemID AND vm.customer_dbID = p.databaseID
Edit: It has been suggested to me that perhaps this is a limitation of the library. I am going to post on the Devart forums and will report back if I get a response.