orm

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDbNotSelected                    = errors.New("db not selecteed")
	ErrTableNotExisted                  = errors.New("table not existed")
	ErrTableNotSelected                 = errors.New("table not selected")
	ErrColumnNotSelected                = errors.New("column not selected")
	ErrColumnNotExisted                 = errors.New("column not existed")
	ErrRawSqlRequired                   = errors.New("raw sql required")
	ErrParamMustBePtr                   = errors.New("param must be ptr")
	ErrParamElemKindMustBeStruct        = errors.New("param elem kind must be struct")
	ErrColumnShouldBeStringOrPtr        = errors.New("select|where column should be string or ptr of Table.T.field")
	ErrDestOfGetToMustBePtr             = errors.New("dest of Get-to must be ptr")
	ErrDestOfGetToSliceElemMustNotBePtr = errors.New("dest of Get-to slice elem kind must not be ptr")
	ErrDestOfGetToMapElemMustNotBePtr   = errors.New("dest of Get-to map elem kind must not be ptr")
	ErrInsertPtrNotAllowed              = errors.New("insert ptr data not allowed")
	ErrUpdateWithoutCondition           = errors.New("update without condition not allowed")
	ErrDeleteWithoutCondition           = errors.New("delete without condition not allowed")
)

Functions

func Open added in v0.7.5

func Open(driverName, dataSourceName string) (*sql.DB, error)

func OpenDB added in v0.7.5

func OpenDB(driver sqldriver.Connector) *sql.DB

func OpenMysql added in v0.7.5

func OpenMysql(dataSourceName string) (*sql.DB, error)

func Register added in v0.7.5

func Register(name string, drvier sqldriver.Driver)

func SetErrorLogger added in v0.9.1

func SetErrorLogger(l ErrorLogger)

func SetInfoLogger added in v0.9.1

func SetInfoLogger(l InfoLogger)

Types

type ErrorLogger added in v0.9.1

type ErrorLogger interface {
	Error(args ...any)
}

type InfoLogger added in v0.9.1

type InfoLogger interface {
	Info(args ...any)
}

type JoinType

type JoinType string
const (
	JoinTypeInner JoinType = "inner join"
	JoinTypeLeft  JoinType = "left join"
	JoinTypeRight JoinType = "right join"
	JoinTypeOuter JoinType = "outer join"
)

type JsonField added in v0.9.6

type JsonField[T any] struct {
	Data T
}

json ojbect/slice <=> go struct/slice <=> db json string

func NewJsonField added in v0.9.6

func NewJsonField[T any](data T) JsonField[T]

func (JsonField[T]) MarshalJSON added in v0.9.6

func (t JsonField[T]) MarshalJSON() ([]byte, error)

func (*JsonField[T]) Scan added in v0.9.6

func (t *JsonField[T]) Scan(raw any) error

func (*JsonField[T]) UnmarshalJSON added in v0.9.6

func (t *JsonField[T]) UnmarshalJSON(data []byte) error

func (JsonField[T]) Value added in v0.9.6

func (t JsonField[T]) Value() (driver.Value, error)

type JsonInt

type JsonInt int64

json int or long string int <=> go int64

func (JsonInt) MarshalJSON

func (t JsonInt) MarshalJSON() ([]byte, error)

func (*JsonInt) Scan

func (t *JsonInt) Scan(v any) error

func (JsonInt) ToString

func (t JsonInt) ToString() string

func (*JsonInt) UnmarshalJSON

func (t *JsonInt) UnmarshalJSON(data []byte) error

func (JsonInt) Value

func (t JsonInt) Value() (driver.Value, error)

type JsonTime

type JsonTime struct {
	time.Time
}

json int(13) <=> go time.time

func (JsonTime) MarshalJSON

func (t JsonTime) MarshalJSON() ([]byte, error)

MarshalJSON on JsonTime format Time field with %Y-%m-%d %H:%M:%S

func (*JsonTime) Scan

func (t *JsonTime) Scan(v any) error

Scan valueof time.Time

func (*JsonTime) UnmarshalJSON

func (t *JsonTime) UnmarshalJSON(data []byte) error

MarshalJSON on JsonTime format Time field with %Y-%m-%d %H:%M:%S

func (JsonTime) Value

func (t JsonTime) Value() (driver.Value, error)

Value insert timestamp into mysql need this function.

type Query

type Query[T Table] struct {
	T T
	// contains filtered or unexported fields
}

func NewQuery added in v0.3.0

func NewQuery[T Table](t T, writeAndReadDbs ...*sql.DB) *Query[T]

query table[struct] generics

func (*Query[T]) Alias added in v0.3.0

func (q *Query[T]) Alias(alias string) *Query[T]

func (*Query[T]) Clone added in v0.8.4

func (q *Query[T]) Clone() *Query[T]

func (*Query[T]) CreateStruct added in v0.8.4

func (q *Query[T]) CreateStruct(file ...string) error

func (*Query[T]) CreateTable added in v0.8.4

func (q *Query[T]) CreateTable() (string, error)

func (*Query[T]) DB

func (q *Query[T]) DB() *sql.DB

func (*Query[T]) DBs added in v0.8.3

func (q *Query[T]) DBs() []*sql.DB

func (*Query[T]) Delete

func (q *Query[T]) Delete(primaryIds ...any) QueryResult

func (*Query[T]) Execute added in v0.3.0

func (q *Query[T]) Execute() QueryResult

excute raw

func (*Query[T]) ForUpdate added in v0.4.0

func (q *Query[T]) ForUpdate(forUpdateType ...SelectForUpdateType) *Query[T]

func (*Query[T]) Get added in v0.3.0

func (q *Query[T]) Get(primaryIds ...any) (T, QueryResult)

get first T

func (*Query[T]) GetBool added in v0.9.1

func (q *Query[T]) GetBool() (bool, QueryResult)

get first bool

func (*Query[T]) GetCount added in v0.3.0

func (q *Query[T]) GetCount() (int64, QueryResult)

get count T

func (*Query[T]) GetInt added in v0.9.1

func (q *Query[T]) GetInt() (int64, QueryResult)

get first int

func (*Query[T]) GetMapInt added in v1.0.7

func (q *Query[T]) GetMapInt() (map[int64]int64, QueryResult)

func (*Query[T]) GetMapIntString added in v1.0.7

func (q *Query[T]) GetMapIntString() (map[int64]string, QueryResult)

func (*Query[T]) GetMapSliceString added in v0.9.5

func (q *Query[T]) GetMapSliceString() (map[string][]string, QueryResult)

func (*Query[T]) GetMapString added in v0.9.5

func (q *Query[T]) GetMapString() (map[string]string, QueryResult)

func (*Query[T]) GetMapStringInt added in v1.0.7

func (q *Query[T]) GetMapStringInt() (map[string]int64, QueryResult)

func (*Query[T]) GetMapStringUint added in v1.0.7

func (q *Query[T]) GetMapStringUint() (map[string]uint64, QueryResult)

func (*Query[T]) GetMapUint added in v1.0.7

func (q *Query[T]) GetMapUint() (map[uint64]uint64, QueryResult)

func (*Query[T]) GetMapUintString added in v1.0.7

func (q *Query[T]) GetMapUintString() (map[uint64]string, QueryResult)

func (*Query[T]) GetSliceInt added in v0.9.5

func (q *Query[T]) GetSliceInt() ([]int64, QueryResult)

↓↓ more Get examples ↓↓

func (*Query[T]) GetSliceString added in v0.9.5

func (q *Query[T]) GetSliceString() ([]string, QueryResult)

func (*Query[T]) GetSliceUint added in v0.9.5

func (q *Query[T]) GetSliceUint() ([]uint64, QueryResult)

func (*Query[T]) GetString added in v0.9.1

func (q *Query[T]) GetString() (string, QueryResult)

get first string

func (*Query[T]) GetTo added in v0.3.0

func (q *Query[T]) GetTo(destPtr any) QueryResult

destPtr, pointer of any value like: value, []value, map[key]value, map[key][]value

func (*Query[T]) GetUint added in v0.9.5

func (q *Query[T]) GetUint() (uint64, QueryResult)

func (*Query[T]) Gets added in v0.3.0

func (q *Query[T]) Gets(primaryIds ...any) ([]T, QueryResult)

get slice T

func (*Query[T]) GroupBy

func (q *Query[T]) GroupBy(columns ...any) *Query[T]

should not use group by after order by

func (*Query[T]) Having

func (q *Query[T]) Having(column any, vals ...any) *Query[T]

func (*Query[T]) Insert

func (q *Query[T]) Insert(data ...T) QueryResult

insert and set primary for first T

func (*Query[T]) InsertSubquery added in v0.3.0

func (q *Query[T]) InsertSubquery(data *SubQuery) QueryResult

func (*Query[T]) Join

func (q *Query[T]) Join(table Table, where func(join *Query[T]) *Query[T], alias ...string) *Query[T]

func (*Query[T]) LeftJoin

func (q *Query[T]) LeftJoin(table Table, where func(join *Query[T]) *Query[T], alias ...string) *Query[T]

func (*Query[T]) Limit

func (q *Query[T]) Limit(limit int) *Query[T]

func (*Query[T]) NewDefault added in v1.0.4

func (q *Query[T]) NewDefault() T

func (*Query[T]) Offset

func (q *Query[T]) Offset(offset int) *Query[T]

func (*Query[T]) OnConflictUpdate added in v0.9.3

func (q *Query[T]) OnConflictUpdate(column any, val any, columnVars ...any) *Query[T]

func (*Query[T]) OnConflictUpdates added in v0.9.3

func (q *Query[T]) OnConflictUpdates(columnVars map[any]any) *Query[T]

func (*Query[T]) OrHaving added in v0.9.3

func (q *Query[T]) OrHaving(column any, vals ...any) *Query[T]

func (*Query[T]) OrWhere

func (q *Query[T]) OrWhere(column any, vals ...any) *Query[T]

"id=1" &obj.id, 1 &obj.id, "=", 1

func (*Query[T]) OrWhereBetween added in v0.9.1

func (q *Query[T]) OrWhereBetween(column any, valLess, valGreat any) *Query[T]

func (*Query[T]) OrWhereFunc added in v0.3.0

func (q *Query[T]) OrWhereFunc(f func(where *Query[T]) *Query[T]) *Query[T]

"id=1" &obj.id, 1 &obj.id, "=", 1

func (*Query[T]) OrWherePrimary added in v0.7.2

func (q *Query[T]) OrWherePrimary(operator any, vals ...any) *Query[T]

short for OrWhere(primaryKey, vals...)

func (*Query[T]) OrderBy

func (q *Query[T]) OrderBy(column any) *Query[T]

func (*Query[T]) OrderByDesc

func (q *Query[T]) OrderByDesc(column any) *Query[T]

func (*Query[T]) OuterJoin added in v0.7.0

func (q *Query[T]) OuterJoin(table Table, where func(join *Query[T]) *Query[T], alias ...string) *Query[T]

func (*Query[T]) PartitionBy added in v0.4.0

func (q *Query[T]) PartitionBy(column any) *Query[T]

func (*Query[T]) Raw added in v0.3.0

func (q *Query[T]) Raw(prepareSql string, bindings ...any) *Query[T]

func (*Query[T]) RightJoin

func (q *Query[T]) RightJoin(table Table, where func(join *Query[T]) *Query[T], alias ...string) *Query[T]

func (*Query[T]) Select

func (q *Query[T]) Select(columns ...any) *Query[T]

func (*Query[T]) SelectAs added in v1.0.6

func (q *Query[T]) SelectAs(column any, as string) *Query[T]

func (*Query[T]) SelectExclude added in v0.9.3

func (q *Query[T]) SelectExclude(exceptColumns ...any) *Query[T]

func (*Query[T]) SelectOver added in v0.5.2

func (q *Query[T]) SelectOver(windowFunc string, f func(query *Query[T]) *Query[T], alias string) *Query[T]

func (*Query[T]) SelectOverRaw added in v0.5.3

func (q *Query[T]) SelectOverRaw(windowFunc string, windowName string, alias string) *Query[T]

func (*Query[T]) SelectRank added in v0.4.0

func (q *Query[T]) SelectRank(column any, alias string) *Query[T]

func (*Query[T]) SelectRankDesc added in v0.5.2

func (q *Query[T]) SelectRankDesc(column any, alias string) *Query[T]

func (*Query[T]) SelectRowNumber added in v0.9.1

func (q *Query[T]) SelectRowNumber(column any, alias string) *Query[T]

func (*Query[T]) SelectRowNumberDesc added in v0.9.1

func (q *Query[T]) SelectRowNumberDesc(column any, alias string) *Query[T]

func (*Query[T]) SelectWithTimeout added in v0.7.6

func (q *Query[T]) SelectWithTimeout(duration time.Duration) *Query[T]

func (*Query[T]) SubQuery added in v0.3.0

func (q *Query[T]) SubQuery() *SubQuery

func (*Query[T]) Transaction

func (q *Query[T]) Transaction(f func(query *Query[T]) error) error

func (*Query[T]) Tx added in v0.8.4

func (q *Query[T]) Tx() *sql.Tx

func (*Query[T]) Union added in v0.5.1

func (q *Query[T]) Union(subquery *SubQuery) *Query[T]

func (*Query[T]) UnionAll added in v0.5.1

func (q *Query[T]) UnionAll(subquery *SubQuery) *Query[T]

func (*Query[T]) Update

func (q *Query[T]) Update(column any, val any, columnVars ...any) QueryResult

func (*Query[T]) Updates

func (q *Query[T]) Updates(columnVars map[any]any) QueryResult

func (*Query[T]) UseDB

func (q *Query[T]) UseDB(db ...*sql.DB) *Query[T]

func (*Query[T]) UseFirstDB added in v1.0.4

func (q *Query[T]) UseFirstDB() *Query[T]

func (*Query[T]) UseTx

func (q *Query[T]) UseTx(tx *sql.Tx) *Query[T]

func (*Query[T]) Where

func (q *Query[T]) Where(column any, vals ...any) *Query[T]

"id=1" &obj.id, 1 &obj.id, "=", 1

func (*Query[T]) WhereBetween added in v0.9.1

func (q *Query[T]) WhereBetween(column any, valLess, valGreat any) *Query[T]

func (*Query[T]) WhereFunc added in v0.3.0

func (q *Query[T]) WhereFunc(f func(where *Query[T]) *Query[T]) *Query[T]

"id=1" &obj.id, 1 &obj.id, "=", 1

func (*Query[T]) WherePrimary added in v0.7.2

func (q *Query[T]) WherePrimary(operator any, vals ...any) *Query[T]

short for Where(primaryKey, vals...)

func (*Query[T]) WherePrimaryIfNotZero added in v0.9.3

func (q *Query[T]) WherePrimaryIfNotZero(val any) *Query[T]

func (*Query[T]) WithChildrenOnColumn added in v0.5.1

func (q *Query[T]) WithChildrenOnColumn(pidColumn any) *Query[T]

func (*Query[T]) WithContext added in v0.7.6

func (q *Query[T]) WithContext(ctx context.Context) *Query[T]

func (*Query[T]) WithCte added in v0.5.1

func (q *Query[T]) WithCte(subquery *SubQuery, cteName string, columns ...string) *Query[T]

func (*Query[T]) WithParentsOnColumn added in v0.5.1

func (q *Query[T]) WithParentsOnColumn(pidColumn any) *Query[T]

func (*Query[T]) WithRecursiveCte added in v0.5.1

func (q *Query[T]) WithRecursiveCte(subquery *SubQuery, cteName string, columns ...string) *Query[T]

func (*Query[T]) WithWindow added in v0.5.1

func (q *Query[T]) WithWindow(subquery *SubQuery, windowName string) *Query[T]

type QueryResult

type QueryResult struct {
	PrepareSql   string
	Bindings     []any
	LastInsertId int64
	RowsAffected int64
	Err          error
}

func (QueryResult) Error added in v0.3.0

func (q QueryResult) Error() error

func (QueryResult) Sql

func (q QueryResult) Sql() string

type Raw

type Raw string

type SelectForUpdateType added in v0.4.0

type SelectForUpdateType string
const (
	SelectForUpdateTypeDefault    SelectForUpdateType = "for update"
	SelectForUpdateTypeNowait     SelectForUpdateType = "for update nowait"
	SelectForUpdateTypeSkipLocked SelectForUpdateType = "for update skip locked"
)

type SubQuery added in v0.3.0

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

func (SubQuery) Connections added in v0.8.5

func (m SubQuery) Connections() []*sql.DB

func (SubQuery) DatabaseName added in v0.3.0

func (m SubQuery) DatabaseName() string

func (SubQuery) Error added in v0.3.0

func (m SubQuery) Error() error

func (SubQuery) Sql added in v0.5.3

func (m SubQuery) Sql() string

func (SubQuery) TableName added in v0.3.0

func (m SubQuery) TableName() string

type Table

type Table interface {
	Connections() []*sql.DB
	DatabaseName() string
	TableName() string
}

type WhereOperator

type WhereOperator Raw
const (
	WhereEqual          WhereOperator = "="
	WhereNotEqual       WhereOperator = "!="
	WhereGreatThan      WhereOperator = ">"
	WhereGreaterOrEqual WhereOperator = ">="
	WhereLessThan       WhereOperator = "<"
	WhereLessOrEqual    WhereOperator = "<="
	WhereIn             WhereOperator = "in"
	WhereNotIn          WhereOperator = "not in"
	WhereLike           WhereOperator = "like"
	WhereNotLike        WhereOperator = "not like"
	WhereRlike          WhereOperator = "rlike"
	WhereNotRlike       WhereOperator = "not rlike"
	WhereIsNull         WhereOperator = "is null"
	WhereIsNotNull      WhereOperator = "is not null"
)

Jump to

Keyboard shortcuts

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