[英]Generalizing *sql.Rows Scan in Go
我正在使用 Go 開發 web API 並且有很多冗余的數據庫查詢掃描代碼。
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
傳遞給 function 並獲得包含掃描行的結構切片。 我知道sqlx package 中有一個StructScan
方法,但這不能使用,因為我有大量使用 go 標准數據庫/sql ZEFE90A8E604A7C840E88D03A67F 編寫的代碼。
使用反射 package,我可以創建通用 StructScan function 但反射 package 類型無法從傳遞的接口創建結構切片{} 我需要實現的是如下
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
}
我似乎找不到一種方法來掃描作為 model 參數傳遞的結構並使用反射 package 創建一個切片。 是否有任何解決方法,或者我是否以錯誤的方式看待這個問題?
結構字段從結果返回的列數正確且順序正確
您可以通過將指向目標切片的指針作為參數傳遞來避免在調用 function 中使用類型斷言。 這是帶有該修改的 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
}
通過將查詢移動到幫助程序 function 來消除更多樣板:
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
}
“......但反映 package 無法從傳遞的 interface{} 類型創建結構切片。” ——你在找這個嗎?
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.