Permalink
Browse files

Resolved issue where collection names were not being adhered to via t…

…he LINQ provider.
  • Loading branch information...
1 parent 0defe15 commit d7e1bc81920803e6f9a864aba9d4c132d6e1e438 @atheken committed Jul 2, 2010
@@ -20,6 +20,26 @@ public LinqTests()
session.Drop<TestProduct>();
session.Drop<Post>();
}
+ using (var db = Mongo.Create(TestHelper.ConnectionString("strict=false")))
+ {
+ db.Database.DropCollection("acmePost");
+ }
+ }
+
+ [Fact]
+ void ProviderDoesntChokeOnCustomCollectionNames()
+ {
+ using (var db = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ var collname = "acmePost";
+ var coll = db.GetCollection<Post>(collname);
+ coll.Insert(new Post { Title = "a" }, new Post { Title = "b" }, new Post { Title = "c" });
+ var results = coll.AsQueryable().Where(y => y.Title == "c").ToArray();
+
+ Assert.Equal("c", results.ElementAt(0).Title);
+
+ }
+
}
[Fact]
@@ -198,7 +218,7 @@ public void LinqQueriesShouldSupportExternalParameters()
Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
-
+
[Fact]
public void LinqQueriesShouldSupportNulls()
{
@@ -61,7 +61,7 @@ public MongoCollection(string collectionName, MongoDatabase db, IConnection conn
/// <returns></returns>
public IQueryable<T> AsQueryable()
{
- return new MongoQuery<T>(MongoQueryProvider.Create(this._db), this._collectionName);
+ return new MongoQuery<T>(MongoQueryProvider.Create(this._db, this._collectionName));
}
/// <summary>
@@ -638,7 +638,7 @@ public IEnumerable<T> Find<U>(U template, int limit, int skip, string fullyQuali
projection = fieldSelection.Compile();
_compiledTransforms[fieldSelection.GetHashCode()] = projection;
}
- return new MongoQueryExecutor<T, U, Z>(qm, (Func<T, Z>)projection);
+ return new MongoQueryExecutor<T, U, Z>(qm, (Func<T, Z>)projection) { CollectionName = this._collectionName };
}
/// <summary>
@@ -36,15 +36,18 @@ public static T GetConstantValue<T>(this Expression exp)
/// </summary>
/// <typeparam retval="T">Type to explain</typeparam>
/// <param retval="expression">The expression.</param>
+ /// <remarks>ATT: I *do not* like this, I would like to see this refactored to not do an explicit cast.</remarks>
/// <returns>Query explain plan</returns>
public static ExplainResponse Explain<T>(this IQueryable<T> expression)
{
- var translator = new MongoQueryTranslator();
- var translationResults = translator.Translate(expression.Expression, false);
+ var mq = expression as MongoQuery<T>;
- if (expression is MongoQuery<T>)
+ if (mq != null)
{
- return (expression as MongoQuery<T>).Explain(translationResults.Where);
+ var translator = new MongoQueryTranslator();
+ var translationResults = translator.Translate(expression.Expression, false);
+ translator.CollectionName = mq.CollectionName;
+ return mq.Explain(translationResults.Where);
}
return null;
@@ -57,13 +60,14 @@ public static ExplainResponse Explain<T>(this IQueryable<T> expression)
/// <param retval="find">The type of document being enumerated.</param>
/// <param retval="hint">The query hint expression.</param>
/// <param retval="direction">Ascending or descending.</param>
+ /// <remarks>ATT: I *do not* like this, I would like to see this refactored to not do an explicit cast.</remarks>
/// <returns></returns>
public static IEnumerable<T> Hint<T>(this IEnumerable<T> find, Expression<Func<T, object>> hint, IndexOption direction)
{
+ var proxy = (MongoQueryExecutor<T, Expando>)find;
var translator = new MongoQueryTranslator();
var index = translator.Translate(hint);
-
- var proxy = (MongoQueryExecutor<T, Expando>)find;
+ translator.CollectionName = proxy.CollectionName;
proxy.AddHint(index.Query, direction);
return find;
}
View
@@ -18,7 +18,8 @@ internal class MongoQuery<T> : IQueryable<T>, IQueryable, IEnumerable<T>, IEnume
{
private readonly Expression _expression;
private readonly MongoQueryProvider _provider;
- private readonly string _collectionName;
+
+ public String CollectionName { get { return this._provider.CollectionName; } }
/// <summary>
/// Initializes a new instance of the <see cref="MongoQuery{T}"/> class.
@@ -31,7 +32,7 @@ internal class MongoQuery<T> : IQueryable<T>, IQueryable, IEnumerable<T>, IEnume
/// </param>
/// <exception cref="ArgumentNullException">
/// </exception>
- public MongoQuery(MongoQueryProvider provider, string collectionName)
+ public MongoQuery(MongoQueryProvider provider)
{
if (provider == null)
{
@@ -40,19 +41,8 @@ public MongoQuery(MongoQueryProvider provider, string collectionName)
_provider = provider;
_expression = Expression.Constant(this);
- _collectionName = collectionName;
}
- /// <summary>
- /// Initializes a new instance of the <see cref="MongoQuery{T}"/> class.
- /// </summary>
- /// <param retval="provider">
- /// The provider.
- /// </param>
- public MongoQuery(MongoQueryProvider provider)
- : this(provider, MongoConfiguration.GetCollectionName(typeof(T)))
- {
- }
/// <summary>
/// Initializes a new instance of the <see cref="MongoQuery{T}"/> class.
@@ -69,6 +59,7 @@ public MongoQuery(MongoQueryProvider provider)
/// </exception>
public MongoQuery(MongoQueryProvider provider, Expression expression)
{
+
if (provider == null)
{
throw new ArgumentNullException("provider");
@@ -83,7 +74,7 @@ public MongoQuery(MongoQueryProvider provider, Expression expression)
{
throw new ArgumentOutOfRangeException("expression");
}
-
+
_provider = provider;
_expression = expression;
}
@@ -149,18 +140,14 @@ IEnumerator IEnumerable.GetEnumerator()
/// <returns></returns>
internal ExplainResponse Explain(Expando query)
{
- var collectionName = MongoConfiguration.GetCollectionName(typeof(T));
- return this.GetCollection<ExplainResponse>(collectionName).Explain(query);
+
+ return this.GetCollection<ExplainResponse>(this._provider.CollectionName).Explain(query);
}
/// <summary>TODO::Description.</summary>
private IMongoCollection<TCollection> GetCollection<TCollection>()
{
- var collectionName = _collectionName == string.Empty
- ? MongoConfiguration.GetCollectionName(typeof(TCollection))
- : _collectionName;
-
- return GetCollection<TCollection>(collectionName);
+ return GetCollection<TCollection>(this._provider.CollectionName);
}
/// <summary>TODO::Description.</summary>
@@ -17,9 +17,9 @@ internal class MongoQueryProvider : IQueryProvider, IMongoQueryResults
{
private QueryTranslationResults _results;
- internal static MongoQueryProvider Create(MongoDatabase db)
+ internal static MongoQueryProvider Create(MongoDatabase db, String collectionName)
{
- return new MongoQueryProvider() { DB = db };
+ return new MongoQueryProvider() { DB = db, CollectionName = collectionName };
}
/// <summary>
@@ -43,6 +43,8 @@ IQueryable<S> IQueryProvider.CreateQuery<S>(Expression expression)
return query;
}
+ public String CollectionName { get; set; }
+
/// <summary>
/// The i query provider. create query.
/// </summary>
@@ -97,6 +99,7 @@ public object ExecuteQuery<T>(Expression expression)
expression = PartialEvaluator.Eval(expression, this.CanBeEvaluatedLocally);
var translator = new MongoQueryTranslator();
+ translator.CollectionName = this.CollectionName;
var results = translator.Translate(expression);
_results = results;
var executor = new MongoQueryExecutor(this.DB, results);
@@ -32,7 +32,7 @@ internal class MongoQueryTranslator : ExpressionVisitor
private string AggregatePropName { get; set; }
private string TypeName { get; set; }
- private string CollectionName { get; set; }
+ public string CollectionName { get; set; }
private string MethodCall { get; set; }
/// <summary>
@@ -616,8 +616,7 @@ protected override Expression VisitConstant(ConstantExpression c)
{
// set the collection retval
TypeName = q.ElementType.Name;
- CollectionName = MongoConfiguration.GetCollectionName(q.ElementType);
-
+
// this is our Query wrapper - see if it has an expression
var qry = (IMongoQuery)c.Value;
var innerExpression = qry.GetExpression();
@@ -26,6 +26,8 @@ public MongoQueryExecutor(QueryMessage<T,U> message)
/// <typeparam retval="O">The output type.</typeparam>
public class MongoQueryExecutor<T, U, O> : IEnumerable<O>
{
+ internal String CollectionName { get; set; }
+
private readonly Expando _hints = new Expando();
public MongoQueryExecutor(QueryMessage<T, U> message, Func<T, O> projection)

0 comments on commit d7e1bc8

Please sign in to comment.