From c64ee4eac4a63ec80da109c8cfad79a86750c9d3 Mon Sep 17 00:00:00 2001 From: qydysky Date: Thu, 27 Apr 2023 00:51:02 +0800 Subject: [PATCH] Add Row --- sql/Sql.go | 43 +++++++++++++++++++++++++++++++++++++++++++ sql/Sql_test.go | 26 ++++++++++++++++---------- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/sql/Sql.go b/sql/Sql.go index b4a6b02..4618270 100644 --- a/sql/Sql.go +++ b/sql/Sql.go @@ -5,6 +5,7 @@ import ( "database/sql" "errors" "fmt" + "reflect" ) const ( @@ -124,3 +125,45 @@ func (t *SqlTx[T]) Fin() (e error) { func IsFin[T any](t *SqlTx[T]) bool { return t == nil || t.fin } + +func DealRows[T any](rows *sql.Rows, createF func() T) ([]T, error) { + rowNames, err := rows.Columns() + if err != nil { + return nil, err + } + var res []T + + for rows.Next() { + rowP := make([]any, len(rowNames)) + for i := 0; i < len(rowNames); i++ { + rowP[i] = new(any) + } + + err = rows.Scan(rowP...) + if err != nil { + return nil, err + } + + var rowT = createF() + refV := reflect.ValueOf(&rowT) + for i := 0; i < len(rowNames); i++ { + v := refV.Elem().FieldByName(rowNames[i]) + if v.IsValid() { + if v.CanSet() { + val := reflect.ValueOf(*rowP[i].(*any)) + if val.Kind() == v.Kind() { + v.Set(val) + } else { + return nil, fmt.Errorf("reflectFail:%s KindNotMatch:%v !> %v", rowNames[i], val.Kind(), v.Kind()) + } + } else { + return nil, fmt.Errorf("reflectFail:%s CanSet:%v", rowNames[i], v.CanSet()) + } + } + } + res = append(res, rowT) + + } + + return res, nil +} diff --git a/sql/Sql_test.go b/sql/Sql_test.go index 930fba5..c1706ad 100644 --- a/sql/Sql_test.go +++ b/sql/Sql_test.go @@ -165,7 +165,7 @@ func TestMain3(t *testing.T) { 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)", + Query: "create table log123 (msg text,msg2 text)", }).Fin(); e != nil { t.Fatal(e) } @@ -173,12 +173,12 @@ func TestMain3(t *testing.T) { tx1 := BeginTx[any](db, context.Background(), &sql.TxOptions{}).Do(SqlFunc[any]{ Ty: Execf, - Query: "insert into log123 values ('1')", + Query: "insert into log123 values ('1','a')", }) tx2 := BeginTx[any](db, context.Background(), &sql.TxOptions{}).Do(SqlFunc[any]{ Ty: Execf, - Query: "insert into log123 values ('2')", + Query: "insert into log123 values ('2','b')", }) if e := tx1.Fin(); e != nil { @@ -190,12 +190,20 @@ func TestMain3(t *testing.T) { tx1 = BeginTx[any](db, context.Background(), &sql.TxOptions{}).Do(SqlFunc[any]{ Ty: Queryf, - Query: "select count(1) as c from log123", + Query: "select 1 as Msg, msg2 as Msg2 from log123", AfterQF: func(_ *any, rows *sql.Rows, txE error) (_ *any, stopErr error) { - for rows.Next() { - var row int64 - stopErr = rows.Scan(&row) - if row != 2 { + type logg struct { + Msg int64 + Msg2 string + } + + if v, err := DealRows(rows, func() logg { return logg{} }); err != nil { + return nil, err + } else { + if v[0].Msg2 != "a" { + t.Fatal() + } + if v[1].Msg2 != "b" { t.Fatal() } } @@ -205,8 +213,6 @@ func TestMain3(t *testing.T) { if e := tx1.Fin(); e != nil { t.Fatal(e) } - - time.Sleep(time.Second) } func TestMain4(t *testing.T) { -- 2.39.2