Workaround for https://www.re-motion.org/jira/browse/RMLNQSQL-54

20 views
Skip to first unread message

Toni Wenzel

unread,
Mar 1, 2017, 3:14:35 AM3/1/17
to re-motion Users
Hi There,

is there any workaround for the https://www.re-motion.org/jira/browse/RMLNQSQL-54 bug? I mean, is there any option that I can write a transformer or subclass any visitor in order to fix this issue?

Michael Ketting

unread,
Mar 1, 2017, 4:27:41 AM3/1/17
to re-motion Users
Hi Toni!

Without looking up particular extension points: Since the issue is the ?: operator, you should be able to write a visitor which detects the ConditionalExpression and return this as a the &&, || combination from the issue's comment.

Let me know if that helps :)

Best regards, Michael

Toni Wenzel

unread,
Mar 1, 2017, 7:53:36 AM3/1/17
to re-motion Users
Thanks a lot.

I've created following transformer and added it to the ExpressionTransformerRegistry.

Imports System.Linq.Expressions
Imports System.Reflection
Imports Remotion.Linq.Clauses.Expressions
Imports Remotion.Linq.Parsing.ExpressionVisitors.Transformation

''' <summary>
''' Transforms IIF expressions to ((&lt;cond1&gt; &amp;&amp; &lt;cond2&gt;) || &lt;cond3&gt;) due to https://www.re-motion.org/jira/browse/RMLNQSQL-54
''' </summary>
''' <seealso cref="Remotion.Linq.Parsing.ExpressionVisitors.Transformation.IExpressionTransformer(Of ConditionalExpression)" />
Friend Class IIFTransformer
Implements IExpressionTransformer(Of ConditionalExpression)

''' <summary>
''' Transforms a given <see cref="T:System.Linq.Expressions.Expression" />.
''' </summary>
''' <param name="expression">The expression to be transformed.</param>
''' <returns>
''' The result of the transformation, or <paramref name="expression" /> if no transformation was applied.
''' </returns>
Public Function Transform(expression As ConditionalExpression) As Expression Implements IExpressionTransformer(Of ConditionalExpression).Transform
ArgumentUtility.CheckNotNull(NameOf(expression), expression)

If (expression.Test.Type = TYPE_BOOL AndAlso expression.IfTrue.Type = TYPE_BOOL) Then
' we should rewrite the  <cond1> ? <cond2> : <cond3>
' to ((<cond1> && <cond2>) || <cond3>).
Dim condition1 As Expression = expression.Test
Dim condition2 As Expression = expression.IfTrue
Dim condition3 As Expression = expression.IfFalse

Dim first As Expression = Expressions.Expression.MakeBinary(ExpressionType.AndAlso, condition1, condition2)

Return Expressions.Expression.MakeBinary(ExpressionType.OrElse, first, condition3)
Else
Return expression
End If
End Function

''' <summary>
''' Gets the expression types supported by this <see cref="IIFTransformer" />.
''' </summary>
''' <value>
''' The supported expression types.
''' </value>
Public ReadOnly Property SupportedExpressionTypes As ExpressionType() Implements IExpressionTransformer(Of ConditionalExpression).SupportedExpressionTypes
Get
Return {ExpressionType.Conditional}
End Get
End Property

End Class


Everything is working fine, so far. Any thoughts on the code?

Regards
Toni

Michael Ketting

unread,
Mar 1, 2017, 8:57:45 AM3/1/17
to re-motion Users
Hi Toni!

Glad it works! I think the code's just fine / would have written something that looks just like it :)

Best regards, Michael

Toni Wenzel

unread,
Mar 1, 2017, 9:45:23 AM3/1/17
to re-motion Users
Thanks.

I'm currently running in a issue where the condition2 and condition3 are of type nullable<boolean>.
if condition1 is bool the MakeBinary fails because the two types can't be compared.

Any idea how I can manage that?

Michael Ketting

unread,
Mar 1, 2017, 10:59:34 AM3/1/17
to re-motion Users
Hmm... I think you could just throw a ConvertExpression at it. Casts to nullable types are transparent for SQL, it will just compare VALUE with NULL and result in FALSE.
Reply all
Reply to author
Forward
0 new messages