dac

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: Apache-2.0 Imports: 14 Imported by: 6

README

DAC

It is a database access layer for sql.

Define

Table

Define a struct which implements dac.Table.
Examples:
User table.

type User struct {
	Id          string                  `column:"ID,PK"`
	CreateBY    string                  `column:"CREATE_BY,ACB"`
	CreateAT    time.Time               `column:"CREATE_AT,ACT"`
	ModifyBY    string                  `column:"MODIFY_BY,AMB"`
	ModifyAT    time.Time               `column:"MODIFY_AT,AMT"`
	DeleteBY    string                  `column:"DELETE_BY,ADB"`
	DeleteAT    time.Time               `column:"DELETE_AT,ADT"`
	Version     int64                   `column:"VERSION,AOL"`
	Nickname    string                  `column:"NICKNAME"`
	Mobile      string                  `column:"MOBILE"`
	Gender      string                  `column:"GENDER"`
	Birthday    time.Time               `column:"BIRTHDAY"`
	Avatar      sql.NullJson[Avatar]    `column:"AVATAR,json"`
	BD          times.Date              `column:"BD"`
	BT          times.Time              `column:"BT"`
}

func (row User) TableInfo() dac.TableInfo {
	return dac.Info("USER", dac.Schema("FNS"))
}

User avatar json typed column

// Avatar
// json column
type Avatar struct {
	Schema      string `json:"schema"`
	Domain      string `json:"domain"`
	Path        string `json:"path"`
	MimeType    string `json:"mimeType"`
	URL         string `json:"url"`
}

Post table.
Author column is a reference column, which means many post to one user.
Comments column is a links column, which means one post to many comments.
Links columns is a virtual column, which means that column is select from another source.

type Post struct {
	Id          string          `column:"ID,pk"`
	CreateBY    string          `column:"CREATE_BY,ACB"`
	CreateAT    time.Time       `column:"CREATE_AT,act"`
	Version     int64           `column:"VERSION,aol"`
	Author      User            `column:"AUTHOR,ref,Id"`
	Title       string          `column:"TITLE"`
	Content     string          `column:"CONTENT"`
	Comments    []PostComment   `column:"COMMENTS,links,Id+PostId,orders:Id@desc,length:10"`
	Likes       int64           `column:"LIKES,vc,basic,SELECT COUNT(1) FROM \"FNS\".\"POST_LIKE\" WHERE \"POST_ID\" = \"FNS\".\"POST\".\"ID\""`
}

func (row Post) TableInfo() dac.TableInfo {
	return dac.Info("POST", dac.Schema("FNS"))
}

Comment table.

type PostComment struct {
	Id       int64      `column:"ID,pk,incr"`
	PostId   string     `column:"POST_ID"`
	User     User       `column:"USER_ID,ref,Id"`
	CreateAT time.Time  `column:"CREATE_AT,act"`
	Content  string     `column:"CONTENT"`
}

func (row PostComment) TableInfo() dac.TableInfo {
	return dac.Info("POST_COMMENT", dac.Schema("FNS"))
}

Like table.

type PostLike struct {
	Id     int64  `column:"ID,pk,incr"`
	PostId string `column:"POST_ID"`
	UserId string `column:"USER_ID"`
}

func (row PostLike) TableInfo() dac.TableInfo {
	return dac.Info("POST_LIKE", dac.Schema("FNS"))
}
View

There are two kinds view, one is pure view, another is projection (used for group by).
Define a struct which implements dac.View. Example:
Count group genders.

type UserGenderCount struct {
	Gender string `column:"GENDER"` // group by column
	Count  int64  `column:"ID,vc,agg,COUNT"` // agg column
}

func (u UserGenderCount) ViewInfo() dac.ViewInfo {
	return dac.TableView(User{}) // projection of User
}
Column

Format of column tag is {column name | ident},{kind},{options of kind}.
Kinds:

  • normal: which is default.
  • pk: primary key, when it is increment, then add incr option, such as id,pk,incr.
  • acb: used for create_by column, only support int or string type.
  • act: used for create_at column, only support int or time type.
  • amb: used for modify_by column, only support int or string type.
  • amt: used for modify_at column, only support int or time type.
  • adb: used for delete_by column, only support int or string type.
  • adt: used for delete_at column, only support int or time type.
  • aol: used for version column, only support int type.
  • ref: used for many to one or one to one. first option is {target struct field name}.
  • link: used for one to one but host table has no target table column. first option is {host struct field name}+{target struct field name}.
  • links: used for one to many. first option is {host struct field name}+{target struct field name}, when use order, then add order option, such as orders:{field name}{@desc}. when use limit, then add limit option, such as length:{size}.
  • virtual: used for add out source column, first option is type of virtual.
    • basic type means the column value is basic value.
    • object type means the column value is one row which will be encoded by json.
    • array type means the column value is many rows which will be encoded by json.
    • agg type means the column value is result of aggregation.
Note
  • DON'T use ptr to implement Table or View.
  • Anonymous field is supported, but can not be ptr and must be exported.
  • When reference is not null, then use value, not use ptr, also as link.
  • Element of links slice should be value not ptr.
  • InsertOrUpdate only used for which table has conflict columns.
  • When dialect does not support returning, then InsertMulti is not fully worked.

Methods

  • Insert
  • InsertMulti
  • InsertOrUpdate
  • InsertWhenNotExist
  • InsertWhenExist
  • Update
  • UpdateFields
  • Delete
  • DeleteByCondition
  • Query
  • One
  • ALL
  • Views
  • ViewOne
  • ViewALL

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ALL

func ALL[T Table](ctx context.Context, options ...QueryOption) (entries []T, err error)

func Asc

func Asc(name string) orders.Orders

func Begin

func Begin(ctx context.Context, options ...databases.TransactionOption) (err error)

func Between

func Between(field string, left any, right any) conditions.Condition

func Bool

func Bool(b bool) conditions.Literal

func Commit

func Commit(ctx context.Context) (err error)

func Count

func Count[T Table](ctx context.Context, cond conditions.Condition) (count int64, err error)

func Delete

func Delete[T Table](ctx context.Context, entry T) (v T, ok bool, err error)

func DeleteByCondition

func DeleteByCondition[T Table](ctx context.Context, cond conditions.Condition) (affected int64, err error)

func Desc

func Desc(name string) orders.Orders

func Dialect

func Dialect(ctx context.Context) (dialect string, err error)

func Disuse

func Disuse(ctx context.Context) context.Context

func Eq

func Eq(field string, expression any) conditions.Condition

func Exist

func Exist[T Table](ctx context.Context, cond conditions.Condition) (has bool, err error)

func Float

func Float(f float32) conditions.Literal

func Float64

func Float64(f float64) conditions.Literal

func ForceDialect

func ForceDialect(ctx context.Context, dialect string) context.Context

func Gt

func Gt(field string, expression any) conditions.Condition

func Gte

func Gte(field string, expression any) conditions.Condition

func In

func In(field string, expression ...any) conditions.Condition

func Insert

func Insert[T Table](ctx context.Context, entry T) (v T, ok bool, err error)

func InsertMulti

func InsertMulti[T Table](ctx context.Context, entries []T) (affected int64, err error)

func InsertOrUpdate

func InsertOrUpdate[T Table](ctx context.Context, entry T) (v T, ok bool, err error)

func InsertWhenExist

func InsertWhenExist[T Table](ctx context.Context, entry T, source conditions.QueryExpr) (v T, ok bool, err error)

func InsertWhenNotExist

func InsertWhenNotExist[T Table](ctx context.Context, entry T, source conditions.QueryExpr) (v T, ok bool, err error)

func Int

func Int(n int) conditions.Literal

func Int64

func Int64(n int64) conditions.Literal

func Like

func Like(field string, expression string) conditions.Condition

func LikeContains

func LikeContains(field string, expression string) conditions.Condition

func LikeLast

func LikeLast(field string, expression string) conditions.Condition

func Lit

func Lit(v string) conditions.Literal

func LitSubQuery

func LitSubQuery(query string) conditions.QueryExpr

func Lt

func Lt(field string, expression any) conditions.Condition

func Lte

func Lte(field string, expression any) conditions.Condition

func Named

func Named(name string, value any) sql.NamedArg

func NotEq

func NotEq(field string, expression any) conditions.Condition

func NotIn

func NotIn(field string, expression ...any) conditions.Condition

func One

func One[T Table](ctx context.Context, options ...QueryOption) (entry T, has bool, err error)

func Query

func Query[T Table](ctx context.Context, offset int, length int, options ...QueryOption) (entries []T, err error)

func Rollback

func Rollback(ctx context.Context)

func String

func String(s string) conditions.Literal

func SubQuery

func SubQuery(query any, field string, cond conditions.Condition) conditions.QueryExpr

func Time

func Time(t time.Time) conditions.Literal

func Tree

func Tree[T Table](ctx context.Context, options ...QueryOption) (entry T, err error)

func Trees

func Trees[T Table](ctx context.Context, options ...QueryOption) (entries []T, err error)

func Update

func Update[T Table](ctx context.Context, entry T) (v T, ok bool, err error)

func UpdateFields

func UpdateFields[T Table](ctx context.Context, fields FieldValues, cond conditions.Condition) (affected int64, err error)

func Use

func Use(ctx context.Context, endpointName []byte) context.Context

func ViewALL

func ViewALL[V View](ctx context.Context, options ...QueryOption) (entries []V, err error)

func ViewOne

func ViewOne[V View](ctx context.Context, options ...QueryOption) (entry V, has bool, err error)

func Views

func Views[V View](ctx context.Context, offset int, length int, options ...QueryOption) (entries []V, err error)

Types

type Audit

func NewAudit

func NewAudit(id string) Audit

func (Audit) WithCreation

func (audit Audit) WithCreation(id string) Audit

func (Audit) WithDeletion

func (audit Audit) WithDeletion(id string) Audit

func (Audit) WithModification

func (audit Audit) WithModification(id string) Audit

type AuditCreation

type AuditCreation[Id ~string | ~int64] struct {
	CreateBY Id        `column:"CREATE_BY,ACB" json:"createBY"`
	CreateAT time.Time `column:"CREATE_AT,ACT" json:"createAT"`
}

func NewAuditCreation

func NewAuditCreation[Id ~string | ~int64](id Id) AuditCreation[Id]

type AuditDeletion

type AuditDeletion[Id ~string | ~int64] struct {
	DeleteBY Id        `column:"DELETE_BY,ADB" json:"deleteBY"`
	DeleteAT time.Time `column:"DELETE_AT,ADT" json:"deleteAT"`
}

func NewAuditDeletion

func NewAuditDeletion[Id ~string | ~int64](id Id) AuditDeletion[Id]

type AuditModification

type AuditModification[Id ~string | ~int64] struct {
	ModifyBY Id        `column:"MODIFY_BY,AMB" json:"modifyBY"`
	ModifyAT time.Time `column:"MODIFY_AT,AMT" json:"modifyAT"`
}

func NewAuditModification

func NewAuditModification[Id ~string | ~int64](id Id) AuditModification[Id]

type AuditVersion

type AuditVersion struct {
	Version int64 `column:"VERSION,AOL" json:"version"`
}

type AuditWithoutDeletion

type AuditWithoutDeletion struct {
	UID
	AuditCreation[string]
	AuditModification[string]
	AuditVersion
}

func NewAuditWithoutDeletion

func NewAuditWithoutDeletion(id string) AuditWithoutDeletion

func (AuditWithoutDeletion) WithCreation

func (audit AuditWithoutDeletion) WithCreation(id string) AuditWithoutDeletion

func (AuditWithoutDeletion) WithModification

func (audit AuditWithoutDeletion) WithModification(id string) AuditWithoutDeletion

type AuditWithoutModificationAndDeletion

type AuditWithoutModificationAndDeletion struct {
	UID
	AuditCreation[string]
}

func NewAuditWithoutModificationAndDeletion

func NewAuditWithoutModificationAndDeletion(id string) AuditWithoutModificationAndDeletion

func (AuditWithoutModificationAndDeletion) WithCreation

type FieldValues

type FieldValues []specifications.FieldValue

func Field

func Field(name string, value any) FieldValues

func (FieldValues) Field

func (fields FieldValues) Field(name string, value any) FieldValues

type IncrPkAudit

func NewAuditWithIncrPk

func NewAuditWithIncrPk(id int64) IncrPkAudit

func (IncrPkAudit) WithCreation

func (audit IncrPkAudit) WithCreation(id int64) IncrPkAudit

func (IncrPkAudit) WithDeletion

func (audit IncrPkAudit) WithDeletion(id int64) IncrPkAudit

func (IncrPkAudit) WithModification

func (audit IncrPkAudit) WithModification(id int64) IncrPkAudit

type IncrPkAuditWithoutDeletion

type IncrPkAuditWithoutDeletion struct {
	SID
	AuditCreation[int64]
	AuditModification[int64]
	AuditVersion
}

func NewIncrPkAuditWithoutDeletion

func NewIncrPkAuditWithoutDeletion(id int64) IncrPkAuditWithoutDeletion

func (IncrPkAuditWithoutDeletion) WithCreation

func (IncrPkAuditWithoutDeletion) WithModification

func (audit IncrPkAuditWithoutDeletion) WithModification(id int64) IncrPkAuditWithoutDeletion

type IncrPkAuditWithoutModificationAndDeletion

type IncrPkAuditWithoutModificationAndDeletion struct {
	SID
	AuditCreation[int64]
}

func NewIncrPkAuditWithoutModificationAndDeletion

func NewIncrPkAuditWithoutModificationAndDeletion(id int64) IncrPkAuditWithoutModificationAndDeletion

func (IncrPkAuditWithoutModificationAndDeletion) WithCreation

type Pager

type Pager[T Table] struct {
	// No
	// @title no
	// @description no of page
	No int `json:"no"`
	// Pages
	// @title pages
	// @description total pages
	Pages int64 `json:"pages"`
	// Total
	// @title total
	// @description total entries
	Total int64 `json:"total"`
	// Entries
	// @title entries
	// @description entries of page
	Entries []T `json:"entries"`
}

func Page

func Page[T Table](ctx context.Context, no int, size int, options ...QueryOption) (page Pager[T], err error)

type QueryOption

type QueryOption func(options *QueryOptions)

func Conditions

func Conditions(cond conditions.Condition) QueryOption

func GroupBy

func GroupBy(by groups.GroupBy) QueryOption

func Orders

func Orders(orders orders.Orders) QueryOption

type QueryOptions

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

type SID

type SID struct {
	Id int64 `column:"ID,PK,INCR" json:"id"`
}

type Table

type Table interface {
	TableInfo() TableInfo
}

Table the recv of TableInfo method must be value, can not be ptr

type TableInfo

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

func Info

func Info(name string, options ...TableInfoOption) TableInfo

func (TableInfo) Conflicts

func (info TableInfo) Conflicts() []string

func (TableInfo) Name

func (info TableInfo) Name() string

func (TableInfo) Schema

func (info TableInfo) Schema() string

type TableInfoOption

type TableInfoOption func(options *TableInfoOptions)

func Conflicts

func Conflicts(conflicts ...string) TableInfoOption

Conflicts params are field not column

func Schema

func Schema(schema string) TableInfoOption

type TableInfoOptions

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

type UID

type UID struct {
	Id string `column:"ID,PK" json:"id"`
}

func NewUID

func NewUID() UID

type View

type View interface {
	ViewInfo() ViewInfo
}

type ViewInfo

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

func PureView

func PureView(name string, schema ...string) ViewInfo

func TableView

func TableView(table Table) ViewInfo

func (ViewInfo) Base

func (info ViewInfo) Base() Table

func (ViewInfo) Pure

func (info ViewInfo) Pure() (string, string, bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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