PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NHibernate.Test/SqlCommandTest/SqlStringFixture.cs

https://github.com/okb/nhibernate-core
C# | 465 lines | 337 code | 91 blank | 37 comment | 1 complexity | 698e6f5d0d763e0a670dd489f6c60392 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, LGPL-3.0, Apache-2.0, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using NHibernate.SqlCommand;
  6. using NUnit.Framework;
  7. using SharpTestsEx;
  8. namespace NHibernate.Test.SqlCommandTest
  9. {
  10. /// <summary>
  11. /// Summary description for SqlStringFixture.
  12. /// </summary>
  13. [TestFixture]
  14. public class SqlStringFixture
  15. {
  16. //[Test]
  17. //public void StringPerf()
  18. //{
  19. //
  20. // // Just a quick piece of code to measure performance of some SqlString operations. Commented out,
  21. // // since we don't want this to run on every build.
  22. //
  23. // var str = new SqlString(new object[] { " select x from xs where ", Parameter.Placeholder, " and ", Parameter.Placeholder, " and ", Parameter.Placeholder, " and ", Parameter.Placeholder, " and ", Parameter.Placeholder, " and ", Parameter.Placeholder, " " });
  24. // double[] allSub = new double[5];
  25. // for (int a = 0; a < 5; ++a)
  26. // {
  27. // Stopwatch sw = Stopwatch.StartNew();
  28. // for (int i = 0; i < 10000; ++i)
  29. // str.Substring(6, 20);
  30. // sw.Stop();
  31. // Console.WriteLine(" Substring 10000 iterations (ms): " + sw.Elapsed.TotalMilliseconds);
  32. // allSub[a] = sw.Elapsed.TotalMilliseconds;
  33. // }
  34. // Console.WriteLine("Substring average per 10000 iters (ms): " + allSub.Average());
  35. // double[] allTrim = new double[5];
  36. // for (int a = 0; a < 5; ++a)
  37. // {
  38. // Stopwatch sw = Stopwatch.StartNew();
  39. // for (int i = 0; i < 10000; ++i)
  40. // str.Trim();
  41. // sw.Stop();
  42. // Console.WriteLine(" Trim 10000 iterations (ms): " + sw.Elapsed.TotalMilliseconds);
  43. // allTrim[a] = sw.Elapsed.TotalMilliseconds;
  44. // }
  45. // Console.WriteLine("Trim average per 10000 iters (ms): " + allTrim.Average());
  46. //}
  47. [Test]
  48. public void Append()
  49. {
  50. SqlString sql = new SqlString(new string[] { "select", " from table" });
  51. SqlString postAppendSql = sql.Append(" where A=B");
  52. Assert.IsFalse(sql == postAppendSql, "should be a new object");
  53. Assert.AreEqual(1, postAppendSql.Count);
  54. sql = postAppendSql;
  55. postAppendSql = sql.Append(new SqlString(" and C=D"));
  56. Assert.AreEqual(1, postAppendSql.Count);
  57. Assert.AreEqual("select from table where A=B and C=D", postAppendSql.ToString());
  58. }
  59. [Test]
  60. public void Count()
  61. {
  62. SqlString sql =
  63. new SqlString(
  64. new object[] { "select", " from table where a = ", Parameter.Placeholder, " and b = ", Parameter.Placeholder });
  65. Assert.AreEqual(4, sql.Count, "Count with no nesting failed.");
  66. sql = sql.Append(new SqlString(new object[] { " more parts ", " another part " }));
  67. Assert.AreEqual(5, sql.Count, "Added a SqlString to a SqlString");
  68. }
  69. [Test]
  70. public void Split()
  71. {
  72. SqlString sql = new SqlString(new string[] { "select", " alfa, beta, gamma", " from table" });
  73. var parts1 = sql.Split(",").Select(s => s.ToString()).ToArray();
  74. var expectedParts1 = new[] { "select alfa", " beta", " gamma from table" };
  75. Assert.That(parts1, Is.EqualTo(expectedParts1));
  76. SqlString sql2 = sql.Substring(6);
  77. sql2.Compact();
  78. var parts2 = sql2.Split(",").Select(s => s.ToString()).ToArray();
  79. var expectedParts2 = new[] { " alfa", " beta", " gamma from table" };
  80. Assert.That(parts2, Is.EqualTo(expectedParts2));
  81. }
  82. [Test]
  83. public void EndsWith()
  84. {
  85. SqlString sql = new SqlString(new string[] { "select", " from table" });
  86. Assert.IsTrue(sql.EndsWith("ble"));
  87. Assert.IsFalse(sql.EndsWith("'"));
  88. }
  89. [Test]
  90. public void EndsWithEmptyString()
  91. {
  92. SqlString sql = new SqlString(new string[] { "", "select", " from table", "" });
  93. Assert.IsTrue(sql.EndsWith("ble"));
  94. Assert.IsFalse(sql.EndsWith("'"));
  95. }
  96. [Test]
  97. public void EndsWithParameter()
  98. {
  99. SqlString sql = new SqlString(new object[] { "", "select", " from table where id = ", Parameter.Placeholder });
  100. Assert.IsFalse(sql.EndsWith("'"));
  101. Assert.IsFalse(sql.EndsWith(""));
  102. }
  103. [Test]
  104. public void Replace()
  105. {
  106. SqlString sql =
  107. new SqlString(
  108. new object[] { "select ", "from table ", "where a = ", Parameter.Placeholder, " and c = ", Parameter.Placeholder });
  109. SqlString replacedSql = sql.Replace("table", "replacedTable");
  110. Assert.AreEqual(sql.ToString().Replace("table", "replacedTable"), replacedSql.ToString());
  111. replacedSql = sql.Replace("not found", "not in here");
  112. Assert.AreEqual(sql.ToString().Replace("not found", "not in here"), replacedSql.ToString(), "replace no found string");
  113. replacedSql = sql.Replace("le", "LE");
  114. Assert.AreEqual(sql.ToString().Replace("le", "LE"), replacedSql.ToString(), "multi-match replace");
  115. }
  116. [Test]
  117. public void StartsWith()
  118. {
  119. SqlString sql = new SqlString(new string[] { "select", " from table" });
  120. Assert.IsTrue(sql.StartsWithCaseInsensitive("s"));
  121. Assert.IsFalse(sql.StartsWithCaseInsensitive(","));
  122. }
  123. [Test]
  124. public void StartsWithWhenContainsParameters()
  125. {
  126. SqlString sql = new SqlString(" and ", "(", new SqlString("blah = ", Parameter.Placeholder), ")");
  127. Assert.IsTrue(sql.StartsWithCaseInsensitive(" and"));
  128. Assert.IsFalse(sql.StartsWithCaseInsensitive("blah"));
  129. }
  130. [Test]
  131. public void StartsWithEmptyString()
  132. {
  133. SqlString sql = new SqlString(new string[] { "", "select", " from table" });
  134. Assert.IsTrue(sql.StartsWithCaseInsensitive("s"));
  135. Assert.IsFalse(sql.StartsWithCaseInsensitive(","));
  136. }
  137. [Test]
  138. public void Substring()
  139. {
  140. SqlStringBuilder builder = new SqlStringBuilder();
  141. Parameter p = Parameter.Placeholder;
  142. builder.Add(" select from table");
  143. builder.Add(" where p = ");
  144. builder.Add(p);
  145. SqlString sql = builder.ToSqlString();
  146. sql = sql.Substring(1);
  147. Assert.AreEqual("select from table where p = ?", sql.ToString());
  148. }
  149. [Test]
  150. public void SubstringComplex()
  151. {
  152. SqlString str =
  153. new SqlString(new object[] { "select ", Parameter.Placeholder, " from table where x = ", Parameter.Placeholder });
  154. SqlString substr7 = str.Substring(7);
  155. Assert.AreEqual(
  156. new SqlString(new object[] { Parameter.Placeholder, " from table where x = ", Parameter.Placeholder }), substr7);
  157. SqlString substr10 = str.Substring(10);
  158. Assert.AreEqual(new SqlString(new object[] { "rom table where x = ", Parameter.Placeholder }), substr10);
  159. Assert.AreEqual(SqlString.Empty, str.Substring(200));
  160. }
  161. [Test]
  162. public void Substring2Complex()
  163. {
  164. SqlString str =
  165. new SqlString(new object[] { "select ", Parameter.Placeholder, " from table where x = ", Parameter.Placeholder });
  166. SqlString substr7_10 = str.Substring(7, 10);
  167. Assert.AreEqual(new SqlString(new object[] { Parameter.Placeholder, " from tab" }), substr7_10);
  168. SqlString substr10_200 = str.Substring(10, 200);
  169. Assert.AreEqual(new SqlString(new object[] { "rom table where x = ", Parameter.Placeholder }), substr10_200);
  170. SqlString substr200_10 = str.Substring(200, 10);
  171. Assert.AreEqual(SqlString.Empty, substr200_10);
  172. }
  173. [Test]
  174. public void IndexOf()
  175. {
  176. SqlString str =
  177. new SqlString(new object[] { "select ", Parameter.Placeholder, " from table where x = ", Parameter.Placeholder });
  178. Assert.AreEqual(0, str.IndexOfCaseInsensitive("select"));
  179. Assert.AreEqual(1, str.IndexOfCaseInsensitive("el"));
  180. Assert.AreEqual(7 + 1 + 6, str.IndexOfCaseInsensitive("table"));
  181. }
  182. [Test]
  183. public void IndexOfNonCompacted()
  184. {
  185. SqlString str = new SqlString(new object[] { "select ", " from" });
  186. Assert.AreEqual(6, str.IndexOfCaseInsensitive(" "));
  187. }
  188. [Test]
  189. public void TrimAllString()
  190. {
  191. SqlString sql = new SqlString(new string[] { " extra space", " in the middle", " at the end " });
  192. sql = sql.Trim();
  193. Assert.AreEqual("extra space in the middle at the end", sql.ToString());
  194. }
  195. [Test]
  196. public void TrimBeginParamEndString()
  197. {
  198. Parameter p1 = Parameter.Placeholder;
  199. SqlString sql = new SqlString(new object[] { p1, " extra space " });
  200. sql = sql.Trim();
  201. Assert.AreEqual("? extra space", sql.ToString());
  202. }
  203. [Test]
  204. public void TrimBeginStringEndParam()
  205. {
  206. Parameter p1 = Parameter.Placeholder;
  207. SqlString sql = new SqlString(new object[] { " extra space ", p1 });
  208. sql = sql.Trim();
  209. Assert.AreEqual("extra space ?", sql.ToString());
  210. }
  211. [Test]
  212. public void TrimAllParam()
  213. {
  214. Parameter p1 = Parameter.Placeholder;
  215. Parameter p2 = Parameter.Placeholder;
  216. SqlString sql = new SqlString(new object[] { p1, p2 });
  217. sql = sql.Trim();
  218. Assert.AreEqual("??", sql.ToString());
  219. }
  220. [Test]
  221. public void SubstringStartingWithLast()
  222. {
  223. SqlString sql = new SqlString(new object[] { "select x from y where z = ", Parameter.Placeholder, " order by t" });
  224. Assert.AreEqual("order by t", sql.SubstringStartingWithLast("order by").ToString());
  225. }
  226. [Test]
  227. public void NoSubstringStartingWithLast()
  228. {
  229. SqlString sql = new SqlString(new object[] { "select x from y where z = ", Parameter.Placeholder, " order by t" });
  230. Assert.AreEqual("", sql.SubstringStartingWithLast("zzz").ToString());
  231. }
  232. [Test]
  233. public void SubstringStartingWithLastAndParameters()
  234. {
  235. SqlString sql =
  236. new SqlString(
  237. new object[] { "select x from y where z = ", Parameter.Placeholder, " order by ", Parameter.Placeholder });
  238. Assert.AreEqual(new SqlString(new object[] { "order by ", Parameter.Placeholder }),
  239. sql.SubstringStartingWithLast("order by"));
  240. }
  241. [Test]
  242. public void SubstringStartingWithLastMultiplePossibilities()
  243. {
  244. SqlString sql = new SqlString(new string[] { " order by x", " order by z" });
  245. Assert.AreEqual("order by z", sql.SubstringStartingWithLast("order by").ToString());
  246. }
  247. [Test]
  248. public void Insert()
  249. {
  250. SqlString sql = new SqlString(new object[] { "begin ", Parameter.Placeholder, " end" });
  251. Assert.AreEqual("beginning ? end", sql.Insert(5, "ning").ToString());
  252. Assert.AreEqual("begin middle? end", sql.Insert(6, "middle").ToString());
  253. Assert.AreEqual("begin ?middle end", sql.Insert(7, "middle").ToString());
  254. Assert.AreEqual("beg|in ? end", sql.Insert(3, "|").ToString());
  255. Assert.AreEqual("begin ? ending", sql.Insert(11, "ing").ToString());
  256. Assert.AreEqual("begin ? enXd", sql.Insert(10, "X").ToString());
  257. }
  258. [Test]
  259. public void Parse()
  260. {
  261. SqlString sql =
  262. SqlString.Parse("select col from table where col = ? or col1 = 'a?b' and col2 = ? and col3 = 'x' and col4 = ?");
  263. SqlString expectedSql = new SqlString(
  264. new object[]
  265. {
  266. "select col from table where col = ",
  267. Parameter.Placeholder,
  268. " or col1 = 'a?b' and col2 = ",
  269. Parameter.Placeholder,
  270. " and col3 = 'x' and col4 = ",
  271. Parameter.Placeholder
  272. }
  273. );
  274. Assert.AreEqual(expectedSql, sql);
  275. Assert.AreEqual(new SqlString("simple"), SqlString.Parse("simple"));
  276. Assert.AreEqual(SqlString.Empty, SqlString.Parse(""));
  277. }
  278. [Test]
  279. public void GetSubselectStringSimple()
  280. {
  281. SqlString sql = SqlString.Parse("select col from table where col = test order by col");
  282. Assert.AreEqual(" from table where col = test ", sql.GetSubselectString().ToString());
  283. }
  284. [Test]
  285. public void GetSubselectStringParameterInOrderBy()
  286. {
  287. SqlString sql = SqlString.Parse("select col from table where col = test order by ? asc");
  288. Assert.AreEqual(" from table where col = test ", sql.GetSubselectString().ToString());
  289. }
  290. [Test]
  291. public void GetSubselectStringSimpleEndsWithParameter()
  292. {
  293. SqlString sql = SqlString.Parse("select col from table where col = ? order by col");
  294. Assert.AreEqual(" from table where col = ? ", sql.GetSubselectString().ToString());
  295. }
  296. [Test]
  297. public void GetSubselectStringSimpleParameterInMiddle()
  298. {
  299. SqlString sql = SqlString.Parse("select col from table where col = ? and foo = bar order by col");
  300. Assert.AreEqual(" from table where col = ? and foo = bar ", sql.GetSubselectString().ToString());
  301. }
  302. [Test]
  303. public void GetSubselectStringWithFormulaProperty()
  304. {
  305. SqlString sql =
  306. SqlString.Parse("select (select foo from bar where foo=col order by foo) from table where col = ? order by col");
  307. Assert.AreEqual(" from table where col = ? ", sql.GetSubselectString().ToString());
  308. }
  309. [Test]
  310. public void GetSubselectStringWithSubselectInWhere()
  311. {
  312. SqlString sql =
  313. SqlString.Parse(
  314. "select (select foo from bar where foo=col order by foo) from table where col = (select yadda from blah where yadda=x order by yadda) order by col");
  315. Assert.AreEqual(" from table where col = (select yadda from blah where yadda=x order by yadda) ",
  316. sql.GetSubselectString().ToString());
  317. }
  318. [Test]
  319. public void GetSubselectStringWithFormulaPropertyWithSubselect()
  320. {
  321. SqlString sql =
  322. SqlString.Parse(
  323. "select (select (select blah from yadda where blah=foo order by blah) from bar where foo=col order by foo) from table where col = ? order by col");
  324. Assert.AreEqual(" from table where col = ? ", sql.GetSubselectString().ToString());
  325. }
  326. [Test]
  327. public void GetSubselectStringWithParenthesisOnlyInWhere()
  328. {
  329. SqlString sql = SqlString.Parse("select col from table where (col = test) order by col");
  330. Assert.AreEqual(" from table where (col = test) ", sql.GetSubselectString().ToString());
  331. }
  332. [Test]
  333. public void GetSubselectStringWithTwoFormulas()
  334. {
  335. SqlString sql =
  336. SqlString.Parse(
  337. "select (select foo from bar where foo=col order by foo), (select foo from bar where foo=col order by foo) from table where col = ? order by col");
  338. Assert.AreEqual(" from table where col = ? ", sql.GetSubselectString().ToString());
  339. }
  340. [Test]
  341. public void GetSubselectStringWithOrderByInSubselect()
  342. {
  343. SqlString sql = SqlString.Parse("select col from table where (col = test) and id in (select id from foo order by bar)");
  344. Assert.AreEqual(" from table where (col = test) and id in (select id from foo order by bar)", sql.GetSubselectString().ToString());
  345. }
  346. [Test]
  347. public void ParameterPropertyShouldReturnNewInstances()
  348. {
  349. Parameter[] parameters1 = new Parameter[1];
  350. Parameter[] parameters2 = new Parameter[1];
  351. SqlString parameterString1 = new SqlString(Parameter.Placeholder);
  352. parameterString1.Parts.CopyTo(parameters1, 0);
  353. SqlString parameterString2 = new SqlString(Parameter.Placeholder);
  354. parameterString2.Parts.CopyTo(parameters2, 0);
  355. Assert.AreEqual(parameterString1, parameterString2);
  356. Assert.AreNotSame(parameterString1, parameterString2);
  357. parameters1[0].ParameterPosition = 231;
  358. Assert.IsNull(parameters2[0].ParameterPosition);
  359. // more simple version of the test
  360. Parameter.Placeholder.Should().Not.Be.SameInstanceAs(Parameter.Placeholder);
  361. }
  362. [Test]
  363. public void HashcodeEqualForEqualStringsWithDifferentHistory()
  364. {
  365. // Verify that sql strings that are generated in different ways, but _now_ have
  366. // equal content, also have equal hashcodes.
  367. SqlString sql = new SqlString(new string[] { "select", " from table" });
  368. sql = sql.Substring(6);
  369. SqlString sql2 = new SqlString(new string[] { " from table" });
  370. Assert.That(sql, Is.EqualTo(sql2));
  371. Assert.That(sql.GetHashCode(), Is.EqualTo(sql2.GetHashCode()));
  372. }
  373. }
  374. }