Skip to content
This repository
Browse code

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

…he LINQ provider.
  • Loading branch information...
commit d7e1bc81920803e6f9a864aba9d4c132d6e1e438 1 parent 0defe15
Andrew Theken authored July 02, 2010
22  NoRM.Tests/LinqTests/LinqTests.cs
@@ -20,6 +20,26 @@ public LinqTests()
20 20
                 session.Drop<TestProduct>();
21 21
                 session.Drop<Post>();
22 22
             }
  23
+            using (var db = Mongo.Create(TestHelper.ConnectionString("strict=false")))
  24
+            {
  25
+                db.Database.DropCollection("acmePost");
  26
+            }
  27
+        }
  28
+
  29
+        [Fact]
  30
+        void ProviderDoesntChokeOnCustomCollectionNames()
  31
+        {
  32
+            using (var db = Mongo.Create(TestHelper.ConnectionString()))
  33
+            {
  34
+                var collname = "acmePost";
  35
+                var coll = db.GetCollection<Post>(collname);
  36
+                coll.Insert(new Post { Title = "a" }, new Post { Title = "b" }, new Post { Title = "c" });
  37
+                var results = coll.AsQueryable().Where(y => y.Title == "c").ToArray();
  38
+
  39
+                Assert.Equal("c", results.ElementAt(0).Title);
  40
+
  41
+            }
  42
+
23 43
         }
24 44
 
25 45
         [Fact]
@@ -198,7 +218,7 @@ public void LinqQueriesShouldSupportExternalParameters()
198 218
                 Assert.Equal(false, queryable.QueryStructure().IsComplex);
199 219
             }
200 220
         }
201  
-        
  221
+
202 222
         [Fact]
203 223
         public void LinqQueriesShouldSupportNulls()
204 224
         {
4  NoRM/Collections/MongoCollectionGeneric.cs
@@ -61,7 +61,7 @@ public MongoCollection(string collectionName, MongoDatabase db, IConnection conn
61 61
         /// <returns></returns>
62 62
         public IQueryable<T> AsQueryable()
63 63
         {
64  
-            return new MongoQuery<T>(MongoQueryProvider.Create(this._db), this._collectionName);
  64
+            return new MongoQuery<T>(MongoQueryProvider.Create(this._db, this._collectionName));
65 65
         }
66 66
 
67 67
         /// <summary>
@@ -638,7 +638,7 @@ public IEnumerable<T> Find<U>(U template, int limit, int skip, string fullyQuali
638 638
                 projection = fieldSelection.Compile();
639 639
                 _compiledTransforms[fieldSelection.GetHashCode()] = projection;
640 640
             }
641  
-            return new MongoQueryExecutor<T, U, Z>(qm, (Func<T, Z>)projection);
  641
+            return new MongoQueryExecutor<T, U, Z>(qm, (Func<T, Z>)projection) { CollectionName = this._collectionName };
642 642
         }
643 643
 
644 644
         /// <summary>
16  NoRM/Linq/LinqExtensions.cs
@@ -36,15 +36,18 @@ public static T GetConstantValue<T>(this Expression exp)
36 36
         /// </summary>
37 37
         /// <typeparam retval="T">Type to explain</typeparam>
38 38
         /// <param retval="expression">The expression.</param>
  39
+        /// <remarks>ATT: I *do not* like this, I would like to see this refactored to not do an explicit cast.</remarks>
39 40
         /// <returns>Query explain plan</returns>
40 41
         public static ExplainResponse Explain<T>(this IQueryable<T> expression)
41 42
         {
42  
-            var translator = new MongoQueryTranslator();
43  
-            var translationResults = translator.Translate(expression.Expression, false);
  43
+            var mq = expression as MongoQuery<T>;
44 44
 
45  
-            if (expression is MongoQuery<T>)
  45
+            if (mq != null)
46 46
             {
47  
-                return (expression as MongoQuery<T>).Explain(translationResults.Where);
  47
+                var translator = new MongoQueryTranslator();
  48
+                var translationResults = translator.Translate(expression.Expression, false);
  49
+                translator.CollectionName = mq.CollectionName;
  50
+                return mq.Explain(translationResults.Where);
48 51
             }
49 52
 
50 53
             return null;
@@ -57,13 +60,14 @@ public static ExplainResponse Explain<T>(this IQueryable<T> expression)
57 60
         /// <param retval="find">The type of document being enumerated.</param>
58 61
         /// <param retval="hint">The query hint expression.</param>
59 62
         /// <param retval="direction">Ascending or descending.</param>
  63
+        /// <remarks>ATT: I *do not* like this, I would like to see this refactored to not do an explicit cast.</remarks>
60 64
         /// <returns></returns>
61 65
         public static IEnumerable<T> Hint<T>(this IEnumerable<T> find, Expression<Func<T, object>> hint, IndexOption direction)
62 66
         {
  67
+            var proxy = (MongoQueryExecutor<T, Expando>)find;
63 68
             var translator = new MongoQueryTranslator();
64 69
             var index = translator.Translate(hint);
65  
-
66  
-            var proxy = (MongoQueryExecutor<T, Expando>)find;
  70
+            translator.CollectionName = proxy.CollectionName;
67 71
             proxy.AddHint(index.Query, direction);
68 72
             return find;
69 73
         }
29  NoRM/Linq/MongoQuery.cs
@@ -18,7 +18,8 @@ internal class MongoQuery<T> : IQueryable<T>, IQueryable, IEnumerable<T>, IEnume
18 18
     {
19 19
         private readonly Expression _expression;
20 20
         private readonly MongoQueryProvider _provider;
21  
-        private readonly string _collectionName;
  21
+
  22
+        public String CollectionName { get { return this._provider.CollectionName; } }
22 23
 
23 24
         /// <summary>
24 25
         /// 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
31 32
         /// </param>
32 33
         /// <exception cref="ArgumentNullException">
33 34
         /// </exception>
34  
-        public MongoQuery(MongoQueryProvider provider, string collectionName)
  35
+        public MongoQuery(MongoQueryProvider provider)
35 36
         {
36 37
             if (provider == null)
37 38
             {
@@ -40,19 +41,8 @@ public MongoQuery(MongoQueryProvider provider, string collectionName)
40 41
 
41 42
             _provider = provider;
42 43
             _expression = Expression.Constant(this);
43  
-            _collectionName = collectionName;
44 44
         }
45 45
 
46  
-        /// <summary>
47  
-        /// Initializes a new instance of the <see cref="MongoQuery{T}"/> class.
48  
-        /// </summary>
49  
-        /// <param retval="provider">
50  
-        /// The provider.
51  
-        /// </param>
52  
-        public MongoQuery(MongoQueryProvider provider)
53  
-            : this(provider, MongoConfiguration.GetCollectionName(typeof(T)))
54  
-        {
55  
-        }
56 46
 
57 47
         /// <summary>
58 48
         /// Initializes a new instance of the <see cref="MongoQuery{T}"/> class.
@@ -69,6 +59,7 @@ public MongoQuery(MongoQueryProvider provider)
69 59
         /// </exception>
70 60
         public MongoQuery(MongoQueryProvider provider, Expression expression)
71 61
         {
  62
+           
72 63
             if (provider == null)
73 64
             {
74 65
                 throw new ArgumentNullException("provider");
@@ -83,7 +74,7 @@ public MongoQuery(MongoQueryProvider provider, Expression expression)
83 74
             {
84 75
                 throw new ArgumentOutOfRangeException("expression");
85 76
             }
86  
-
  77
+            
87 78
             _provider = provider;
88 79
             _expression = expression;
89 80
         }
@@ -149,18 +140,14 @@ IEnumerator IEnumerable.GetEnumerator()
149 140
         /// <returns></returns>
150 141
         internal ExplainResponse Explain(Expando query)
151 142
         {
152  
-            var collectionName = MongoConfiguration.GetCollectionName(typeof(T));
153  
-            return this.GetCollection<ExplainResponse>(collectionName).Explain(query);
  143
+           
  144
+            return this.GetCollection<ExplainResponse>(this._provider.CollectionName).Explain(query);
154 145
         }
155 146
 
156 147
         /// <summary>TODO::Description.</summary>
157 148
         private IMongoCollection<TCollection> GetCollection<TCollection>()
158 149
         {
159  
-            var collectionName = _collectionName == string.Empty
160  
-                ? MongoConfiguration.GetCollectionName(typeof(TCollection))
161  
-                : _collectionName;
162  
-
163  
-            return GetCollection<TCollection>(collectionName);
  150
+            return GetCollection<TCollection>(this._provider.CollectionName);
164 151
         }
165 152
 
166 153
         /// <summary>TODO::Description.</summary>
7  NoRM/Linq/MongoQueryProvider.cs
@@ -17,9 +17,9 @@ internal class MongoQueryProvider : IQueryProvider, IMongoQueryResults
17 17
     {
18 18
         private QueryTranslationResults _results;
19 19
 
20  
-        internal static MongoQueryProvider Create(MongoDatabase db)
  20
+        internal static MongoQueryProvider Create(MongoDatabase db, String collectionName)
21 21
         {
22  
-            return new MongoQueryProvider() { DB = db };
  22
+            return new MongoQueryProvider() { DB = db, CollectionName = collectionName };
23 23
         }
24 24
 
25 25
         /// <summary>
@@ -43,6 +43,8 @@ IQueryable<S> IQueryProvider.CreateQuery<S>(Expression expression)
43 43
             return query;
44 44
         }
45 45
 
  46
+        public String CollectionName { get; set; }
  47
+
46 48
         /// <summary>
47 49
         /// The i query provider. create query.
48 50
         /// </summary>
@@ -97,6 +99,7 @@ public object ExecuteQuery<T>(Expression expression)
97 99
             expression = PartialEvaluator.Eval(expression, this.CanBeEvaluatedLocally);
98 100
 
99 101
             var translator = new MongoQueryTranslator();
  102
+            translator.CollectionName = this.CollectionName;
100 103
             var results = translator.Translate(expression);
101 104
             _results = results;
102 105
             var executor = new MongoQueryExecutor(this.DB, results);
5  NoRM/Linq/MongoQueryTranslator.cs
@@ -32,7 +32,7 @@ internal class MongoQueryTranslator : ExpressionVisitor
32 32
 
33 33
         private string AggregatePropName { get; set; }
34 34
         private string TypeName { get; set; }
35  
-        private string CollectionName { get; set; }
  35
+        public string CollectionName { get; set; }
36 36
         private string MethodCall { get; set; }
37 37
 
38 38
         /// <summary>
@@ -616,8 +616,7 @@ protected override Expression VisitConstant(ConstantExpression c)
616 616
             {
617 617
                 // set the collection retval
618 618
                 TypeName = q.ElementType.Name;
619  
-                CollectionName = MongoConfiguration.GetCollectionName(q.ElementType);
620  
-
  619
+                
621 620
                 // this is our Query wrapper - see if it has an expression
622 621
                 var qry = (IMongoQuery)c.Value;
623 622
                 var innerExpression = qry.GetExpression();
2  NoRM/MongoQueryExecutor.cs
@@ -26,6 +26,8 @@ public MongoQueryExecutor(QueryMessage<T,U> message)
26 26
     /// <typeparam retval="O">The output type.</typeparam>
27 27
     public class MongoQueryExecutor<T, U, O> : IEnumerable<O>
28 28
     {
  29
+        internal String CollectionName { get; set; }
  30
+
29 31
         private readonly Expando _hints = new Expando();
30 32
 
31 33
         public MongoQueryExecutor(QueryMessage<T, U> message, Func<T, O> projection)

0 notes on commit d7e1bc8

Please sign in to comment.
Something went wrong with that request. Please try again.