|
| 1 | +namespace Simple.Data.QueryPolyfills |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Concurrent; |
| 5 | + using System.Collections.Generic; |
| 6 | + using System.Linq; |
| 7 | + |
| 8 | + internal class DictionaryQueryRunner |
| 9 | + { |
| 10 | + private static readonly |
| 11 | + Dictionary<Type, Func<SimpleQueryClauseBase, IEnumerable<IDictionary<string, object>>, IEnumerable<IDictionary<string, object>>>> ClauseHandlers = |
| 12 | + new Dictionary<Type, Func<SimpleQueryClauseBase, IEnumerable<IDictionary<string, object>>, IEnumerable<IDictionary<string, object>>>> |
| 13 | + { |
| 14 | + { typeof(DistinctClause), (c,d) => d.Distinct(new DictionaryEqualityComparer()) }, |
| 15 | + { typeof(SkipClause), (c,d) => d.Skip(((SkipClause)c).Count) }, |
| 16 | + { typeof(TakeClause), (c,d) => d.Take(((TakeClause)c).Count) }, |
| 17 | + { typeof(SelectClause), (c,d) => new SelectClauseHandler((SelectClause)c).Run(d) }, |
| 18 | + }; |
| 19 | + |
| 20 | + private readonly IEnumerable<IDictionary<string, object>> _source; |
| 21 | + private readonly IList<SimpleQueryClauseBase> _clauses; |
| 22 | + private readonly WithCountClause _withCountClause; |
| 23 | + |
| 24 | + public DictionaryQueryRunner(IEnumerable<IDictionary<string, object>> source, IEnumerable<SimpleQueryClauseBase> clauses) |
| 25 | + { |
| 26 | + _source = source; |
| 27 | + _clauses = clauses.ToList(); |
| 28 | + _withCountClause = _clauses.OfType<WithCountClause>().FirstOrDefault(); |
| 29 | + if (_withCountClause != null) _clauses.Remove(_withCountClause); |
| 30 | + } |
| 31 | + |
| 32 | + public DictionaryQueryRunner(IEnumerable<IDictionary<string, object>> source, params SimpleQueryClauseBase[] clauses) |
| 33 | + : this(source, clauses.AsEnumerable()) |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + public IEnumerable<IDictionary<string, object>> Run() |
| 38 | + { |
| 39 | + IEnumerable<IDictionary<string, object>> source; |
| 40 | + if (_withCountClause != null) |
| 41 | + { |
| 42 | + source = _source.ToList(); |
| 43 | + _withCountClause.SetCount(source.Count()); |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + source = _source; |
| 48 | + } |
| 49 | + |
| 50 | + foreach (var clause in _clauses) |
| 51 | + { |
| 52 | + Func<SimpleQueryClauseBase, IEnumerable<IDictionary<string, object>>, IEnumerable<IDictionary<string, object>>> handler; |
| 53 | + if (ClauseHandlers.TryGetValue(clause.GetType(), out handler)) |
| 54 | + { |
| 55 | + source = handler(clause, source); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return source; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + class SelectClauseHandler |
| 64 | + { |
| 65 | + private readonly IList<SimpleReference> _references; |
| 66 | + private readonly IList<ObjectReference> _objectReferences; |
| 67 | + private Func<int, IDictionary<string, object>> _creator; |
| 68 | + |
| 69 | + public SelectClauseHandler(SelectClause clause) |
| 70 | + { |
| 71 | + _references = clause.Columns.ToList(); |
| 72 | + _objectReferences = _references.Select(r => r as ObjectReference).ToList(); |
| 73 | + } |
| 74 | + |
| 75 | + public IEnumerable<IDictionary<string,object>> Run(IEnumerable<IDictionary<string,object>> source) |
| 76 | + { |
| 77 | + return source.Select(Run); |
| 78 | + } |
| 79 | + |
| 80 | + private IDictionary<string,object> Run(IDictionary<string,object> source) |
| 81 | + { |
| 82 | + if (_creator == null) _creator = CreateCreator(source); |
| 83 | + var target = _creator(_references.Count); |
| 84 | + for (int i = 0; i < _objectReferences.Count; i++) |
| 85 | + { |
| 86 | + if (!ReferenceEquals(_objectReferences[i], null)) |
| 87 | + { |
| 88 | + var name = _objectReferences[i].GetName(); |
| 89 | + target[name] = source[name]; |
| 90 | + } |
| 91 | + } |
| 92 | + return target; |
| 93 | + } |
| 94 | + |
| 95 | + private Func<int, IDictionary<string, object>> CreateCreator(IDictionary<string,object> source) |
| 96 | + { |
| 97 | + var dictionary = source as Dictionary<string, object>; |
| 98 | + if (dictionary != null) return cap => new Dictionary<string, object>(cap, dictionary.Comparer); |
| 99 | + var sortedDictionary = source as SortedDictionary<string, object>; |
| 100 | + if (sortedDictionary != null) return cap => new SortedDictionary<string, object>(sortedDictionary.Comparer); |
| 101 | + var concurrentDictionary = source as ConcurrentDictionary<string, object>; |
| 102 | + if (concurrentDictionary != null) return cap => new ConcurrentDictionary<string, object>(); |
| 103 | + |
| 104 | + var type = source.GetType(); |
| 105 | + return cap => (IDictionary<string, object>) Activator.CreateInstance(type); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments