Skip to content

Commit

Permalink
Additional updates based on PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mikary committed Mar 9, 2015
1 parent 20486a5 commit 71fde95
Show file tree
Hide file tree
Showing 26 changed files with 303 additions and 271 deletions.
10 changes: 10 additions & 0 deletions src/EntityFramework.Core/DbSet`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ IQueryProvider IQueryable.Provider
get { throw new NotImplementedException(); }
}

/// <summary>
/// <para>
/// Gets the <see cref="DbContext" /> instance.
/// </para>
/// </summary>
public virtual DbContext Context
{
get { throw new NotImplementedException(); }
}

/// <summary>
/// <para>
/// Gets the scoped <see cref="IServiceProvider" /> being used to resolve services.
Expand Down
1 change: 0 additions & 1 deletion src/EntityFramework.Core/EntityFramework.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@
<Compile Include="Query\EntityLoadInfo.cs" />
<Compile Include="Query\ExpressionTreeVisitors\ExpressionStringBuilder.cs" />
<Compile Include="Query\ExpressionTreeVisitors\ExpressionTreeVisitorBase.cs" />
<Compile Include="Query\IEntityQueryable.cs" />
<Compile Include="Query\IIncludableQueryable.cs" />
<Compile Include="Infrastructure\DbContextActivator.cs" />
<Compile Include="DbContextOptions.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/EntityFramework.Core/Internal/InternalDbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public override void UpdateRange(IEnumerable<TEntity> entities)

IQueryProvider IQueryable.Provider => _entityQueryable.Value.Provider;

public override DbContext Context => _context;

IServiceProvider IAccessor<IServiceProvider>.Service => ((IAccessor<IServiceProvider>)_context).Service;
}
}
9 changes: 7 additions & 2 deletions src/EntityFramework.Core/Query/EntityQueryable`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Microsoft.Data.Entity.Query
{
public class EntityQueryable<TResult>
: QueryableBase<TResult>, IAsyncEnumerable<TResult>, IEntityQueryable
: QueryableBase<TResult>, IAsyncEnumerable<TResult>, IMetadata
{
private readonly LazyRef<Annotatable> _annotatable
= new LazyRef<Annotatable>(
Expand Down Expand Up @@ -69,7 +69,12 @@ public virtual Annotation GetAnnotation([NotNull]string annotationName)

public override string ToString()
{
return base.ToString() + string.Join(", ", _annotatable.Value.Annotations.Select(annotation => annotation.Value));
return _annotatable.Value.Annotations.Count() == 0
? base.ToString()
: string.Format("{0} ({1})",
base.ToString(),
string.Join(", ", _annotatable.Value.Annotations.Select(annotation =>
string.Format("{0} = {1}", annotation.Name, annotation.Value))));
}

private class Annotatable : MetadataBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ protected EntityQueryableExpressionTreeVisitor([NotNull] EntityQueryModelVisitor

protected override Expression VisitConstantExpression(ConstantExpression constantExpression)
{
var entityQueryable = constantExpression.Value as IEntityQueryable;

if (entityQueryable != null)
if (constantExpression.Type.GetTypeInfo().IsGenericType
&& constantExpression.Type.GetGenericTypeDefinition() == typeof(EntityQueryable<>))
{
return VisitEntityQueryable(entityQueryable.ElementType);
return VisitEntityQueryable(((IQueryable)constantExpression.Value).ElementType);
}

return constantExpression;
Expand Down
12 changes: 0 additions & 12 deletions src/EntityFramework.Core/Query/IEntityQueryable.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@
<Compile Include="IRelationalDataStoreServices.cs" />
<Compile Include="RelationalDbContextOptions.cs" />
<Compile Include="RelationalDbSetExtensions.cs" />
<Compile Include="RelationalDbSet`.cs" />
<Compile Include="RelationalMetadataExtensions.cs" />
<Compile Include="RelationalOptionsExtension.cs" />
<Compile Include="RelationalConnection.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Query;
Expand Down Expand Up @@ -71,13 +72,16 @@ protected override Expression VisitMethodCallExpression([NotNull] MethodCallExpr

protected override Expression VisitConstantExpression(ConstantExpression constantExpression)
{
var entityQueryable = constantExpression.Value as IEntityQueryable;
Check.NotNull(constantExpression, nameof(constantExpression));

if (entityQueryable != null)
if (constantExpression.Type.GetTypeInfo().IsGenericType
&& constantExpression.Type.GetGenericTypeDefinition() == typeof(EntityQueryable<>))
{
if (entityQueryable["sql"] != null)
var sql = ((IMetadata)constantExpression.Value)["Sql"];

if (sql != null)
{
return VisitRawSqlQueryable(entityQueryable.ElementType, entityQueryable["sql"]);
return VisitRawSqlQueryable(((IQueryable)constantExpression.Value).ElementType, sql);
}
}

Expand All @@ -86,42 +90,45 @@ protected override Expression VisitConstantExpression(ConstantExpression constan

protected override Expression VisitEntityQueryable(Type elementType)
{
Check.NotNull(elementType, nameof(elementType));

return VisitSelectExpression(elementType,
(entityType, tableName) =>
(entityType, tableName, alias) =>
new TableExpression(
tableName,
QueryModelVisitor.QueryCompilationContext.GetSchema(entityType),
_querySource.ItemName.StartsWith("<generated>_")
? tableName.First().ToString().ToLower()
: _querySource.ItemName,
alias,
_querySource));
}

protected virtual Expression VisitRawSqlQueryable(Type elementType, string rawSql)
protected virtual Expression VisitRawSqlQueryable(Type elementType, string sql)
{
Check.NotNull(elementType, nameof(elementType));
Check.NotNull(sql, nameof(sql));

return VisitSelectExpression(elementType,
(entityType, tableName) =>
(entityType, tableName, alias) =>
new RawSqlDerivedTableExpression(
rawSql,
_querySource.ItemName.StartsWith("<generated>_")
? tableName.First().ToString().ToLower()
: _querySource.ItemName,
sql,
alias,
_querySource));
}

private Expression VisitSelectExpression(
Type elementType,
Func<IEntityType, string, TableExpressionBase> createTableExpression)
Func<IEntityType, string, string, TableExpressionBase> createTableExpression)
{
Check.NotNull(elementType, nameof(elementType));

var queryMethodInfo = RelationalQueryModelVisitor.CreateValueReaderMethodInfo;
var entityType = QueryModelVisitor.QueryCompilationContext.Model.GetEntityType(elementType);

var selectExpression = new SelectExpression();
var tableName = QueryModelVisitor.QueryCompilationContext.GetTableName(entityType);

selectExpression.AddTable(createTableExpression(entityType, tableName));
var alias = _querySource.ItemName.StartsWith("<generated>_")
? tableName.First().ToString().ToLower()
: _querySource.ItemName;

selectExpression.AddTable(createTableExpression(entityType, tableName, alias));

QueryModelVisitor.AddQuery(_querySource, selectExpression);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ namespace Microsoft.Data.Entity.Relational.Query.Expressions
public class RawSqlDerivedTableExpression : TableExpressionBase
{
public RawSqlDerivedTableExpression(
[NotNull] string rawSql,
[NotNull] string sql,
[NotNull] string alias,
[NotNull] IQuerySource querySource)
: base(
Check.NotNull(querySource, nameof(querySource)),
Check.NotEmpty(alias, nameof(alias)))
{
Check.NotEmpty(rawSql, nameof(rawSql));
Check.NotEmpty(sql, nameof(sql));

RawSql = rawSql;
Sql = sql;
}

public virtual string RawSql { get; }
public virtual string Sql { get; }

public override Expression Accept([NotNull] ExpressionTreeVisitor visitor)
{
Expand All @@ -40,7 +40,7 @@ public override Expression Accept([NotNull] ExpressionTreeVisitor visitor)

public override string ToString()
{
return RawSql + " " + Alias;
return Sql + " " + Alias;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,16 @@ public virtual Expression VisitRawSqlDerivedTableExpression([NotNull] RawSqlDeri
{
Check.NotNull(rawSqlDerivedTableExpression, nameof(rawSqlDerivedTableExpression));

_sql.Append(DelimitSubQuery(rawSqlDerivedTableExpression.RawSql))
.Append(" AS ")
_sql.Append("(")
.AppendLine();

using (_sql.Indent())
{
_sql.Append(rawSqlDerivedTableExpression.Sql);
}

_sql.AppendLine()
.Append(") AS ")
.Append(DelimitIdentifier(rawSqlDerivedTableExpression.Alias));

return rawSqlDerivedTableExpression;
Expand Down Expand Up @@ -271,7 +279,7 @@ public virtual Expression VisitInExpression(InExpression inExpression)
_sql.Append(" IN (");

VisitJoin(inExpression.Values);

_sql.Append(")");

return inExpression;
Expand Down Expand Up @@ -411,8 +419,8 @@ protected override Expression VisitBinaryExpression([NotNull] BinaryExpression b

VisitExpression(binaryExpression.Left);

if (binaryExpression.IsLogicalOperation()
&& binaryExpression.Left is ColumnExpression
if (binaryExpression.IsLogicalOperation()
&& binaryExpression.Left is ColumnExpression
&& _insideFilter.Peek())
{
_sql.Append(" = 1");
Expand Down Expand Up @@ -704,12 +712,5 @@ protected virtual string DelimitIdentifier([NotNull] string identifier)

return "\"" + identifier + "\"";
}

protected virtual string DelimitSubQuery([NotNull] string query)
{
Check.NotEmpty(query, nameof(query));

return "(" + query + ")";
}
}
}
21 changes: 19 additions & 2 deletions src/EntityFramework.Relational/RelationalDbSetExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using System.Linq.Expressions;
using JetBrains.Annotations;
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Data.Entity.Query;
using Microsoft.Data.Entity.Relational;
using Microsoft.Data.Entity.Utilities;
using Microsoft.Framework.DependencyInjection;

// ReSharper disable once CheckNamespace

namespace Microsoft.Data.Entity
{
public static class RelationalDbSetExtensions
{
public static RelationalDbSet<TEntity> AsRelational<TEntity>([NotNull]this DbSet<TEntity> dbSet) where TEntity :class
public static IQueryable<TEntity> FromSql<TEntity>([NotNull]this DbSet<TEntity> dbSet, [NotNull]string query) where TEntity : class
{
Check.NotNull(dbSet, nameof(dbSet));
Check.NotNull(query, nameof(query));

return new RelationalDbSet<TEntity>(dbSet);
if (dbSet.Context.Database as RelationalDatabase == null)
{
throw new InvalidOperationException(Strings.RelationalNotInUse);
}

var queryProvider = ((IAccessor<IServiceProvider>)dbSet).Service.GetRequiredService<EntityQueryProvider>();

var queryable = new EntityQueryable<TEntity>(queryProvider);
queryable.AddAnnotation("Sql", query);

return queryProvider.CreateQuery<TEntity>(Expression.Constant(queryable));
}
}
}
36 changes: 0 additions & 36 deletions src/EntityFramework.Relational/RelationalDbSet`.cs

This file was deleted.

Loading

0 comments on commit 71fde95

Please sign in to comment.