Skip to first unread message

Toni Wenzel

Feb 22, 2016, 4:23:36 AM2/22/16
to re-motion Users
In your code I found the UserDefinedFunctionTransformer.

Can I use this for SQL creation only or is this only valid when using a System.Data.Linq.DataContext?
Can you provide a sample?

Michael Ketting

Feb 22, 2016, 6:26:43 AM2/22/16
to re-motion Users
Yeah, you could copy the UserDefinedFunctionTransformer and use it / hook it up like we do in the integration tests. But mainly, it was intended to transparantly service LinqToSql in the LinqToSqlAdapter and its general purpose function API.

The more established way is to just do what we do with all the usages of SqlFunctionExpression (when you check for usages) to register functions for existing methods or you can go with MethodCallExpressionTransformerAttribute to hook up custom methods (works for extension/static/instance methods) with dedicated sql functions:

[MethodCallExpressionTransformer (typeof (MySqlFunctionTransformer))] public static T MySqlFunction<T> (this T o, int arg1, string arg2, int arg3) {   throw new NotImplementedException ("Never called in memory."); // or implement it to have the code also work in memory } private class MySqlFunctionTransformerIExpressionTransformer<MethodCallExpression> {   public ExpressionType[] SupportedExpressionTypes   {     get { return new[]{ExpressionType.Xyz}; }   }   public Expression Transform (MethodCallExpression methodCallExpression)   {
// The arguments are extracted from the call an match the requirements of the sql function
     return new SqlFunctionExpression ("MYSQLFUNCTION", methodCallExpression.Arguments[0],  methodCallExpression.Arguments[1],  methodCallExpression.Arguments[2]);   } }
It really depends on what you need: user-defined function support in the user domain, pre-defined functions for custom methods, or representing standard methods in .NET as sql functions.
Best regards, Michael
Reply all
Reply to author
0 new messages