Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scanner, "character varying" column comes as []byte not string #416

Closed
kirubasankars opened this issue Dec 29, 2015 · 2 comments
Closed

Comments

@kirubasankars
Copy link

I used my own Scanner implementation to scan values. I found pg "character varying" column comes as []byte not string. is it a bug ?

And "numeric" type mapped to []byte
How should I differentiate my column is string or byte array?

Any Suggestion?

Example : Name column in below example fall as []byte not a string.

package main

import (
    "database/sql"
    "fmt"
    "time"

    _ "github.com/lib/pq"
)

func main() {

    db, _ := sql.Open("postgres", "user=postgres password=root dbname=test sslmode=disable")
    rows, _ := db.Query("SELECT 1 as ID, 'Jack' as NAME")
    defer rows.Close()

    columns, _ := rows.Columns()

    for rows.Next() {

        row := make([]interface{}, len(columns))
        for idx := range columns {
            row[idx] = new(MetalScanner)
        }

        err := rows.Scan(row...)
        if err != nil {
            fmt.Println(err)
        }

        for idx, column := range columns {
            var scanner = row[idx].(*MetalScanner)
            fmt.Println(column, ":", scanner.value)
        }
    }
}

type MetalScanner struct {
    valid bool
    value interface{}
}

func (scanner *MetalScanner) getBytes(src interface{}) []byte {
    if a, ok := src.([]uint8); ok {
        return a
    }
    return nil
}

func (scanner *MetalScanner) Scan(src interface{}) error {
    switch src.(type) {
    case int64:
        if value, ok := src.(int64); ok {
            scanner.value = value
            scanner.valid = true
        }
    case float64:
        if value, ok := src.(float64); ok {
            scanner.value = value
            scanner.valid = true
        }
    case bool:
        if value, ok := src.(bool); ok {
            scanner.value = value
            scanner.valid = true
        }
    case string:
        value := scanner.getBytes(src)
        scanner.value = string(value)
        scanner.valid = true
    case []byte:
        value := scanner.getBytes(src)
        scanner.value = value
        scanner.valid = true
    case time.Time:
        if value, ok := src.(time.Time); ok {
            scanner.value = value
            scanner.valid = true
        }
    case nil:
        scanner.value = nil
        scanner.valid = true
    }
    return nil
}
@AlekSi
Copy link

AlekSi commented Aug 27, 2016

I believe it's fixed by e2402a7.

@tamird
Copy link
Collaborator

tamird commented Oct 11, 2016

Looks to me like this was indeed fixed in #436. Please let me know if you disagree.

@tamird tamird closed this as completed Oct 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants