0
|
1 using System;
|
|
2 using System.Collections;
|
|
3 using System.Data;
|
|
4 using System.Data.SqlClient;
|
|
5 using System.Data.SqlTypes;
|
|
6 using System.IO;
|
|
7 using System.Linq;
|
|
8 using System.Xml;
|
|
9 using BLToolkit.Data.DataProvider;
|
|
10 using NUnit.Framework;
|
|
11
|
|
12 using BLToolkit.Data;
|
|
13 using BLToolkit.EditableObjects;
|
|
14 using BLToolkit.Mapping;
|
|
15 using BLToolkit.Reflection;
|
|
16
|
|
17 namespace Data
|
|
18 {
|
|
19 [TestFixture]
|
|
20 public class DbManagerTest
|
|
21 {
|
|
22 public enum Gender
|
|
23 {
|
|
24 [MapValue("F")] Female,
|
|
25 [MapValue("M")] Male,
|
|
26 [MapValue("U")] Unknown,
|
|
27 [MapValue("O")] Other
|
|
28 }
|
|
29
|
|
30 public class Person
|
|
31 {
|
|
32 [MapField("PersonID")]
|
|
33 public int ID;
|
|
34 public string FirstName;
|
|
35 public string MiddleName;
|
|
36 public string LastName;
|
|
37 public Gender Gender;
|
|
38 }
|
|
39
|
|
40 public class DataTypeTest
|
|
41 {
|
|
42 [MapField("DataTypeID")]
|
|
43 public int ID;
|
|
44 [MapIgnore(false)]
|
|
45 public Byte[] Binary_;
|
|
46 #if !ORACLE
|
|
47 // Oracle does not know boolean nor guid.
|
|
48 //
|
|
49 public Boolean Boolean_;
|
|
50 public Guid Guid_;
|
|
51 #endif
|
|
52 public Byte Byte_;
|
|
53 [MapIgnore(false)]
|
|
54 public Byte[] Bytes_;
|
|
55 public DateTime DateTime_;
|
|
56 public Decimal Decimal_;
|
|
57 public Double Double_;
|
|
58 public Int16 Int16_;
|
|
59 public Int32 Int32_;
|
|
60 public Int64 Int64_;
|
|
61 public Decimal Money_;
|
|
62 public Single Single_;
|
|
63 public String String_;
|
|
64
|
|
65 public Char Char_;
|
|
66 public SByte SByte_;
|
|
67 public UInt16 UInt16_;
|
|
68 public UInt32 UInt32_;
|
|
69 public UInt64 UInt64_;
|
|
70 #if !SQLCE
|
|
71 [MapIgnore(false)]
|
|
72 public Stream Stream_;
|
|
73 [MapIgnore]
|
|
74 public XmlReader Xml_;
|
|
75 [MapField("Xml_")]
|
|
76 public XmlDocument XmlDoc_;
|
|
77 #endif
|
|
78 }
|
|
79
|
|
80 public class DataTypeSqlTest
|
|
81 {
|
|
82 [MapField("DataTypeID")]
|
|
83 public int ID;
|
|
84 public SqlBinary Binary_;
|
|
85 public SqlBoolean Boolean_;
|
|
86 public SqlByte Byte_;
|
|
87 public SqlDateTime DateTime_;
|
|
88 public SqlDecimal Decimal_;
|
|
89 public SqlDouble Double_;
|
|
90 public SqlGuid Guid_;
|
|
91 public SqlInt16 Int16_;
|
|
92 public SqlInt32 Int32_;
|
|
93 public SqlInt64 Int64_;
|
|
94 public SqlMoney Money_;
|
|
95 public SqlSingle Single_;
|
|
96 public SqlString String_;
|
|
97 #if !SQLCE
|
|
98 [MapIgnore(false)]
|
|
99 public SqlBytes Bytes_;
|
|
100 [MapIgnore(false)]
|
|
101 public SqlChars Chars_;
|
|
102 [MapIgnore(false)]
|
|
103 public SqlXml Xml_;
|
|
104 #endif
|
|
105 }
|
|
106
|
|
107 [Test]
|
|
108 public void ExecuteList1()
|
|
109 {
|
|
110 using (DbManager db = new DbManager())
|
|
111 {
|
|
112 ArrayList list = db
|
|
113 .SetCommand("SELECT * FROM Person")
|
|
114 .ExecuteList(typeof(Person));
|
|
115
|
|
116 Assert.IsNotEmpty(list);
|
|
117 }
|
|
118 }
|
|
119
|
|
120 [Test]
|
|
121 public void ExecuteList2()
|
|
122 {
|
|
123 using (DbManager db = new DbManager())
|
|
124 {
|
|
125 IList list = db
|
|
126 .SetCommand("SELECT * FROM Person")
|
|
127 .ExecuteList(new EditableArrayList(typeof(Person)), typeof(Person));
|
|
128
|
|
129 Assert.IsNotEmpty(list);
|
|
130 }
|
|
131 }
|
|
132
|
|
133 [Test]
|
|
134 public void ExecuteObject()
|
|
135 {
|
|
136 using (DbManager db = new DbManager())
|
|
137 {
|
|
138 Person p = (Person)db
|
|
139 .SetCommand("SELECT * FROM Person WHERE PersonID = " + db.DataProvider.Convert("id", ConvertType.NameToQueryParameter),
|
|
140 db.Parameter("id", 1))
|
|
141 .ExecuteObject(typeof(Person));
|
|
142
|
|
143 TypeAccessor.WriteConsole(p);
|
|
144 }
|
|
145 }
|
|
146
|
|
147 [Test]
|
|
148 public void ExecuteObject2()
|
|
149 {
|
|
150 using (DbManager db = new DbManager())
|
|
151 {
|
|
152 DataTypeTest dt = (DataTypeTest)db
|
|
153 .SetCommand("SELECT * FROM DataTypeTest WHERE DataTypeID = " + db.DataProvider.Convert("id", ConvertType.NameToQueryParameter),
|
|
154 db.Parameter("id", 2))
|
|
155 .ExecuteObject(typeof(DataTypeTest));
|
|
156
|
|
157 TypeAccessor.WriteConsole(dt);
|
|
158 }
|
|
159 }
|
|
160
|
|
161 #if !ORACLE
|
|
162 [Test]
|
|
163 #endif
|
|
164 public void ExecuteObject2Sql()
|
|
165 {
|
|
166 using (DbManager db = new DbManager())
|
|
167 {
|
|
168 DataTypeSqlTest dt = (DataTypeSqlTest)db
|
|
169 .SetCommand("SELECT * FROM DataTypeTest WHERE DataTypeID = " + db.DataProvider.Convert("id", ConvertType.NameToQueryParameter),
|
|
170 db.Parameter("id", 2))
|
|
171 .ExecuteObject(typeof(DataTypeSqlTest));
|
|
172
|
|
173 TypeAccessor.WriteConsole(dt);
|
|
174 }
|
|
175 }
|
|
176
|
|
177 #if MSSQL
|
|
178 [Test]
|
|
179 #endif
|
|
180 public void NativeConnection()
|
|
181 {
|
|
182 string connectionString = DbManager.GetConnectionString(null);
|
|
183
|
|
184 using (DbManager db = new DbManager(new SqlConnection(connectionString)))
|
|
185 {
|
|
186 db
|
|
187 .SetSpCommand ("Person_SelectByName",
|
|
188 db.Parameter("@firstName", "John"),
|
|
189 db.Parameter("@lastName", "Pupkin"))
|
|
190 .ExecuteScalar();
|
|
191 }
|
|
192 }
|
|
193
|
|
194 public class OutRefTest
|
|
195 {
|
|
196 public int ID = 5;
|
|
197 public int outputID;
|
|
198 public int inputOutputID = 10;
|
|
199 public string str = "5";
|
|
200 public string outputStr;
|
|
201 public string inputOutputStr = "10";
|
|
202 }
|
|
203
|
|
204 #if !ACCESS && !SQLCE && !SQLITE
|
|
205 [Test]
|
|
206 public void MapOutput()
|
|
207 {
|
|
208 OutRefTest o = new OutRefTest();
|
|
209
|
|
210 using (DbManager db = new DbManager())
|
|
211 {
|
|
212 db
|
|
213 .SetSpCommand("OutRefTest", db.CreateParameters(o,
|
|
214 new string[] { "outputID", "outputStr" },
|
|
215 new string[] { "inputOutputID", "inputOutputStr" },
|
|
216 null))
|
|
217 .ExecuteNonQuery(o);
|
|
218 }
|
|
219
|
|
220 Assert.AreEqual(5, o.outputID);
|
|
221 Assert.AreEqual(15, o.inputOutputID);
|
|
222 Assert.AreEqual("5", o.outputStr);
|
|
223 Assert.AreEqual("510", o.inputOutputStr);
|
|
224 }
|
|
225
|
|
226 public class ReturnParameter
|
|
227 {
|
|
228 public int Value;
|
|
229 }
|
|
230
|
|
231 [Test]
|
|
232 public void MapReturnValue()
|
|
233 {
|
|
234 ReturnParameter e = new ReturnParameter();
|
|
235
|
|
236 using (DbManager db = new DbManager())
|
|
237 {
|
|
238 db
|
|
239 .SetSpCommand("Scalar_ReturnParameter")
|
|
240 .ExecuteNonQuery("Value", e);
|
|
241 }
|
|
242
|
|
243 Assert.AreEqual(12345, e.Value);
|
|
244 }
|
|
245
|
|
246 [Test]
|
|
247 public void InsertAndMapBack()
|
|
248 {
|
|
249 Person e = new Person();
|
|
250 e.FirstName = "Crazy";
|
|
251 e.LastName = "Frog";
|
|
252 e.Gender = Gender.Other;
|
|
253
|
|
254 using (DbManager db = new DbManager())
|
|
255 {
|
|
256 db
|
|
257 .SetSpCommand("Person_Insert", db.CreateParameters(e, new string[] { "PersonID" }, null, null))
|
|
258 .ExecuteObject(e);
|
|
259
|
|
260 Assert.IsTrue(e.ID > 0);
|
|
261
|
|
262 // Cleanup.
|
|
263 //
|
|
264 db
|
|
265 .SetSpCommand("Person_Delete", db.CreateParameters(e))
|
|
266 .ExecuteNonQuery();
|
|
267 }
|
|
268 }
|
|
269
|
|
270 [Test]
|
|
271 public void MapDataRow()
|
|
272 {
|
|
273 DataTable dataTable = new DataTable();
|
|
274 dataTable.Columns.Add("ID", typeof(int));
|
|
275 dataTable.Columns.Add("outputID", typeof(int));
|
|
276 dataTable.Columns.Add("inputOutputID", typeof(int));
|
|
277 dataTable.Columns.Add("str", typeof(string));
|
|
278 dataTable.Columns.Add("outputStr", typeof(string));
|
|
279 dataTable.Columns.Add("inputOutputStr", typeof(string));
|
|
280
|
|
281 DataRow dataRow = dataTable.Rows.Add(new object[]{5, 0, 10, "5", null, "10"});
|
|
282
|
|
283 using (DbManager db = new DbManager())
|
|
284 {
|
|
285 db
|
|
286 .SetSpCommand("OutRefTest", db.CreateParameters(dataRow,
|
|
287 new string[] { "outputID", "outputStr" },
|
|
288 new string[] { "inputOutputID", "inputOutputStr" },
|
|
289 null))
|
|
290 .ExecuteNonQuery(dataRow);
|
|
291 }
|
|
292
|
|
293 Assert.AreEqual(5, dataRow["outputID"]);
|
|
294 Assert.AreEqual(15, dataRow["inputOutputID"]);
|
|
295 Assert.AreEqual("5", dataRow["outputStr"]);
|
|
296 Assert.AreEqual("510", dataRow["inputOutputStr"]);
|
|
297 }
|
|
298 #endif
|
|
299
|
|
300 [Test]
|
|
301 public void CreateParametersTest()
|
|
302 {
|
|
303 using (var db = new DbManager())
|
|
304 {
|
|
305 var dt = new DataTypeTest
|
|
306 {
|
|
307 ID = 12345,
|
|
308 Binary_ = new byte[2] {1, 2},
|
|
309 #if !ORACLE
|
|
310 Boolean_ = true,
|
|
311 Guid_ = Guid.Empty,
|
|
312 #endif
|
|
313 Byte_ = 250,
|
|
314 Bytes_ = new byte[] { 2, 1 },
|
|
315 DateTime_ = DateTime.Now,
|
|
316 Decimal_ = 9876543210.0m,
|
|
317 Double_ = 12345.67890,
|
|
318 Int16_ = 12345,
|
|
319 Int32_ = 1234567890,
|
|
320 Int64_ = 1234567890123456789,
|
|
321 Money_ = 99876543210.0m,
|
|
322 Single_ = 1234.0f,
|
|
323 String_ = "Crazy Frog",
|
|
324
|
|
325 Char_ = 'F',
|
|
326 SByte_ = 123,
|
|
327 //UInt16_ = 65432,
|
|
328 //UInt32_ = 4000000000,
|
|
329 //UInt64_ = 12345678901234567890,
|
|
330 #if !SQLCE
|
|
331 Stream_ = new MemoryStream(5),
|
|
332 Xml_ = new XmlTextReader(new StringReader("<xml/>")),
|
|
333 XmlDoc_ = new XmlDocument(),
|
|
334 #endif
|
|
335 };
|
|
336
|
|
337 dt.XmlDoc_.LoadXml("<xmldoc/>");
|
|
338
|
|
339 var parameters = db.CreateParameters(dt);
|
|
340
|
|
341 Assert.IsNotNull(parameters);
|
|
342 Assert.AreEqual(ObjectMapper<DataTypeTest>.Instance.Count, parameters.Length);
|
|
343
|
|
344 foreach (MemberMapper mm in ObjectMapper<DataTypeTest>.Instance)
|
|
345 {
|
|
346 var paramName = (string)db.DataProvider.Convert(mm.Name, db.GetConvertTypeToParameter());
|
|
347 var p = parameters.First(obj => obj.ParameterName == paramName);
|
|
348
|
|
349 Assert.IsNotNull(p);
|
|
350 Assert.AreEqual(mm.GetValue(dt), p.Value);
|
|
351 }
|
|
352 }
|
|
353 }
|
|
354
|
|
355 #if!ORACLE
|
|
356 [Test]
|
|
357 #endif
|
|
358 public void CreateParametersSqlTest()
|
|
359 {
|
|
360 using (DbManager db = new DbManager())
|
|
361 {
|
|
362 DataTypeSqlTest dt = new DataTypeSqlTest();
|
|
363
|
|
364 dt.ID = 12345;
|
|
365 dt.Binary_ = new SqlBinary(new byte[2] {1, 2});
|
|
366 dt.Boolean_ = new SqlBoolean(1);
|
|
367 dt.Byte_ = new SqlByte(250);
|
|
368 dt.DateTime_ = new SqlDateTime(DateTime.Now);
|
|
369 dt.Decimal_ = new SqlDecimal(9876543210.0m);
|
|
370 dt.Double_ = new SqlDouble(12345.67890);
|
|
371 dt.Guid_ = new SqlGuid(Guid.Empty);
|
|
372 dt.Int16_ = new SqlInt16(12345);
|
|
373 dt.Int32_ = new SqlInt32(1234567890);
|
|
374 dt.Int64_ = new SqlInt64(1234567890123456789);
|
|
375 dt.Money_ = new SqlMoney(99876543210.0m);
|
|
376 dt.Single_ = new SqlSingle(1234.0f);
|
|
377 dt.String_ = new SqlString("Crazy Frog");
|
|
378
|
|
379 #if !SQLCE
|
|
380 dt.Bytes_ = new SqlBytes(new byte[2] {2, 1});
|
|
381 dt.Chars_ = new SqlChars(new char[2] {'B', 'L'});
|
|
382 dt.Xml_ = new SqlXml(new XmlTextReader(new StringReader("<xml/>")));
|
|
383 #endif
|
|
384
|
|
385 var parameters = db.CreateParameters(dt);
|
|
386
|
|
387 Assert.IsNotNull(parameters);
|
|
388 Assert.AreEqual(ObjectMapper<DataTypeSqlTest>.Instance.Count, parameters.Length);
|
|
389
|
|
390 foreach (MemberMapper mm in ObjectMapper<DataTypeSqlTest>.Instance)
|
|
391 {
|
|
392 var pName = (string)db.DataProvider.Convert(mm.Name, db.GetConvertTypeToParameter());
|
|
393 var p = Array.Find(parameters, obj => obj.ParameterName == pName);
|
|
394
|
|
395 Assert.IsNotNull(p);
|
|
396 Assert.AreEqual(mm.GetValue(dt), p.Value);
|
|
397 }
|
|
398 }
|
|
399 }
|
|
400
|
|
401 public struct DBInfo
|
|
402 {
|
|
403 public DateTime TimeValue;
|
|
404 }
|
|
405
|
|
406 [Test]
|
|
407 public void CreateParametersStructTest()
|
|
408 {
|
|
409 var dbInfo = new DBInfo { TimeValue = DateTime.Now };
|
|
410
|
|
411 using (var db = new DbManager())
|
|
412 {
|
|
413 var parameters = db.CreateParameters(dbInfo);
|
|
414
|
|
415 Assert.IsNotNull(parameters);
|
|
416 Assert.AreEqual(1, parameters.Length);
|
|
417 Assert.AreEqual(dbInfo.TimeValue, parameters[0].Value);
|
|
418
|
|
419 }
|
|
420 }
|
|
421
|
|
422 public class FirstPart
|
|
423 {
|
|
424 public string FirstName;
|
|
425 }
|
|
426
|
|
427 public class SecondPart
|
|
428 {
|
|
429 public string LastName;
|
|
430 }
|
|
431
|
|
432 #if !SQLITE && !SQLCE
|
|
433 [Test]
|
|
434 #endif
|
|
435 public void CreateManyParametersTest()
|
|
436 {
|
|
437 FirstPart f = new FirstPart();
|
|
438 SecondPart s = new SecondPart();
|
|
439
|
|
440 f.FirstName = "John";
|
|
441 s.LastName = "Pupkin";
|
|
442
|
|
443 using (DbManager db = new DbManager())
|
|
444 {
|
|
445 Person p = (Person)db
|
|
446 .SetSpCommand ("Person_SelectByName", db.CreateParameters(f), db.CreateParameters(s))
|
|
447 .ExecuteObject(typeof(Person));
|
|
448
|
|
449 Assert.IsNotNull(p);
|
|
450 Assert.AreEqual(f.FirstName, p.FirstName);
|
|
451 Assert.AreEqual(s.LastName, p.LastName);
|
|
452 }
|
|
453 }
|
|
454
|
|
455 [Test]
|
|
456 public void EnumExecuteScalarTest1()
|
|
457 {
|
|
458 using (var dbm = new DbManager())
|
|
459 {
|
|
460 var gender = dbm.SetCommand(CommandType.Text, "select 'M'")
|
|
461 .ExecuteScalar<Gender>();
|
|
462
|
|
463 Assert.That(gender, Is.EqualTo(Gender.Male));
|
|
464 }
|
|
465 }
|
|
466
|
|
467 public enum ABType
|
|
468 {
|
|
469 Error = -1,
|
|
470 A = 0,
|
|
471 B,
|
|
472 }
|
|
473
|
|
474 [Test]
|
|
475 public void EnumExecuteScalarTest2()
|
|
476 {
|
|
477 using (var db = new DbManager())
|
|
478 {
|
|
479 var type = db.SetCommand("select 1 where 1 = 2").ExecuteScalar<ABType>();
|
|
480 Assert.That(type, Is.EqualTo(ABType.A));
|
|
481 }
|
|
482 }
|
|
483 }
|
|
484 }
|