go-nullable-mysql

module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2019 License: ISC

README

go-nullable-mysql

Enhanced support of NULL values in JSON marshalling and unmarshalling for MySQL datatypes in Go. I.e., SQL NULL <-> JSON null. Also, add support for MySQL DATE types.

Usage

import (
...
  "database/sql"
  "time"

  "github.com/Liquid-Labs/go-nullable-mysql/nulls"
)

type Foo struct {
  ADate       nulls.Date      `json:"aDate"`
  ATimestamp  nulls.Timestamp `json:"aTime"`
}

func SaveData(foo *Foo) (error) {
  _, err := insertFooStmt.Exec(foo.ADate, foo.ATimestamp)
  ...
}

func ScanFoo(row sql.Row) (*Foo, error) {
  foo := Foo{}
  if err := row.Scan(&foo.ADate, &foo.ATimestamp); err != nil {
    return nil, err
   }
   return &foo, nil
}

func MakeFoo(dateString string, time time.timeStamp) (Foo) {
  return Foo{nulls.NewDate(dateString), nulls.NewTimestamp(time)}
}

Supported types are:

  • NullBool - a MySQL BOOL (==TINYINT(1)) <-> golang bool
  • NullDate - a MySQL DATE <-> golang string in 'YYYY-MM-DD' format
  • NullFloat64 - a MySQL DOUBLE (or FLOAT if you like) <-> golang float64
  • NullInt64 - a MySQL INT <-> golang int64
  • NullString - a MySQL VARCHAR, CHAR, TEXT, etc. <-> golang string
  • NullTimestamp - a MySQL TIMESTAMP <-> golang time.Time

References

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL