Skip to content

Commit

Permalink
modularize db functions
Browse files Browse the repository at this point in the history
  • Loading branch information
doneill committed Dec 20, 2023
1 parent ec195fb commit fa4edf4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
30 changes: 10 additions & 20 deletions cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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})
Expand Down
46 changes: 40 additions & 6 deletions data/persistent_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,58 @@ 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
}

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
}

0 comments on commit fa4edf4

Please sign in to comment.