-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvehicle_registration_parsed_partiql.go
295 lines (247 loc) · 9.99 KB
/
vehicle_registration_parsed_partiql.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"context"
"fmt"
"strings"
)
const (
VehicleRegistrationTableName = "VehicleRegistration"
VinMappingTableName = "VinMapping"
VehicleOwnershipTableName = "VehicleOwnership"
)
var doltSqlVehicleRegistrationTableInsertTemplate = "INSERT INTO VehicleRegistration (Id,State,City,PendingPenaltyTicketAmount,ValidFromDate,ValidToDate) VALUES ('%s', '%s', '%s', '%2f', '%s', '%s');"
var doltSqlVehicleRegistrationTableUpdateTemplate = "UPDATE VehicleRegistration SET `State` = '%s', City = '%s', PendingPenaltyTicketAmount = '%.2f', ValidFromDate = '%s', ValidToDate = '%s' WHERE Id = '%s';"
var doltSqlVinMappingTableVehicleRegistrationTableInsertOrUpdateTemplate = "INSERT INTO VinMapping (VIN,VehicleRegistrationIdFk) VALUES ('%s', '%s') ON DUPLICATE KEY UPDATE VehicleRegistrationIdFk = '%s';"
var doltSqlVehicleOwnershipTableVehicleRegistrationTableInsertOrUpdateTemplate = "INSERT INTO VehicleOwnership (VehicleRegistrationIdFk,PersonIdFk,IsPrimaryOwner) VALUES ('%s', '%s', %t) ON DUPLICATE KEY UPDATE IsPrimaryOwner = %t;"
var doltSqlLicensePlateNumberMappingTableVehicleRegistrationTableInsertTemplate = "INSERT INTO LicensePlateNumberMapping (LicensePlateNumber,VehicleRegistrationIdFk) VALUES ('%s', '%s') ON DUPLICATE KEY UPDATE VehicleRegistrationIdFk = '%s';"
type vehicleRegistrationParsedPartiQl struct {
st StatementType
stmt string
data []VehicleRegistrationData
revs []VehicleRegistrationRevision
}
var _ DoltParsedPartiQl = &vehicleRegistrationParsedPartiQl{}
func (d *vehicleRegistrationParsedPartiQl) addAndCommitDoltSql(ctx context.Context, message string) string {
return addAndCommitDoltSql(message)
}
func (d *vehicleRegistrationParsedPartiQl) formatPendingPenaltyTicketAmount(amount float64) float64 {
// todo: format amount for dolt
return amount
}
func (d *vehicleRegistrationParsedPartiQl) formatDate(dateStr string) string {
// todo: format date for dolt
return dateStr
}
func (d *vehicleRegistrationParsedPartiQl) getInsertOrUpdateLicensePlateNumberMappingRowData(ctx context.Context, regData []VehicleRegistrationData) ([]LicensePlateNumberMappingData, error) {
rows := make([]LicensePlateNumberMappingData, len(regData))
for idx, reg := range regData {
rows[idx] = LicensePlateNumberMappingData{
LicensePlateNumber: reg.LicensePlateNumber,
VehicleRegistrationIdFk: reg.Id,
}
}
return rows, nil
}
func (d *vehicleRegistrationParsedPartiQl) getInsertOrUpdateVinMappingRowData(ctx context.Context, regData []VehicleRegistrationData) ([]VinMappingData, error) {
rows := make([]VinMappingData, len(regData))
for idx, reg := range regData {
rows[idx] = VinMappingData{
VIN: reg.VIN,
VehicleRegistrationIdFk: reg.Id,
}
}
return rows, nil
}
func (d *vehicleRegistrationParsedPartiQl) getInsertOrUpdateVehicleOwnershipRowData(ctx context.Context, regData []VehicleRegistrationData) ([]VehicleOwnershipData, error) {
rows := make([]VehicleOwnershipData, 0)
for _, reg := range regData {
// add primary owner
if reg.Owners.PrimaryOwner.PersonId != "" {
rows = append(rows, VehicleOwnershipData{
PersonIdFk: reg.Owners.PrimaryOwner.PersonId,
VehicleRegistrationIdFk: reg.Id,
IsPrimaryOwner: true,
})
}
// add secondary owners
for _, secondary := range reg.Owners.SecondaryOwners {
if secondary.PersonId != "" {
rows = append(rows, VehicleOwnershipData{
PersonIdFk: secondary.PersonId,
VehicleRegistrationIdFk: reg.Id,
})
}
}
}
return rows, nil
}
func (d *vehicleRegistrationParsedPartiQl) getInsertOrUpdateVehicleRegistrationRowData(ctx context.Context) ([]VehicleRegistrationData, error) {
rows := make([]VehicleRegistrationData, len(d.revs))
// cheat and use the revisions
for idx, r := range d.revs {
secondaryOwners := make([]Owner, len(r.Data.Owners.SecondaryOwners))
for i, owner := range r.Data.Owners.SecondaryOwners {
secondaryOwners[i] = owner
}
rows[idx] = VehicleRegistrationData{
Id: r.Metadata.Id,
VIN: r.Data.VIN,
LicensePlateNumber: r.Data.LicensePlateNumber,
State: r.Data.State,
City: r.Data.City,
PendingPenaltyTicketAmount: d.formatPendingPenaltyTicketAmount(r.Data.PendingPenaltyTicketAmount),
ValidFromDate: d.formatDate(r.Data.ValidFromDate),
ValidToDate: d.formatDate(r.Data.ValidToDate),
Owners: Owners{
PrimaryOwner: Owner{PersonId: r.Data.Owners.PrimaryOwner.PersonId},
SecondaryOwners: secondaryOwners,
},
}
}
return rows, nil
}
func (d *vehicleRegistrationParsedPartiQl) insertAsDoltSql(ctx context.Context, transactionId string) (string, error) {
insertStmt := ""
regData, err := d.getInsertOrUpdateVehicleRegistrationRowData(ctx)
if err != nil {
return "", err
}
// handle vehicle registration inserts
for _, reg := range regData {
insertStmt += fmt.Sprintf(doltSqlVehicleRegistrationTableInsertTemplate,
reg.Id,
reg.State,
reg.City,
reg.PendingPenaltyTicketAmount,
reg.ValidFromDate,
reg.ValidToDate)
insertStmt += "\n"
}
// handle vin mapping inserts
vinData, err := d.getInsertOrUpdateVinMappingRowData(ctx, regData)
if err != nil {
return "", err
}
for _, vin := range vinData {
insertStmt += fmt.Sprintf(doltSqlVinMappingTableVehicleRegistrationTableInsertOrUpdateTemplate,
vin.VIN,
vin.VehicleRegistrationIdFk,
vin.VehicleRegistrationIdFk)
insertStmt += "\n"
}
// handle vehicle ownership inserts
ownershipData, err := d.getInsertOrUpdateVehicleOwnershipRowData(ctx, regData)
if err != nil {
return "", err
}
for _, owner := range ownershipData {
insertStmt += fmt.Sprintf(doltSqlVehicleOwnershipTableVehicleRegistrationTableInsertOrUpdateTemplate,
owner.VehicleRegistrationIdFk,
owner.PersonIdFk,
owner.IsPrimaryOwner,
owner.IsPrimaryOwner)
insertStmt += "\n"
}
// handle license plate number mapping
licenseMappingData, err := d.getInsertOrUpdateLicensePlateNumberMappingRowData(ctx, regData)
if err != nil {
return "", err
}
for _, license := range licenseMappingData {
insertStmt += fmt.Sprintf(doltSqlLicensePlateNumberMappingTableVehicleRegistrationTableInsertTemplate,
license.LicensePlateNumber,
license.VehicleRegistrationIdFk,
license.VehicleRegistrationIdFk)
insertStmt += "\n"
}
insertStmt += d.addAndCommitDoltSql(ctx, fmt.Sprintf("insert into %s, %s, %s, %s qldb transaction id: %s", VehicleRegistrationTableName, VinMappingTableName, VehicleOwnershipTableName, LicensePlateNumberMappingTableName, transactionId))
return insertStmt, nil
}
func (d *vehicleRegistrationParsedPartiQl) updateAsDoltSql(ctx context.Context, transactionId string) (string, error) {
updateStmt := ""
regData, err := d.getInsertOrUpdateVehicleRegistrationRowData(ctx)
if err != nil {
return "", err
}
// handle vehicle registration updates
for _, reg := range regData {
updateStmt += fmt.Sprintf(doltSqlVehicleRegistrationTableUpdateTemplate,
reg.State,
reg.City,
reg.PendingPenaltyTicketAmount,
reg.ValidFromDate,
reg.ValidToDate,
reg.Id)
updateStmt += "\n"
}
// handle vin mapping updates
vinData, err := d.getInsertOrUpdateVinMappingRowData(ctx, regData)
if err != nil {
return "", err
}
for _, vin := range vinData {
updateStmt += fmt.Sprintf(doltSqlVinMappingTableVehicleRegistrationTableInsertOrUpdateTemplate,
vin.VIN,
vin.VehicleRegistrationIdFk,
vin.VehicleRegistrationIdFk)
updateStmt += "\n"
}
// handle vehicle ownership updates
ownershipData, err := d.getInsertOrUpdateVehicleOwnershipRowData(ctx, regData)
if err != nil {
return "", err
}
for _, owner := range ownershipData {
updateStmt += fmt.Sprintf(doltSqlVehicleOwnershipTableVehicleRegistrationTableInsertOrUpdateTemplate,
owner.VehicleRegistrationIdFk,
owner.PersonIdFk,
owner.IsPrimaryOwner,
owner.IsPrimaryOwner)
updateStmt += "\n"
}
// handle license plate number mapping updates
licenseMappingData, err := d.getInsertOrUpdateLicensePlateNumberMappingRowData(ctx, regData)
if err != nil {
return "", err
}
for _, license := range licenseMappingData {
updateStmt += fmt.Sprintf(doltSqlLicensePlateNumberMappingTableVehicleRegistrationTableInsertTemplate,
license.LicensePlateNumber,
license.VehicleRegistrationIdFk,
license.VehicleRegistrationIdFk)
updateStmt += "\n"
}
updateStmt += d.addAndCommitDoltSql(ctx, fmt.Sprintf("update %s, %s, %s, %s qldb transaction id: %s", VehicleRegistrationTableName, VinMappingTableName, VehicleOwnershipTableName, LicensePlateNumberMappingTableName, transactionId))
return updateStmt, nil
}
func (d *vehicleRegistrationParsedPartiQl) deleteAsDoltSql(ctx context.Context, transactionId string) (string, error) {
deleteStmt := ""
// todo: actually parse partiql
// cheat and just check against hardcoded VIN
vin := "1G1ZG57B38F112851"
shouldDelete := false
if strings.Contains(d.stmt, vin) {
shouldDelete = true
}
if !shouldDelete {
return "", ErrNoDoltSqlEquivalent
}
deleteStmt += fmt.Sprintf("DELETE FROM VehicleRegistration WHERE Id = (SELECT vm.VehicleRegistrationIdFk FROM VinMapping as vm where vm.VIN = '%s');", vin) + "\n"
deleteStmt += fmt.Sprintf("UPDATE VinMapping SET VehicleRegistrationIdFk = NULL WHERE VIN = '%s';", vin) + "\n"
deleteStmt += fmt.Sprintf("DELETE FROM VinMapping WHERE VehicleRegistrationIdFk IS NULL AND VehicleIdFk IS NULL;") + "\n"
deleteStmt += d.addAndCommitDoltSql(ctx, fmt.Sprintf("delete from %s, %s qldb transaction id: %s", VehicleTableName, VinMappingTableName, transactionId))
return deleteStmt, nil
}
func (d *vehicleRegistrationParsedPartiQl) AsDoltSql(ctx context.Context, transactionId string) (string, error) {
switch d.st {
case SqlStatementTypeSelect, SqlStatementTypeCreate:
return "", ErrNoDoltSqlEquivalent
case SqlStatementTypeInsert:
return d.insertAsDoltSql(ctx, transactionId)
case SqlStatementTypeUpdate:
return d.updateAsDoltSql(ctx, transactionId)
case SqlStatementTypeDelete:
return d.deleteAsDoltSql(ctx, transactionId)
}
return "", ErrNoDoltSqlEquivalent
}