|
1 |
| -/* Copyright 2010-2012 10gen Inc. |
| 1 | +/* Copyright 2010-2013 10gen Inc. |
2 | 2 | *
|
3 | 3 | * Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | * you may not use this file except in compliance with the License.
|
@@ -55,31 +55,37 @@ public static bool ContainsAny<TSource>(this IEnumerable<TSource> source, IEnume
|
55 | 55 | /// <summary>
|
56 | 56 | /// Returns an explanation of how the query was executed (instead of the results).
|
57 | 57 | /// </summary>
|
58 |
| - /// <param name="query">The LINQ query to explain</param> |
| 58 | + /// <param name="source">The LINQ query to explain.</param> |
59 | 59 | /// <returns>An explanation of thow the query was executed.</returns>
|
60 |
| - public static BsonDocument Explain<T>(this IQueryable<T> query) |
| 60 | + public static BsonDocument Explain<T>(this IQueryable<T> source) |
61 | 61 | {
|
62 |
| - return Explain(query, false); |
| 62 | + return Explain(source, false); |
63 | 63 | }
|
64 | 64 |
|
65 | 65 | /// <summary>
|
66 | 66 | /// Returns an explanation of how the query was executed (instead of the results).
|
67 | 67 | /// </summary>
|
68 |
| - /// <param name="query">The LINQ query to explain</param> |
| 68 | + /// <param name="source">The LINQ query to explain</param> |
69 | 69 | /// <param name="verbose">Whether the explanation should contain more details.</param>
|
70 | 70 | /// <returns>An explanation of thow the query was executed.</returns>
|
71 |
| - public static BsonDocument Explain<T>(this IQueryable<T> query, bool verbose) |
| 71 | + public static BsonDocument Explain<T>(this IQueryable<T> source, bool verbose) |
72 | 72 | {
|
73 |
| - var selectQuery = (SelectQuery)MongoQueryTranslator.Translate(query); |
| 73 | + var queryProvider = source.Provider as MongoQueryProvider; |
| 74 | + if (queryProvider == null) |
| 75 | + { |
| 76 | + throw new NotSupportedException("Explain can only be called on a Linq to Mongo queryable."); |
| 77 | + } |
| 78 | + |
| 79 | + var selectQuery = (SelectQuery)MongoQueryTranslator.Translate(queryProvider, source.Expression); |
74 | 80 | if (selectQuery.Take.HasValue && selectQuery.Take.Value == 0)
|
75 | 81 | {
|
76 | 82 | throw new NotSupportedException("A query that has a .Take(0) expression will not be sent to the server and can't be explained");
|
77 | 83 | }
|
78 | 84 | var projector = selectQuery.Execute() as IProjector;
|
79 | 85 | if (projector == null)
|
80 | 86 | {
|
81 |
| - //This is mainly for .Distinct() queries. First, Last, FirstOrDefault, LastOrDefault don't return |
82 |
| - //IQueryable<T>, so .Explain() can't be called on them anyway. |
| 87 | + // this is mainly for .Distinct() queries. First, Last, FirstOrDefault, LastOrDefault don't return |
| 88 | + // IQueryable<T>, so .Explain() can't be called on them anyway. |
83 | 89 | throw new NotSupportedException("Explain can only be called on Linq queries that return an IProjector");
|
84 | 90 | }
|
85 | 91 | return projector.Cursor.Explain(verbose);
|
@@ -111,31 +117,31 @@ public static bool Inject(this IMongoQuery query)
|
111 | 117 | /// Sets an index hint on the query that's being built.
|
112 | 118 | /// </summary>
|
113 | 119 | /// <typeparam name="TSource">The type of the elements of source.</typeparam>
|
114 |
| - /// <param name="query">The query being built.</param> |
| 120 | + /// <param name="source">The query being built.</param> |
115 | 121 | /// <param name="indexName">The name of the index to use.</param>
|
116 | 122 | /// <returns>New query where the expression includes a WithIndex method call.</returns>
|
117 |
| - public static IQueryable<TSource> WithIndex<TSource>(this IQueryable<TSource> query, string indexName) |
| 123 | + public static IQueryable<TSource> WithIndex<TSource>(this IQueryable<TSource> source, string indexName) |
118 | 124 | {
|
119 |
| - return WithIndex(query, (BsonValue)indexName); |
| 125 | + return WithIndex(source, (BsonValue)indexName); |
120 | 126 | }
|
121 | 127 |
|
122 | 128 | /// <summary>
|
123 | 129 | /// Sets an index hint on the query that's being built.
|
124 | 130 | /// </summary>
|
125 | 131 | /// <typeparam name="TSource">The type of the elements of source.</typeparam>
|
126 |
| - /// <param name="query">The query being built.</param> |
| 132 | + /// <param name="source">The query being built.</param> |
127 | 133 | /// <param name="indexHint">Hint for what index to use.</param>
|
128 | 134 | /// <returns>New query where the expression includes a WithIndex method call.</returns>
|
129 |
| - public static IQueryable<TSource> WithIndex<TSource>(this IQueryable<TSource> query, BsonDocument indexHint) |
| 135 | + public static IQueryable<TSource> WithIndex<TSource>(this IQueryable<TSource> source, BsonDocument indexHint) |
130 | 136 | {
|
131 |
| - return WithIndex(query, (BsonValue)indexHint); |
| 137 | + return WithIndex(source, (BsonValue)indexHint); |
132 | 138 | }
|
133 | 139 |
|
134 | 140 | // private static methods
|
135 |
| - private static IQueryable<TSource> WithIndex<TSource>(IQueryable<TSource> query, BsonValue index) |
| 141 | + private static IQueryable<TSource> WithIndex<TSource>(IQueryable<TSource> query, BsonValue indexHint) |
136 | 142 | {
|
137 | 143 | var method = ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(typeof(TSource));
|
138 |
| - var args = new[] { query.Expression, Expression.Constant(index) }; |
| 144 | + var args = new[] { query.Expression, Expression.Constant(indexHint) }; |
139 | 145 | var expression = Expression.Call(null, method, args);
|
140 | 146 | return query.Provider.CreateQuery<TSource>(expression);
|
141 | 147 | }
|
|
0 commit comments