Mapping a simple SQL function

Posts   
 
    
cipone
User
Posts: 8
Joined: 20-Jul-2010
# Posted on: 26-Jul-2010 12:08:53   

Hello , I am trying to map a function to LLBLgenPro. So far I've settled for a really simple one , just for testing purposes. I am a noob at this so I may be missing something very basic such as using LLBL Gen Pro to generate some code out of SQL functions if there is need for something like that, I couldn't find a function section in the project.

Here is ome of my code


GO
ALTER FUNCTION [dbo].[fn_CalculateDistance](@jobID int)
RETURNS int
AS
BEGIN
    DECLARE @toReturn int
    set @toReturn = @JobID
    RETURN @toReturn
END


 public class SQLFunctionMappings : FunctionMappingStore

    {
        public const string CATALOG_NAME = "xyz";
        public const string SCHEMA_NAME = "dbo";

        public SQLFunctionMappings(): base()
        {
            this.Add(new FunctionMapping(typeof(SQLFunctions), "CalculateDistanceBetweeenCoordinates",1 ,"fn_CalculateDistance({0})", SQLFunctionMappings.CATALOG_NAME, SQLFunctionMappings.SCHEMA_NAME));
        }
    }


       public class SQLFunctions
        {
            public static int CalculateDistanceBetweenCoordinates(int JobID)
            {
                // empty body, as it's just here to make the query compile. The call is converted to a SQL function.
                return 0;
            }
        }


           IDataAccessAdapter adapter = dataTransaction.DataAccessAdapter;
            LinqMetaData metaData = new LinqMetaData(adapter);
            metaData.CustomFunctionMappings = new SQLFunctionMappings();
                 var q = from job in metaData.Job
                    where job.OwningCompanyID == this.CurrentUser.CompanyID
                    select new { r = SQLFunctions.CalculateDistanceBetweenCoordinates(job.OwningCompanyID) };

            foreach (var job in q)
            {
                System.Diagnostics.Debug.WriteLine(job.r);
            };

And it just doesn't work. It always return 0. It looks like it is ignoring the function mapping. BTW we are using 3.5 framework so i couldn't enable tracing of the DQE through the config because I didn't find the 3.5 visualizers and it gives me a cast exception. I am using the latest runtime libraries though. I also use SQL2005 express.

Can anybody take a quick look at this and maybe tell me what I am missing ? Thank you very much.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 26-Jul-2010 13:53:05   

The code looks fine as far as I can see. Which runtime library version (build number) are you using?

BTW we are using 3.5 framework so i couldn't enable tracing of the DQE through the config because I didn't find the 3.5 visualizers and it gives me a cast exception.

btw, Tracing has nothing to do with visulaliers.

cipone
User
Posts: 8
Joined: 20-Jul-2010
# Posted on: 26-Jul-2010 14:00:23   

SD.LLBLGen.Pro.LinqSupportClasses.NET35 v2.0.50727 SD.LLBLGen.Pro.ORMSupportClasses.NET20 v2.0.50727

I'll dig into the DQE config some more.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 26-Jul-2010 14:05:26   

That's the .NET version number. Please check the following link to know how to get the version/build number of the runtime libraries. (ORM & Linq) http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7717

cipone
User
Posts: 8
Joined: 20-Jul-2010
# Posted on: 26-Jul-2010 14:09:25   

Oops sorry. SD.LLBLGen.Pro.LinqSupportClasses.NET35.dll 2.6.10.517 SD.LLBLGen.Pro.ORMSupportClasses.NET20.dll 2.6.10.526

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 26-Jul-2010 14:25:54   

Could you please enable DQE tracing to see if there is a SQL function call generated in the SQL code.

cipone
User
Posts: 8
Joined: 20-Jul-2010
# Posted on: 26-Jul-2010 15:30:46   

found it . Also changes the linq a little bit:


var q = from job in metaData.Job
                        where job.OwningCompanyID == 68
                        select new { r = SQLFunctions.CalculateDistanceBetweenCoordinates(job.OwningCompanyID) };


Generated Sql query: 
    Query: SELECT [LPLA_1].[OwningCompanyID] AS [r] FROM [Ridge_Bolt].[dbo].[Job] [LPLA_1]  WHERE ( ( ( ( ( [LPLA_1].[OwningCompanyID] = @OwningCompanyID1)))))
    Parameter: @OwningCompanyID1 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 68.

Is it because i use SQL Express ?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 27-Jul-2010 04:59:33   

Weird it returns 0 records. Does that OwningCompanyID ( 68 ) actually exist? It should't matter that you use SQLExpress. Could you please post (or attach) the complete trace?

David Elizondo | LLBLGen Support Team
cipone
User
Posts: 8
Joined: 20-Jul-2010
# Posted on: 27-Jul-2010 08:35:25   

It doesn't return 0 records, it returns tons of records which is normal, but their value is 0. I've attached the debug output as well.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 27-Jul-2010 09:00:03   

var q = from job in metaData.Job where job.OwningCompanyID == 68 select new { r = SQLFunctions.CalculateDistanceBetweenCoordinates(job.OwningCompanyID) };

Generated Sql query: Query: SELECT [LPLA_1].[OwningCompanyID] AS [r] FROM [Ridge_Bolt].[dbo].[Job] [LPLA_1] WHERE ( ( ( ( ( [LPLA_1].[OwningCompanyID] = @OwningCompanyID1))))) Parameter: @OwningCompanyID1 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 68.

It seems that the SQL function is not called. Can you please modify the Linq to the following (for testing):

var q = from job in metaData.Job
                        where job.OwningCompanyID == 68
                        select new { job.OwningCompanyID, r = SQLFunctions.CalculateDistanceBetweenCoordinates(job.OwningCompanyID) };
cipone
User
Posts: 8
Joined: 20-Jul-2010
# Posted on: 27-Jul-2010 09:07:10   

{ OwningCompanyID = 68, r = 0 }
{ OwningCompanyID = 68, r = 0 }
{ OwningCompanyID = 68, r = 0 }
{ OwningCompanyID = 68, r = 0 }
{ OwningCompanyID = 68, r = 0 }
{ OwningCompanyID = 68, r = 0 }
{ OwningCompanyID = 68, r = 0 }

1 passed, 0 failed, 0 skipped, took 28.53 seconds (NUnit 2.4).

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 27-Jul-2010 10:03:55   

SD.LLBLGen.Pro.LinqSupportClasses.NET35.dll 2.6.10.517 SD.LLBLGen.Pro.ORMSupportClasses.NET20.dll 2.6.10.526

Could you please upgrade to the latest build and try again?

cipone
User
Posts: 8
Joined: 20-Jul-2010
# Posted on: 27-Jul-2010 10:47:15   

I downloaded the RuntimeLibraries_07022010.zip which seemed the latest for 2.6 and those were the file versions. Should i look for another file ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39903
Joined: 17-Aug-2003
# Posted on: 27-Jul-2010 12:12:48   

You made a typo in the function mappings: CalculateDistanceBetweeenCoordinates

3 'e's instead of '2'. If you put a breakpoint in the .net method you mapped, you'll see that it is called, as no mapping is found for the function (as the real one has 2 'e's instead of '3' wink )

Frans Bouma | Lead developer LLBLGen Pro
cipone
User
Posts: 8
Joined: 20-Jul-2010
# Posted on: 27-Jul-2010 13:07:34   

I'll crawl back under my rock now. Thanks a bunch, It worked.