JordanMarr / SqlHydra

SqlHydra is a suite of NuGet packages for working with databases in F# including code generation tools and query expressions.
MIT License
212 stars 21 forks source link

User Defined Functions/ Table Valued Functions #43

Open sydsutton opened 1 year ago

sydsutton commented 1 year ago

Hello,

I was wondering if you would be open to the idea of either adding and/or receiving a PR for User Defined Functions and Table Valued Functions? I use MS SQL Server, and would very much like to use SQL Hydra in conjunction with UDF's/TVF's. I might know someone who can help with the PR if that's the route you decide to go.

Thanks so much.

JordanMarr commented 1 year ago

I assume this feature would exist within the SqlHydra.Query library. Would it involve type generation? Can you give a code example of how you imagine it would be used?

sydsutton commented 1 year ago

Yes, it would involve type generation. And I will get back to you as soon as possible with a code example.

sydsutton commented 1 year ago

This is my current code, just with the table that I'm referencing taking a parameter instead of taking no parameter. In MS SQL Server, the table being called would take the "calculation" that was passed in and do some math based on the calculation. image

JordanMarr commented 1 year ago

What kind of support does Microsoft.Data.SqlClient provide for getting metadata on table valued functions?

JordanMarr commented 1 year ago

While GetSchema / Procedures does list UDFs, including a custom table function that I made, GetSchema / Columns doesn't list any of the returned columns.

So there would need to be a metadata query that returns:

sydsutton commented 1 year ago

-- To get Table Functions
SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'FUNCTION' AND DATA_TYPE = 'TABLE'

-- To get Scalar functions, same as above except filter out DATA_TYPE 'TABLE'
SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'FUNCTION' AND DATA_TYPE <> 'TABLE'

-- To get output columns
SELECT * FROM INFORMATION_SCHEMA.ROUTINE_COLUMNS -- Join on `TABLE_SCHEMA` and `TABLE_NAME`

-- To get input parameters
SELECT * FROM INFORMATION_SCHEMA.PARAMETERS -- Join on `SPECIFIC_SCHEMA` and `SPECIFIC_NAME`
JordanMarr commented 1 year ago

This seems doable. However, there is some refactoring in process that needs to be completed before this can be started.

sydsutton commented 1 year ago

Ok, that sounds good. Thank you for being open to the idea. Please let me know if there's anything else you need from me.

RJSonnenberg commented 4 months ago

@JordanMarr Is there any movement on this? Our team has run into an issue where we have to run both table and scalar functions and get the results. Would this solution be able to support scalar functions in addition to the table functions?

JordanMarr commented 4 months ago

It would be a nice feature to have for sure, but it’s not something I need and therefore no movement. Currently, vanilla ado.net would probably be the easiest way to do it (unless you have a ton of functions, in which case I could understand why you would want this as a feature).

sydsutton commented 4 months ago

@RJSonnenberg I think I ended up using Dapper for this particular issue. I can send you more specifics in the morning if you need them.

JordanMarr commented 4 months ago

This feature looks like it could be challenging to implement because: