Skip to content

Commit

Permalink
feat: list supported
Browse files Browse the repository at this point in the history
  • Loading branch information
kiririx committed Dec 16, 2024
1 parent 0e0cbaf commit 511bfa3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
47 changes: 45 additions & 2 deletions ec/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import (
"sync"
)

var defaultModule = "DEFAULT"

type Item struct {
module string
key string
value string
}

type StorageType int

var (
Expand Down Expand Up @@ -59,8 +67,6 @@ func (receiver *MySQLStorage) getStorage() StorageType {
return MySQL
}

//var db *sql.DB

var dbCache = &sync.Map{}

func getDbCacheKey(module, key string) string {
Expand Down Expand Up @@ -110,13 +116,17 @@ func NewMySQLStorage(host string, port int, user, passwd, database string) *MySQ

// Initialize 入口函数
func Initialize(storage Storage, module string) Handler {
if strings.TrimSpace(module) == "" {
module = defaultModule
}
return storage.init(module)
}

type Handler interface {
Get(key string) string
Set(key string, value string) error
Remove(key string)
List() []Item
}

type MySQLHandler struct {
Expand Down Expand Up @@ -201,6 +211,25 @@ func (h *MySQLHandler) Remove(key string) {
}
}

func (h *MySQLHandler) List() []Item {
rows, err := h.db.Query("SELECT module, name, value FROM "+h.tableName+" where module = ? ORDER BY name", h.module)
if err != nil {
log.Println(err)
return nil
}
defer rows.Close()
var items []Item
for rows.Next() {
var item Item
if err := rows.Scan(&item.module, &item.key, &item.value); err != nil {
log.Println(err)
return []Item{}
}
items = append(items, item)
}
return items
}

// 检查表是否存在
func checkTableExists(db *sql.DB, tableName string) bool {
query := fmt.Sprintf("SHOW TABLES LIKE '%s'", tableName)
Expand Down Expand Up @@ -268,6 +297,20 @@ func (h *PropertiesHandler) Remove(key string) {
delete(*h.configMap, keyOfFile)
}

func (h *PropertiesHandler) List() []Item {
var items []Item
for k, v := range *h.configMap {
if strings.HasPrefix(k, h.module+".") {
items = append(items, Item{
module: h.module,
key: k[len(h.module)+1:],
value: v,
})
}
}
return items
}

func removeKeyFromProperties(path string, key string) error {
// 尝试打开文件,如果文件不存在则返回错误
file, err := os.OpenFile(path, os.O_RDWR, 0644)
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module github.com/kiririx/easy-config

go 1.23.3

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
)
require github.com/go-sql-driver/mysql v1.8.1

require filippo.io/edwards25519 v1.1.0 // indirect

0 comments on commit 511bfa3

Please sign in to comment.