0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3
|
|
4 using BLToolkit.Common;
|
|
5 using BLToolkit.Data;
|
|
6 using BLToolkit.Data.Linq;
|
|
7 using BLToolkit.Data.Sql;
|
|
8
|
|
9 using NUnit.Framework;
|
|
10
|
|
11 namespace Data.Exceptions
|
|
12 {
|
|
13 using Linq;
|
|
14 using Linq.Model;
|
|
15
|
|
16 [TestFixture]
|
|
17 public class Common : TestBase
|
|
18 {
|
|
19 class MyDbManager : TestDbManager
|
|
20 {
|
|
21 public MyDbManager() : base("Sql2008") {}
|
|
22
|
|
23 protected override SqlQuery ProcessQuery(SqlQuery sqlQuery)
|
|
24 {
|
|
25 if (sqlQuery.IsInsert && sqlQuery.Insert.Into.Name == "Parent")
|
|
26 {
|
|
27 var expr =
|
|
28 new QueryVisitor().Find(sqlQuery.Insert, e =>
|
|
29 {
|
|
30 if (e.ElementType == QueryElementType.SetExpression)
|
|
31 {
|
|
32 var se = (SqlQuery.SetExpression)e;
|
|
33 return ((SqlField)se.Column).Name == "ParentID";
|
|
34 }
|
|
35
|
|
36 return false;
|
|
37 }) as SqlQuery.SetExpression;
|
|
38
|
|
39 if (expr != null)
|
|
40 {
|
|
41 var value = ConvertTo<int>.From(((IValueContainer)expr.Expression).Value);
|
|
42
|
|
43 if (value == 555)
|
|
44 {
|
|
45 var tableName = "Parent1";
|
|
46 var dic = new Dictionary<IQueryElement,IQueryElement>();
|
|
47
|
|
48 sqlQuery = new QueryVisitor().Convert(sqlQuery, e =>
|
|
49 {
|
|
50 if (e.ElementType == QueryElementType.SqlTable)
|
|
51 {
|
|
52 var oldTable = (SqlTable)e;
|
|
53
|
|
54 if (oldTable.Name == "Parent")
|
|
55 {
|
|
56 var newTable = new SqlTable(oldTable) { Name = tableName, PhysicalName = tableName };
|
|
57
|
|
58 foreach (var field in oldTable.Fields.Values)
|
|
59 dic.Add(field, newTable.Fields[field.Name]);
|
|
60
|
|
61 return newTable;
|
|
62 }
|
|
63 }
|
|
64
|
|
65 IQueryElement ex;
|
|
66 return dic.TryGetValue(e, out ex) ? ex : null;
|
|
67 });
|
|
68 }
|
|
69 }
|
|
70 }
|
|
71
|
|
72 return sqlQuery;
|
|
73 }
|
|
74 }
|
|
75
|
|
76 [Test, ExpectedException(typeof(DataException), ExpectedMessage = "Invalid object name 'Parent1'.")]
|
|
77 public void ReplaceTableTest([IncludeDataContexts("Sql2008", "Sql2012")] string context)
|
|
78 {
|
|
79 using (var db = new MyDbManager())
|
|
80 {
|
|
81 var n = 555;
|
|
82
|
|
83 db.Parent.Insert(() => new Parent
|
|
84 {
|
|
85 ParentID = n,
|
|
86 Value1 = n
|
|
87 });
|
|
88
|
|
89 db.Parent.Delete(p => p.ParentID == n);
|
|
90 }
|
|
91 }
|
|
92 }
|
|
93 }
|