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 RelationalDbSet for raw query #1734

Closed
wants to merge 3 commits into from
Closed

Add RelationalDbSet for raw query #1734

wants to merge 3 commits into from

Conversation

mikary
Copy link
Contributor

@mikary mikary commented Mar 2, 2015

Add custom relational queries off of DbSet to provide functionality for #624
@ajcvickers @anpete @divega

@ghost ghost added the cla-not-required label Mar 2, 2015
@@ -11,7 +11,7 @@

namespace Microsoft.Framework.DependencyInjection
{
internal static class ServiceProviderExtensions
public static class ServiceProviderExtensions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need these, share them, rather than making this public.

@anpete
Copy link
Contributor

anpete commented Mar 2, 2015

Can you please describe what's in this change?

@@ -148,5 +158,82 @@ var keyFactory
Expression.Call(queryMethodInfo, queryMethodArguments),
_readerParameter));
}

protected Expression VisitRelationalCustomQueryable(string sql, Type elementType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't duplicate the code below. Refactor both into something that pivots on the source table expr.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we want an optional expression parameter so the existing code path can use the element type as it was but we still have a way to pass in the query string?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, just extract a new method and call it from both places. It may be DRY'er to have the new method take a Func as a factory for the table expr. but I would see what it looks like to just build the table expr. externally first.

@mikary
Copy link
Contributor Author

mikary commented Mar 5, 2015

Updated based on PR feedback


public override string ToString()
{
return base.ToString() + string.Join(", ", _annotatable.Value.Annotations.Select(annotation => annotation.Value));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix the formatting - e.g. space between base.ToString() and annotations, maybe wrap annotations in some kind of parenthesis.

@mikary mikary force-pushed the dbSetRawQuery branch 3 times, most recently from 0f4b83e to 71fde95 Compare March 9, 2015 21:57
@mikary
Copy link
Contributor Author

mikary commented Mar 9, 2015

More updates based on PR input


SELECT [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[CustomerID], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM (
SELECT * FROM Customers WHERE Customers.City = 'Seattle'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the raw SQL query contains multiple lines, will they all be indented the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't any logic in place to do this right now, did we want to try?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be easy if you use a StringReader to read each line and append to the output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we need to worry about literal line-breaks in quoted strings etc.?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you can do that - at least in TSQL.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is not too bad, but the alternative is to not worry about it at all and not try to indent raw SQL.

@divega
Copy link
Contributor

divega commented Mar 10, 2015

@mikary @anpete @maumar Is there any more reviewing happening on this or are we good with it? I think we should figure out handling parameters (unless I am missing something) but that could go in a different commit.


public override string ToString()
{
return Sql + " " + Alias;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"(" + Sql + ") AS " + Alias?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't seem very DRY to me, since this would be guessing at the interpretation of the SQL generator that consumes this object.

The current implementation is consistent with the other TableExpressions, but we could revisit this if it's important / useful enough.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just for debugging so do whatever makes it clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added #1795 to track this

@mikary
Copy link
Contributor Author

mikary commented Mar 10, 2015

Updated based on feedback


public virtual Annotation AddAnnotation([NotNull] string annotationName, [NotNull] string value)
{
Check.NotNull(annotationName, nameof(annotationName));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NotEmpty

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a stronger constraint than we have on Annotatable itself, do we want to update that as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, seems like an oversight.

@mikary
Copy link
Contributor Author

mikary commented Mar 12, 2015

Rebased, squashed, updated, and ready for another pass.
Of note:

  • Simplified extension method
  • DataStore now has an InSupported(IAnnotation) method that is checked when the query is compiled
  • Simplified Query Tree Visitor (added IAnnotatable parameter to constant expression and simplified implementation logic)

@mikary
Copy link
Contributor Author

mikary commented Mar 13, 2015

After talk with @anpete, looking at using QueryAnnotations to store the FromSql string and parameters.

@divega
Copy link
Contributor

divega commented Mar 13, 2015

@mikary @anpete Are we going to check in what we have and then do that or just keep iterating on this PR?

@anpete
Copy link
Contributor

anpete commented Mar 13, 2015

This is mostly a rewrite so a new PR.

@mikary
Copy link
Contributor Author

mikary commented Mar 16, 2015

Closing PR, will make a new one for the next revision.

@mikary mikary closed this Mar 16, 2015
@mikary mikary deleted the dbSetRawQuery branch March 19, 2015 00:06
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.

5 participants