douwen1937 2019-03-17 15:42
浏览 29
已采纳

从特定功能创建通用功能(重构)

I'm using the createUsers func to populate my fake DB, just for tests.

I'm using the bulk imports feature of pq (https://godoc.org/github.com/lib/pq#hdr-Bulk_imports).

func createUsers() {

    users := []models.User{}

    for i := 0; i < 10; i++ {
        users = append(users, models.User{Username: "username"+i, Age: i})
    }

    connStr := "user=postgres password=postgres dbname=dbname sslmode=disable"
    DB, err = sql.Open("postgres", connStr)
    checkErr(err)

    txn, err := DB.Begin()
    checkErr(err)

    stmt, err := txn.Prepare(pq.CopyIn("users", "username", "age"))
    checkErr(err)

    for _, user := range users {
        _, err = stmt.Exec(user.Username, user.Age)
        checkErr(err)
    }

    _, err = stmt.Exec()
    checkErr(err)

    err = stmt.Close()
    checkErr(err)

    err = txn.Commit()
    checkErr(err)
}

Everything in this code is working good.

THE NEED:

What I need now is to make it "general", not just for User model.

I think I need something like:

DBBulkInsert(users, "users", "username", "age")

with func DBBulkInsert like:

func DBBulkInsert(rows []interface{}, tableName string, tableColumns ...string) {
    // DB var from connection func

    txn, err := DB.Begin()
    checkErr(err)

    stmt, err := txn.Prepare(pq.CopyIn(tableName, tableColumns...))
    checkErr(err)

    for _, row := range rows {
        _, err = stmt.Exec(row[0], row[1]) //THIS IS TOTALLY WRONG! WHAT TO DO HERE?
        checkErr(err)
    }

    _, err = stmt.Exec()
    checkErr(err)

    err = stmt.Close()
    checkErr(err)

    err = txn.Commit()
    checkErr(err)
}

THE PROBLEM:

Obviously _, err = stmt.Exec(row[0], row[1]) is totally wrong. I don't understand how to call DBBulkInsert with my users array.

STILL BETTER:

Maybe I can remove also the parameters "users", "username", "age" in DBBulkInsert(users, "users", "username", "age"), but how? Reflection?

  • 写回答

1条回答 默认 最新

  • douruanfan3030 2019-03-17 18:40
    关注

    Your rows type needs to be [][]interface{}, i.e. a list of rows where each row is a list of column values. Then with that type each single row can be "unpacked" into the Exec call using ....

    That is:

    for _, row := range rows {
        _, err = stmt.Exec(row...)
    }
    

    To get from []model.User or []model.Whatever to [][]interface{} you'll need to use reflection. And if you want, you can also use reflection to get the column names and the table name as well.

    Say you have a model type like:

    type User struct {
        _        struct{} `rel:"users"`
        Username string   `col:"username"`
        Age      int      `col:"age"`
    }
    

    now you could use reflection to get table name and the list of columns from the fields' struct tags. (Note that use of the _ (blank) field is just one option of how to specify the table name, it has its downsides and upsides so it's up to you to choose, here I'm just trying to demonstrate how the reflect package can be leveraged).

    The following is a more complete example of how to collect the "meta" data from tags and how to aggregate the column values from struct fields.

    func DBBulkInsert(source interface{}) {
        slice := reflect.ValueOf(source)
        if slice.Kind() != reflect.Slice {
            panic("not a slice")
        }
    
        elem := slice.Type().Elem()
        if elem.Kind() == reflect.Ptr {
            elem = elem.Elem()
        }
        if elem.Kind() != reflect.Struct {
            panic("slice elem not a struct, nor a pointer to a struct")
        }
    
        // get table and column names
        var tableName string
        var cols []string
        for i := 0; i < elem.NumField(); i++ {
            f := elem.Field(i)
            if rel := f.Tag.Get("rel"); len(rel) > 0 {
                tableName = rel
            }
            if col := f.Tag.Get("col"); len(col) > 0 {
                cols = append(cols, col)
            }
        }
    
        // aggregate rows
        rows := [][]interface{}{}
        for i := 0; i < slice.Len(); i++ {
            m := slice.Index(i)
            if m.Kind() == reflect.Ptr {
                m = m.Elem()
            }
    
            vals := []interface{}{}
            for j := 0; j < m.NumField(); j++ {
                ft := m.Type().Field(j)
                if col := ft.Tag.Get("col"); len(col) > 0 {
                    f := m.Field(j)
                    vals = append(vals, f.Interface())
                }
            }
    
            rows = append(rows, vals)
        }
    
        // ...
    }
    

    Run it on playground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab