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

Advanced SQL query #3042

Merged
merged 8 commits into from
Mar 26, 2024
Merged

Advanced SQL query #3042

merged 8 commits into from
Mar 26, 2024

Conversation

e-tobi
Copy link
Contributor

@e-tobi e-tobi commented Mar 11, 2024

AdvancedSqlQueryAsync<T, ...>(sql, ...) allows to perform more complex SQL queries than would be possible with Query(sql). It allows to query documents, scalars and deserializable JSON objects or any combination of these returned as a tuple.
This also allows to query for related documents. Metadata mapping, dirty tracking etc. for documents is fully supported. The full SQL must be provided (no "magic" SQL building) so the user is responsible to provide all the columns needed by Marten in the correct order.

Currently result tuples with up to 3 elements are possible (a single result type will not be returned as a tuple). This could be easily extend, but then I would move all the overloads into a partial class. For the same reason I have not created an extra overload for the async methods without a CancellationToken.

I've tried to implement this as least "invasive" as possible. Only IQuerySession and its implementation are extended, the magic happens in the new AdvancedQueryHandler class.

Future improvments might be:

  • More result types
  • Instead of mapping columns by index, map them by name (would require larger changes, especially to the generated code)

@e-tobi e-tobi force-pushed the advanced-sql-query branch 6 times, most recently from 4e40b58 to 58ac8d7 Compare March 15, 2024 19:53
@jeremydmiller
Copy link
Member

I'm going to put this into 7.4 later this week if that's okay. I'm rushing out a bug fix release first

@e-tobi e-tobi force-pushed the advanced-sql-query branch 2 times, most recently from 9a035ec to 1a5c797 Compare March 24, 2024 19:31
Remove token parameter comment on overloades without a CancellationToken
http://jasperfx.github.io/marten/documentation/... -> https://martendb.io/...
The URL's should probably removed - these are the only 3 URLs referencing
the online documentation.
AdvancedSqlQuery<T>(sql, token, parameters) allows to provide
a complete SQL query containing all required columns. The columns
in the select clause must be in the correct order.
...in SystemTextJsonSerializer. The DbDataReader might be implemented
by a NpgsqlNestedDataReader instead of  a NpgsqlDataReader.
Use GetFieldValueAsync<Stream>() instead, which is called by
NpgsqlDataReader.GetStreamAsync(),

See: https://github.com/npgsql/npgsql/blob/fbbb28245ed3475d0857888622939ba7f13cb04f/src/Npgsql/NpgsqlDataReader.cs#L1456
@jeremydmiller
Copy link
Member

This will go into 7.5 shortly. I didn't mean to keep this lingering so long.

@jeremydmiller jeremydmiller merged commit 720d40d into JasperFx:master Mar 26, 2024
11 checks passed
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

Successfully merging this pull request may close these issues.

3 participants