Expression.Sql should support aliases other than {alias}

Description

Expression.Sql("") only allows use of the root alias using {alias}. It should allow usage of any subcriteria alias. Attached is a patch which implements this.

The patch is non-ideal in that it pokes a whole through to CriteriaImpl, which it probably should not do. I'm seeking advice on the best way to obtain the required information.

Environment

None

Attachments

3

Activity

Alex Zaytsev 
May 26, 2020 at 11:26 PM

Moved here.

Josh Berke 
January 16, 2013 at 3:11 PM

I ran into this issue as well...aliases should work the same way across the system.

Boris Drajer 
April 9, 2012 at 10:46 AM

+1. This is important as a last-resort solution for all database-specific features that aren't supported in another way. Supporting a single alias here means supporting only the most trivial cases.

Note that the previous patches don't seem to resolve certain types of subqueries - if I'm not mistaken, it's the detached queries that don't work. For example, a query like

mainQuery.Where(Subqueries.Exists(detachedCriteria.Where(Expression.Sql(sql_expression)))

will fail to find mainQuery aliases referenced by sql_expression. Looking through the source, I have been able to find the way to traverse from the inner expression to the outer, but it involved a bit of reflection and a casting of interfaces to real types, so it's a bit of a hack. I'm including a patch as a proof of concept, but it needs to be worked on by someone who knows more about NH.

LarsL 
November 11, 2009 at 5:17 AM

+1 Especially when working with legacy-databases it is often necessary to create custom sql-statements. It would be great if we could reuse the aliases there.

luis cornejo 
October 28, 2008 at 11:34 PM

Also,

if it won't be implemented, can anyone suggest a way to get the right alias of subcriterias in there for custom sql use with criterias?

Fixed

Details

Assignee

Reporter

Labels

Components

Fix versions

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created August 7, 2008 at 8:04 AM
Updated May 26, 2020 at 11:26 PM
Resolved May 26, 2020 at 11:26 PM
Who's Looking?