Created
January 19, 2022 21:52
-
-
Save crossworth/719fada9f346767069dc903a5f429b04 to your computer and use it in GitHub Desktop.
Ent custom driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package customdriver | |
import ( | |
"context" | |
"entgo.io/ent/dialect" | |
) | |
var _ dialect.Driver = (*CustomDriver)(nil) | |
type CustomDriver struct { | |
dialect.Driver | |
} | |
func (c CustomDriver) Exec(ctx context.Context, query string, args, v interface{}) error { | |
// create the span here | |
return c.Driver.Exec(ctx, query, args, v) | |
} | |
func (c CustomDriver) Query(ctx context.Context, query string, args, v interface{}) error { | |
// create the span here | |
return c.Driver.Exec(ctx, query, args, v) | |
} | |
func (c CustomDriver) Tx(ctx context.Context) (dialect.Tx, error) { | |
tx, err := c.Driver.Tx(ctx) | |
if err != nil { | |
return nil, err | |
} | |
return &CustomTx{tx, ctx}, nil | |
} | |
func (c CustomDriver) Close() error { | |
return c.Driver.Close() | |
} | |
func (c CustomDriver) Dialect() string { | |
return c.Driver.Dialect() | |
} | |
var _ dialect.Tx = (*CustomTx)(nil) | |
type CustomTx struct { | |
dialect.Tx | |
ctx context.Context | |
} | |
func (c CustomTx) Exec(ctx context.Context, query string, args, v interface{}) error { | |
// create the span here | |
return c.Tx.Exec(ctx, query, args, v) | |
} | |
func (c CustomTx) Query(ctx context.Context, query string, args, v interface{}) error { | |
// create the span here | |
return c.Tx.Query(ctx, query, args, v) | |
} | |
func (c CustomTx) Commit() error { | |
return c.Tx.Commit() | |
} | |
func (c CustomTx) Rollback() error { | |
return c.Tx.Rollback() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment