sql

package
v0.0.0-...-71a7f5d Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JsonColumn

type JsonColumn[T any] struct {
	Val   T
	Valid bool
}

JsonColumn 代表存储字段的 json 类型 主要用于没有提供默认 json 类型的数据库 T 可以是结构体,也可以是切片或者 map 一切可以被 json 库所处理的类型都能被用作 T

func (*JsonColumn[T]) Scan

func (j *JsonColumn[T]) Scan(src any) error

Scan 将 src 转化为对象 src 的类型必须是 []byte, *[]byte, string, sql.RawBytes, *sql.RawBytes 之一

Example
js := JsonColumn[User]{}
err := js.Scan(`{"Name":"Tom"}`)
if err != nil {
	fmt.Println(err)
}
fmt.Print(js.Val)
Output:

{Tom}

func (JsonColumn[T]) Value

func (j JsonColumn[T]) Value() (driver.Value, error)

Value 返回一个 json 串。类型是 []byte

Example
js := JsonColumn[User]{Valid: true, Val: User{Name: "Tom"}}
value, err := js.Value()
if err != nil {
	fmt.Println(err)
}
fmt.Print(string(value.([]byte)))
Output:

{"Name":"Tom"}

Jump to

Keyboard shortcuts

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