Golang: Store Query Result in a Map

1. Golang: Store Query Result in a Map

注意: 使用这个可能会造成列名和列值乱串的现象,解决这个可以使用 AS 语法:

SELECT 
  TENANT_ID AS TENANT_ID,
  SVR_IP AS SVR_IP,
  SVR_PORT AS SVR_PORT,
  PLAN_ID AS PLAN_ID,
  SQL_ID AS SQL_ID,
  TYPE AS TYPE,
  DB_ID AS DB_ID,
  STATEMENT AS STATEMENT,
  PLAN_HASH AS PLAN_HASH,
  LAST_ACTIVE_TIME AS LAST_ACTIVE_TIME,
  ELAPSED_TIME AS ELAPSED_TIME
FROM GV$PLAN_CACHE_PLAN_STAT
WHERE LAST_ACTIVE_TIME > '%s' AND LAST_ACTIVE_TIME <= '%s' AND ELAPSED_TIME > %d

Converting the results of a SQL query to a struct in Go is trivial, but sometimes you don’t know ahead of time exactly what columns and types you’re going to be retrieving, and a map may be better suited for storing the results.

1.1. Using Structs

First, here’s the standard way we can convert results to a struct:

rows, _ := db.Query("SELECT ...") // Note: Ignoring errors for brevity
for rows.Next() {
    s := myStruct{}
    if err := rows.Scan(&s); err != nil {
        return err
    }
    
    // Do something with 's'
}

Easy enough, but storing your query result in a map is a bit trickier.

1.2. Using Maps

Let’s say for example you’re working with a user’s database where you don’t know the schema ahead of time. You can’t write a struct to store the results, because you don’t know what columns and data types you’re going to be retrieving. What we want in this case is a map[string]interface{} where the key is the column name and the value could be any data type.

You might assume you can do the following:

rows, _ := db.Query("SELECT ...") // Note: Ignoring errors for brevity
for rows.Next() {
    m := make(map[string]interface{})
    
    // This WON'T WORK
    if err := rows.Scan(&m); err != nil {
        // ERROR: sql: expected X destination arguments in Scan, not 1
    }
}

Basically the SQL package is thinking that you expect a single column to be returned and for it to be a map[string]interface{} compatible type, which isn’t what we we’re trying to do.

Instead what we have to do in order to make this work is the following:

rows, _ := db.Query("SELECT ...") // Note: Ignoring errors for brevity
cols, _ := rows.Columns()

for rows.Next() {
    // Create a slice of interface{}'s to represent each column,
    // and a second slice to contain pointers to each item in the columns slice.
    columns := make([]interface{}, len(cols))
    columnPointers := make([]interface{}, len(cols))
    for i, _ := range columns {
        columnPointers[i] = &columns[i]
    }
    
    // Scan the result into the column pointers...
    if err := rows.Scan(columnPointers...); err != nil {
        return err
    }

    // Create our map, and retrieve the value for each column from the pointers slice,
    // storing it in the map with the name of the column as the key.
    m := make(map[string]interface{})
    for i, colName := range cols {
        val := columnPointers[i].(*interface{})
        m[colName] = *val
    }
    
    // Outputs: map[columnName:value columnName2:value2 columnName3:value3 ...] 
    fmt.Print(m)
}

So how does this work? Well first we query and get our rows as usual, but this time we use rows.Columns() to get a reference to all column names in the result.

Then for each row, we create a slice of interface{}’s called columns who’s length matches the number of columns. Next we create a second slice with the same length called columnPointers, but this time we iterate over each element in columns and store a pointer to the interface{} element in our columnPointers slice. This is necessary because the sql package requires pointers when scanning. So now we have two slices, one of interface{}s and one of pointers to the interface{}s.

Now we can scan the row into the slice of interface{} pointers (ie. columnPointers).

Finally, we create our map[string]interface{}, and iterate over the column names. For each column name (colName), we deference the interface{} pointer at the current loop index from the columnPointers slice, which references the value in the columns slice. We take this dereferenced value and store it in the map as the value, with the key being the column name.

Now we can use the map however we need, essentially allowing us to perform queries dynamically, without requiring knowledge of the schema we’re going to be querying when we write our code.

This may seem a little confusing at first, especially if you have no prior experience with pointers. If so, I’d recommend reading up on Pointers and trying the code out, inserting some debug logging to fully understand what’s happening during each step of the process.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云满笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值