comparison UnitTests/Fluent/MockDataBase/MockCommand.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System.Collections;
2 using System.Collections.Generic;
3 using System.Data;
4 using System.Linq;
5 using System.Text.RegularExpressions;
6
7 namespace BLToolkit.Fluent.Test.MockDataBase
8 {
9 public partial class MockDb
10 {
11 /// <summary>
12 /// IDbCommand
13 /// </summary>
14 private partial class MockCommand : IDbCommand
15 {
16 private readonly MockDb _db;
17 private readonly Regex _findTableRx = new Regex(MockSqlProvider.TableMarker + @"(\w+)");
18 private readonly Regex _findFieldRx = new Regex(MockSqlProvider.FieldMarker + @"(\w+)");
19
20 public MockCommand(MockDb db)
21 {
22 _db = db;
23 Parameters = new DataParameterCollection();
24 }
25
26 public void Dispose()
27 {
28 }
29
30 public void Prepare()
31 {
32 }
33
34 public void Cancel()
35 {
36 }
37
38 public IDbDataParameter CreateParameter()
39 {
40 return new MockDbDataParameter();
41 }
42
43 public int ExecuteNonQuery()
44 {
45 return MockCommandData().NonQueryResult;
46 }
47
48 public IDataReader ExecuteReader()
49 {
50 return new MockReader(MockCommandData(false));
51 }
52
53 public IDataReader ExecuteReader(CommandBehavior behavior)
54 {
55 return ExecuteReader();
56 }
57
58 public object ExecuteScalar()
59 {
60 return MockCommandData().ScalarResult;
61 }
62
63 public IDbConnection Connection { get; set; }
64
65 public IDbTransaction Transaction { get; set; }
66
67 public string CommandText { get; set; }
68
69 public int CommandTimeout { get; set; }
70
71 public CommandType CommandType { get; set; }
72
73 public IDataParameterCollection Parameters { get; private set; }
74
75 public UpdateRowSource UpdatedRowSource { get; set; }
76
77 private MockCommandData MockCommandData(bool isUsing = true)
78 {
79 Dictionary<string, int> fields;
80 List<string> tables;
81 FindFields(CommandText, out fields);
82 FindTables(CommandText, out tables);
83 CommandText = ClearMockMarkers(CommandText);
84
85 var cmd = _db.NextCommand();
86 cmd.CommandText = CommandText;
87 cmd.Parameters = Parameters.Cast<MockDbDataParameter>().ToList();
88 cmd.Fields = fields;
89 cmd.Tables = tables;
90 cmd.IsUsing = isUsing;
91 return cmd;
92 }
93
94 private string ClearMockMarkers(string commandText)
95 {
96 return commandText
97 .Replace(MockSqlProvider.FieldMarker, "")
98 .Replace(MockSqlProvider.TableMarker, "");
99 }
100
101 private void FindTables(string commandText, out List<string> tables)
102 {
103 tables = (from Match match in _findTableRx.Matches(commandText) select match.Groups[1].Value)
104 .Distinct().ToList();
105 }
106
107 private void FindFields(string commandText, out Dictionary<string, int> fields)
108 {
109 fields = (from Match match in _findFieldRx.Matches(commandText) select match.Groups[1].Value)
110 .GroupBy(s => s, (s, ss) => new { Key = s, Value = ss.Count() })
111 .ToDictionary(kv => kv.Key, kv => kv.Value);
112 }
113
114 private class DataParameterCollection : List<MockDbDataParameter>, IDataParameterCollection
115 {
116 public bool Contains(string parameterName)
117 {
118 return this.Any(p => p.ParameterName == parameterName);
119 }
120
121 public int IndexOf(string parameterName)
122 {
123 for (int i = 0; i < Count; i++)
124 {
125 if (this[i].ParameterName == parameterName)
126 {
127 return i;
128 }
129 }
130 return -1;
131 }
132
133 public void RemoveAt(string parameterName)
134 {
135 var data = this.FirstOrDefault(p => p.ParameterName == parameterName);
136 if (null != data)
137 {
138 Remove(data);
139 }
140 }
141
142 public object this[string parameterName]
143 {
144 get
145 {
146 var data = this.FirstOrDefault(p => p.ParameterName == parameterName);
147 return null == data ? null : data.Value;
148 }
149 set
150 {
151 var data = this.FirstOrDefault(p => p.ParameterName == parameterName);
152 if (null == data)
153 {
154 Add(new MockDbDataParameter { ParameterName = parameterName, Value = value });
155 }
156 else
157 {
158 data.Value = value;
159 }
160 }
161 }
162 }
163 }
164 }
165 }