Bitwise comparison in QueryOver

195 views
Skip to first unread message

Luka

unread,
May 9, 2011, 7:46:19 AM5/9/11
to nhusers
Hi,
How to do Bitwise comparison in QueryOver queries?

ie how to do this query in queryover
var query = from t in Session.Query<Ticket>()
from ticketUser in t.TicketUsers
from usersTicketAccess in
t.UsersTicketAccess
join user in Session.Query<User>() on
ticketUser.User.Id equals user.Id
where user.Id == parameters.UserId
&& (usersTicketAccess.AccessMask &
AccessMask.Read) > 0
select t;

please help.

Chris Sano

unread,
May 9, 2011, 12:13:31 PM5/9/11
to nhu...@googlegroups.com
Hey Luka,

Try something along those lines:

.Where(Expression.Gt(Projections.SqlProjection(String.Format("({{alias}}.AccessMask & {0}) as AccessMask", (int)AccessMask.Read), null, null), 0))

If anyone has a simpler way of writing the above expression, I'd love to hear it.

Let me know how that works for you.
-cs


--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


Luka

unread,
May 17, 2011, 10:54:14 AM5/17/11
to nhusers
Yes, this works,.
Thanks

We need to Find a simpler way of doing it.

Fabio Maulo, can you help? Please

:-)

Richard Brown (gmail)

unread,
May 17, 2011, 3:57:49 PM5/17/11
to nhusers
If you raise a JIRA and assign it to me I'll take a look.

(Not sure if it will need a custom extension to handle it).

Luka

unread,
May 18, 2011, 3:41:05 AM5/18/11
to nhusers
Ok,
I have created an Issue on jira but don't know how to assign it to
you.
The issue number is: NH-2709

On May 17, 9:57 pm, "Richard Brown \(gmail\)"
> > > How to doBitwisecomparison in QueryOver queries?

Richard Brown (gmail)

unread,
May 18, 2011, 4:58:54 AM5/18/11
to nhusers
Hi Luka,

I've assigned it to me, but I'm not sure if we can easily fix it (I'll have
a chew on it and see if I can come up with anything).

You can use HQL ... this translates to the & operator in SQL Server, however
I don't think this is cross-platform because someone has noted in the
comments here that Oracle uses the function bitand():
http://216.121.112.228/browse/NH-1192

You could also try the LINQ provider (.Query<>) since it may generate the
same HQL syntax and hopefully the same SQL as the HQL parser.

I suspect that the longer-term solution to this may be to add a new function
to the Dialect (e.g., bitand) and then implement the 'correct' syntax in
each inherited dialect (there doesn't seem to be an SQL standard for this
unfortunately).

In the meantime, if you are determined to get this working with QueryOver, I
think your best bet is to create a custom SQL Server function and use that.
You can register a custom extension method to make the syntax nicer ... for
an example have a look at the RestrictionsExtensions here:
http://nhibernate.svn.sourceforge.net/viewvc/nhibernate/trunk/nhibernate/src/NHibernate/Criterion/RestrictionsExtensions.cs?revision=5837&view=markup

.. and the ExpressionProcessor.RegisterCustomMethodCall() method.

Hope that helps.

Richard

Luka

unread,
May 18, 2011, 7:22:35 AM5/18/11
to nhusers
thanks Richard.
I have implemented it using Linq.


On May 18, 10:58 am, "Richard Brown \(gmail\)"
<fluke...@googlemail.com> wrote:
> Hi Luka,
>
> I've assigned it to me, but I'm not sure if we can easily fix it (I'll have
> a chew on it and see if I can come up with anything).
>
> You can use HQL ... this translates to the & operator in SQL Server, however
> I don't think this is cross-platform because someone has noted in the
> comments here that Oracle uses the function bitand():http://216.121.112.228/browse/NH-1192
>
> You could also try the LINQ provider (.Query<>) since it may generate the
> same HQL syntax and hopefully the same SQL as the HQL parser.
>
> I suspect that the longer-term solution to this may be to add a new function
> to the Dialect (e.g., bitand) and then implement the 'correct' syntax in
> each inherited dialect (there doesn't seem to be an SQL standard for this
> unfortunately).
>
> In the meantime, if you are determined to get this working with QueryOver, I
> think your best bet is to create a custom SQL Server function and use that.
> You can register a custom extension method to make the syntax nicer ... for
> an example have a look at the RestrictionsExtensions here:http://nhibernate.svn.sourceforge.net/viewvc/nhibernate/trunk/nhibern...
Reply all
Reply to author
Forward
0 new messages