sqlx

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2023 License: Apache-2.0 Imports: 10 Imported by: 15 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EncryptColumn

type EncryptColumn[T any] struct {
	Val   T
	Valid bool
	Key   string
}

EncryptColumn 代表一个加密的列 一般来说加密可以选择依赖于数据库进行加密 EncryptColumn 并不打算使用极其难破解的加密算法 而是选择使用 AES GCM 模式。 如果你觉得安全性不够,那么你可以考虑自己实现类似的结构体.

func (*EncryptColumn[T]) Scan

func (e *EncryptColumn[T]) Scan(src any) error

Scan 方法会把写入的数据转化进行解密, 并将解密后的数据进行反序列化,构造 T

func (EncryptColumn[T]) Value

func (e EncryptColumn[T]) Value() (driver.Value, error)

Value 返回加密后的值 如果 T 是基本类型,那么会对 T 进行直接加密 否则,将 T 按照 JSON 序列化之后进行加密,返回加密后的数据

type JsonColumn

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

JsonColumn 代表存储字段的 json 类型 主要用于没有提供默认 json 类型的数据库 T 可以是结构体,也可以是切片或者 map 理论上来说一切可以被 json 库所处理的类型都能被用作 T 不建议使用指针作为 T 的类型 如果 T 是指针,那么在 Val 为 nil 的情况下,一定要把 Valid 设置为 false

func (*JsonColumn[T]) Scan

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

Scan 将 src 转化为对象 src 的类型必须是 []byte, string 或者 nil 如果是 nil,我们不会做任何处理

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