These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Expression - NotExpression.cs missing parantheses..
PostPosted: Thu Aug 24, 2006 6:42 pm 
Newbie

Joined: Thu Aug 24, 2006 6:30 pm
Posts: 1
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 25, 2006 10:00 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
This is already fixed in the current SVN.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.