Files
x_admin/docs/server/数据库null值.md
2024-09-13 16:55:52 +08:00

1.2 KiB
Raw Blame History

数据库null值

问题点数据库在int,string等类型时同时可能允许null值但是go中int不允许null值读取等操作可能报错。

数据库应尽量避免允许null值避免不了时使用https://github.com/guregu/null

相关解读:https://blog.csdn.net/qq_15437667/article/details/78780945

第三方库:https://pkg.go.dev/database/sql#NullInt64

// 示例
type NullInt64 struct {
	Int64 int64
	Valid bool // Valid is true if Int64 is not NULL
}
func (n *NullInt64) Scan(value any) error
func (n NullInt64) Value() (driver.Value, error)

第三方库继承database/sql并补充json等https://github.com/guregu/null

// 示例
type Int struct {
	sql.NullInt64
}
func (i *Int) UnmarshalJSON(data []byte) error {
	err := internal.UnmarshalIntJSON(data, &i.Int64, &i.Valid, 64, strconv.ParseInt)
	if err != nil {
		return err
	}
	i.Valid = i.Int64 != 0
	return nil
}
func (i Int) MarshalJSON() ([]byte, error) {
	n := i.Int64
	if !i.Valid {
		n = 0
	}
	return []byte(strconv.FormatInt(n, 10)), nil
}

自带了三个可为null类型

  1. 相对于guregu/null的优点
  2. NullFloat、NullInt支持前端传递null、字符串数字、数字
core.NullFloat
core.NullInt
core.NullTime