-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: bugtower100 <[email protected]> Co-authored-by: fy <[email protected]>
- Loading branch information
1 parent
fb44c24
commit 230748f
Showing
26 changed files
with
889 additions
and
481 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# DB_TYPE=mysql | ||
# DB_DSN="root:root@tcp(127.0.0.1:3306)/nokoti?charset=utf8mb4&parseTime=True&loc=Local" | ||
|
||
# DB_TYPE=postgres | ||
# DB_DSN="postgres://postgres:pinenut666@localhost:5432/nokoti?sslmode=disable" | ||
# SQLITE IS DIFFERENT | ||
DB_TYPE=sqlite | ||
# DATA_DIR="./data/default" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package model | ||
|
||
const ( | ||
SQLITE = "sqlite" | ||
MYSQL = "mysql" | ||
POSTGRESQL = "postgres" | ||
) |
2 changes: 1 addition & 1 deletion
2
dice/model/gormcache.go → dice/model/database/cache/gormcache.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package model | ||
package cache | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package database | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
|
||
"sealdice-core/dice/model/database/cache" | ||
) | ||
|
||
func MySQLDBInit(dsn string) (*gorm.DB, error) { | ||
// 构建 MySQL DSN (Data Source Name) | ||
// 使用 GORM 连接 MySQL | ||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: logger.New( | ||
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer | ||
logger.Config{ | ||
SlowThreshold: time.Second, // 慢 SQL 阈值 | ||
LogLevel: logger.Info, // 记录所有SQL操作 | ||
Colorful: true, // 是否启用彩色打印 | ||
}, | ||
)}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// 存疑,MYSQL是否需要使用缓存 | ||
cacheDB, err := cache.GetBuntCacheDB(db) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// 返回数据库连接 | ||
return cacheDB, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package database | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
|
||
"sealdice-core/dice/model/database/cache" | ||
) | ||
|
||
func PostgresDBInit(dsn string) (*gorm.DB, error) { | ||
// 构建 PostgreSQL DSN (Data Source Name) | ||
|
||
// 使用 GORM 连接 PostgreSQL | ||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{ | ||
Logger: logger.New( | ||
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer | ||
logger.Config{ | ||
SlowThreshold: time.Second, // 慢 SQL 阈值 | ||
LogLevel: logger.Info, // 记录所有SQL操作 | ||
Colorful: true, // 是否启用彩色打印 | ||
}, | ||
), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// GetBuntCacheDB 逻辑保持不变 | ||
cacheDB, err := cache.GetBuntCacheDB(db) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// 返回数据库连接 | ||
return cacheDB, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//go:build !cgo | ||
// +build !cgo | ||
|
||
package database | ||
|
||
import ( | ||
"github.com/glebarez/sqlite" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
|
||
"sealdice-core/dice/model/database/cache" | ||
) | ||
|
||
func SQLiteDBInit(path string, useWAL bool) (*gorm.DB, error) { | ||
db, err := gorm.Open(sqlite.Open(path), &gorm.Config{ | ||
// 注意,这里虽然是Info,但实际上打印就变成了Debug. | ||
Logger: logger.Default.LogMode(logger.Info), | ||
}) | ||
// https://github.com/glebarez/sqlite/issues/52 尚未遇见问题,可以先考虑不使用 | ||
// sqlDB, _ := db.DB() | ||
// sqlDB.SetMaxOpenConns(1) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Enable Cache Mode | ||
db, err = cache.GetBuntCacheDB(db) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// enable WAL mode | ||
if useWAL { | ||
err = db.Exec("PRAGMA journal_mode=WAL").Error | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return db, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//go:build cgo | ||
// +build cgo | ||
|
||
package database | ||
|
||
import ( | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
|
||
"sealdice-core/dice/model/database/cache" | ||
) | ||
|
||
func SQLiteDBInit(path string, useWAL bool) (*gorm.DB, error) { | ||
open, err := gorm.Open(sqlite.Open(path), &gorm.Config{ | ||
// 注意,这里虽然是Info,但实际上打印就变成了Debug. | ||
Logger: logger.Default.LogMode(logger.Info), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Enable Cache Mode | ||
open, err = cache.GetBuntCacheDB(open) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// enable WAL mode | ||
if useWAL { | ||
err = open.Exec("PRAGMA journal_mode=WAL").Error | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
return open, err | ||
} |
Oops, something went wrong.