-
Notifications
You must be signed in to change notification settings - Fork 0
/
multidriver.go
64 lines (50 loc) · 1.58 KB
/
multidriver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package entx
import (
"context"
"database/sql"
"entgo.io/ent/dialect"
)
// MultiWriteDriver allows you to write to a primary and secondary database
type MultiWriteDriver struct {
// Wp (write-primary), Ws (write-secondary) Drivers
Wp, Ws dialect.Driver
}
var _ dialect.Driver = (*MultiWriteDriver)(nil)
// Query will query the primary write database
func (d *MultiWriteDriver) Query(ctx context.Context, query string, args, v any) error {
return d.Wp.Query(ctx, query, args, v)
}
// Exec logs its params and calls the underlying driver Exec method for both write drivers
func (d *MultiWriteDriver) Exec(ctx context.Context, query string, args, v any) error {
err := d.Ws.Exec(ctx, query, args, v)
if err != nil {
return err
}
return d.Wp.Exec(ctx, query, args, v)
}
// Tx wraps the Exec and Query operations in transaction.
func (d *MultiWriteDriver) Tx(ctx context.Context) (dialect.Tx, error) {
return d.Wp.Tx(ctx)
}
// BeginTx adds an log-id for the transaction and calls the underlying driver BeginTx command if it is supported.
func (d *MultiWriteDriver) BeginTx(ctx context.Context, opts *sql.TxOptions) (dialect.Tx, error) {
return d.Wp.(interface {
BeginTx(context.Context, *sql.TxOptions) (dialect.Tx, error)
}).BeginTx(ctx, opts)
}
// Close the underlying connections
func (d *MultiWriteDriver) Close() error {
wserr := d.Ws.Close()
wperr := d.Wp.Close()
if wperr != nil {
return wserr
}
if wserr != nil {
return wserr
}
return nil
}
// Dialect returns the dialect name of the primary driver
func (d *MultiWriteDriver) Dialect() string {
return d.Wp.Dialect()
}