From fa4edf4aa80decc8368a3d9cb3ba3d32a75300cd Mon Sep 17 00:00:00 2001 From: Dan O'Neill Date: Tue, 19 Dec 2023 19:20:46 -0800 Subject: [PATCH] modularize db functions --- cmd/open.go | 30 +++++++++----------------- data/persistent_store.go | 46 ++++++++++++++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/cmd/open.go b/cmd/open.go index a61c386..63e6d0d 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -28,25 +28,18 @@ var openCmd = &cobra.Command{ } // ---------------------------------------------- -// funtions +// functions // ---------------------------------------------- func open(file string) { - var count int64 - - db, err := data.DbConnect(file) - if err != nil { - fmt.Println(err) - return - } + data.OpenDB(file) switch { case dbUser: - var user []data.Accounts_User - db.First(&user) - fmt.Println(user[0].Username) + user := data.SelectUser() + fmt.Println(user.Username) case displayTables: - tables, err := data.GetTables(*db) + tables, err := data.GetTables() if err != nil { fmt.Println(err) return @@ -56,28 +49,25 @@ func open(file string) { table.SetHeader([]string{"Name", "Count"}) for _, tableName := range tables { - db.Table(tableName).Count(&count) + count := data.GetTableRowCount(tableName) table.Append([]string{tableName, fmt.Sprintf("%d", count)}) } table.Render() case events: - var events []data.Event - var profile []data.User_Profile - var user data.Accounts_User var users []string - db.Where("is_draft = 0").Where(db.Where("remote_id IS NULL").Or("remote_id = ?", "")).Find(&events) + events := data.SelectPendingSyncEvents() table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"ID", "User", "Title"}) for _, event := range events { if event.ProfileID != 0 { - db.Where("id = ?", event.ProfileID).Find(&profile) - users = append(users, profile[0].Username) + profile := data.SelectUserProfileById(event.ProfileID) + users = append(users, profile.Username) } else { - db.First(&user) + user := data.SelectUser() users = append(users, user.Username) } table.Append([]string{fmt.Sprintf("%d", event.ID), users[len(users)-1], event.Title}) diff --git a/data/persistent_store.go b/data/persistent_store.go index 3b98fce..8325440 100644 --- a/data/persistent_store.go +++ b/data/persistent_store.go @@ -5,20 +5,26 @@ import ( "gorm.io/gorm" ) +var db *gorm.DB + // ---------------------------------------------- -// exported funtions +// DB connection // ---------------------------------------------- -func DbConnect(file string) (*gorm.DB, error) { - db, err := gorm.Open(sqlite.Open(file), &gorm.Config{}) +func OpenDB(file string) { + database, err := gorm.Open(sqlite.Open(file), &gorm.Config{}) if err != nil { - return nil, err + panic("Failed to open database") } - return db, nil + db = database } -func GetTables(db gorm.DB) (tableList []string, err error) { +// ---------------------------------------------- +// table data +// ---------------------------------------------- + +func GetTables() (tableList []string, err error) { tables, err := db.Migrator().GetTables() if err != nil { return nil, err @@ -26,3 +32,31 @@ func GetTables(db gorm.DB) (tableList []string, err error) { return tables, nil } + +func GetTableRowCount(name string) int64 { + var count int64 + db.Table(name).Count(&count) + return count +} + +// ---------------------------------------------- +// queries +// ---------------------------------------------- + +func SelectUser() Accounts_User { + var user Accounts_User + db.First(&user) + return user +} + +func SelectUserProfileById(id int) User_Profile { + var profile User_Profile + db.Where("id = ?", id).Find(&profile) + return profile +} + +func SelectPendingSyncEvents() []Event { + var events []Event + db.Where("is_draft = 0").Where(db.Where("remote_id IS NULL").Or("remote_id = ?", "")).Find(&events) + return events +}