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
Expand file tree
/
Copy pathXpoDataStoreProxy.vb
More file actions
106 lines (100 loc) · 5.49 KB
/
XpoDataStoreProxy.vb
File metadata and controls
106 lines (100 loc) · 5.49 KB
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
Imports System.Linq
Imports DevExpress.Xpo
Imports DevExpress.Xpo.DB
Imports DevExpress.Xpo.Helpers
Imports DevExpress.Xpo.Metadata
Imports System.Collections.Generic
Namespace WinWebSolution.Module
Public Class XpoDataStoreProxy
Implements IDataStore, ICommandChannel
Private legacyDataLayer As SimpleDataLayer
Private legacyDataStore As IDataStore
Private tempDataLayer As SimpleDataLayer
Private tempDataStore As IDataStore
Private tempDatabaseTables() As String = {"ModuleInfo", "XPObjectType"}
Private Function IsTempDatabaseTable(ByVal tableName As String) As Boolean
If Not String.IsNullOrEmpty(tableName) Then
For Each currentTableName As String In tempDatabaseTables
If tableName.EndsWith(currentTableName) Then
Return True
End If
Next currentTableName
End If
Return False
End Function
Public Sub Initialize(ByVal dictionary As XPDictionary, ByVal legacyConnectionString As String, ByVal tempConnectionString As String)
Dim legacyDictionary As New ReflectionDictionary()
Dim tempDictionary As New ReflectionDictionary()
For Each ci As XPClassInfo In dictionary.Classes
If Not IsTempDatabaseTable(ci.TableName) Then
legacyDictionary.QueryClassInfo(ci.ClassType)
Else
tempDictionary.QueryClassInfo(ci.ClassType)
End If
Next ci
legacyDataStore = XpoDefault.GetConnectionProvider(legacyConnectionString, AutoCreateOption.DatabaseAndSchema)
legacyDataLayer = New SimpleDataLayer(legacyDictionary, legacyDataStore)
tempDataStore = XpoDefault.GetConnectionProvider(tempConnectionString, AutoCreateOption.DatabaseAndSchema)
tempDataLayer = New SimpleDataLayer(tempDictionary, tempDataStore)
End Sub
Public ReadOnly Property AutoCreateOption() As AutoCreateOption Implements IDataStore.AutoCreateOption
Get
Return AutoCreateOption.DatabaseAndSchema
End Get
End Property
Public Function ModifyData(ByVal ParamArray dmlStatements() As ModificationStatement) As ModificationResult Implements IDataStore.ModifyData
Dim legacyChanges As New List(Of ModificationStatement)(dmlStatements.Length)
Dim tempChanges As New List(Of ModificationStatement)(dmlStatements.Length)
For Each stm As ModificationStatement In dmlStatements
If IsTempDatabaseTable(stm.Table.Name) Then
tempChanges.Add(stm)
Else
legacyChanges.Add(stm)
End If
Next stm
Dim resultSet As New List(Of ParameterValue)()
If legacyChanges.Count > 0 Then
resultSet.AddRange(legacyDataLayer.ModifyData(legacyChanges.ToArray()).Identities)
End If
If tempChanges.Count > 0 Then
resultSet.AddRange(tempDataLayer.ModifyData(tempChanges.ToArray()).Identities)
End If
Return New ModificationResult(resultSet)
End Function
Public Function SelectData(ByVal ParamArray selects() As SelectStatement) As SelectedData Implements IDataStore.SelectData
Dim isExternals = selects.Select(Function(stmt) IsTempDatabaseTable(stmt.Table.Name)).ToList()
Dim mainSelects As New List(Of SelectStatement)(selects.Length)
Dim externalSelects As New List(Of SelectStatement)(selects.Length)
For i As Integer = 0 To isExternals.Count - 1
Dim temp = If(isExternals(i), externalSelects, mainSelects)
temp.Add(selects(i))
Next i
Dim externalResults = (If(externalSelects.Count = 0, Enumerable.Empty(Of SelectStatementResult)(), tempDataLayer.SelectData(externalSelects.ToArray()).ResultSet)).GetEnumerator()
Dim mainResults = (If(mainSelects.Count = 0, Enumerable.Empty(Of SelectStatementResult)(), legacyDataLayer.SelectData(mainSelects.ToArray()).ResultSet)).GetEnumerator()
Dim results(isExternals.Count - 1) As SelectStatementResult
For i As Integer = 0 To results.Length - 1
Dim enumerator = If(isExternals(i), externalResults, mainResults)
enumerator.MoveNext()
results(i) = enumerator.Current
Next i
Return New SelectedData(results)
End Function
Public Function UpdateSchema(ByVal dontCreateIfFirstTableNotExist As Boolean, ByVal ParamArray tables() As DBTable) As UpdateSchemaResult Implements IDataStore.UpdateSchema
Dim db1Tables As New List(Of DBTable)()
Dim db2Tables As New List(Of DBTable)()
For Each table As DBTable In tables
If Not IsTempDatabaseTable(table.Name) Then
db1Tables.Add(table)
Else
db2Tables.Add(table)
End If
Next table
legacyDataStore.UpdateSchema(False, db1Tables.ToArray())
tempDataStore.UpdateSchema(False, db2Tables.ToArray())
Return UpdateSchemaResult.SchemaExists
End Function
Public Function [Do](ByVal command As String, ByVal args As Object) As Object Implements ICommandChannel.Do
Return DirectCast(legacyDataLayer, ICommandChannel).Do(command, args)
End Function
End Class
End Namespace