Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
361dca5c85
|
|||
9f85a243e8
|
@@ -20,6 +20,7 @@ Potentially needs `export GOPRIVATE="gogs.mikescher.com"`
|
|||||||
| zipext | Mike | Utility for zip/gzip/tar etc |
|
| zipext | Mike | Utility for zip/gzip/tar etc |
|
||||||
| reflectext | Mike | Utility for golang reflection |
|
| reflectext | Mike | Utility for golang reflection |
|
||||||
| fsext | Mike | Utility for filesytem access |
|
| fsext | Mike | Utility for filesytem access |
|
||||||
|
| ctxext | Mike | Utility for context.Context |
|
||||||
| | | |
|
| | | |
|
||||||
| mongoext | Mike | Utility/Helper functions for mongodb (kinda abandoned) |
|
| mongoext | Mike | Utility/Helper functions for mongodb (kinda abandoned) |
|
||||||
| cursortoken | Mike | MongoDB cursortoken implementation |
|
| cursortoken | Mike | MongoDB cursortoken implementation |
|
||||||
|
27
ctxext/getter.go
Normal file
27
ctxext/getter.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package ctxext
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
func Value[T any](ctx context.Context, key any) (T, bool) {
|
||||||
|
v := ctx.Value(key)
|
||||||
|
if v == nil {
|
||||||
|
return *new(T), false
|
||||||
|
}
|
||||||
|
if tv, ok := v.(T); !ok {
|
||||||
|
return *new(T), false
|
||||||
|
} else {
|
||||||
|
return tv, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ValueOrDefault[T any](ctx context.Context, key any, def T) T {
|
||||||
|
v := ctx.Value(key)
|
||||||
|
if v == nil {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
if tv, ok := v.(T); !ok {
|
||||||
|
return def
|
||||||
|
} else {
|
||||||
|
return tv
|
||||||
|
}
|
||||||
|
}
|
@@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.519"
|
const GoextVersion = "0.0.521"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2024-10-05T00:58:15+0200"
|
const GoextVersionTimestamp = "2024-10-05T01:06:36+0200"
|
||||||
|
@@ -61,7 +61,7 @@ func (db *database) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Resul
|
|||||||
|
|
||||||
t0 := time.Now()
|
t0 := time.Now()
|
||||||
|
|
||||||
preMeta := PreExecMeta{}
|
preMeta := PreExecMeta{Context: ctx, TransactionConstructorContext: nil}
|
||||||
for _, v := range db.lstr {
|
for _, v := range db.lstr {
|
||||||
err := v.PreExec(ctx, nil, &sqlstr, &prep, preMeta)
|
err := v.PreExec(ctx, nil, &sqlstr, &prep, preMeta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -73,7 +73,7 @@ func (db *database) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Resul
|
|||||||
|
|
||||||
res, err := db.db.NamedExecContext(ctx, sqlstr, prep)
|
res, err := db.db.NamedExecContext(ctx, sqlstr, prep)
|
||||||
|
|
||||||
postMeta := PostExecMeta{Init: t0, Start: t1, End: time.Now()}
|
postMeta := PostExecMeta{Context: ctx, TransactionConstructorContext: nil, Init: t0, Start: t1, End: time.Now()}
|
||||||
for _, v := range db.lstr {
|
for _, v := range db.lstr {
|
||||||
v.PostExec(nil, origsql, sqlstr, prep, postMeta)
|
v.PostExec(nil, origsql, sqlstr, prep, postMeta)
|
||||||
}
|
}
|
||||||
@@ -90,7 +90,7 @@ func (db *database) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx.Ro
|
|||||||
|
|
||||||
t0 := time.Now()
|
t0 := time.Now()
|
||||||
|
|
||||||
preMeta := PreQueryMeta{}
|
preMeta := PreQueryMeta{Context: ctx, TransactionConstructorContext: nil}
|
||||||
for _, v := range db.lstr {
|
for _, v := range db.lstr {
|
||||||
err := v.PreQuery(ctx, nil, &sqlstr, &prep, preMeta)
|
err := v.PreQuery(ctx, nil, &sqlstr, &prep, preMeta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -102,7 +102,7 @@ func (db *database) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx.Ro
|
|||||||
|
|
||||||
rows, err := sqlx.NamedQueryContext(ctx, db.db, sqlstr, prep)
|
rows, err := sqlx.NamedQueryContext(ctx, db.db, sqlstr, prep)
|
||||||
|
|
||||||
postMeta := PostQueryMeta{Init: t0, Start: t1, End: time.Now()}
|
postMeta := PostQueryMeta{Context: ctx, TransactionConstructorContext: nil, Init: t0, Start: t1, End: time.Now()}
|
||||||
for _, v := range db.lstr {
|
for _, v := range db.lstr {
|
||||||
v.PostQuery(nil, origsql, sqlstr, prep, postMeta)
|
v.PostQuery(nil, origsql, sqlstr, prep, postMeta)
|
||||||
}
|
}
|
||||||
@@ -118,7 +118,7 @@ func (db *database) Ping(ctx context.Context) error {
|
|||||||
|
|
||||||
t0 := time.Now()
|
t0 := time.Now()
|
||||||
|
|
||||||
preMeta := PrePingMeta{}
|
preMeta := PrePingMeta{Context: ctx}
|
||||||
for _, v := range db.lstr {
|
for _, v := range db.lstr {
|
||||||
err := v.PrePing(ctx, preMeta)
|
err := v.PrePing(ctx, preMeta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -130,7 +130,7 @@ func (db *database) Ping(ctx context.Context) error {
|
|||||||
|
|
||||||
err := db.db.PingContext(ctx)
|
err := db.db.PingContext(ctx)
|
||||||
|
|
||||||
postMeta := PostPingMeta{Init: t0, Start: t1, End: time.Now()}
|
postMeta := PostPingMeta{Context: ctx, Init: t0, Start: t1, End: time.Now()}
|
||||||
for _, v := range db.lstr {
|
for _, v := range db.lstr {
|
||||||
v.PostPing(err, postMeta)
|
v.PostPing(err, postMeta)
|
||||||
}
|
}
|
||||||
@@ -151,7 +151,7 @@ func (db *database) BeginTransaction(ctx context.Context, iso sql.IsolationLevel
|
|||||||
db.txctr += 1 // with overflow !
|
db.txctr += 1 // with overflow !
|
||||||
db.lock.Unlock()
|
db.lock.Unlock()
|
||||||
|
|
||||||
preMeta := PreTxBeginMeta{}
|
preMeta := PreTxBeginMeta{Context: ctx}
|
||||||
for _, v := range db.lstr {
|
for _, v := range db.lstr {
|
||||||
err := v.PreTxBegin(ctx, txid, preMeta)
|
err := v.PreTxBegin(ctx, txid, preMeta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -163,7 +163,7 @@ func (db *database) BeginTransaction(ctx context.Context, iso sql.IsolationLevel
|
|||||||
|
|
||||||
xtx, err := db.db.BeginTxx(ctx, &sql.TxOptions{Isolation: iso})
|
xtx, err := db.db.BeginTxx(ctx, &sql.TxOptions{Isolation: iso})
|
||||||
|
|
||||||
postMeta := PostTxBeginMeta{Init: t0, Start: t1, End: time.Now()}
|
postMeta := PostTxBeginMeta{Context: ctx, Init: t0, Start: t1, End: time.Now()}
|
||||||
for _, v := range db.lstr {
|
for _, v := range db.lstr {
|
||||||
v.PostTxBegin(txid, err, postMeta)
|
v.PostTxBegin(txid, err, postMeta)
|
||||||
}
|
}
|
||||||
|
@@ -6,9 +6,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type PrePingMeta struct {
|
type PrePingMeta struct {
|
||||||
|
Context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
type PreTxBeginMeta struct {
|
type PreTxBeginMeta struct {
|
||||||
|
Context context.Context
|
||||||
ConstructorContext context.Context
|
ConstructorContext context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,22 +23,27 @@ type PreTxRollbackMeta struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PreQueryMeta struct {
|
type PreQueryMeta struct {
|
||||||
|
Context context.Context
|
||||||
|
TransactionConstructorContext context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
type PreExecMeta struct {
|
type PreExecMeta struct {
|
||||||
|
Context context.Context
|
||||||
|
TransactionConstructorContext context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostPingMeta struct {
|
type PostPingMeta struct {
|
||||||
Init time.Time
|
Context context.Context
|
||||||
Start time.Time
|
Init time.Time
|
||||||
End time.Time
|
Start time.Time
|
||||||
|
End time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostTxBeginMeta struct {
|
type PostTxBeginMeta struct {
|
||||||
ConstructorContext context.Context
|
Context context.Context
|
||||||
Init time.Time
|
Init time.Time
|
||||||
Start time.Time
|
Start time.Time
|
||||||
End time.Time
|
End time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostTxCommitMeta struct {
|
type PostTxCommitMeta struct {
|
||||||
@@ -58,15 +65,19 @@ type PostTxRollbackMeta struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PostQueryMeta struct {
|
type PostQueryMeta struct {
|
||||||
Init time.Time
|
Context context.Context
|
||||||
Start time.Time
|
TransactionConstructorContext context.Context
|
||||||
End time.Time
|
Init time.Time
|
||||||
|
Start time.Time
|
||||||
|
End time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostExecMeta struct {
|
type PostExecMeta struct {
|
||||||
Init time.Time
|
Context context.Context
|
||||||
Start time.Time
|
TransactionConstructorContext context.Context
|
||||||
End time.Time
|
Init time.Time
|
||||||
|
Start time.Time
|
||||||
|
End time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type Listener interface {
|
type Listener interface {
|
||||||
|
@@ -109,7 +109,7 @@ func (tx *transaction) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Re
|
|||||||
|
|
||||||
t0 := time.Now()
|
t0 := time.Now()
|
||||||
|
|
||||||
preMeta := PreExecMeta{}
|
preMeta := PreExecMeta{Context: ctx, TransactionConstructorContext: tx.constructorContext}
|
||||||
for _, v := range tx.db.lstr {
|
for _, v := range tx.db.lstr {
|
||||||
err := v.PreExec(ctx, langext.Ptr(tx.id), &sqlstr, &prep, preMeta)
|
err := v.PreExec(ctx, langext.Ptr(tx.id), &sqlstr, &prep, preMeta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -126,7 +126,7 @@ func (tx *transaction) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Re
|
|||||||
tx.status = TxStatusActive
|
tx.status = TxStatusActive
|
||||||
}
|
}
|
||||||
|
|
||||||
postMeta := PostExecMeta{Init: t0, Start: t1, End: time.Now()}
|
postMeta := PostExecMeta{Context: ctx, TransactionConstructorContext: tx.constructorContext, Init: t0, Start: t1, End: time.Now()}
|
||||||
for _, v := range tx.db.lstr {
|
for _, v := range tx.db.lstr {
|
||||||
v.PostExec(langext.Ptr(tx.id), origsql, sqlstr, prep, postMeta)
|
v.PostExec(langext.Ptr(tx.id), origsql, sqlstr, prep, postMeta)
|
||||||
}
|
}
|
||||||
@@ -142,7 +142,7 @@ func (tx *transaction) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx
|
|||||||
|
|
||||||
t0 := time.Now()
|
t0 := time.Now()
|
||||||
|
|
||||||
preMeta := PreQueryMeta{}
|
preMeta := PreQueryMeta{Context: ctx, TransactionConstructorContext: tx.constructorContext}
|
||||||
for _, v := range tx.db.lstr {
|
for _, v := range tx.db.lstr {
|
||||||
err := v.PreQuery(ctx, langext.Ptr(tx.id), &sqlstr, &prep, preMeta)
|
err := v.PreQuery(ctx, langext.Ptr(tx.id), &sqlstr, &prep, preMeta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -159,7 +159,7 @@ func (tx *transaction) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx
|
|||||||
tx.status = TxStatusActive
|
tx.status = TxStatusActive
|
||||||
}
|
}
|
||||||
|
|
||||||
postMeta := PostQueryMeta{Init: t0, Start: t1, End: time.Now()}
|
postMeta := PostQueryMeta{Context: ctx, TransactionConstructorContext: tx.constructorContext, Init: t0, Start: t1, End: time.Now()}
|
||||||
for _, v := range tx.db.lstr {
|
for _, v := range tx.db.lstr {
|
||||||
v.PostQuery(langext.Ptr(tx.id), origsql, sqlstr, prep, postMeta)
|
v.PostQuery(langext.Ptr(tx.id), origsql, sqlstr, prep, postMeta)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user