Hello,
Sorry in advance, if I have totally misunderstood something ;)
I just wanted to post a modif. that helped me when using the NotExpression-class with an MySQL server..
It might be misuse, but when I used:
Code:
crit.Add(NHibernate.Expression.Expression.Not((NHibernate.Expression.Expression.Eq("AttributeName",""))));
The generated SQL was missing some parantheses causing the resultset to be empty..
When I tracked the error, it ran into this comment:
// TODO: h2.1 SYNCH: add a MySqlDialect check
Not knowing exactly what that means, I just wrote some code to quickly solve the problem and let me go on with my life..
Maybe my code is flawed or maybe it shouldn't be there at all.. I reckon that there might be a more aesthetically pleasing way to do this...
Anyways - here it is (for the file Expression\NotExpression.cs):
Code:
using System.Collections;
using NHibernate.Engine;
using NHibernate.SqlCommand;
namespace NHibernate.Expression
{
/// <summary>
/// An <see cref="ICriterion"/> that negates another <see cref="ICriterion"/>.
/// </summary>
public class NotExpression : AbstractCriterion
{
private ICriterion _criterion;
/// <summary>
/// Initialize a new instance of the <see cref="NotExpression" /> class for an
/// <see cref="ICriterion"/>
/// </summary>
/// <param name="criterion">The <see cref="ICriterion"/> to negate.</param>
internal NotExpression( ICriterion criterion )
{
_criterion = criterion;
}
/// <summary>
///
/// </summary>
/// <param name="factory"></param>
/// <param name="persistentClass"></param>
/// <param name="alias"></param>
/// <returns></returns>
public override SqlString ToSqlString( ISessionFactoryImplementor factory, System.Type persistentClass, string alias, IDictionary aliasClasses )
{
//TODO: set default capacity
SqlStringBuilder builder = new SqlStringBuilder();
builder.Add( "not " );
// TODO: h2.1 SYNCH: add a MySqlDialect check
// emro: Added check for MySqlDialect check and added parentheses
if (factory.Dialect.ToString().ToLower().IndexOf("mysqldialect")>-1) {
builder.Add("(");
builder.Add( _criterion.ToSqlString( factory, persistentClass, alias, aliasClasses ) );
builder.Add(")");
} else {
builder.Add( _criterion.ToSqlString( factory, persistentClass, alias, aliasClasses ) );
}
return builder.ToSqlString();
}
/// <summary>
///
/// </summary>
/// <param name="sessionFactory"></param>
/// <param name="persistentClass"></param>
/// <returns></returns>
public override TypedValue[ ] GetTypedValues( ISessionFactoryImplementor sessionFactory, System.Type persistentClass, IDictionary aliasClasses )
{
return _criterion.GetTypedValues( sessionFactory, persistentClass, aliasClasses );
}
/// <summary></summary>
public override string ToString()
{
return "not " + _criterion.ToString();
}
}
}
Regards,
Emil Rossing