Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process all tables in parallel #61

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
uses: actions/setup-go@v2
with:
stable: false
go-version: 1.18
go-version: 1.20.14

- name: Build
run: go build -v ./...
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/olachat/gola/v2

go 1.18
go 1.20

replace github.com/dolthub/go-mysql-server v0.12.0 => github.com/Wuvist/go-mysql-server v0.10.1-0.20220815042140-eac9e0ba16d8

Expand Down
75 changes: 44 additions & 31 deletions golalib/golalib.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"

"github.com/olachat/gola/v2/mysqldriver"
"github.com/olachat/gola/v2/mysqlparser"
"github.com/olachat/gola/v2/ormtpl"
"github.com/olachat/gola/v2/structs"
)

// GenWithParser generate orm stubs from config provided
func GenWithParser(config mysqlparser.MySQLParserConfig, output string) int {
m := &mysqlparser.MySQLParser{}
db, err := m.Assemble(config)
Expand All @@ -30,46 +33,58 @@ func GenWithParser(config mysqlparser.MySQLParserConfig, output string) int {
output = "temp"
}

if !strings.HasPrefix(output, "/") {
// output folder is relative path
wd, err := os.Getwd()
if err != nil {
wd = "."
}
output = wd + string(filepath.Separator) + output
}

if !strings.HasSuffix(output, string(filepath.Separator)) {
output = output + string(filepath.Separator)
output, err = filepath.Abs(output)
if err != nil {
panic(err)
}
output += string(filepath.Separator)

var hasFailure atomic.Bool
wg := &sync.WaitGroup{}
for _, t := range db.Tables {
if len(t.GetPKColumns()) == 0 {
println(t.Name + " doesn't have primay key")
continue
}
wg.Add(1)
go func(table *structs.Table, wgInner *sync.WaitGroup) {
defer wgInner.Done()
files := genORM(table)
needMkdir := true
for path, data := range files {
if needMkdir {
pos := strings.LastIndex(path, string(filepath.Separator))
expectedFileFolder := output + path[0:pos]
errMkdir := os.Mkdir(expectedFileFolder, os.ModePerm)
if errMkdir != nil && os.IsNotExist(errMkdir) {
println("Failed to create folder, please ensure " + output[:len(output)-1] + " exists")
hasFailure.Store(true)
break
}
needMkdir = false
}

files := genORM(t)
needMkdir := true
for path, data := range files {
if needMkdir {
pos := strings.LastIndex(path, string(filepath.Separator))
expectedFileFolder := output + path[0:pos]
err = os.Mkdir(expectedFileFolder, os.ModePerm)
if err != nil && os.IsNotExist(err) {
println("Failed to create folder, please ensure " + output[:len(output)-1] + " exists")
return 1
errWritefile := os.WriteFile(output+path, data, 0o644)
if errWritefile != nil {
println("Failed to write file" + output + path)
hasFailure.Store(true)
break
}
needMkdir = false
}

ioutil.WriteFile(output+path, data, 0644)
}
}(t, wg)
}
wg.Wait()
if hasFailure.Load() {
return 1
}

files := genPackage(db)
for path, data := range files {
ioutil.WriteFile(output+path, data, 0644)
err = os.WriteFile(output+path, data, 0o644)
if err != nil {
println("Failed to write file" + output + path)
return 1
}
}

fmt.Printf("code generated in %s\n", output[:len(output)-1])
Expand Down Expand Up @@ -125,13 +140,13 @@ func Run(config mysqldriver.DBConfig, output string) int {
needMkdir = false
}

ioutil.WriteFile(output+path, data, 0644)
ioutil.WriteFile(output+path, data, 0o644)
}
}

files := genPackage(db)
for path, data := range files {
ioutil.WriteFile(output+path, data, 0644)
ioutil.WriteFile(output+path, data, 0o644)
}

fmt.Printf("code generated in %s\n", output[:len(output)-1])
Expand Down Expand Up @@ -190,9 +205,7 @@ func genORM(t *structs.Table) map[string][]byte {
return files
}

var (
rgxSyntaxError = regexp.MustCompile(`(\d+):\d+: `)
)
var rgxSyntaxError = regexp.MustCompile(`(\d+):\d+: `)

func formatBuffer(buf []byte) ([]byte, error) {
output, err := format.Source(buf)
Expand Down
Loading