Skip to content

Custom int type not working with interpolateParams=true #915

Closed
@nemith

Description

@nemith

Issue description

My SQL enviroment does not support Prepared statements so I turned on interpolateParams=True and my custom type's Scan function gets a []uint8 instead of an int64.

buck run //experimental/bbennett/sqltest
Building: finished in 3.1 sec (100%) 112/112 jobs, 2 updated
  Total time: 3.2 sec
2019/01/22 12:06:37 sql: Scan error on column index 2, name "start_time": cannot scan type []uint8 into NanoTime: [49 52 57 50 55 49 56 53 56 57 52 55 54 54 57 55 53 51 57]

Example code

 describe my_table;
+---------------+----------------------+------+-----+---------+----------------+
| Field         | Type                 | Null | Key | Default | Extra          |
+---------------+----------------------+------+-----+---------+----------------+
| id            | bigint(20) unsigned  | NO   | PRI | NULL    | auto_increment |
| name          | varchar(255)         | NO   |     | NULL    |                |
| start_time    | bigint(20) unsigned  | YES  |     | NULL    |                |
+---------------+----------------------+------+-----+---------+----------------+
11 rows in set (0.06 sec)

type NanoTime struct {
        time.Time
}

func (t *NanoTime) Scan(v interface{}) error {
        switch x := v.(type) {
        case nil:
                t.Time = time.Time{}
                return nil
        case int64:  // works fine with interplateParams=false
                if x == 0 {
                        t.Time = time.Time{}
                } else {
                        t.Time = time.Unix(0, x)
                }
                return nil
        }
        return fmt.Errorf("cannot scan type %T into NanoTime: %v", v, v)
}


type MyRow struct {
        Id        int      `db:"id"`
        Name      string   `db:"name"`
        StartTime NanoTime `db:"start_time"`
}

func main() {
        // ....

        const query = `SELECT id, name, start_time FROM my_table`
        rows, err := conn.Query(query)
        if err != nil {
                fmt.Fprintf(os.Stderr, "error querying: %v\n", err)
                os.Exit(1)
        }

        for rows.Next() {
                var r MyRow
                if err := rows.Scan(&r.Id, &r.Name, &r.StartTime); err != nil {
                        log.Fatal(err)
                }
                fmt.Printf("Row: %#v\n", r)
        }
        fmt.Println()
       //...
}

Configuration

Driver version (or git SHA): lastest

Go version: go version go1.11.2 linux/amd64

Server version: MySQL 5.6.35

Server OS: E.g. Centos 7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions