-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql-manipulator.go
65 lines (62 loc) · 1.37 KB
/
sql-manipulator.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
package main
import (
"fmt"
"log"
"strings"
)
func CreateSelectInsertionFromSchema(schemaName, tableName string, columns [][2]string) string {
var sb strings.Builder
write(&sb, `SELECT CONCAT('INSERT INTO %s.%s SET ', `, schemaName, tableName)
for i, v := range columns {
name := v[0]
_type := v[1]
nullVal := getNullValue(_type)
isNull := "x.%s"
setVal := `x.%s`
if nullVal != 0 {
isNull = "CAST(x.%s AS CHAR)"
setVal = "QUOTE(x.%s)"
}
isNull = fmt.Sprintf(isNull, name)
setVal = fmt.Sprintf(setVal, name)
write(
&sb,
`'%s = ', IF(ISNULL(%s), %v, %s)`,
name,
isNull,
nullVal,
setVal,
)
if i < len(columns)-1 {
write(&sb, ", ', ', ")
}
}
write(&sb, `, ';' ) AS s FROM %s.%s x;`, schemaName, tableName)
return sb.String()
}
func write(sb *strings.Builder, template string, a ...any) (int, error) {
s := fmt.Sprintf(template, a...)
return sb.WriteString(s)
}
func getNullValue(t string) any {
log.Println(t)
intTypes := []string{"int", "integer", "bigint", "tinyint", "smallint", "mediumint", "decimal", "numeric", "float", "double"}
if contains(t, intTypes) {
return 0
}
if t == "date" {
return "QUOTE('1990-01-01')"
}
if t == "datetime" {
return "QUOTE('1990-01-01 00:00:00')"
}
return `QUOTE('NULL')`
}
func contains(v string, a []string) bool {
for _, i := range a {
if i == v {
return true
}
}
return false
}