96 lines
2.8 KiB
Go
96 lines
2.8 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
|
|
"rag-local/kb/consts"
|
|
"rag-local/kb/model/entity"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
)
|
|
|
|
var Dataset = &datasetDao{}
|
|
|
|
type datasetDao struct{}
|
|
|
|
func init() {
|
|
ctx := context.Background()
|
|
_, err := g.DB(consts.DbGroupDefault).Exec(ctx, `CREATE TABLE IF NOT EXISTS `+consts.TableNameDataset+` (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL DEFAULT '',
|
|
description TEXT NOT NULL DEFAULT '',
|
|
embedding_cfg_id INTEGER NOT NULL DEFAULT 0,
|
|
status INTEGER NOT NULL DEFAULT 1,
|
|
created_at DATETIME DEFAULT (datetime('now','localtime')),
|
|
updated_at DATETIME DEFAULT (datetime('now','localtime'))
|
|
)`)
|
|
if err != nil {
|
|
g.Log().Warningf(ctx, "create kb_dataset table failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func (d *datasetDao) GetOne(ctx context.Context, id int64) (*entity.Dataset, error) {
|
|
var m entity.Dataset
|
|
err := g.DB(consts.DbGroupDefault).Model(consts.TableNameDataset).Ctx(ctx).Where("id", id).Scan(&m)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &m, nil
|
|
}
|
|
|
|
func (d *datasetDao) List(ctx context.Context) ([]*entity.Dataset, error) {
|
|
var list []*entity.Dataset
|
|
err := g.DB(consts.DbGroupDefault).Model(consts.TableNameDataset).Ctx(ctx).OrderAsc("id").Scan(&list)
|
|
return list, err
|
|
}
|
|
|
|
func (d *datasetDao) Insert(ctx context.Context, data *entity.Dataset) (int64, error) {
|
|
now := gtime.Now().Format("Y-m-d H:i:s")
|
|
r, err := g.DB(consts.DbGroupDefault).Model(consts.TableNameDataset).Ctx(ctx).Data(g.Map{
|
|
"name": data.Name,
|
|
"description": data.Description,
|
|
"embedding_cfg_id": data.EmbeddingCfgId,
|
|
"status": data.Status,
|
|
"created_at": now,
|
|
"updated_at": now,
|
|
}).Insert()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return r.LastInsertId()
|
|
}
|
|
|
|
func (d *datasetDao) Update(ctx context.Context, data *entity.Dataset) error {
|
|
_, err := g.DB(consts.DbGroupDefault).Model(consts.TableNameDataset).Ctx(ctx).Data(g.Map{
|
|
"name": data.Name,
|
|
"description": data.Description,
|
|
"embedding_cfg_id": data.EmbeddingCfgId,
|
|
"status": data.Status,
|
|
"updated_at": gtime.Now().Format("Y-m-d H:i:s"),
|
|
}).Where("id", data.Id).Update()
|
|
return err
|
|
}
|
|
|
|
func (d *datasetDao) Delete(ctx context.Context, id int64) error {
|
|
_, err := g.DB(consts.DbGroupDefault).Model(consts.TableNameDataset).Ctx(ctx).Where("id", id).Delete()
|
|
return err
|
|
}
|
|
|
|
func (d *datasetDao) GetEmbeddingCfgId(ctx context.Context, id int64) (int64, error) {
|
|
r, err := g.DB(consts.DbGroupDefault).Model(consts.TableNameDataset).Ctx(ctx).
|
|
Fields("embedding_cfg_id").Where("id", id).One()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if r == nil {
|
|
return 0, nil
|
|
}
|
|
return r["embedding_cfg_id"].Int64(), nil
|
|
}
|