Home > Backend Development > Golang > How to Use Reflection to Call the Database/SQL Rows.Scan() Variadic Function?

How to Use Reflection to Call the Database/SQL Rows.Scan() Variadic Function?

Mary-Kate Olsen
Release: 2024-12-05 09:56:11
Original
756 people have browsed it

How to Use Reflection to Call the Database/SQL Rows.Scan() Variadic Function?

Calling the Scan() Variadic Function with Reflection

The Rows.Scan() function provides a convenient way to retrieve data from a database query. However, it utilizes a variable number of pointers as its arguments, which can be challenging to incorporate using reflection. Consider the following scenario:

You want to dynamically populate a slice with values obtained from a query and utilize Rows.Scan() to extract the data. In this case, you'll need to determine the number of columns and create a slice to store the values.

A Common Pitfall:
An attempt to use reflection to call the Scan() function may lead to unexpected results. This is because Rows.Scan() expects pointers to the values, and simply passing a slice of interface{} values would result in nil references.

The Solution:

To successfully call Scan() with reflection, a dual-slice approach is employed. The first slice, values, holds the actual data, while the second slice, valuesPtrs, contains pointers to each element in values.

For each column in the query result, a pointer in valuesPtrs is mapped to the corresponding element in values. Subsequently, Rows.Scan() can be invoked with valuesPtrs as its argument, effectively filling values.

Working Example:

package main

import (
    "fmt"
    _ "github.com/lib/pq"
    "database/sql"
)

func main() {
    db, _ := sql.Open(
        "postgres",
        "user=postgres dbname=go_testing password=pass sslmode=disable")

    rows, _ := db.Query("SELECT * FROM _user;")

    columns, _ := rows.Columns()
    count := len(columns)
    values := make([]interface{}, count)
    valuesPtrs := make([]interface{}, count)

    for rows.Next() {
        for i := range columns {
            valuesPtrs[i] = &values[i]
        }

        rows.Scan(valuesPtrs...)

        for i, col := range columns {
            val := values[i]

            b, ok := val.([]byte)
            var v interface{}
            if (ok) {
                v = string(b)
            } else {
                v = val
            }

            fmt.Println(col, v)
        }
    }
}
Copy after login

In this example, rows.Scan() is invoked with the pointer slice valuesPtrs, populating the values slice with the retrieved data. The loop then prints each column name and its corresponding value.

The above is the detailed content of How to Use Reflection to Call the Database/SQL Rows.Scan() Variadic Function?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template