Add udf in predicates to help...

Posts   
 
    
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 16-Oct-2007 00:40:14   

I could not find how to call a UDF in the help file. I did find what I needed on the forums and used:

            FieldCompareValuePredicate predicate = new FieldCompareValuePredicate(CompanyFields.CompanyId, ComparisonOperator.Equal, false);
            predicate.FieldCore.ExpressionToApply = SqlFunctionFactory.IsDemoCompany(CompanyFields.CompanyId);
            filter.AddWithAnd(predicate); 

Just thought would be good if you had this in the help section under how to call UDFs.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Oct-2007 10:32:56   

LLBLGen Pro manual: Using the generated code -> Calling a database function Please check the Example at the end of the page.

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 16-Oct-2007 15:00:53   

Of course I had already looked there when I wrote this.

I now looked at the 2.5 version of help and still do not see it. It shows how to call a database function in the SELECT part of the query, not in the WHERE part - and it is very different code.

If I am missing it, let me know.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Oct-2007 16:22:05   

I now looked at the 2.5 version of help and still do not see it. It shows how to call a database function in the SELECT part of the query, not in the WHERE part - and it is very different code.

I see what you mean. This part in the docs is describing how to call a database function regardless of where you need to call it. A DBFunction returns an Expression, and just in the previous section in the docs Using the generated code -> Field expressions and aggregates, it is described how you can use an expression in different locations (select lists, predicates...etc) Please check the sub-Section called: Expressions in predicates

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 16-Oct-2007 16:28:37   

Yes, I see that section. That is close. I could not of course find that - as I looked in Calling a database function and in Advanced filter usage.

What you need to do to call the database function is still not clear in the docs you presented. How would you know to use the 'Fieldcore' and such to set the ExpressionToApply? If you did not know the answer, I bet $5 you could not determine how to do it from any of the documentation you pointed to.

That is why I have suggested to add a simple section in the Calling a database function that shows how to call a database function using predicates.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Oct-2007 17:23:17   

That is why I have suggested to add a simple section in the Calling a database function that shows how to call a database function using predicates.

Adding this part won't hurt anyway, better than paying the $5 simple_smile I'll escilate it.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 16-Oct-2007 18:28:33   

It's indeed perhaps something less obvious.

There are other ways to construct the filter btw, you can create a new EntityField(2) instance and pass the expression to the constructor, and use that field in the predicate construction. It's less obvious as well, as it requires to combine a couple of things, which are obvious if you know to do that, but less obvious if you don't know to combine these. I'll see if I can add a small remark about the filter.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 16-Oct-2007 19:50:12   

Humm....that is interesting, sounds better than what I am doing. So, I could do this:

EntityField myField = new EntityField(SqlFunctionFactory.IsDemoCompany(CompanyFields.CompanyId)); IPredicate filter = myField == false;

If that is true, I could easily alter my SqlFunctionFactory to do that for me, so that I could turn any Expression into a predicate ready to use like 'normal':

IPredicate filter = SqlFunctionFactory.IsDemoCompany_AsField(CompanyFields.CompanyId) == false;

That sound right?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 16-Oct-2007 20:32:20   

That should work indeed simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 16-Oct-2007 20:58:36   

Yes, it does....that is much better. (I think this confirms the need for a blurb in the docs! wink )

For the record:

I generate a file called SqlFunctionCalls.cs that generates:

1) a CachedDbFunctionCall class that I use for all my functions. Remember this class lets it cache the text from a function call - so you can reuse parameters in multiple parts of the sql so SQL Server does not give errors.

2) a partial class SqlFunctionFactory that contains: public static object GetScalar(IExpression expressionToApply, ITransaction transaction) - this is a wrapper around dao.GetScalar so you can easily get the result of a function. and it contains all the SQL Server date and string built in function wrappers, like:


              public static IExpression GetDate()
            {
                return new CachedDbFunctionCall("GETDATE", null);
            }

(Which I sent to you and you posted in user section)

I then extend the partial class of SqlFunctionFactory to add in my custom function calls all returning IExpressions. I overload them so they take the actual parameter types as well as object parameter types. This way I can pass in an EntityField or the actual value.

Additionally I add easy functions to call to just get the return value of the function, like: public static bool IsDemoCompany(int companyId, ITransaction transaction) { return (bool)SqlFunctionFactory.GetScalar(SqlFunctionFactory.IsDemoCompany(companyId), transaction); }

and now, I added this class:

    public partial class SqlFunctionForPredicate
    {
        private SqlFunctionForPredicate()
        {
        }

        public static EntityField IsDemoCompany(object companyId)
        {
            return new EntityField("IsDemoCompany", SqlFunctionFactory.IsDemoCompany(companyId));
        }

        public static EntityField IsDemoCompany(int companyId)
        {
            return new EntityField("IsDemoCompany", SqlFunctionFactory.IsDemoCompany(companyId));
        }
    }

Anyway, all this seems to work pretty well for me and enables me to use functions quite easily...

Man, I wish I had the meta-data so I could just generate this ALL without writing this code wink

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 17-Oct-2007 11:20:44   

Sounds good! simple_smile

(ps, if you want others to see this tread, let me know so I'll move it to another forum)

Frans Bouma | Lead developer LLBLGen Pro
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 17-Oct-2007 15:42:41   

Sure move it public