在go中泛化*sql.rows扫描

dw1jzc5e  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(406)

我正在使用go开发一个webapi,其中有很多冗余的数据库查询扫描代码。

func (m *ContractModel) WorkQuestions(cid int) ([]models.WorkQuestion, error) {
    results, err := m.DB.Query(queries.WORK_QUESTIONS, cid)
    if err != nil {
        return nil, err
    }

    var workQuestions []models.WorkQuestion
    for results.Next() {
        var wq models.WorkQuestion
        err = results.Scan(&wq.ContractStateID, &wq.QuestionID, &wq.Question, &wq.ID, &wq.Answer, &wq.Compulsory)
        if err != nil {
            return nil, err
        }
        workQuestions = append(workQuestions, wq)
    }

    return workQuestions, nil
}

func (m *ContractModel) Questions(cid int) ([]models.Question, error) {
    results, err := m.DB.Query(queries.QUESTIONS, cid)
    if err != nil {
        return nil, err
    }

    var questions []models.Question
    for results.Next() {
        var q models.Question
        err = results.Scan(&q.Question, &q.Answer)
        if err != nil {
            return nil, err
        }
        questions = append(questions, q)
    }

    return questions, nil
}

func (m *ContractModel) Documents(cid int) ([]models.Document, error) {
    results, err := m.DB.Query(queries.DOCUMENTS, cid)
    if err != nil {
        return nil, err
    }

    var documents []models.Document
    for results.Next() {
        var d models.Document
        err = results.Scan(&d.Document, &d.S3Region, &d.S3Bucket, &d.Source)
        if err != nil {
            return nil, err
        }
        documents = append(documents, d)
    }

    return documents, nil
}

我需要对这个代码进行泛化,以便传入结果 *sql.Rows 并获取包含扫描行的结构片。我知道有一个 StructScan 方法,但这不能使用,因为我有大量使用go标准数据库/sql包编写的代码。
使用reflect包,我可以创建一个泛型structscan函数,但是reflect包不能从传递的接口{}类型创建一个结构片。我需要达到的目标如下

func RowsToStructs(rows *sql.Rows, model interface{}) ([]interface{}, error) {
    // 1. Create a slice of structs from the passed struct type of model
    // 2. Loop through each row,
    // 3. Create a struct of passed mode interface{} type
    // 4. Scan the row results to a slice of interface{}
    // 5. Set the field values of struct created in step 3 using the slice in step 4
    // 6. Add the struct created in step 3 to slice created in step 1
    // 7. Return the struct slice
}

我似乎找不到一种方法来扫描作为模型参数传递的结构,并使用reflect包创建它的一个片段。有什么解决办法吗?还是我看问题的方式不对?
结构字段具有从结果返回的正确列数,并且顺序正确

niknxzdl

niknxzdl1#

通过将指向目标片的指针作为参数传递,可以避免在调用函数中使用类型Assert。下面是经过修改的rowstostructs:

// RowsToStructs scans rows to the slice pointed to by dest.
// The slice elements must be pointers to structs with exported
// fields corresponding to the the columns in the result set.
//
// The function panics if dest is not as described above.
func RowsToStructs(rows *sql.Rows, dest interface{}) error {

    // 1. Create a slice of structs from the passed struct type of model
    //
    // Not needed, the caller passes pointer to destination slice.
    // Elem() dereferences the pointer.
    //
    // If you do need to create the slice in this function
    // instead of using the argument, then use
    // destv := reflect.MakeSlice(reflect.TypeOf(model).

    destv := reflect.ValueOf(dest).Elem()

    // Allocate argument slice once before the loop.

    args := make([]interface{}, destv.Type().Elem().NumField())

    // 2. Loop through each row

    for rows.Next() {

        // 3. Create a struct of passed mode interface{} type
        rowp := reflect.New(destv.Type().Elem())
        rowv := rowp.Elem()

        // 4. Scan the row results to a slice of interface{}
        // 5. Set the field values of struct created in step 3 using the slice in step 4
        //
        // Scan directly to the struct fields so the database
        // package handles the conversion from database
        // types to a Go types.
        //
        // The slice args is filled with pointers to struct fields.

        for i := 0; i < rowv.NumField(); i++ {
            args[i] = rowv.Field(i).Addr().Interface()
        }

        if err := rows.Scan(args...); err != nil {
            return err
        }

        // 6. Add the struct created in step 3 to slice created in step 1

        destv.Set(reflect.Append(destv, rowv))

    }
    return nil
}

这样称呼:

func (m *ContractModel) Documents(cid int) ([]*models.Document, error) {
    results, err := m.DB.Query(queries.DOCUMENTS, cid)
    if err != nil {
        return nil, err
    }
    defer results.Close()
    var documents []*models.Document
    err := RowsToStruct(results, &documents)
    return documents, err
}

通过将查询移动到助手函数来消除更多样板文件:

func QueryToStructs(dest interface{}, db *sql.DB, q string, args ...interface{}) error {
    rows, err := db.Query(q, args...)
    if err != nil {
        return err
    }
    defer rows.Close()
    return RowsToStructs(rows, dest)
}

这样称呼:

func (m *ContractModel) Documents(cid int) ([]*models.Document, error) {
    var documents []*model.Document
    err := QueryToStructs(&documents, m.DB, queries.DOCUMENTS, cid)
    return documents, err
}
bq8i3lrv

bq8i3lrv2#

"... 但是reflect包无法从传递的接口{}类型创建结构片?

func sliceFromElemValue(v interface{}) (interface{}) {
    rt := reflect.TypeOf(v)
    rs := reflect.MakeSlice(reflect.SliceOf(rt), 0, 0)

    for i := 0; i < 3; i++ { // dummy loop
        rs = reflect.Append(rs, reflect.New(rt).Elem())
    }

    return rs.Interface()
}

https://play.golang.com/p/o4aj-f71egw
“我要达到的目标如下”

func RowsToStructs(rows *sql.Rows, model interface{}) ([]interface{}, error) { ...

或者你在找这个?

func sliceFromElemValue(v interface{}) ([]interface{}) {
    rt := reflect.TypeOf(v)
    s := []interface{}{}

    for i := 0; i < 3; i++ { // dummy loop
        s = append(s, reflect.New(rt).Elem().Interface())
    }

    return s
}

https://play.golang.com/p/i57z8oo8n7g

相关问题