-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrivers_license_parsed_sql.go
126 lines (104 loc) · 4.16 KB
/
drivers_license_parsed_sql.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
package main
import (
"context"
"fmt"
)
const (
DriversLicenseTableName = "DriversLicense"
LicensePlateNumberMappingTableName = "LicensePlateNumberMapping"
)
type driversLicenseParsedPartiQl struct {
st StatementType
stmt string
data []DriversLicenseData
revs []DriversLicenseRevision
}
var _ DoltParsedPartiQl = &driversLicenseParsedPartiQl{}
var doltSqlDriversLicenseTableInsertTemplate = "INSERT INTO DriversLicense (Id,LicenseType,ValidFromDate,ValidToDate,PersonIdFk) VALUES ('%s', '%s', '%s', '%s', '%s');"
var doltSqlDriversLicenseTableUpdateTemplate = "UPDATE DriversLicense SET LicenseType = '%s', ValidFromDate = '%s', ValidToDate = '%s', PersonIdFk = '%s' WHERE Id = '%s';"
var doltSqlLicensePlateNumberMappingTableDriversLicenseTableInsertTemplate = "INSERT INTO LicensePlateNumberMapping (LicensePlateNumber,DriversLicenseIdFk) VALUES ('%s', '%s') ON DUPLICATE KEY UPDATE DriversLicenseIdFk = '%s';"
func (d *driversLicenseParsedPartiQl) addAndCommitDoltSql(ctx context.Context, message string) string {
return addAndCommitDoltSql(message)
}
func (d *driversLicenseParsedPartiQl) formatDate(dateStr string) string {
// todo: format date for dolt
return dateStr
}
func (d *driversLicenseParsedPartiQl) getInsertDriversLicenseRowData(ctx context.Context) ([]DriversLicenseData, error) {
rows := make([]DriversLicenseData, len(d.revs))
// cheat and use the revisions
for idx, r := range d.revs {
rows[idx] = DriversLicenseData{
Id: r.Metadata.Id,
LicensePlateNumber: r.Data.LicensePlateNumber,
LicenseType: r.Data.LicenseType,
ValidFromDate: r.Data.ValidFromDate,
ValidToDate: r.Data.ValidToDate,
PersonIdFk: r.Data.PersonIdFk,
}
}
return rows, nil
}
func (d *driversLicenseParsedPartiQl) getInsertLicensePlateNumberMappingRowData(ctx context.Context, licenseData []DriversLicenseData) ([]LicensePlateNumberMappingData, error) {
rows := make([]LicensePlateNumberMappingData, len(licenseData))
for idx, license := range licenseData {
rows[idx] = LicensePlateNumberMappingData{
LicensePlateNumber: license.LicensePlateNumber,
DriversLicenseIdFk: license.Id,
}
}
return rows, nil
}
func (d *driversLicenseParsedPartiQl) insertAsDoltSql(ctx context.Context, transactionId string) (string, error) {
insertStmt := ""
driversLicenseData, err := d.getInsertDriversLicenseRowData(ctx)
if err != nil {
return "", err
}
// handle drivers license inserts
for _, license := range driversLicenseData {
insertStmt += fmt.Sprintf(doltSqlDriversLicenseTableInsertTemplate,
license.Id,
license.LicenseType,
d.formatDate(license.ValidFromDate),
d.formatDate(license.ValidToDate),
license.PersonIdFk,
)
insertStmt += "\n"
}
// handle license plate mapping inserts
licensePlateMappingData, err := d.getInsertLicensePlateNumberMappingRowData(ctx, driversLicenseData)
if err != nil {
return "", err
}
for _, license := range licensePlateMappingData {
insertStmt += fmt.Sprintf(doltSqlLicensePlateNumberMappingTableDriversLicenseTableInsertTemplate,
license.LicensePlateNumber,
license.DriversLicenseIdFk,
license.DriversLicenseIdFk)
insertStmt += "\n"
}
insertStmt += d.addAndCommitDoltSql(ctx, fmt.Sprintf("insert into %s, %s qldb transaction id: %s", DriversLicenseTableName, LicensePlateNumberMappingTableName, transactionId))
return insertStmt, nil
}
func (d *driversLicenseParsedPartiQl) updateAsDoltSql(ctx context.Context, transactionId string) (string, error) {
// todo: handle updates
return "", ErrNoDoltSqlEquivalent
}
func (d *driversLicenseParsedPartiQl) deleteAsDoltSql(ctx context.Context, transactionId string) (string, error) {
// todo: handle deletes
return "", ErrNoDoltSqlEquivalent
}
func (d *driversLicenseParsedPartiQl) 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
}