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

Scan receives unexpected type []uint8 when using db.QueryRow #441

Closed
nkovacs opened this issue Mar 28, 2016 · 5 comments
Closed

Scan receives unexpected type []uint8 when using db.QueryRow #441

nkovacs opened this issue Mar 28, 2016 · 5 comments

Comments

@nkovacs
Copy link

nkovacs commented Mar 28, 2016

Issue description

I've created a custom type that implements sql.Scanner.

The documentation of this interface claims that Scan will receive one of the following types:

  • int64
  • float64
  • bool
  • []byte
  • string
  • time.Time
  • nil - for NULL values

If I use a prepared statement, it receives an int64, which is correct. If I just use db.QueryRow, it receives a []uint8 with the value [52 50] instead, which is actually the string "42". It should receive int64 in both cases.

Example code

-- phpMyAdmin SQL Dump
-- version 4.4.13.1deb1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Mar 28, 2016 at 12:24 PM
-- Server version: 5.6.28-0ubuntu0.15.10.1
-- PHP Version: 5.6.11-1ubuntu3.1

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `testdb`
--

-- --------------------------------------------------------

--
-- Table structure for table `foo_models`
--

CREATE TABLE IF NOT EXISTS `foo_models` (
  `foo` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

--
-- Dumping data for table `foo_models`
--

INSERT INTO `foo_models` (`foo`) VALUES
(42);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
package main

import (
    "database/sql"
    "fmt"

    _ "github.com/go-sql-driver/mysql"
)

type Foo int

// Scan implements the database/sql.Scanner interface
func (f *Foo) Scan(value interface{}) error {
    switch value := value.(type) {
    case int64:
        *f = Foo(value)
    default:
        return fmt.Errorf("Invalid database type: %T %v", value, value)
    }
    return nil
}

func main() {
    db, err := sql.Open("mysql", "user:pass@/testdb?parseTime=true")
    if err != nil {
        panic(err)
    }

    stmtOut, err := db.Prepare("SELECT foo FROM foo_models LIMIT 1")
    if err != nil {
        panic(err)
    }
    defer stmtOut.Close()

    var foo Foo
    err = stmtOut.QueryRow().Scan(&foo)
    if err != nil {
        panic(err)
    }

    fmt.Println(foo)

    row := db.QueryRow("SELECT foo FROM foo_models LIMIT 1")
    err = row.Scan(&foo)
    if err != nil {
        panic(err)
    }

    fmt.Println(foo)
}

Configuration

Driver version (or git SHA): 66312f7

Go version: go1.6 linux/amd64

Server version: 5.6.28-0ubuntu0.15.10.1

Server OS: Ubuntu 15.10

@julienschmidt
Copy link
Member

See #407 / #366 / #86.

If the driver would know what type the user passed to .Scan(..) we would certainly do this. But the database/sql interface hides that information from the driver.

@nkovacs
Copy link
Author

nkovacs commented Mar 28, 2016

Yeah, I looked at those issues, your reply was to not scan into interface{}, but I'm not doing that.
Though that probably doesn't make much of a difference, since the conversion is also handled by the sql package.

@YasiruR
Copy link

YasiruR commented Jul 16, 2020

Any update on this?

@99848873
Copy link

99848873 commented May 6, 2021

Is this issue resloved?

@shogo82148
Copy link
Contributor

it's a specification of the driver.
see #441 (comment)
this issue will be never updated.

scbizu added a commit to ezbuy/ezorm that referenced this issue Jul 26, 2023
…ToResult helper

MySQL driver will scan `any` type field into `[]byte` as [it mentioned](go-sql-driver/mysql#441)
However, we cannot determine the underlying type of every mysql functions currently.
We need a helper function to generic the `[]byte` -> wellknown SQL type(e.g.: `NullInt64`) to avoid spamming the conversion code all over our business codebase.
scbizu added a commit to ezbuy/ezorm that referenced this issue Jul 26, 2023
…ToResult helper

MySQL driver will scan `any` type field into `[]byte` as [it mentioned](go-sql-driver/mysql#441)
However, we cannot determine the underlying type of every mysql functions currently.
We need a helper function to generic the `[]byte` -> wellknown SQL type(e.g.: `NullInt64`) to avoid spamming the conversion code all over our business codebase.
scbizu added a commit to ezbuy/ezorm that referenced this issue Jul 26, 2023
…ToResult helper

MySQL driver will scan `any` type field into `[]byte` as [it mentioned](go-sql-driver/mysql#441)
However, we cannot determine the underlying type of every mysql functions currently.
We need a helper function to generic the `[]byte` -> wellknown SQL type(e.g.: `NullInt64`) to avoid spamming the conversion code all over our business codebase.
scbizu added a commit to ezbuy/ezorm that referenced this issue Jul 26, 2023
…ToResult helper

MySQL driver will scan `any` type field into `[]byte` as [it mentioned](go-sql-driver/mysql#441)
However, we cannot determine the underlying type of every mysql functions currently.
We need a helper function to generic the `[]byte` -> wellknown SQL type(e.g.: `NullInt64`) to avoid spamming the conversion code all over our business codebase.
scbizu added a commit to ezbuy/ezorm that referenced this issue Jul 27, 2023
…ToResult helper

MySQL driver will scan `any` type field into `[]byte` as [it mentioned](go-sql-driver/mysql#441)
However, we cannot determine the underlying type of every mysql functions currently.
We need a helper function to generic the `[]byte` -> wellknown SQL type(e.g.: `NullInt64`) to avoid spamming the conversion code all over our business codebase.
scbizu added a commit to ezbuy/ezorm that referenced this issue Jul 27, 2023
…ToResult helper

MySQL driver will scan `any` type field into `[]byte` as [it mentioned](go-sql-driver/mysql#441)
However, we cannot determine the underlying type of every mysql functions currently.
We need a helper function to generic the `[]byte` -> wellknown SQL type(e.g.: `NullInt64`) to avoid spamming the conversion code all over our business codebase.
scbizu added a commit to ezbuy/ezorm that referenced this issue Jul 27, 2023
…ToResult helper

MySQL driver will scan `any` type field into `[]byte` as [it mentioned](go-sql-driver/mysql#441)
However, we cannot determine the underlying type of every mysql functions currently.
We need a helper function to generic the `[]byte` -> wellknown SQL type(e.g.: `NullInt64`) to avoid spamming the conversion code all over our business codebase.
scbizu added a commit to ezbuy/ezorm that referenced this issue Jul 27, 2023
…ToResult helper

MySQL driver will scan `any` type field into `[]byte` as [it mentioned](go-sql-driver/mysql#441)
However, we cannot determine the underlying type of every mysql functions currently.
We need a helper function to generic the `[]byte` -> wellknown SQL type(e.g.: `NullInt64`) to avoid spamming the conversion code all over our business codebase.
scbizu added a commit to ezbuy/ezorm that referenced this issue Jul 27, 2023
…ToResult helper

MySQL driver will scan `any` type field into `[]byte` as [it mentioned](go-sql-driver/mysql#441)
However, we cannot determine the underlying type of every mysql functions currently.
We need a helper function to generic the `[]byte` -> wellknown SQL type(e.g.: `NullInt64`) to avoid spamming the conversion code all over our business codebase.
scbizu added a commit to ezbuy/ezorm that referenced this issue Jul 27, 2023
…ToResult helper

MySQL driver will scan `any` type field into `[]byte` as [it mentioned](go-sql-driver/mysql#441)
However, we cannot determine the underlying type of every mysql functions currently.
We need a helper function to generic the `[]byte` -> wellknown SQL type(e.g.: `NullInt64`) to avoid spamming the conversion code all over our business codebase.
scbizu added a commit to ezbuy/ezorm that referenced this issue Jul 27, 2023
…ToResult helper

MySQL driver will scan `any` type field into `[]byte` as [it mentioned](go-sql-driver/mysql#441)
However, we cannot determine the underlying type of every mysql functions currently.
We need a helper function to generic the `[]byte` -> wellknown SQL type(e.g.: `NullInt64`) to avoid spamming the conversion code all over our business codebase.
scbizu added a commit to ezbuy/ezorm that referenced this issue Jul 27, 2023
…ToResult helper (#296)

MySQL driver will scan `any` type field into `[]byte` as [it mentioned](go-sql-driver/mysql#441)
However, we cannot determine the underlying type of every mysql functions currently.
We need a helper function to generic the `[]byte` -> wellknown SQL type(e.g.: `NullInt64`) to avoid spamming the conversion code all over our business codebase.
scbizu added a commit to ezbuy/ezorm that referenced this issue Jul 27, 2023
…ToResult helper (#296)

MySQL driver will scan `any` type field into `[]byte` as [it mentioned](go-sql-driver/mysql#441)
However, we cannot determine the underlying type of every mysql functions currently.
We need a helper function to generic the `[]byte` -> wellknown SQL type(e.g.: `NullInt64`) to avoid spamming the conversion code all over our business codebase.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants