diff --git a/trv/db.go b/trv/db.go index 8e805e4..5c95c76 100644 --- a/trv/db.go +++ b/trv/db.go @@ -13,7 +13,9 @@ import ( ) type DB struct { - tables []Table + Tables []Table `json:"tables"` + Name string `json:"name"` + Desc string `json:"desc"` } // If there is DB data locally, load it and return it. @@ -23,7 +25,7 @@ func (d *DB) loadData(repo, path string) { log.Printf("loadData fail:%s", err) } raw, _ := ioutil.ReadFile(fmt.Sprintf("%s/.trv/%s-%s.json", home, repo, strings.Replace(path, "/", "-", -1))) - json.Unmarshal(raw, &d.tables) + json.Unmarshal(raw, &d.Tables) } // Store DB data locally. @@ -37,7 +39,7 @@ func (d *DB) saveData(repo, path string) { log.Printf("saveData fail:%s", err) } } - file, err := json.MarshalIndent(d.tables, "", " ") + file, err := json.MarshalIndent(d.Tables, "", " ") if err != nil { log.Printf("saveData fail:%s", err) } @@ -47,21 +49,39 @@ func (d *DB) saveData(repo, path string) { } func (d *DB) fetchDBInfo(client *github.Client, ctx context.Context, source Source) error { - _, contents, _, err := client.Repositories.GetContents(ctx, source.Owner, source.Repo, source.Path, nil) - if err != nil { - return fmt.Errorf("fech DB info fail:%w", err) - } - for _, v := range contents { - path := v.GetPath() - if strings.Contains(path, ".md") { - if strings.Contains(path, "README.md") { - continue - } - var table Table - if err := table.fetchTableInfo(client, ctx, source.Owner, source.Repo, path); err != nil { - return fmt.Errorf("fech DB info fail:%w", err) + content, _, _, _ := client.Repositories.GetContents(ctx, source.Owner, source.Repo, fmt.Sprintf("%s/schema.json", source.Path), nil) + if content != nil { + text, err := content.GetContent() + if err != nil { + return fmt.Errorf("fetch table info fail:%w", err) + } + var table DB + err = json.Unmarshal([]byte(text), &table) + if err != nil { + fmt.Println(err) + } + d.Tables = table.Tables + } else { + if len(d.Tables) != 0 { + return nil + } + // Processing in the absence of schema.json + _, contents, _, err := client.Repositories.GetContents(ctx, source.Owner, source.Repo, source.Path, nil) + if err != nil { + return fmt.Errorf("fech DB info fail:%w", err) + } + for _, v := range contents { + path := v.GetPath() + if strings.Contains(path, ".md") { + if strings.Contains(path, "README.md") { + continue + } + var table Table + if err := table.fetchTableInfoFromMarkdown(client, ctx, source.Owner, source.Repo, path); err != nil { + return fmt.Errorf("fech DB info fail:%w", err) + } + d.Tables = append(d.Tables, table) } - d.tables = append(d.tables, table) } } return nil diff --git a/trv/source.go b/trv/source.go index 9043408..42cbc6c 100644 --- a/trv/source.go +++ b/trv/source.go @@ -26,10 +26,6 @@ func (s Source) setDbData() (DB, error) { return DB{}, err } - if len(db.tables) != 0 { - return db, nil - } - if err := db.fetchDBInfo(client, ctx, s); err != nil { return DB{}, fmt.Errorf("set DB data fail:%w", err) } diff --git a/trv/table.go b/trv/table.go index c99111e..f26cfd0 100644 --- a/trv/table.go +++ b/trv/table.go @@ -10,16 +10,17 @@ import ( ) type Column struct { - Name string - Type string - Defaul bool - Comment string + Name string `json:"name"` + Type string `json:"type"` + Nullable bool `json:"nullable"` + Defaul bool `json:"default"` + Comment string `json:"comment"` } type Table struct { - Name string - Description string - Columns []Column - UpdateDate time.Time + Name string `json:"name"` + Description string `json:"comment"` + Columns []Column `json:"columns"` + UpdateDate time.Time `json:"tables"` } // return table_name.column_name @@ -27,7 +28,7 @@ func (t Table) getFullName(i int) string { return t.Name + "." + t.Columns[i].Name } -func (t *Table) fetchTableInfo(client *github.Client, ctx context.Context, owner, repo, path string) error { +func (t *Table) fetchTableInfoFromMarkdown(client *github.Client, ctx context.Context, owner, repo, path string) error { content, _, _, err := client.Repositories.GetContents(ctx, owner, repo, path, nil) if err != nil { return fmt.Errorf("fetch table info fail:%w", err) diff --git a/trv/trv.go b/trv/trv.go index 7061eef..01344da 100644 --- a/trv/trv.go +++ b/trv/trv.go @@ -103,7 +103,7 @@ func (t *Trv) setSourceSelecter() error { t.Pages.ShowPage("error") return } - t.Tables = t.DB.tables + t.Tables = t.DB.Tables t.filterList() t.App.SetFocus(t.Searcher) }) @@ -125,7 +125,7 @@ func (t *Trv) addDropdownOption() error { t.SourceSelecter.RemoveOption(currentOptionCount - 1) t.SourceSelecter.AddOption(t.Source[lastOptionIndex], func() { t.DB, err = t.Config.Source[lastOptionIndex].setDbData() - t.Tables = t.DB.tables + t.Tables = t.DB.Tables t.filterList() t.App.SetFocus(t.Searcher) })