Documentation
¶
Overview ¶
Package model is the initial database driver and define the data structures corresponding to the tables.
test_helper.go 提供测试专用的数据库初始化函数。 该文件是导出的(非 _test.go),供 model 包内部测试和 taskstore 等跨包测试使用。
Index ¶
- Variables
- func CloseDB() error
- func DeleteById[T any](id int) (*gorm.DB, error)
- func FindFirstByID[T any](id int) (*T, error)
- func FindItems[T any]() ([]*T, error)
- func GetDB() (*gorm.DB, error)
- func InitDB() error
- func InitSqlite() error
- func InitTestDB(t *testing.T) *gorm.DB
- func Save(value interface{}) (*gorm.DB, error)
- func ToList(items []*Quick) []list.Item
- type File
- func FindFilesByDir(dir string, page int) ([]*File, error)
- func FindFilesPrefixPath(path string, isDir bool) ([]*File, error)
- func FindFirstByPath(path string) (*File, error)
- func FindNeedRefreshFiles(path string) ([]*File, error)
- func NewFile(f *bdpan.FileInfo) *File
- func NewFiles(files []*bdpan.FileInfo) []*File
- func NewRootFile() *File
- type ORMModel
- type Pan
- type Quick
- type Task
- type TaskChild
- type UploadHistory
- type User
Constants ¶
This section is empty.
Variables ¶
var ErrRecordNotFound = gorm.ErrRecordNotFound
ErrRecordNotFound no records found
Functions ¶
func CloseDB ¶
func CloseDB() error
CloseDB 关闭数据库连接
实现逻辑: 1. 获取底层 *sql.DB 2. 调用 Close() 关闭连接池 3. WAL 模式下会自动执行 checkpoint
返回: error
func DeleteById ¶
DeleteById 根据 ID 软删除记录(设置 is_delete=1)
返回 (*gorm.DB, error),调用方需检查 error 后再使用返回的 *gorm.DB。
func GetDB ¶
GetDB 获取数据库连接
使用 sync.Once 确保数据库只初始化一次,通过 dbErr 捕获初始化过程中的错误。 返回 (*gorm.DB, error),调用方需检查 error 后再使用数据库连接。
func InitSqlite ¶
func InitSqlite() error
InitSqlite 初始化 SQLite 数据库连接
实现逻辑(与代码顺序一致): 1. 从 config.GetDBFile() 获取 DSN,并展开用户目录(~/) 2. 确保数据库文件所在目录存在(权限 0755) 3. 使用 glebarez/sqlite(纯 Go 驱动)打开数据库(禁用 SQL 日志,启用 PrepareStmt) 4. 配置连接池(SetMaxOpenConns=2, SetMaxIdleConns=2, SetConnMaxLifetime=0) 5. 执行 PRAGMA 兜底(journal_mode=WAL, synchronous=NORMAL, busy_timeout=5000, foreign_keys=ON, temp_store=MEMORY) 6. 自动迁移(AutoMigrate)模型表结构
参数: 无 返回: error(失败时返回具体错误信息)
func InitTestDB ¶ added in v0.0.9
InitTestDB 初始化测试专用数据库,使用 t.TempDir() 创建临时 SQLite 文件。 该函数是导出的,供 model 包内部测试和 taskstore 等跨包测试使用。 测试结束后通过 t.Cleanup 自动重置全局 DB 状态。
Types ¶
type File ¶
type File struct {
*bdpan.FileInfo `gorm:"-"`
ID uint64 `gorm:"primaryKey;"`
FSID uint64 `json:"fs_id" gorm:"column:fs_id"`
Path string `json:"path"`
Dir string `json:"dir"`
Size int `json:"size"`
FileType int `json:"isdir" gorm:"column:is_dir"`
Filename string `json:"filename"`
ServerFilename string `json:"server_filename"`
Category int `json:"category"`
Dlink string `json:"dlink"`
MD5 string `json:"md5"`
ServerCTime int64 `json:"server_ctime"`
ServerMTime int64 `json:"server_mtime"`
LocalCTime int64 `json:"local_ctime"`
LocalMTime int64 `json:"local_mtime"`
// custom
IsRefresh int `json:"is_refresh"`
Level int `json:"level"`
IsSelect bool `gorm:"-" json:"is_select"` // 是否选中,tui 表格使用
ORMModel
}
生成 sqlite3 建表语句,开头加上删除语句 sqlite3 字段要符合 gorm 命名规则
func FindFilesPrefixPath ¶
FindFilesPrefixPath 根据路径前缀查找文件列表
返回 ([]*File, error),调用方需检查 error 后再使用返回的列表。
func FindNeedRefreshFiles ¶
FindNeedRefreshFiles 查找需要刷新的目录文件
返回 ([]*File, error),调用方需检查 error 后再使用返回的列表。
func NewRootFile ¶
func NewRootFile() *File
type ORMModel ¶
type Pan ¶
type Quick ¶
type Quick struct {
ID uint64 `gorm:"primaryKey;"`
Filename string `json:"filename"`
Path string `json:"path"`
Key string `json:"key"`
ORMModel
}
func (Quick) Description ¶
func (Quick) FilterValue ¶
type Task ¶ added in v0.0.5
type Task struct {
ID string `gorm:"primaryKey;column:id" json:"id"`
Type string `gorm:"column:type;index" json:"type"`
Identity string `gorm:"column:identity;uniqueIndex" json:"identity"`
Status string `gorm:"column:status;index" json:"status"`
Progress float64 `gorm:"column:progress" json:"progress"`
TotalBytes int64 `gorm:"column:total_bytes" json:"total_bytes"`
DownloadedBytes int64 `gorm:"column:downloaded_bytes" json:"downloaded_bytes"`
PID int `gorm:"column:pid" json:"pid"`
Hostname string `gorm:"column:hostname" json:"hostname"`
Concurrency int `gorm:"column:concurrency" json:"concurrency"`
StartTime string `gorm:"column:start_time;index" json:"start_time"`
UpdateTime string `gorm:"column:update_time;index" json:"update_time"`
SpeedBPS int64 `gorm:"column:speed_bps" json:"speed_bps"`
ETASeconds int64 `gorm:"column:eta_seconds" json:"eta_seconds"`
Error string `gorm:"column:error" json:"error"`
CancelRequested int `gorm:"column:cancel_requested" json:"cancel_requested"`
Data string `gorm:"column:data;type:text" json:"data"`
Version int `gorm:"column:version" json:"version"`
}
type TaskChild ¶ added in v0.0.5
type TaskChild struct {
ID int64 `gorm:"primaryKey;column:id" json:"id"`
TaskID string `gorm:"column:task_id;index" json:"task_id"`
Name string `gorm:"column:name" json:"name"`
Path string `gorm:"column:path" json:"path"`
Size int64 `gorm:"column:size" json:"size"`
Downloaded int64 `gorm:"column:downloaded" json:"downloaded"`
Status string `gorm:"column:status;index" json:"status"`
Error string `gorm:"column:error" json:"error"`
UpdateTime string `gorm:"column:update_time;index" json:"update_time"`
}
type UploadHistory ¶ added in v0.0.3
type UploadHistory struct {
FSID uint64 `json:"fs_id" gorm:"primaryKey;column:fs_id"`
Path string `json:"path"`
Size uint64 `json:"size"`
ServerFilename string `json:"server_filename"`
Category int32 `json:"category"`
MD5 string `json:"md5"`
LocalMD5 string `json:"local_md5"`
CTime uint64 `json:"ctime"`
MTime uint64 `json:"mtime"`
ORMModel
}
func FindUploadHistoryByLocalMD5 ¶ added in v0.0.3
func FindUploadHistoryByLocalMD5(md5 string) (*UploadHistory, error)
FindUploadHistoryByLocalMD5 根据本地 MD5 查找上传历史记录
返回 (*UploadHistory, error),调用方需检查 error 后再使用返回的记录。 如果记录不存在,返回 nil 和 ErrRecordNotFound。
func (UploadHistory) TableName ¶ added in v0.0.3
func (UploadHistory) TableName() string
type User ¶
type User struct {
*bdpan.GetUserInfoRes `gorm:"-"`
ID int `gorm:"primaryKey;"`
Uk int `json:"uk,omitempty"`
AvatarUrl string `json:"avatar_url,omitempty"`
BaiduName string `json:"baidu_name,omitempty"`
NetdiskName string `json:"netdisk_name,omitempty"`
VipType int32 `json:"vip_type,omitempty"`
VipName string `json:"vip_name,omitempty"`
}
func NewUser ¶
func NewUser(user *bdpan.GetUserInfoRes) *User