Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for handling multiple result sets in a single npgsql command #83

Open
baronfel opened this issue Jan 20, 2021 · 2 comments
Open

Comments

@baronfel
Copy link
Contributor

It's possible in psql to execute multiple commands in a single transaction, returning distinct result sets:

SELECT u.username, ... FROM user u WHERE u.user_id = $1;
SELECT r.rolename, ... FROM role r WHERE r.user_id = $1;

It would be lovely to be able to execute these kinds of queries in Npgsql.FSharp and retain the ability to use the analyzer, etc to maintain compatibility.

Logically, the data reader returned from such queries has embedded 'stop' points between the result sets of such queries, so it's possible to detect and use different mappers for each result set (here's a simple logical example for two queries in the result set):

let reader = <do queries>
let result1 = ResizeArray<_>()
while (reader.Read()) do
	result1.Add(read rowReader1)
reader.NextResult()
let result2 = ResizeArray<_>()
while (reader.Read()) do
	result2.Add(read rowReader2)

return (Seq.toList result1, Seq.toList result2)

Is such a thing possible and/or easy in the current design?

@Zaid-Ajaj
Copy link
Owner

Hi @baronfel, I somehow missed this issue 😓 sorry for the late reply! This is an interesting problem and the challenge really lies in making an API that can be analyzed and then actually analyzing each result. Initial thoughts are

let (users, roles) = 
  connectionString
  |> Sql.connect
  |> Sql.query "<query with multiple result sets>"
  |> Sql.executeReaderTwo { 
       firstResultSet = fun reader -> User.create(...)
       secondResultSet = fun reader -> Role.create(...)
  }

Having a Sql.executeReaderTwo and Sql.executeReaderThree would solve the most common scenarios right?

What do you think?

@aaronmu
Copy link
Contributor

aaronmu commented Aug 19, 2021

ADO.NET 6 will introduce a batching API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants