Skip to content

Commit

Permalink
Remove caching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lewissteele committed Nov 29, 2024
1 parent d332bce commit f103bbb
Showing 1 changed file with 23 additions and 64 deletions.
87 changes: 23 additions & 64 deletions internal/db/user.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package db

import (
"fmt"
"strings"

"github.com/gookit/goutil/maputil"
"github.com/lewissteele/dbat/internal/model"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
Expand All @@ -12,14 +12,9 @@ import (
)

var Conn *gorm.DB
var Databases []string
var Tables map[string][]string
var UserDB model.Database

func Connect(name string) {
Databases = []string{}
Tables = map[string][]string{}

LocalDB.Where("name = ?", name).Find(&UserDB)

var dialector gorm.Dialector
Expand Down Expand Up @@ -47,7 +42,9 @@ func Connect(name string) {
d, _ := Conn.DB()
d.SetMaxOpenConns(1)

go populateDatabases()
Select("api-search")

go cacheObjects()
}

func UserDBNames() []string {
Expand All @@ -63,73 +60,35 @@ func UserDBNames() []string {
return names
}

func Selected() string {
var database string
Conn.Raw("select database()").Scan(&database)
return database
}

func Select(d string) {
Conn.Exec(fmt.Sprintf("use `%s`", d))
}

func Port(d Driver) string {
if d == PostgreSQL {
return "5432"
}
return "3306"
}

func populateDatabases() {
rows, err := Conn.Raw("show databases").Rows()

if err != nil {
panic("could not get databases")
}

var results []map[string]interface{}

rows.Next()
err = Conn.ScanRows(rows, &results)

for _, val := range results {
d := val["Database"].(string)

Databases = append(
Databases,
d,
)

populateTables(d)
}
}

func populateTables(d string) {
t := Conn.Begin()

t.Exec(strings.Join([]string{
"use ",
"`",
d,
"`",
}, ""))

rows, err := t.Raw("show tables").Rows()

if err != nil {
panic("could not get tables")
}

var results []map[string]interface{}

rows.Next()
err = Conn.ScanRows(rows, &results)

for _, val := range results {
v := maputil.Values(val)

if len(v) == 0 {
continue
}
func dialector(u model.Database) gorm.Dialector {
var dialector gorm.Dialector

Tables[d] = append(
Tables[d],
v[0].(string),
)
switch Driver(UserDB.Driver) {
case PostgreSQL:
dialector = postgres.Open(dsn(UserDB))
default:
dialector = mysql.Open(dsn(UserDB))
}

t.Rollback()
}
return dialector
}

func dsn(d model.Database) string {
if Driver(d.Driver) == PostgreSQL {
Expand Down

0 comments on commit f103bbb

Please sign in to comment.