0
|
1 using System;
|
|
2
|
|
3 using BLToolkit.Data;
|
|
4 using BLToolkit.Data.DataProvider;
|
|
5
|
|
6 using NUnit.Framework;
|
|
7
|
|
8 namespace Data.Linq.ProviderSpecific
|
|
9 {
|
|
10 using Model;
|
|
11
|
|
12 [TestFixture]
|
|
13 [Category("MySql")]
|
|
14 public class MySqlSprocParameterPrefixTests : TestBase
|
|
15 {
|
|
16 [Test]
|
|
17 public void ParameterPrefixTest([IncludeDataContexts(ProviderName.MySql)] string context)
|
|
18 {
|
|
19 var oldPrefix = MySqlDataProvider.SprocParameterPrefix;
|
|
20 MySqlDataProvider.SprocParameterPrefix = "_";
|
|
21
|
|
22 try
|
|
23 {
|
|
24 using (var db = new TestDbManager(context))
|
|
25 {
|
|
26 var person = db.SetSpCommand("GetPersonById", db.Parameter("?ID", 1)).ExecuteObject<Person>();
|
|
27 Assert.IsNotNull(person);
|
|
28
|
|
29 var person2 = db.SetSpCommand("GetPersonByName", db.CreateParameters(person)).ExecuteObject<Person>();
|
|
30 Assert.IsNotNull(person2);
|
|
31
|
|
32 Assert.AreEqual(person, person2);
|
|
33 }
|
|
34 }
|
|
35 finally
|
|
36 {
|
|
37 MySqlDataProvider.SprocParameterPrefix = oldPrefix;
|
|
38 }
|
|
39 }
|
|
40
|
|
41 [Test]
|
|
42 public void SetCommandWorksCorrectlyWithSprocParameterPrefixSet([IncludeDataContexts(ProviderName.MySql)] string context)
|
|
43 {
|
|
44 var oldPrefix = MySqlDataProvider.SprocParameterPrefix;
|
|
45 MySqlDataProvider.SprocParameterPrefix = "_";
|
|
46
|
|
47 try
|
|
48 {
|
|
49 using (var db = new DbManager(context))
|
|
50 {
|
|
51 var person = db.SetCommand(
|
|
52 "SELECT * FROM Person WHERE PersonID = ?PersonID",
|
|
53 db.Parameter("?PersonID", 1))
|
|
54 .ExecuteObject<Person>();
|
|
55
|
|
56 Assert.IsNotNull(person);
|
|
57 Assert.AreEqual(1, person.ID);
|
|
58
|
|
59 var person2 = db.SetCommand(
|
|
60 "SELECT * FROM Person WHERE FirstName = ?firstName AND LastName = ?lastName",
|
|
61 db.CreateParameters(person))
|
|
62 .ExecuteObject<Person>();
|
|
63
|
|
64 Assert.IsNotNull(person2);
|
|
65 Assert.AreEqual(person, person2);
|
|
66 }
|
|
67 }
|
|
68 finally
|
|
69 {
|
|
70 MySqlDataProvider.SprocParameterPrefix = oldPrefix;
|
|
71 }
|
|
72 }
|
|
73
|
|
74 [Test]
|
|
75 public void SprocParameterPrefixShouldBeSpecifiedManuallyWhenItIsNotSet([IncludeDataContexts(ProviderName.MySql)] string context)
|
|
76 {
|
|
77 using (var db = new DbManager(context))
|
|
78 {
|
|
79 var person = db.SetSpCommand("GetPersonById", db.Parameter("?_ID", 1)).ExecuteObject<Person>();
|
|
80 Assert.IsNotNull(person);
|
|
81
|
|
82 var person2 = db.SetSpCommand(
|
|
83 "GetPersonByName",
|
|
84 db.Parameter("?_lastName", person.LastName),
|
|
85 db.Parameter("?_firstName", person.FirstName))
|
|
86 .ExecuteObject<Person>();
|
|
87 Assert.IsNotNull(person2);
|
|
88 }
|
|
89 }
|
|
90
|
|
91 [Test]
|
|
92 public void ParameterSymbolMayBeOmitedForSpCommand([IncludeDataContexts(ProviderName.MySql)] string context)
|
|
93 {
|
|
94 // I am not sure this is a good thing though
|
|
95 // Maybe we need to be more strict on syntax and on the library users
|
|
96 // and do not allow different syntax (omiting parameter symbol)?
|
|
97
|
|
98 using (var db = new DbManager(context))
|
|
99 {
|
|
100 var person = db.SetSpCommand("GetPersonById", db.Parameter("_ID", 1)).ExecuteObject<Person>();
|
|
101 Assert.IsNotNull(person);
|
|
102
|
|
103 var person2 = db.SetSpCommand(
|
|
104 "GetPersonByName",
|
|
105 db.Parameter("_lastName", person.LastName),
|
|
106 db.Parameter("_firstName", person.FirstName))
|
|
107 .ExecuteObject<Person>();
|
|
108 Assert.IsNotNull(person2);
|
|
109 Assert.AreEqual(person, person2);
|
|
110
|
|
111 var oldPrefix = MySqlDataProvider.SprocParameterPrefix;
|
|
112 MySqlDataProvider.SprocParameterPrefix = "_";
|
|
113
|
|
114 try
|
|
115 {
|
|
116 var person3 = db.SetSpCommand("GetPersonById", db.Parameter("ID", 1)).ExecuteObject<Person>();
|
|
117 Assert.AreEqual(person, person3);
|
|
118 }
|
|
119 finally
|
|
120 {
|
|
121 MySqlDataProvider.SprocParameterPrefix = oldPrefix;
|
|
122 }
|
|
123 }
|
|
124 }
|
|
125
|
|
126 [Test]
|
|
127 public void SpecifyingParameterPrefixManuallyIsAlsoOk([IncludeDataContexts(ProviderName.MySql)] string context)
|
|
128 {
|
|
129 var oldPrefix = MySqlDataProvider.SprocParameterPrefix;
|
|
130 MySqlDataProvider.SprocParameterPrefix = "_";
|
|
131
|
|
132 try
|
|
133 {
|
|
134 using (var db = new DbManager(context))
|
|
135 {
|
|
136 // we specify parameter name with a prefix, though SprocParameterPrefix is specified
|
|
137 // in this case additional parameter prefix will not be added, so everything will be ok
|
|
138 var person = db.SetSpCommand("GetPersonById", db.Parameter("?_ID", 1)).ExecuteObject<Person>();
|
|
139 Assert.IsNotNull(person);
|
|
140 }
|
|
141 }
|
|
142 finally
|
|
143 {
|
|
144 MySqlDataProvider.SprocParameterPrefix = oldPrefix;
|
|
145 }
|
|
146 }
|
|
147
|
|
148 [Test]
|
|
149 public void PrefixIsAddedWhenRetrievingParameterFromDbManager([IncludeDataContexts(ProviderName.MySql)] string context)
|
|
150 {
|
|
151 var oldPrefix = MySqlDataProvider.SprocParameterPrefix;
|
|
152 MySqlDataProvider.SprocParameterPrefix = "_";
|
|
153
|
|
154 try
|
|
155 {
|
|
156 using (var db = new DbManager(context))
|
|
157 {
|
|
158 db.SetSpCommand("GetPersonById", db.Parameter("?ID", 1)).Prepare();
|
|
159
|
|
160 foreach (var personID in new[] { 1, 2 })
|
|
161 {
|
|
162 // prefix is not specified but it will be added internally before retrieving parameter from
|
|
163 // command parameters
|
|
164 db.Parameter("?ID").Value = personID;
|
|
165 var person = db.ExecuteObject<Person>();
|
|
166 Assert.IsNotNull(person);
|
|
167 Assert.AreEqual(personID, person.ID);
|
|
168
|
|
169 // specifying prefix is also ok
|
|
170 db.Parameter("?_ID").Value = personID;
|
|
171 person = db.ExecuteObject<Person>();
|
|
172 Assert.IsNotNull(person);
|
|
173 Assert.AreEqual(personID, person.ID);
|
|
174 }
|
|
175 }
|
|
176 }
|
|
177 finally
|
|
178 {
|
|
179 MySqlDataProvider.SprocParameterPrefix = oldPrefix;
|
|
180 }
|
|
181 }
|
|
182 }
|
|
183 }
|