entity

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Err_0100010001 err.ErrCode = err.New(
	"0100010001",
	"Add Connection failed, tag is empty.",
	"",
)

Err_0100010001 添加连接时,配置中的tag为空。

View Source
var Err_0100010002 err.ErrCode = err.New(
	"0100010002",
	"Add Connection failed, driverName '%s' not support.",
	"",
)

Err_0100010002 添加连接时,配置中的driverName不支持。

Verbs:

0: 配置中的driverName。
View Source
var Err_0100010003 err.ErrCode = err.New(
	"0100010003",
	"Received a connection error because the connection tag '%s' does not exist.",
	"",
)

Err_0100010003 得到连接错误,因为连接信息标签不存在。

Verbs:

0: 连接信息标签。
View Source
var Err_0100010004 err.ErrCode = err.New(
	"0100010004",
	"Add Connection failed, the connection tag '%s' already exists.",
	"",
)

Err_0100010004 添加连接错误,因为连接信息标签已经存在。

Verbs:

0: 连接信息标签。
View Source
var Err_0100010005 err.ErrCode = err.New(
	"0100010005",
	"unknown driver %q.",
	"1. check import driver package.",
)

Err_0100010005 创建数据库实例时,遇到未知的驱动,可能是忘记导入驱动包。

View Source
var Err_010001000x err.ErrCode = err.New(
	"010001000x",
	"%s.",
	"",
)

Err_010001000x 初始化时遇到的未知错误。

Verbs:

0: 未知的错误。
View Source
var Err_0100030001 err.ErrCode = err.New(
	"0100030001",
	"entity table %s field %s required,but value is nil.",
	"",
)

Err_0100030001 在创建语句中,必填但没有默认值的字段的值为空。

Verbs:

0: 实体表的名字。
1: 字段名。
View Source
var Err_0100030002 err.ErrCode = err.New(
	"0100030002",
	"entity table %s no fields need to update.",
	"",
)

Err_0100030002 在升级语句中,没有需要更新的字段。

Verbs:

0: 实体表的名字。
View Source
var Err_0100030003 err.ErrCode = err.New(
	"0100030003",
	"change entity tracking status to %s failed.The current state needs to be %s.",
	"",
)

Err_0100030003 改变实体类的跟踪状态失败。

Verbs:

0: 需要改变成的状态。
1: 当前应该属于的状态。

Functions

func AddConnection

func AddConnection(conn ConnectionConfig) error

AddConnection 在添加一个数据库连接配置。

func GetConnection

func GetConnection(tag string) (dialect.Driver, error)

GetConnection 获取一个数据库连接。

Types

type ConnectionConfig

type ConnectionConfig struct {
	// 数据库驱动
	Driver dialect.DbDriver

	// 用于标记当前的连接,Db通过这个tag绑定连接。
	Tag string
	// 数据库链接。
	Host string
	// 数据库端口。
	Port int
	// 数据库用户。
	User string
	// 数据库密码。
	Password string
	// 数据库名称。
	DBName string

	// 是否开启SSL的verify-ca。
	IsVerifyCa bool
	// 根证书路径。
	RootCertPath string
	// 客户端证书路径。
	ClientCertPath string
	// 客户端私钥路径。
	ClientKeyPath string
}

和数据库连接相关定义。

type Database

type Database struct {
	DbInterface
}

在定义Database时,要添加这个匿名字段,用于生成代码。

func (Database) Config

func (Database) Config() DbConfig

type DbConfig

type DbConfig struct {
	// 数据库名称。
	Name string
	// 连接的标签。
	Tag string
	// 数据库驱动
	Type dialect.DbDriver
}

数据库的配置。

type DbInterface

type DbInterface interface {
	Config() DbConfig
}

ORM生成中和数据库相关定义。

type Descriptor

type Descriptor struct {
	// Name 字段在结构体中的名字,这个会在codegen/load中通过Init被赋值。
	Name string `json:"name,omitempty"`
	// AttrName 字段的数据库属性名,
	// 如果为空,会使用Name的名字,,但是会变成snake_case形式
	AttrName string `json:"attr_name,omitempty"`
	// Type 字段的类型。如"entity.Int64"。
	Type string `json:"type,omitempty"`
	// AttrType 字段的数据库类型。如"entity.Int64"在PostgreSQL中对应"int8",
	// 这AttrType的值为"int8",这个通过AttrType()获得,所以自定义类型应该正确定义这个方法。
	AttrType string `json:"attr_type,omitempty"`
	// Size 字段的长度大小。
	Size int64 `json:"size,omitempty"`
	// Required 是否是必填字段,如果为true,在数据表中的表现就是这个字段非空。
	Required bool `json:"required,omitempty"`
	// Primary 字段是否为主键,大于等于1的才会被认为是主键。
	// 在生成的sql中Primary的值越小,越靠前,比如ID的Primary = 1,UUID的Primay = 2,
	// 则在sql中PRIMARY KEY (ID,UUID)会是这样
	Primary int `json:"primary,omitempty"`
	// Comment 字段的注释。
	Comment string `json:"comment,omitempty"`
	// Default 字段默认值。
	Default bool `json:"default,omitempty"`
	// DefaultValue 字段默认值的字符串形式。
	DefaultValue string `json:"default_value,omitempty"`
	// Sequence 字段的序列,
	// 不是所有的字段类型都可以设置序列,内置的类型中只有Int(Int16,Int32,Int64)
	// 才有Sequence()方法,自定义字段要看是否实现了设置序列的相关方法。
	Sequence Sequence `json:"validators,omitempty"`
	// Validators 字段验证函数。
	Validators []any `json:"sequence,omitempty"`
}

包含了关于字段的描述,配置信息等。 这个在生成代码时会被调用。

type Entity

type Entity struct {
	EntityInterface
	// contains filtered or unexported fields
}

在定义Entity时,要添加这个匿名字段,用于生成代码。 例如:

type UseEntity struct {
		entity.Entity
}

func (Entity) Config

func (Entity) Config() EntityConfig

func (Entity) Fields

func (Entity) Fields() []FieldBuilder

type EntityConfig

type EntityConfig struct {
	// AttrName entity的数据库属性名,
	// 如果没有指定,会使用定义的结构体名称,但是会变成snake_case形式。
	//
	// 在codegen中会用于生成entity配置信息的文件和文件夹名,
	// 但是对于entity的结构体名字,还是使用定义的结构体名称,不使用AttrName,
	// 防止和别的database和entity名字冲突。
	AttrName string
}

实体表配置。

type EntityInterface

type EntityInterface interface {
	Config() EntityConfig
	Fields() []FieldBuilder
}

这个接口定义了Entity需要实现的方法。

这个接口在代码生成中会被调用,用于生成代码, 见codengen/load/entity.go中的[Marshal]。

type EntityState

type EntityState = int16

实体类状态,用于标识实体类的状态。

const (
	// 未追踪,不存在于数据库中、属性未修改、调用Save()方法时不会执行执行操作。
	Detached EntityState = 0
	// 未修改,存在于数据库中、属性未修改、调用Save()方法时,不会执行执行操作。
	Unchanged EntityState = 1
	// 已删除,存在于数据库中、调用Save()方法时,会执行删除操作。
	Deleted EntityState = 2
	// 已修改,存在于数据库中、属性已修改、调用Save()方法时,会执行更新操作。
	Modified EntityState = 3
	// 已添加,不存在于数据库中、属性已修改、调用Save()方法时,会执行插入操作。
	Added EntityState = 4
)

type FieldBuilder

type FieldBuilder interface {
	// codegen中使用,用于初始化字段。
	Init(initDesc *Descriptor) error
	// codegen中使用,用于获取字段的描述。
	Descriptor() *Descriptor
	// codegen中使用,获取字段的数据库中的类型名,如果返回空字符串,会出现错误。
	// 如果dbType没有匹配的返回空字符串
	AttrType(dbType dialect.DbDriver) string
	// 用于设置字段的值的类型名称。例如entity.Int64的ValueType为"int64"。
	ValueType() string
}

这个接口定义了字段在生成代码阶段需要的方法。

type FieldStorager

type FieldStorager interface {
	// 用于扫描数据库返回的值,将值赋值给字段。
	Scan(value interface{}) error
	// 用于打印字段的值。
	String() string
	// 用于内部sql中获取字段的值。如果需要获得值,推荐通过Get()方法获得。
	Value() FieldValue
}

这个接口定义了字段在运行时需要的方法。

type FieldValue

type FieldValue driver.Value

ORM生成中实体表中的字段。

type Mutation

type Mutation struct {
	// contains filtered or unexported fields
}

func NewMutation

func NewMutation(state EntityState) *Mutation

func (*Mutation) Fields

func (m *Mutation) Fields() []string

func (*Mutation) SetFields

func (m *Mutation) SetFields(fields ...string)

func (*Mutation) SetState

func (m *Mutation) SetState(state EntityState)

func (*Mutation) State

func (m *Mutation) State() EntityState

type Mutator

type Mutator interface {
	// State 获取实体的状态。
	State() EntityState
	// SetState 设置实体的状态。
	SetState(state EntityState)
	// Fields 获取改变值的字段。主要是用于更新操作。
	Fields() []string
	SetFields(fields ...string)
	// Exec 根据实体的状态执行操作。
	Exec() error
}

Mutator 是一个接口,用于标记实体的状态以及根据状态执行操作。

type Sequence

type Sequence struct {
	// Name 序列的名称,不能为空字符串。
	Name *string
	// Increment 每次序列递增的值,默认1。
	Increament *int64
	// Min 序列的最小值,默认1。
	Min *int64
	// Max 序列的最大值,默认为9223372036854775807。
	Max *int64
	// Start 序列的起始值,默认1。
	Start *int64
	// Cache 指定序列中要预先分配的值的数量,默认1。
	Cache *int64
}

Sequence 字段使用的序列,序列的类型默认为Int64。

func NewSequence

func NewSequence(name string) Sequence

NewSequence 创建一个Sequence,name不能为空。

type Tracker

type Tracker interface {
	Add(...Mutator)
	Mutators() []Mutator
	Clear()
}

type Tracking

type Tracking struct {
	// contains filtered or unexported fields
}

Tracking 用于跟踪实体类的状态。

func (*Tracking) Add

func (t *Tracking) Add(m ...Mutator)

Add 用于添加一个实体到追踪器中。

func (*Tracking) Clear

func (t *Tracking) Clear()

Clear 用于清空追踪器中的实体。

func (*Tracking) Mutators

func (t *Tracking) Mutators() []Mutator

Directories

Path Synopsis
cmd
gen
sql

Jump to

Keyboard shortcuts

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