Sqlx在将数据扫描到无效类型时没有抛出错误。

huangapple go评论99阅读模式
英文:

Sqlx not throwing error when scanning into invalid type

问题

我正在使用Go语言中的sqlx,它非常有帮助,但是当我使用struct scan时,似乎没有在结构体的类型与SQL类型不匹配时抛出错误。例如,我设置了一个数据库,其中包含一个名字(string类型)和年龄(int类型):

+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name  | varchar(255) | NO   |     | NULL    |       |
| age   | int(11)      | NO   |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
+------+-----+
| name | age |
+------+-----+
| bob  |  10 |
+------+-----+

然后,我使用sqlx将这些值读取到一个结构体中,但是结构体的类型是错误的。

package main

import (
  "log"
  "github.com/jmoiron/sqlx"
  _ "github.com/go-sql-driver/mysql"
)

// 在数据库中,name是string类型,age是int类型

type Person struct{
  Name int
  Age string
}

func main() {
  sqlSession, err := sqlx.Open("mysql", "root:@(localhost:3306)/dashboard?parseTime=true")
  if err != nil {
    log.Panic(err)
  }
  err = sqlSession.Ping()
  if err != nil {
    log.Panic(err)
  }
  query := "SELECT * FROM test"
  rows, errSql := sqlSession.Queryx(query)
  if errSql != nil {
    log.Panic(errSql)
  }
  for rows.Next() {
    var p Person
    errScan := rows.StructScan(&p)
    if errScan != nil {
      log.Panic(errScan)
    }
    log.Println("Person:", p)
  }
}

所以,它没有给我报错,而是将值置为零。Person: {0 }

有其他人遇到过这个问题吗?有没有人认为这是一个bug?我认为当我尝试将值扫描到无效类型时,它应该给我一个错误。

英文:

I am using sqlx in Go, which is very helpful, but it does not seem to throw errors when I use struct scan and the types of the struct don't match the sql types. For example here I set up a database to have a name (string) and age(int):

+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name  | varchar(255) | NO   |     | NULL    |       |
| age   | int(11)      | NO   |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
+------+-----+
| name | age |
+------+-----+
| bob  |  10 |
+------+-----+

I then use sqlx to read out the values into a struct, but the struct has the wrong types.

package main

import (
  "log"
  "github.com/jmoiron/sqlx"
  _ "github.com/go-sql-driver/mysql"
)

// in database name is a string and age is an int

type Person struct{
  Name int
  Age string
}

func main() {
  sqlSession, err := sqlx.Open("mysql", "root:@(localhost:3306)/dashboard?parseTime=true")
  if err != nil {
    log.Panic(err)
  }
  err = sqlSession.Ping()
  if err != nil {
    log.Panic(err)
  }
  query := "SELECT * FROM test"
  rows, errSql := sqlSession.Queryx(query)
  if errSql != nil {
    log.Panic(errSql)
  }
  for rows.Next() {
    var p Person
    errScan := rows.StructScan(&p)
    if errScan != nil {
      log.Panic(errScan)
    }
    log.Println("Person:", p)
  }
}

So instead of giving me an error, it has zeroed out values. Person: {0 }

Has anyone else run into this problem? Does any one else think this is a bug? I think it should give me an error when I try to scan into an invalid type.

答案1

得分: 0

我怀疑这是决定在找不到放置返回值的位置时不返回错误的副作用。不返回错误的一个原因是,如果你执行了一个"select *"语句,即使你的结构体包含了所有的列,你也无法添加新的列,否则代码会立即开始返回错误。

有人可能会认为,如果所有查询的列都没有使用,那么应该返回一个错误。可能最好的方法是向该项目提交一个问题。

英文:

I suspect that is a side-effect of deciding not to return errors when it can't find a place to put the returned value. One reason you may not want to return an error is if you do a "select *", then even if your struct has all columns, you will not be able to add new columns without the code immediately beginning to return errors.

One could argue that if all the query columns were unused, then it should return an error. Probably opening an issue with the project would be the way to go.

huangapple
  • 本文由 发表于 2014年1月23日 05:22:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/21294236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定