Confused about custom types in SQL when sql.DB.Exec
Asked Answered
O

1

6

Having this table structure:

CREATE TABLE `tableName` (
    `Id` int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `Status` enum('pending','rejected','sent','invalid') NOT NULL,
    `Body` varchar(255) NULL
) ENGINE='MyISAM' COLLATE 'utf8_general_ci';

I have this (not complete) code working fine:

type StatusEnum string

const (
    STATUS_PENDING  StatusEnum = "pending"
    STATUS_REJECTED StatusEnum = "rejected"
    STATUS_SENT     StatusEnum = "sent"
    STATUS_INVALID  StatusEnum = "invalid"
)

func (s *StatusEnum) Scan(src interface{}) error {
    if src == nil {
        return errors.New("This field cannot be NULL")
    }

    if stringStatus, ok := src.([]byte); ok {
        *s = StatusEnum(string(stringStatus[:]))

        return nil
    }

    return errors.New("Cannot convert enum to string")
}

func (s *StatusEnum) Value() (driver.Value, error) {
    return []byte(*s), nil
}

type EmailQueue struct {
    Id        uint64
    Status    StatusEnum
    Body      sql.NullString
}

func Save (db *sql.DB) error {
    _, err = db.Exec(
        "UPDATE `tableName` SET `Status` = ?, `Body` = ? WHERE `id` = ?",
        &eqi.Status,
        eqi.Body,
        eqi.Id,
    )

    return err
}

So my question is: Why do I need to use pointer reference (&eqi.Status) on db.Exec?

Both sql.NullString and my custom StatusEnum are not implemented in github.com/go-sql-driver/mysql, so why the difference?

If I don't use the pointer reference (eqi.Status), I'm getting this error (throwing in database/sql/convert.go):

sql: converting Exec argument #0's type: unsupported type emailqueue.StatusEnum, a string

I was trying to implement some other interfaces I've found with no luck.

I have other custom types implemented with sql.Scanner and sql.driver.Valuer interfaces, but the problem is the same.

I was guessing about struct and type inheritance differentiation, but I couldn't get any hint on that... :(

Please help to understand what's going on. Thanks!!! :)

Ofay answered 16/5, 2015 at 15:12 Comment(0)
S
5

You implement the Valuer interface for your StatusEnum type as operating on a *StatusEnum. Therefore when you pass one as a parameter to Exec it only makes sense as a pointer as only pointers to StatusEnum implement Value(), requiring a deference inline.

You don't have a definition for the EmailList type so I can suggest an alteration to your given Valuer interface.

func (s StatusEnum) Value() (driver.Value, error) {
    return []byte(s), nil
}

This is the standard library implements custom nullable types.

Salol answered 16/5, 2015 at 16:28 Comment(2)
Sorry, I fixed the code replacing EmailList with StatusEnum. This is more confusing though because this code just works perfectly: v, err := eqi.Status.Value() v, err = (&eqi.Status).Value()Royster
For anyone coming in and reading, this is because you can call a pointer method on a non pointer value and it will automatically take the address, but only if it is a non interface method, i.e. the variable is an interface. golang.org/ref/spec#Method_valuesDitmore

© 2022 - 2024 — McMap. All rights reserved.