comparison HowTo/DataAccess/CustomSqlQuery2.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;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Reflection;
5 using System.Xml;
6
7 using NUnit.Framework;
8
9 using BLToolkit.Data;
10 using BLToolkit.DataAccess;
11
12 namespace HowTo.DataAccess
13 {
14 [TestFixture]
15 public class CustomSqlQuery2
16 {
17 public abstract class TestAccessorBase<T> : /*[a]*/DataAccessor/*[/a]*/
18 where T : TestAccessorBase<T>
19 {
20 const int Sql = 0;
21 const int Access = 1;
22 const int Oracle = 2;
23 const int Fdp = 3;
24 const int SQLite = 4;
25
26 Dictionary<int, string> _sql = new Dictionary<int,string>();
27
28 private string GetSql(string providerName, int provider, int queryID)
29 {
30 Stream stream = Assembly.GetCallingAssembly().GetManifestResourceStream(
31 "HowTo.DataAccess.Sql." + providerName + ".xml");
32
33 XmlDocument doc = new XmlDocument();
34
35 doc.Load(stream);
36
37 XmlNode node = doc.SelectSingleNode(string.Format("/sql/query[@id={0}]", queryID));
38
39 return node != null? node.InnerText: null;
40
41 }
42
43 protected /*[a]*/override/*[/a]*/ string /*[a]*/PrepareSqlQuery/*[/a]*/(DbManager db, int queryID, int uniqueID, string sqlQuery)
44 {
45 int provider = Sql;
46 string providerName = db.DataProvider.Name;
47
48 switch (providerName)
49 {
50 case "Sql" : provider = Sql; break;
51 case "Access": provider = Access; break;
52 case "Oracle": provider = Oracle; break;
53 case "Fdp" : provider = Fdp; break;
54 case "SQLite": provider = SQLite; break;
55 default:
56 throw new ApplicationException(
57 string.Format("Unknown data provider '{0}'", providerName));
58 }
59
60 string text;
61 int key = provider * 1000000 + uniqueID;
62
63 if (_sql.TryGetValue(key, out text))
64 return text;
65
66 _sql[key] = text = GetSql(providerName, provider, queryID) ?? GetSql("Sql", Sql, queryID);
67
68 return text;
69 }
70
71 public static T CreateInstance()
72 {
73 return DataAccessor.CreateInstance<T>();
74 }
75 }
76
77 public abstract class PersonAccessor : TestAccessorBase<PersonAccessor>
78 {
79 [SqlQuery(/*[a]*/ID = 1/*[/a]*/)]
80 public abstract List<Person> SelectByLastName(string lastName);
81
82 [SqlQuery(/*[a]*/ID = 2/*[/a]*/)]
83 public abstract List<Person> SelectBy([Format] string fieldName, string value);
84
85 [SqlQuery(/*[a]*/ID = 3/*[/a]*/)]
86 public abstract List<Person> SelectByLastName(string lastName, [Format(0)] int top);
87
88 [SqlQuery(/*[a]*/ID = 4/*[/a]*/)]
89 public abstract List<Person> SelectID(int @id);
90 }
91
92 [Test]
93 public void Test1()
94 {
95 PersonAccessor da = PersonAccessor.CreateInstance();
96
97 List<Person> list = da.SelectByLastName("Testerson");
98
99 Assert.AreNotEqual(0, list.Count);
100 }
101
102 [Test]
103 public void Test2()
104 {
105 PersonAccessor da = PersonAccessor.CreateInstance();
106
107 List<Person> list = da.SelectBy("FirstName", "John");
108
109 Assert.AreNotEqual(0, list.Count);
110 }
111
112 [Test]
113 public void Test3()
114 {
115 PersonAccessor da = PersonAccessor.CreateInstance();
116
117 List<Person> list = da.SelectByLastName("Testerson", 1);
118
119 Assert.AreNotEqual(0, list.Count);
120 }
121
122 [Test]
123 public void Test4()
124 {
125 PersonAccessor da = PersonAccessor.CreateInstance();
126
127 List<Person> list = da.SelectID(42);
128
129 Assert.AreEqual(42, list[0].ID);
130 }
131 }
132 }