orm

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

README

cozy-orm

cozy-orm是用于Golang的轻量级ORM框架,用极少的API满足所有开发需求,具有全面的数据库驱动兼容方案:

  • SQL方言。使用字符串而非SQL组装方法来构建SQL,最大限度兼容各种数据库方言,同时还提供了动态构建SQL方法。
  • 参数占位符 (reference : http://go-database-sql.org/prepared.html )。使用字符串构建SQL,因此不需要关注具体是哪种数据库,用户使用对应数据库驱动的参数占位符即可。有些数据库驱动的参数占位符是动态的,cozy-orm也提供了参数占位符的动态构建方法。
  • 获取自动生成ID。有些数据库驱动支持sql.Result#LastInsertId方法来获取自动生成ID,有些不支持此方法而是其他方式,cozy-orm对此做了兼容性设计。

Go Reference Go Report Card coverage

安装

go get github.com/jishaocong0910/cozy-orm

文档

详见Wiki:https://github.com/jishaocong0910/cozy-orm/wiki

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LastInsertIdAs_ = e.NewEnum(_LastInsertIdAs{})
View Source
var LogLevel_ = e.NewEnum(_LogLevel{})
View Source
var RowAs_ = e.NewEnum(_RowAs{})

Functions

func Config

func Config(c Cfg)

func DaoBuilder

func DaoBuilder[T Entity]() *daoBuilder[T]

func P

func P[T any](t T) *T

func SetTx

func SetTx(ctx context.Context, tx *sql.Tx) context.Context

func Tx

func Tx(ctx context.Context, do func(ctx context.Context) error, opts ...TxOption) (err error)

func V

func V[T any](t *T) T

Types

type BaseType

type BaseType interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 | ~bool | ~string | time.Time
}

type Cfg

type Cfg struct {
	// DefaultDB is the default database connection
	DefaultDB *sql.DB
	// Logger is the logger
	Logger Logger
	// SqlLogLevel is the log level of sql, e.g. orm.LogLevel_.DEBUG
	SqlLogLevel LogLevel
	// CompressSqlLog is whether to compress the SQL to one line in the log
	CompressSqlLog bool
	// ColumnMapper is the global default column name mapper, create by orm.NewNameMapper()
	ColumnMapper *NameMapper
}

type Convert

type Convert[T BaseType, F any] interface {
	OrmValue() T
	OrmField(value T) F
}

type Count

type Count struct {
	Value *int64
}

func (*Count) Bool

func (c *Count) Bool() bool

func (*Count) BoolPtr

func (c *Count) BoolPtr() *bool

func (*Count) Int

func (c *Count) Int() int

func (*Count) Int8

func (c *Count) Int8() int8

func (*Count) Int8Ptr

func (c *Count) Int8Ptr() *int8

func (*Count) Int16

func (c *Count) Int16() int16

func (*Count) Int16Ptr

func (c *Count) Int16Ptr() *int16

func (*Count) Int32

func (c *Count) Int32() int32

func (*Count) Int32Ptr

func (c *Count) Int32Ptr() *int32

func (*Count) Int64

func (c *Count) Int64() int64

func (*Count) Int64Ptr

func (c *Count) Int64Ptr() *int64

func (*Count) IntPtr

func (c *Count) IntPtr() *int

type Dao

type Dao[T Entity] struct {
	// contains filtered or unexported fields
}

func (*Dao[T]) Count

func (d *Dao[T]) Count() *count[T]

func (*Dao[T]) DB

func (d *Dao[T]) DB() *sql.DB

func (*Dao[T]) Exec

func (d *Dao[T]) Exec() *exec[T]

func (*Dao[T]) Query

func (d *Dao[T]) Query() *query[T]

type Entity

type Entity interface {
	Table() string
}

type LastInsertIdAs

type LastInsertIdAs struct {
	e.EnumElem
}

type LogLevel

type LogLevel struct {
	e.EnumElem
}

type Logger

type Logger interface {
	Debugf(ctx context.Context, msg string, args ...any)
	Infof(ctx context.Context, msg string, args ...any)
	Warnf(ctx context.Context, msg string, args ...any)
	Errorf(ctx context.Context, msg string, args ...any)
}

type NameMapper

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

func NewNameMapper

func NewNameMapper() *NameMapper

func (*NameMapper) AddPrefix

func (n *NameMapper) AddPrefix(prefix string) *NameMapper

func (*NameMapper) AddSuffix

func (n *NameMapper) AddSuffix(suffix string) *NameMapper

func (*NameMapper) Convert

func (n *NameMapper) Convert(str string) string

func (*NameMapper) LowerCamelCase

func (n *NameMapper) LowerCamelCase() *NameMapper

func (*NameMapper) LowerFirstLiteral

func (n *NameMapper) LowerFirstLiteral() *NameMapper

func (*NameMapper) LowerSnakeCase

func (n *NameMapper) LowerSnakeCase() *NameMapper

func (*NameMapper) SubPrefix

func (n *NameMapper) SubPrefix(prefix string) *NameMapper

func (*NameMapper) SubSuffix

func (n *NameMapper) SubSuffix(suffix string) *NameMapper

func (*NameMapper) UpperCamelCase

func (n *NameMapper) UpperCamelCase() *NameMapper

func (*NameMapper) UpperFirstLiteral

func (n *NameMapper) UpperFirstLiteral() *NameMapper

func (*NameMapper) UpperSnakeCase

func (n *NameMapper) UpperSnakeCase() *NameMapper

type PlainSqlBuilder

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

PlainSqlBuilder 无泛型的SQL构造器

func (*PlainSqlBuilder) Args

func (b *PlainSqlBuilder) Args() []any

func (*PlainSqlBuilder) Cancel

func (b *PlainSqlBuilder) Cancel() bool

func (*PlainSqlBuilder) Error

func (b *PlainSqlBuilder) Error() error

func (*PlainSqlBuilder) Ph

func (b *PlainSqlBuilder) Ph(prefix string) string

func (*PlainSqlBuilder) Repeat

func (b *PlainSqlBuilder) Repeat(num int, sep *Separate, filter func(i int) bool, handle func(n, i int))

func (*PlainSqlBuilder) Sep

func (b *PlainSqlBuilder) Sep(separator string) *Separate

func (*PlainSqlBuilder) SepFix

func (b *PlainSqlBuilder) SepFix(prefix, separator, suffix string, omitempty bool) *Separate

func (*PlainSqlBuilder) SetArgs

func (b *PlainSqlBuilder) SetArgs(args ...any) *PlainSqlBuilder

func (*PlainSqlBuilder) SetCancel

func (b *PlainSqlBuilder) SetCancel(cancel bool)

func (*PlainSqlBuilder) SetError

func (b *PlainSqlBuilder) SetError(err error)

func (*PlainSqlBuilder) Sql

func (b *PlainSqlBuilder) Sql() string

func (*PlainSqlBuilder) Write

func (b *PlainSqlBuilder) Write(str string, args ...any) *PlainSqlBuilder

func (*PlainSqlBuilder) WriteIf

func (b *PlainSqlBuilder) WriteIf(str string, check bool, args ...any) *PlainSqlBuilder

type RowAs

type RowAs struct {
	e.EnumElem
}

type Separate

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

type SqlBuilder

type SqlBuilder[T Entity] struct {
	*PlainSqlBuilder
	// contains filtered or unexported fields
}

func (*SqlBuilder[T]) AssignedColumns

func (b *SqlBuilder[T]) AssignedColumns(entity *T, fixed []string, ignored []string) (columns []string)

func (*SqlBuilder[T]) AutoColumns

func (b *SqlBuilder[T]) AutoColumns() []string

func (*SqlBuilder[T]) ColumnValue

func (b *SqlBuilder[T]) ColumnValue(entity *T, column string) any

func (*SqlBuilder[T]) Columns

func (b *SqlBuilder[T]) Columns(ignored []string) (columns []string)

func (*SqlBuilder[T]) EachColumn

func (b *SqlBuilder[T]) EachColumn(entity *T, sep *Separate, handle func(n int, column string, value any), columns ...string) *SqlBuilder[T]

func (*SqlBuilder[T]) EachEntity

func (b *SqlBuilder[T]) EachEntity(sep *Separate, handle func(n int, entity *T)) *SqlBuilder[T]

func (*SqlBuilder[T]) Entity

func (b *SqlBuilder[T]) Entity() *T

func (*SqlBuilder[T]) EntityAt

func (b *SqlBuilder[T]) EntityAt(index int) *T

func (*SqlBuilder[T]) SetArgs

func (b *SqlBuilder[T]) SetArgs(args ...any) *SqlBuilder[T]

func (*SqlBuilder[T]) Write

func (b *SqlBuilder[T]) Write(str string, args ...any) *SqlBuilder[T]

func (*SqlBuilder[T]) WriteColumns

func (b *SqlBuilder[T]) WriteColumns(columns ...string) *SqlBuilder[T]

func (*SqlBuilder[T]) WriteIf

func (b *SqlBuilder[T]) WriteIf(str string, check bool, args ...any) *SqlBuilder[T]

func (*SqlBuilder[T]) WriteTable

func (b *SqlBuilder[T]) WriteTable() *SqlBuilder[T]

type TxOption

type TxOption func(*txOption)

func WithDefaultTx

func WithDefaultTx(db *sql.DB, opts *sql.TxOptions) TxOption

func WithMust

func WithMust() TxOption

Jump to

Keyboard shortcuts

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