Walaa wrote:
Please check the following thread:
http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=9144
Thanks, I missed that one. So, I tried
IEntityField2 fn = ArticleFields.Id.SetExpression(ArticleFields.Title + " " + ArticleFields.SubTitle);
foreach (string pattern in query.Pattern.Split(' '))
{
FieldLikePredicate like = new FieldLikePredicate(fn, null, "%" + pattern + "%");
rpb.PredicateExpression.Add(like);
}
Which does produce
WHERE article.title + ' ' + article.subtitle LIKE '%foo%'
Which would be fine on MSSQL, however on MySql this is not how you concatenate strings (neither on Oracle, see the thread mentioned above). Shouldn't LLBLGen know how to concatenate strings? Anyway. I switched to
IRelationPredicateBucket rpb;
...
// this is mysql-specific
IEntityField2 fn = ArticleFields.Id.SetExpression(
new DbFunctionCall("CONCAT_WS(' ', {0}, {1})", new object[] { ArticleFields.Title, ArticleFields.SubTitle })
);
foreach (string pattern in query.Pattern.Split(' '))
{
FieldLikePredicate like = new FieldLikePredicate(fn, null, "%" + pattern + "%");
rpb.PredicateExpression.Add(like);
}
which does produce
WHERE CONCAT_WS(' ', article.title, article.subtitle) LIKE '%foo%'
which is fine. Now the tricky part is trying to produce
WHERE STR_AI_CI(CONCAT_WS(' ', article.title, article.subtitle)) LIKE STR_AI_CI('%foo%')
because FieldLikePredicate wants a string, not an expression, as a pattern?
I suppose I could have a .Net implementation of STR_AI_CI and process the pattern on the .Net side... but I can think of some cases where it will not do. Such as, what if the pattern itself comes from another database field?
I suppose I could create a database function that would look like (pseudo-code)
function myfunc(a, b) { if str_ai_cs(a) like str_ai_cs(b) return 1 else return 0 }
and do
IRelationPredicateBucket rpb;
...
foreach (string pattern in query.Pattern.Split(' '))
{
rpb.PredicateExpression.Add(
ArticleFields.Id.SetExpression(
new DbFunctionCall("MYFUNC(CONCAT_WS(' ', {0}, {1})",
new object[] { ArticleFields.Title, ArticleFields.SubTitle }
)
) == 1
);
}
because then, pretty much everything is possible...
What do you think?
(edit: http://www.llblgen.com/TinyForum//Messages.aspx?ThreadID=10651 suggests using charindex/instr/... function instead of like when comparing two database fields)