opts *sql.TxOptions
sqlFuncs []*SqlFunc[T]
dataP *T
+ fin bool
}
type SqlFunc[T any] struct {
}
func (t *SqlTx[T]) Fin() (e error) {
+ if t.fin {
+ return fmt.Errorf("BeginTx; [] >> fin")
+ }
+
tx, err := t.canTx.BeginTx(t.ctx, t.opts)
if err != nil {
e = fmt.Errorf("BeginTx; [] >> %s", err)
e = errors.Join(e, fmt.Errorf("Commit; [] >> %s", err))
}
}
+ t.fin = true
return e
}
+
+func IsFin[T any](t *SqlTx[T]) bool {
+ return t == nil || t.fin
+}
time.Sleep(time.Second)
}
+
+func TestMain4(t *testing.T) {
+ // connect
+ db, err := sql.Open("sqlite", "test.sqlite3")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer db.Close()
+ defer file.New("test.sqlite3", 0, true).Delete()
+
+ conn, _ := db.Conn(context.Background())
+ if e := BeginTx[any](conn, context.Background(), &sql.TxOptions{}).Do(SqlFunc[any]{
+ Ty: Execf,
+ Query: "create table log123 (msg text)",
+ }).Fin(); e != nil {
+ t.Fatal(e)
+ }
+ conn.Close()
+
+ tx1 := BeginTx[any](db, context.Background(), &sql.TxOptions{}).Do(SqlFunc[any]{
+ Ty: Execf,
+ Query: "insert into log123 values ('1')",
+ })
+
+ if e := tx1.Fin(); e != nil {
+ t.Log(e)
+ }
+
+ if !IsFin(tx1) {
+ t.Fatal()
+ }
+}