2026-05-30 19:27:02 +08:00
|
|
|
|
package db
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
2026-05-31 20:36:21 +08:00
|
|
|
|
"strings"
|
2026-05-30 19:27:02 +08:00
|
|
|
|
|
2026-05-31 20:36:21 +08:00
|
|
|
|
"apigo.cc/go/cast"
|
|
|
|
|
|
"apigo.cc/go/file"
|
2026-05-30 19:27:02 +08:00
|
|
|
|
"apigo.cc/go/jsmod"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
jsmod.Register("db", map[string]any{
|
|
|
|
|
|
"get": func(ctx context.Context, name string) (*jsDB, error) {
|
2026-05-31 20:36:21 +08:00
|
|
|
|
isReadOnly := false
|
|
|
|
|
|
|
|
|
|
|
|
// 安全模式下的动态判定
|
|
|
|
|
|
if jsmod.IsSafeMode(ctx) {
|
|
|
|
|
|
if strings.HasPrefix(name, "sqlite://") {
|
|
|
|
|
|
// 1. SQLite 路径校验
|
|
|
|
|
|
path := strings.TrimPrefix(name, "sqlite://")
|
|
|
|
|
|
if idx := strings.Index(path, "?"); idx != -1 {
|
|
|
|
|
|
path = path[:idx]
|
|
|
|
|
|
}
|
|
|
|
|
|
// 如果路径不在沙箱内,VerifyPathForSafeMode 会直接返回 error,阻止连接
|
|
|
|
|
|
_, err := file.VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
isReadOnly = false
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 2. 远程数据库前缀校验
|
|
|
|
|
|
var allowedDSNs []string
|
2026-06-10 09:52:55 +08:00
|
|
|
|
cast.Convert(&allowedDSNs, jsmod.Get(ctx, "AllowedDSNs"))
|
2026-05-31 20:36:21 +08:00
|
|
|
|
matched := false
|
|
|
|
|
|
for _, prefix := range allowedDSNs {
|
|
|
|
|
|
if strings.HasPrefix(name, prefix) {
|
|
|
|
|
|
matched = true
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if !matched {
|
|
|
|
|
|
// 未命中白名单,降级为只读模式
|
|
|
|
|
|
isReadOnly = true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-30 19:27:02 +08:00
|
|
|
|
d := GetDB(name, nil)
|
|
|
|
|
|
if d.Error != nil {
|
|
|
|
|
|
return nil, d.Error
|
|
|
|
|
|
}
|
2026-05-31 20:36:21 +08:00
|
|
|
|
return &jsDB{db: d, ctx: ctx, isReadOnly: isReadOnly}, nil
|
2026-05-30 19:27:02 +08:00
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type jsDB struct {
|
2026-05-31 20:36:21 +08:00
|
|
|
|
db *DB
|
|
|
|
|
|
ctx context.Context
|
|
|
|
|
|
isReadOnly bool
|
2026-05-30 19:27:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-31 20:36:21 +08:00
|
|
|
|
var errReadOnly = errors.New("database is in read-only mode for this context")
|
2026-05-30 19:27:02 +08:00
|
|
|
|
|
2026-05-31 20:36:21 +08:00
|
|
|
|
func (jd *jsDB) checkWrite() error {
|
|
|
|
|
|
if jd.isReadOnly {
|
|
|
|
|
|
return errReadOnly
|
2026-05-30 19:27:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-30 19:44:32 +08:00
|
|
|
|
// Read Operations
|
2026-05-30 19:27:02 +08:00
|
|
|
|
func (jd *jsDB) Query(query string, args ...any) *QueryResult {
|
|
|
|
|
|
return jd.db.Query(query, args...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-30 19:44:32 +08:00
|
|
|
|
// Write Operations
|
2026-05-30 19:27:02 +08:00
|
|
|
|
func (jd *jsDB) Exec(query string, args ...any) *ExecResult {
|
2026-05-31 20:36:21 +08:00
|
|
|
|
if err := jd.checkWrite(); err != nil {
|
|
|
|
|
|
return &ExecResult{Error: err}
|
2026-05-30 19:27:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
return jd.db.Exec(query, args...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (jd *jsDB) Insert(table string, data any) *ExecResult {
|
2026-05-31 20:36:21 +08:00
|
|
|
|
if err := jd.checkWrite(); err != nil {
|
|
|
|
|
|
return &ExecResult{Error: err}
|
2026-05-30 19:27:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
return jd.db.Insert(table, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (jd *jsDB) Update(table string, data any, conditions string, args ...any) *ExecResult {
|
2026-05-31 20:36:21 +08:00
|
|
|
|
if err := jd.checkWrite(); err != nil {
|
|
|
|
|
|
return &ExecResult{Error: err}
|
2026-05-30 19:27:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
return jd.db.Update(table, data, conditions, args...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (jd *jsDB) Delete(table string, conditions string, args ...any) *ExecResult {
|
2026-05-31 20:36:21 +08:00
|
|
|
|
if err := jd.checkWrite(); err != nil {
|
|
|
|
|
|
return &ExecResult{Error: err}
|
2026-05-30 19:27:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
return jd.db.Delete(table, conditions, args...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (jd *jsDB) Replace(table string, data any) *ExecResult {
|
2026-05-31 20:36:21 +08:00
|
|
|
|
if err := jd.checkWrite(); err != nil {
|
|
|
|
|
|
return &ExecResult{Error: err}
|
2026-05-30 19:27:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
return jd.db.Replace(table, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-30 19:44:32 +08:00
|
|
|
|
// Transaction Support
|
|
|
|
|
|
func (jd *jsDB) Begin() (*jsTx, error) {
|
|
|
|
|
|
tx := jd.db.Begin()
|
|
|
|
|
|
if tx.Error != nil {
|
|
|
|
|
|
return nil, tx.Error
|
|
|
|
|
|
}
|
2026-05-31 20:36:21 +08:00
|
|
|
|
return &jsTx{tx: tx, ctx: jd.ctx, isReadOnly: jd.isReadOnly}, nil
|
2026-05-30 19:27:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-30 19:44:32 +08:00
|
|
|
|
// jsTx wraps *Tx for JS environment
|
|
|
|
|
|
type jsTx struct {
|
2026-05-31 20:36:21 +08:00
|
|
|
|
tx *Tx
|
|
|
|
|
|
ctx context.Context
|
|
|
|
|
|
isReadOnly bool
|
2026-05-30 19:27:02 +08:00
|
|
|
|
}
|
2026-05-30 19:44:32 +08:00
|
|
|
|
|
2026-05-31 20:36:21 +08:00
|
|
|
|
func (jt *jsTx) checkWrite() error {
|
|
|
|
|
|
if jt.isReadOnly {
|
|
|
|
|
|
return errReadOnly
|
2026-05-30 19:44:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (jt *jsTx) Query(query string, args ...any) *QueryResult {
|
|
|
|
|
|
return jt.tx.Query(query, args...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (jt *jsTx) Exec(query string, args ...any) *ExecResult {
|
2026-05-31 20:36:21 +08:00
|
|
|
|
if err := jt.checkWrite(); err != nil {
|
|
|
|
|
|
return &ExecResult{Error: err}
|
2026-05-30 19:44:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
return jt.tx.Exec(query, args...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (jt *jsTx) Insert(table string, data any) *ExecResult {
|
2026-05-31 20:36:21 +08:00
|
|
|
|
if err := jt.checkWrite(); err != nil {
|
|
|
|
|
|
return &ExecResult{Error: err}
|
2026-05-30 19:44:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
return jt.tx.Insert(table, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (jt *jsTx) Update(table string, data any, conditions string, args ...any) *ExecResult {
|
2026-05-31 20:36:21 +08:00
|
|
|
|
if err := jt.checkWrite(); err != nil {
|
|
|
|
|
|
return &ExecResult{Error: err}
|
2026-05-30 19:44:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
return jt.tx.Update(table, data, conditions, args...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (jt *jsTx) Delete(table string, conditions string, args ...any) *ExecResult {
|
2026-05-31 20:36:21 +08:00
|
|
|
|
if err := jt.checkWrite(); err != nil {
|
|
|
|
|
|
return &ExecResult{Error: err}
|
2026-05-30 19:44:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
return jt.tx.Delete(table, conditions, args...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (jt *jsTx) Replace(table string, data any) *ExecResult {
|
2026-05-31 20:36:21 +08:00
|
|
|
|
if err := jt.checkWrite(); err != nil {
|
|
|
|
|
|
return &ExecResult{Error: err}
|
2026-05-30 19:44:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
return jt.tx.Replace(table, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-31 20:36:21 +08:00
|
|
|
|
func (jt *jsTx) Commit() error {
|
|
|
|
|
|
if err := jt.checkWrite(); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
return jt.tx.Commit()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-30 19:44:32 +08:00
|
|
|
|
func (jt *jsTx) Rollback() error { return jt.tx.Rollback() }
|
|
|
|
|
|
|
|
|
|
|
|
// Metadata
|
|
|
|
|
|
func (jd *jsDB) InKeys(numArgs int) string { return jd.db.InKeys(numArgs) }
|
|
|
|
|
|
func (jd *jsDB) Quote(text string) string { return jd.db.Quote(text) }
|