-
Notifications
You must be signed in to change notification settings - Fork 45
/
databasescriptview.go
192 lines (175 loc) · 5.91 KB
/
databasescriptview.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package database
import (
"fmt"
"path/filepath"
"github.com/anz-bank/sysl/pkg/sysl"
"github.com/sirupsen/logrus"
"sort"
"strings"
)
type TableDetails struct {
table *sysl.Type
tableOld *sysl.Type
action string
name string
}
type ScriptView struct {
stringBuilder *strings.Builder
logger *logrus.Logger
title string
}
type ScriptOutput struct {
filename string
content string
}
func MakeScriptOutput(filename, content string) *ScriptOutput {
return &ScriptOutput{
filename: filename,
content: content,
}
}
func MakeTableDetails(table, tableOld *sysl.Type,
action, name string,
) *TableDetails {
return &TableDetails{
table: table,
tableOld: tableOld,
action: action,
name: name,
}
}
func MakeDatabaseScriptView(title string, logger *logrus.Logger,
) *ScriptView {
var stringBuilder strings.Builder
return &ScriptView{
stringBuilder: &stringBuilder,
title: title,
logger: logger,
}
}
func (v *ScriptView) GenerateDatabaseScriptCreate(tableMap map[string]*sysl.Type,
dbType, appName string) string {
v.stringBuilder.WriteString(fmt.Sprintf("/*TITLE : %s*/\n", v.title))
v.stringBuilder.WriteString(databaseScriptHeader)
appHeader := "\n\n/*-----------------------Relation Model : " +
appName + "-----------------------------------------------*/\n"
v.stringBuilder.WriteString(appHeader)
visitedAttributes := map[string]string{}
completedTableDepthMap := CreateTableDepthMap(tableMap)
var depthsFound []int
for depth := range completedTableDepthMap {
depthsFound = append(depthsFound, depth)
}
sort.Ints(depthsFound)
for _, depth := range depthsFound {
tableNames := completedTableDepthMap[depth]
var lineNumbers []int32
var entityNames []string
lineNumberMap := map[int32]string{}
for _, tableName := range tableNames {
table := tableMap[tableName]
lineNumber := table.GetSourceContext().GetStart().GetLine()
lineNumberMap[lineNumber] = tableName
lineNumbers = append(lineNumbers, lineNumber)
}
sort.Slice(lineNumbers, func(i, j int) bool { return lineNumbers[i] < lineNumbers[j] })
for _, lineNo := range lineNumbers {
entityName := lineNumberMap[lineNo]
entityNames = append(entityNames, entityName)
}
for _, entityName := range entityNames {
entityType := tableMap[entityName]
if relEntity := entityType.GetRelation(); relEntity != nil {
v.writeCreateSQLForATable(entityName, relEntity, visitedAttributes)
}
}
}
return v.stringBuilder.String()
}
func (v *ScriptView) ProcessModSysls(appsOld, appsNew map[string]*sysl.Application,
appNames []string, outputDir, dbType string) []ScriptOutput {
var outputSlice []ScriptOutput
for _, appName := range appNames {
appOld := appsOld[appName]
appNew := appsNew[appName]
if appOld != nil && appNew != nil {
v.stringBuilder.Reset()
typeMapOld := appOld.GetTypes()
typeMapNew := appNew.GetTypes()
tableDepthMapOld := CreateTableDepthMap(typeMapOld)
tableDepthMapNew := CreateTableDepthMap(typeMapNew)
tablesWithActions := findAddedDeletedRetainedTables(typeMapOld, typeMapNew, tableDepthMapOld, tableDepthMapNew)
outStr := v.processTablesForModifiedApps(tablesWithActions, v.title, appName, dbType)
outputFile := filepath.Join(outputDir, appName+SQLExtension)
outputStruct := MakeScriptOutput(outputFile, outStr)
outputSlice = append(outputSlice, *outputStruct)
} else if appNew != nil && appOld == nil {
v.stringBuilder.Reset()
outStr := v.GenerateDatabaseScriptCreate(appNew.GetTypes(), dbType, appName)
outputFile := filepath.Join(outputDir, appName+SQLExtension)
outputStruct := MakeScriptOutput(outputFile, outStr)
outputSlice = append(outputSlice, *outputStruct)
}
}
return outputSlice
}
func (v *ScriptView) processTablesForModifiedApps(tableDetails []TableDetails,
title, appName, dbType string) string {
outputStr := v.generateDatabaseScriptModify(tableDetails, dbType, appName)
return outputStr
}
func findAddedDeletedRetainedTables(
tableMapOld map[string]*sysl.Type,
tableMapNew map[string]*sysl.Type,
tableDepthMapOld, tableDepthMapNew map[int][]string) []TableDetails {
tableDepthsListNew := makeSortedListOfTableDepth(tableDepthMapNew)
var tableWithAction []TableDetails
//Add the retained and added tables
for _, depth := range tableDepthsListNew {
tableNames := tableDepthMapNew[depth]
sort.Strings(tableNames)
for _, tableName := range tableNames {
_, ok := tableMapOld[tableName]
if ok {
tableDetails := MakeTableDetails(tableMapNew[tableName],
tableMapOld[tableName], Retain, tableName)
tableWithAction = append(tableWithAction, *tableDetails)
} else {
tableDetails := MakeTableDetails(tableMapNew[tableName],
nil, Add, tableName)
tableWithAction = append(tableWithAction, *tableDetails)
}
}
}
return tableWithAction
}
func makeSortedListOfTableDepth(tableDepthMap map[int][]string) []int {
var depthList []int
for depth := range tableDepthMap {
depthList = append(depthList, depth)
}
sort.Ints(depthList)
return depthList
}
func (v *ScriptView) generateDatabaseScriptModify(tableDetails []TableDetails,
dbType, appName string) string {
v.stringBuilder.WriteString(fmt.Sprintf("/*TITLE : %s*/\n", v.title))
v.stringBuilder.WriteString(databaseScriptHeader)
appHeader := "\n\n/*-----------------------Relation Model : " +
appName + "-----------------------------------------------*/\n"
v.stringBuilder.WriteString(appHeader)
visitedAttributes := map[string]string{}
for _, tableDetail := range tableDetails {
switch tableDetail.action {
case "ADD":
v.writeCreateSQLForATable(tableDetail.name, tableDetail.table.GetRelation(), visitedAttributes)
case "RETAIN":
v.writeModifySQLForATable(tableDetail.name, tableDetail.table.GetRelation(),
tableDetail.tableOld.GetRelation(), visitedAttributes)
default:
v.logger.Warnf("the table action is spcified as %s, which is not valid. Hence ignored\n",
tableDetail.action)
}
}
return v.stringBuilder.String()
}