xavehoo
/
XAF_how-to-prevent-altering-the-legacy-database-schema-when-creating-an-xaf-application-e1150
Public
forked from DevExpress-Examples/xaf-how-to-prevent-altering-the-legacy-database-schema-when-creating-an-xaf-application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XpoDataStoreProxy.cs
106 lines (101 loc) · 4.24 KB
/
XpoDataStoreProxy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Linq;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.Xpo.Helpers;
using DevExpress.Xpo.Metadata;
using System.Collections.Generic;
namespace WinWebSolution.Module {
public class XpoDataStoreProxy : IDataStore, ICommandChannel {
private SimpleDataLayer legacyDataLayer;
private IDataStore legacyDataStore;
private SimpleDataLayer tempDataLayer;
private IDataStore tempDataStore;
private string[] tempDatabaseTables = new string[] { "ModuleInfo", "XPObjectType" };
private bool IsTempDatabaseTable(string tableName) {
if(!string.IsNullOrEmpty(tableName)) {
foreach(string currentTableName in tempDatabaseTables) {
if(tableName.EndsWith(currentTableName)) {
return true;
}
}
}
return false;
}
public void Initialize(XPDictionary dictionary, string legacyConnectionString, string tempConnectionString) {
ReflectionDictionary legacyDictionary = new ReflectionDictionary();
ReflectionDictionary tempDictionary = new ReflectionDictionary();
foreach(XPClassInfo ci in dictionary.Classes) {
if(!IsTempDatabaseTable(ci.TableName)) {
legacyDictionary.QueryClassInfo(ci.ClassType);
}
else {
tempDictionary.QueryClassInfo(ci.ClassType);
}
}
legacyDataStore = XpoDefault.GetConnectionProvider(legacyConnectionString, AutoCreateOption.DatabaseAndSchema);
legacyDataLayer = new SimpleDataLayer(legacyDictionary, legacyDataStore);
tempDataStore = XpoDefault.GetConnectionProvider(tempConnectionString, AutoCreateOption.DatabaseAndSchema);
tempDataLayer = new SimpleDataLayer(tempDictionary, tempDataStore);
}
public AutoCreateOption AutoCreateOption {
get {
return AutoCreateOption.DatabaseAndSchema;
}
}
public ModificationResult ModifyData(params ModificationStatement[] dmlStatements) {
List<ModificationStatement> legacyChanges = new List<ModificationStatement>(dmlStatements.Length);
List<ModificationStatement> tempChanges = new List<ModificationStatement>(dmlStatements.Length);
foreach(ModificationStatement stm in dmlStatements) {
if(IsTempDatabaseTable(stm.Table.Name)) {
tempChanges.Add(stm);
}
else {
legacyChanges.Add(stm);
}
}
List<ParameterValue> resultSet = new List<ParameterValue>();
if(legacyChanges.Count > 0) {
resultSet.AddRange(legacyDataLayer.ModifyData(legacyChanges.ToArray()).Identities);
}
if(tempChanges.Count > 0) {
resultSet.AddRange(tempDataLayer.ModifyData(tempChanges.ToArray()).Identities);
}
return new ModificationResult(resultSet);
}
public SelectedData SelectData(params SelectStatement[] selects) {
var isExternals = selects.Select(stmt => IsTempDatabaseTable(stmt.Table.Name)).ToList();
List<SelectStatement> mainSelects = new List<SelectStatement>(selects.Length);
List<SelectStatement> externalSelects = new List<SelectStatement>(selects.Length);
for(int i = 0; i < isExternals.Count; ++i) {
(isExternals[i] ? externalSelects : mainSelects).Add(selects[i]);
}
var externalResults = (externalSelects.Count == 0 ? Enumerable.Empty<SelectStatementResult>() : tempDataLayer.SelectData(externalSelects.ToArray()).ResultSet).GetEnumerator();
var mainResults = (mainSelects.Count == 0 ? Enumerable.Empty<SelectStatementResult>() : legacyDataLayer.SelectData(mainSelects.ToArray()).ResultSet).GetEnumerator();
SelectStatementResult[] results = new SelectStatementResult[isExternals.Count];
for(int i = 0; i < results.Length; ++i) {
var enumerator = isExternals[i] ? externalResults : mainResults;
enumerator.MoveNext();
results[i] = enumerator.Current;
}
return new SelectedData(results);
}
public UpdateSchemaResult UpdateSchema(bool dontCreateIfFirstTableNotExist, params DBTable[] tables) {
List<DBTable> db1Tables = new List<DBTable>();
List<DBTable> db2Tables = new List<DBTable>();
foreach(DBTable table in tables) {
if(!IsTempDatabaseTable(table.Name)) {
db1Tables.Add(table);
}
else {
db2Tables.Add(table);
}
}
legacyDataStore.UpdateSchema(false, db1Tables.ToArray());
tempDataStore.UpdateSchema(false, db2Tables.ToArray());
return UpdateSchemaResult.SchemaExists;
}
public object Do(string command, object args) {
return ((ICommandChannel)legacyDataLayer).Do(command, args);
}
}
}