修改orm方法

This commit is contained in:
gem 2025-10-16 17:23:22 +08:00
parent 76259cc817
commit 7b9c666f05
2 changed files with 58 additions and 21 deletions

View File

@ -659,3 +659,28 @@ func newDBWithAlias(al *alias) Ormer {
} }
return o return o
} }
// NewOrmUsingDBWithCtx create new orm with the name
func NewOrmUsingDBWithTraceId(traceId string, aliasName string) Ormer {
if al, ok := dataBaseCache.get(aliasName); ok {
return newDBWithAliasTraceId(traceId, al)
}
panic(fmt.Errorf("<Ormer.Using> unknown db alias name `%s`", aliasName))
}
func newDBWithAliasTraceId(traceId string, al *alias) Ormer {
BootStrapWithAlias(al.Name) // execute only once
o := new(orm)
o.alias = al
if Debug {
o.db = newDbQueryLogWithTraceId(traceId, al, al.DB)
} else {
o.db = al.DB
}
if len(globalFilterChains) > 0 {
return NewFilterOrmDecorator(o, globalFilterChains...)
}
return o
}

View File

@ -35,7 +35,7 @@ func NewLog(out io.Writer) *logs.Log {
// LogFunc costomer log func // LogFunc costomer log func
var LogFunc func(query map[string]interface{}) var LogFunc func(query map[string]interface{})
func debugLogQueies(alias *alias, operation, query string, t time.Time, err error, args ...interface{}) { func debugLogQueies(traceId string, alias *alias, operation, query string, t time.Time, err error, args ...interface{}) {
logMap := make(map[string]interface{}) logMap := make(map[string]interface{})
sub := time.Since(t) / 1e5 sub := time.Since(t) / 1e5
elsp := float64(int(sub)) / 10.0 elsp := float64(int(sub)) / 10.0
@ -46,7 +46,7 @@ func debugLogQueies(alias *alias, operation, query string, t time.Time, err erro
} }
logMap["flag"] = flag logMap["flag"] = flag
con := fmt.Sprintf(" -[Queries/%s] - [ %s / %11s / %7.1fms] - [%s]", alias.Name, flag, operation, elsp, query) con := fmt.Sprintf("TraceId[%s] -[Queries/%s] - [ %s / %11s / %7.1fms] - [%s]", traceId, alias.Name, flag, operation, elsp, query)
logMap["alias_name"] = alias.Name logMap["alias_name"] = alias.Name
logMap["operation"] = operation logMap["operation"] = operation
logMap["query"] = query logMap["query"] = query
@ -75,6 +75,7 @@ type stmtQueryLog struct {
alias *alias alias *alias
query string query string
stmt stmtQuerier stmt stmtQuerier
traceId string
} }
var _ stmtQuerier = new(stmtQueryLog) var _ stmtQuerier = new(stmtQueryLog)
@ -82,7 +83,7 @@ var _ stmtQuerier = new(stmtQueryLog)
func (d *stmtQueryLog) Close() error { func (d *stmtQueryLog) Close() error {
a := time.Now() a := time.Now()
err := d.stmt.Close() err := d.stmt.Close()
debugLogQueies(d.alias, "st.Close", d.query, a, err) debugLogQueies(d.traceId, d.alias, "st.Close", d.query, a, err)
return err return err
} }
@ -93,7 +94,7 @@ func (d *stmtQueryLog) Exec(args ...interface{}) (sql.Result, error) {
func (d *stmtQueryLog) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error) { func (d *stmtQueryLog) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error) {
a := time.Now() a := time.Now()
res, err := d.stmt.ExecContext(ctx, args...) res, err := d.stmt.ExecContext(ctx, args...)
debugLogQueies(d.alias, "st.Exec", d.query, a, err, args...) debugLogQueies(d.traceId, d.alias, "st.Exec", d.query, a, err, args...)
return res, err return res, err
} }
@ -104,7 +105,7 @@ func (d *stmtQueryLog) Query(args ...interface{}) (*sql.Rows, error) {
func (d *stmtQueryLog) QueryContext(ctx context.Context, args ...interface{}) (*sql.Rows, error) { func (d *stmtQueryLog) QueryContext(ctx context.Context, args ...interface{}) (*sql.Rows, error) {
a := time.Now() a := time.Now()
res, err := d.stmt.QueryContext(ctx, args...) res, err := d.stmt.QueryContext(ctx, args...)
debugLogQueies(d.alias, "st.Query", d.query, a, err, args...) debugLogQueies(d.traceId, d.alias, "st.Query", d.query, a, err, args...)
return res, err return res, err
} }
@ -115,7 +116,7 @@ func (d *stmtQueryLog) QueryRow(args ...interface{}) *sql.Row {
func (d *stmtQueryLog) QueryRowContext(ctx context.Context, args ...interface{}) *sql.Row { func (d *stmtQueryLog) QueryRowContext(ctx context.Context, args ...interface{}) *sql.Row {
a := time.Now() a := time.Now()
res := d.stmt.QueryRow(args...) res := d.stmt.QueryRow(args...)
debugLogQueies(d.alias, "st.QueryRow", d.query, a, nil, args...) debugLogQueies(d.traceId, d.alias, "st.QueryRow", d.query, a, nil, args...)
return res return res
} }
@ -124,6 +125,7 @@ func newStmtQueryLog(alias *alias, stmt stmtQuerier, query string) stmtQuerier {
d.stmt = stmt d.stmt = stmt
d.alias = alias d.alias = alias
d.query = query d.query = query
d.traceId = ""
return d return d
} }
@ -134,6 +136,7 @@ type dbQueryLog struct {
db dbQuerier db dbQuerier
tx txer tx txer
txe txEnder txe txEnder
traceId string
} }
var ( var (
@ -149,7 +152,7 @@ func (d *dbQueryLog) Prepare(query string) (*sql.Stmt, error) {
func (d *dbQueryLog) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) { func (d *dbQueryLog) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
a := time.Now() a := time.Now()
stmt, err := d.db.PrepareContext(ctx, query) stmt, err := d.db.PrepareContext(ctx, query)
debugLogQueies(d.alias, "db.Prepare", query, a, err) debugLogQueies(d.traceId, d.alias, "db.Prepare", query, a, err)
return stmt, err return stmt, err
} }
@ -160,7 +163,7 @@ func (d *dbQueryLog) Exec(query string, args ...interface{}) (sql.Result, error)
func (d *dbQueryLog) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { func (d *dbQueryLog) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
a := time.Now() a := time.Now()
res, err := d.db.ExecContext(ctx, query, args...) res, err := d.db.ExecContext(ctx, query, args...)
debugLogQueies(d.alias, "db.Exec", query, a, err, args...) debugLogQueies(d.traceId, d.alias, "db.Exec", query, a, err, args...)
return res, err return res, err
} }
@ -171,7 +174,7 @@ func (d *dbQueryLog) Query(query string, args ...interface{}) (*sql.Rows, error)
func (d *dbQueryLog) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) { func (d *dbQueryLog) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
a := time.Now() a := time.Now()
res, err := d.db.QueryContext(ctx, query, args...) res, err := d.db.QueryContext(ctx, query, args...)
debugLogQueies(d.alias, "db.Query", query, a, err, args...) debugLogQueies(d.traceId, d.alias, "db.Query", query, a, err, args...)
return res, err return res, err
} }
@ -182,7 +185,7 @@ func (d *dbQueryLog) QueryRow(query string, args ...interface{}) *sql.Row {
func (d *dbQueryLog) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row { func (d *dbQueryLog) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
a := time.Now() a := time.Now()
res := d.db.QueryRowContext(ctx, query, args...) res := d.db.QueryRowContext(ctx, query, args...)
debugLogQueies(d.alias, "db.QueryRow", query, a, nil, args...) debugLogQueies(d.traceId, d.alias, "db.QueryRow", query, a, nil, args...)
return res return res
} }
@ -193,28 +196,28 @@ func (d *dbQueryLog) Begin() (*sql.Tx, error) {
func (d *dbQueryLog) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) { func (d *dbQueryLog) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) {
a := time.Now() a := time.Now()
tx, err := d.db.(txer).BeginTx(ctx, opts) tx, err := d.db.(txer).BeginTx(ctx, opts)
debugLogQueies(d.alias, "db.BeginTx", "START TRANSACTION", a, err) debugLogQueies(d.traceId, d.alias, "db.BeginTx", "START TRANSACTION", a, err)
return tx, err return tx, err
} }
func (d *dbQueryLog) Commit() error { func (d *dbQueryLog) Commit() error {
a := time.Now() a := time.Now()
err := d.db.(txEnder).Commit() err := d.db.(txEnder).Commit()
debugLogQueies(d.alias, "tx.Commit", "COMMIT", a, err) debugLogQueies(d.traceId, d.alias, "tx.Commit", "COMMIT", a, err)
return err return err
} }
func (d *dbQueryLog) Rollback() error { func (d *dbQueryLog) Rollback() error {
a := time.Now() a := time.Now()
err := d.db.(txEnder).Rollback() err := d.db.(txEnder).Rollback()
debugLogQueies(d.alias, "tx.Rollback", "ROLLBACK", a, err) debugLogQueies(d.traceId, d.alias, "tx.Rollback", "ROLLBACK", a, err)
return err return err
} }
func (d *dbQueryLog) RollbackUnlessCommit() error { func (d *dbQueryLog) RollbackUnlessCommit() error {
a := time.Now() a := time.Now()
err := d.db.(txEnder).RollbackUnlessCommit() err := d.db.(txEnder).RollbackUnlessCommit()
debugLogQueies(d.alias, "tx.RollbackUnlessCommit", "ROLLBACK UNLESS COMMIT", a, err) debugLogQueies(d.traceId, d.alias, "tx.RollbackUnlessCommit", "ROLLBACK UNLESS COMMIT", a, err)
return err return err
} }
@ -226,5 +229,14 @@ func newDbQueryLog(alias *alias, db dbQuerier) dbQuerier {
d := new(dbQueryLog) d := new(dbQueryLog)
d.alias = alias d.alias = alias
d.db = db d.db = db
d.traceId = ""
return d
}
func newDbQueryLogWithTraceId(traceId string, alias *alias, db dbQuerier) dbQuerier {
d := new(dbQueryLog)
d.alias = alias
d.db = db
d.traceId = traceId
return d return d
} }