types

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: MIT Imports: 6 Imported by: 0

README

types

types 包提供了一些实现了 sql.Scannerdriver.Valuer 接口的实用类型, 适合用作 database/sql 的扫描和赋值目标。

Documentation

Overview

Package types 提供了一些实现了 sql.Scanner 和 driver.Valuer 接口的 实用类型,适合用作 database/sql 的扫描和赋值目标。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BitBool

type BitBool bool

BitBool 是 MySQL BIT(1) 类型的 bool 实现。 此类型使你可以避免为 MySQL 的布尔类型 TINYINT 浪费整个字节。

func (*BitBool) Scan

func (b *BitBool) Scan(src any) error

Scan 实现 sql.Scanner 接口, 将 MySQL 传入的 bitfield 转换为 BitBool。

func (BitBool) Value

func (b BitBool) Value() (driver.Value, error)

Value 实现 driver.Valuer 接口, 将 BitBool 转换为 bitfield (BIT(1)) 用于 MySQL 存储。

type GzippedText

type GzippedText []byte

GzippedText 是一个 []byte,在提交到数据库时自动 gzip 压缩数据, 并在从数据库中 Scan 时自动解压数据。

func (*GzippedText) Scan

func (g *GzippedText) Scan(src any) error

Scan 实现 sql.Scanner 接口,解压从网络传输过来的值, 并将原始结果存储在 GzippedText 中。

func (GzippedText) Value

func (g GzippedText) Value() (driver.Value, error)

Value 实现 driver.Valuer 接口,对此 GzippedText 的原始值进行 gzip 压缩。

type JSONText

type JSONText json.RawMessage

JSONText 是 json.RawMessage 的类型别名,底层是 []byte。 Value() 验证源数据中的 JSON 格式,如果 JSON 无效则返回错误。 Scan 不进行验证。JSONText 还实现了 Unmarshal 方法, 将内部的 JSON 反序列化到 any。

func (JSONText) MarshalJSON

func (j JSONText) MarshalJSON() ([]byte, error)

MarshalJSON 返回 *j 的 JSON 编码。

func (*JSONText) Scan

func (j *JSONText) Scan(src any) error

Scan 将 src 存储到 *j 中,不进行验证。

func (JSONText) String

func (j JSONText) String() string

String 支持 JSONText 类型的格式化输出。

func (*JSONText) Unmarshal

func (j *JSONText) Unmarshal(v any) error

Unmarshal 将 j 中的 JSON 反序列化到 v,类似于 json.Unmarshal。

func (*JSONText) UnmarshalJSON

func (j *JSONText) UnmarshalJSON(data []byte) error

UnmarshalJSON 将 *j 设置为 data 的副本。

func (JSONText) Value

func (j JSONText) Value() (driver.Value, error)

Value 将 j 作为值返回。它会执行一次验证性的反序列化到另一个 RawMessage。如果 j 是无效的 JSON,则返回错误。

type NullJSONText

type NullJSONText struct {
	JSONText
	Valid bool // Valid 为 true 表示 JSONText 不为 NULL
}

NullJSONText 表示一个可能为 null 的 JSONText。 NullJSONText 实现了 Scanner 接口,因此可以像 NullString 一样 用作扫描目标。

func (*NullJSONText) Scan

func (n *NullJSONText) Scan(value any) error

Scan 实现 Scanner 接口。

func (NullJSONText) Value

func (n NullJSONText) Value() (driver.Value, error)

Value 实现 driver.Valuer 接口。

Jump to

Keyboard shortcuts

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