Hi all,
I intended to provide my view/business layer with the possibility to send HQL queries in strings to my data layer.
However, the data layer needs to analyze and manipulate these queries (in particular, add a criterion in the where clause).
The supported forms of HQL queries are any combination of the following:
from ...
where ...
order by ...
I think this kind of simplified HQL query should be regex-able, and this is the Regex I defined:
public const string HqlRegex = @"^(\bfrom\b\s*(?<FromPart>.+)\s*)?"
+ @"(\bwhere\b\s*(?<WherePart>.+)\s*)?"
+ @"(\border\b\s+\bby\b\s*(?<OrderByPart>.+))?$";
Regex re = new Regex(Novartis.MapAdmeBs.NHibernateDAO.DAOFactory.HqlRegex,
RegexOptions.Singleline);
Update: I've even tried with the non-greedy modifier:
public const string HqlRegex = @"^(\bfrom\b\s*(?<FromPart>.+?)\s*)?"
+ @"(\bwhere\b\s*(?<WherePart>.+?)\s*)?"
+ @"(\border\b\s+\bby\b\s*(?<OrderByPart>.+?))?$";
However, when I try to match a string containing the where and the order by clause, the "order by" keyword is regarded as part of the where clause:
Match m = re.Match("where (e.Name not like '%X') and e.StatusID not in (7, 8, 9)"
+ " order by e.DateCreated desc");
Console.WriteLine(m.Groups["WherePart"].Value);
gives
(e.Name not like '%X') and e.StatusID not in (7, 8, 9) order by e.DateCreated desc
Any of the following help is appreciated:
- How to fix the regexp?
- Is there a regexp for HQL? (Googling leads to regexp-features of the HQL language)
- Better idea which is still simple enough to implement in a day or less?