Mercurial > pub > bltoolkit
comparison HowTo/DataAccess/CustomSqlQuery1.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 | |
4 using NUnit.Framework; | |
5 | |
6 using BLToolkit.Data; | |
7 using BLToolkit.DataAccess; | |
8 | |
9 namespace HowTo.DataAccess | |
10 { | |
11 [TestFixture] | |
12 public class CustomSqlQuery1 | |
13 { | |
14 public class TestQueryAttribute : /*[a]*/SqlQueryAttribute/*[/a]*/ | |
15 { | |
16 public TestQueryAttribute() | |
17 { | |
18 /*[a]*/IsDynamic = true/*[/a]*/; | |
19 } | |
20 | |
21 public string OracleText { get; set; } | |
22 public string FbText { get; set; } | |
23 public string SQLiteText { get; set; } | |
24 | |
25 public /*[a]*/override/*[/a]*/ string /*[a]*/GetSqlText/*[/a]*/(DataAccessor accessor, DbManager dbManager) | |
26 { | |
27 switch (dbManager.DataProvider.Name) | |
28 { | |
29 case "Sql" : | |
30 case "Access": return SqlText; | |
31 case "Oracle": return OracleText ?? SqlText; | |
32 case "Fdp" : return FbText ?? SqlText; | |
33 case "SQLite": return SQLiteText ?? SqlText; | |
34 } | |
35 | |
36 throw new ApplicationException(string.Format("Unknown data provider '{0}'", dbManager.DataProvider.Name)); | |
37 } | |
38 } | |
39 | |
40 public abstract class PersonAccessor : DataAccessor | |
41 { | |
42 [TestQuery( | |
43 /*[a]*/SqlText/*[/a]*/ = "SELECT * FROM Person WHERE LastName = @lastName", | |
44 /*[a]*/OracleText/*[/a]*/ = "SELECT * FROM Person WHERE LastName = :lastName")] | |
45 public abstract List<Person> SelectByLastName(string lastName); | |
46 | |
47 [TestQuery( | |
48 /*[a]*/SqlText/*[/a]*/ = "SELECT * FROM Person WHERE {0} = @value", | |
49 /*[a]*/OracleText/*[/a]*/ = "SELECT * FROM Person WHERE {0} = :value")] | |
50 public abstract List<Person> SelectBy([Format] string fieldName, string value); | |
51 | |
52 [TestQuery( | |
53 /*[a]*/SqlText/*[/a]*/ = "SELECT TOP {0} * FROM Person WHERE LastName = @lastName", | |
54 /*[a]*/OracleText/*[/a]*/ = "SELECT * FROM Person WHERE LastName = :lastName AND rownum <= {0}", | |
55 /*[a]*/FbText/*[/a]*/ = "SELECT FIRST {0} * FROM Person WHERE LastName = @lastName", | |
56 /*[a]*/SQLiteText/*[/a]*/ = "SELECT * FROM Person WHERE LastName = @lastName LIMIT {0}")] | |
57 public abstract List<Person> SelectByLastName(string lastName, [Format(0)] int top); | |
58 | |
59 [TestQuery( | |
60 /*[a]*/SqlText/*[/a]*/ = "SELECT @id as PersonID", | |
61 /*[a]*/OracleText/*[/a]*/ = "SELECT :id PersonID FROM Dual", | |
62 /*[a]*/FbText/*[/a]*/ = "SELECT CAST(@id AS INTEGER) PersonID FROM Dual")] | |
63 public abstract List<Person> SelectID(int @id); | |
64 } | |
65 | |
66 [Test] | |
67 public void Test1() | |
68 { | |
69 PersonAccessor da = DataAccessor.CreateInstance<PersonAccessor>(); | |
70 | |
71 List<Person> list = da.SelectByLastName("Testerson"); | |
72 | |
73 Assert.AreNotEqual(0, list.Count); | |
74 } | |
75 | |
76 [Test] | |
77 public void Test2() | |
78 { | |
79 PersonAccessor da = DataAccessor.CreateInstance<PersonAccessor>(); | |
80 | |
81 List<Person> list = da.SelectBy("FirstName", "John"); | |
82 | |
83 Assert.AreNotEqual(0, list.Count); | |
84 } | |
85 | |
86 [Test] | |
87 public void Test3() | |
88 { | |
89 PersonAccessor da = DataAccessor.CreateInstance<PersonAccessor>(); | |
90 | |
91 List<Person> list = da.SelectByLastName("Testerson", 1); | |
92 | |
93 Assert.AreNotEqual(0, list.Count); | |
94 } | |
95 | |
96 [Test] | |
97 public void Test4() | |
98 { | |
99 PersonAccessor da = DataAccessor.CreateInstance<PersonAccessor>(); | |
100 | |
101 List<Person> list = da.SelectID(42); | |
102 | |
103 Assert.AreEqual(42, list[0].ID); | |
104 } | |
105 } | |
106 } |