gosora/cmd/query_gen/spitter.go

46 lines
1.2 KiB
Go
Raw Normal View History

package main
import "strings"
import "github.com/Azareal/Gosora/query_gen"
type PrimaryKeySpitter struct {
2022-02-21 03:16:15 +00:00
keys map[string]string
}
func NewPrimaryKeySpitter() *PrimaryKeySpitter {
2022-02-21 03:16:15 +00:00
return &PrimaryKeySpitter{make(map[string]string)}
}
func (spit *PrimaryKeySpitter) Hook(name string, args ...interface{}) error {
2022-02-21 03:16:15 +00:00
if name == "CreateTableStart" {
var found string
var table = args[0].(*qgen.DBInstallTable)
for _, key := range table.Keys {
if key.Type == "primary" {
expl := strings.Split(key.Columns, ",")
if len(expl) > 1 {
continue
}
found = key.Columns
}
if found != "" {
table := table.Name
spit.keys[table] = found
}
}
}
return nil
}
func (spit *PrimaryKeySpitter) Write() error {
2022-02-21 03:16:15 +00:00
out := `// Generated by Gosora's Query Generator. DO NOT EDIT.
package main
var dbTablePrimaryKeys = map[string]string{
`
2022-02-21 03:16:15 +00:00
for table, key := range spit.keys {
out += "\t\"" + table + "\":\"" + key + "\",\n"
}
return writeFile("./gen_tables.go", out+"}\n")
}