- CHANGELOG.md:v2.8.0 条目(中英双语); - README.md:版本行 2.7.1→2.8.0 + 愿景第 6 条(知识可证明); - doc/releases/v2.8.0.md:双语发版说明(亮点/用法/已知限制); - skills/marqdo-dev/index.mq.md:新增 prove 任务(路径/目录模式/ require_lean/json;参数用 + 行声明升参,check 与 narrative-ratio 通过); - 设计文档 §12.5 标记已合入;发版链接统一指向 doc/releases/v2.8.0.md。
172 lines
3.7 KiB
Go
Executable File
172 lines
3.7 KiB
Go
Executable File
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// dbConn is *sql.DB or a transaction-bound *sql.Conn.
|
|
type dbConn interface {
|
|
Exec(query string, args ...any) (sql.Result, error)
|
|
Query(query string, args ...any) (*sql.Rows, error)
|
|
QueryRow(query string, args ...any) *sql.Row
|
|
}
|
|
|
|
type txnConn struct {
|
|
conn *sql.Conn
|
|
url string
|
|
}
|
|
|
|
func (t *txnConn) Exec(query string, args ...any) (sql.Result, error) {
|
|
if IsPostgres(t.url) {
|
|
query = RewritePlaceholdersPG(query)
|
|
}
|
|
return t.conn.ExecContext(context.Background(), query, args...)
|
|
}
|
|
|
|
func (t *txnConn) Query(query string, args ...any) (*sql.Rows, error) {
|
|
if IsPostgres(t.url) {
|
|
query = RewritePlaceholdersPG(query)
|
|
}
|
|
return t.conn.QueryContext(context.Background(), query, args...)
|
|
}
|
|
|
|
func (t *txnConn) QueryRow(query string, args ...any) *sql.Row {
|
|
if IsPostgres(t.url) {
|
|
query = RewritePlaceholdersPG(query)
|
|
}
|
|
return t.conn.QueryRowContext(context.Background(), query, args...)
|
|
}
|
|
|
|
var (
|
|
txnsMu sync.Mutex
|
|
txns = map[string]*txnConn{}
|
|
txnSeq atomic.Uint64
|
|
txnSeedM sync.Mutex
|
|
txnSeed = uint64(0x243f_6a88_85a3_08d3)
|
|
)
|
|
|
|
func nextTxnID() string {
|
|
nanos := time.Now().UnixNano() & 0xffffffff
|
|
pid := os.Getpid()
|
|
txnSeedM.Lock()
|
|
x := txnSeed
|
|
x ^= x << 13
|
|
x ^= x >> 7
|
|
x ^= x << 17
|
|
txnSeed = x
|
|
txnSeedM.Unlock()
|
|
n := txnSeq.Add(1)
|
|
return fmt.Sprintf("txn-%x%x%x%x", pid, nanos, x, n)
|
|
}
|
|
|
|
func optTxn(txnID []string) string {
|
|
if len(txnID) == 0 {
|
|
return ""
|
|
}
|
|
return txnID[0]
|
|
}
|
|
|
|
// connFor returns the pooled DB or an active transaction connection.
|
|
func connFor(url, txnID string) (dbConn, error) {
|
|
if txnID != "" {
|
|
txnsMu.Lock()
|
|
defer txnsMu.Unlock()
|
|
t, ok := txns[txnID]
|
|
if !ok {
|
|
return nil, fmt.Errorf("unknown transaction `%s`", txnID)
|
|
}
|
|
return t, nil
|
|
}
|
|
raw, err := open(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &rewritingDB{db: raw, pg: IsPostgres(url)}, nil
|
|
}
|
|
|
|
func takeTxn(txnID string) (*txnConn, error) {
|
|
txnsMu.Lock()
|
|
defer txnsMu.Unlock()
|
|
t, ok := txns[txnID]
|
|
if !ok {
|
|
return nil, fmt.Errorf("unknown transaction `%s`", txnID)
|
|
}
|
|
delete(txns, txnID)
|
|
return t, nil
|
|
}
|
|
|
|
func resetTxns() {
|
|
txnsMu.Lock()
|
|
defer txnsMu.Unlock()
|
|
for id, t := range txns {
|
|
_, _ = t.conn.ExecContext(context.Background(), "ROLLBACK")
|
|
_ = t.conn.Close()
|
|
delete(txns, id)
|
|
}
|
|
}
|
|
|
|
// Begin starts a transaction (BEGIN IMMEDIATE on SQLite, BEGIN on Postgres).
|
|
// Returns {"_type":"txn","txn":id,"url":url,"事务":id,"地址":url}.
|
|
func Begin(url string) (map[string]any, error) {
|
|
db, err := open(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
conn, err := db.Conn(context.Background())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
beginSQL := "BEGIN IMMEDIATE"
|
|
if IsPostgres(url) {
|
|
beginSQL = "BEGIN"
|
|
}
|
|
if _, err := conn.ExecContext(context.Background(), beginSQL); err != nil {
|
|
_ = conn.Close()
|
|
return nil, err
|
|
}
|
|
id := nextTxnID()
|
|
resolved := ResolveURL(url)
|
|
txnsMu.Lock()
|
|
txns[id] = &txnConn{conn: conn, url: resolved}
|
|
txnsMu.Unlock()
|
|
return map[string]any{
|
|
"_type": "txn",
|
|
"txn": id,
|
|
"url": resolved,
|
|
"事务": id,
|
|
"地址": resolved,
|
|
}, nil
|
|
}
|
|
|
|
// Commit commits a transaction. Returns {"ok":true}.
|
|
func Commit(txnID string) (map[string]any, error) {
|
|
t, err := takeTxn(txnID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer t.conn.Close()
|
|
if _, err := t.conn.ExecContext(context.Background(), "COMMIT"); err != nil {
|
|
return nil, err
|
|
}
|
|
return map[string]any{"ok": true}, nil
|
|
}
|
|
|
|
// Rollback rolls back a transaction. Returns {"ok":true}.
|
|
func Rollback(txnID string) (map[string]any, error) {
|
|
t, err := takeTxn(txnID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer t.conn.Close()
|
|
if _, err := t.conn.ExecContext(context.Background(), "ROLLBACK"); err != nil {
|
|
return nil, err
|
|
}
|
|
return map[string]any{"ok": true}, nil
|
|
}
|