postgres

package module
v0.12.4 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2022 License: Apache-2.0 Imports: 16 Imported by: 5

README

Postgres ORM

Features

  • mapping use json in one request
  • commons query functions
  • support virtual column
  • support conflict column

Usage

Define object reference mapping
type Bar struct {
	Id    string `col:"ID,pk"`
	Name  string `col:"NAME"`
	FooId string `col:"FOO_ID"`
}

func (t Bar) TableName() (string, string) {
	return "schema", "bar_table_name"
}

type Foo struct {
    Id       string    `col:"ID,pk"`
    CreateBY string    `col:"CREATE_BY,acb"`
    CreateAT time.Time `col:"CREATE_AT,act"`
    ModifyBY string    `col:"MODIFY_BY,amb"`
    ModifyAT time.Time `col:"MODIFY_AT,amt"`
    DeleteBY string    `col:"DELETE_BY,adb"`
    DeleteAT time.Time `col:"DELETE_AT,adt"`
    Version  int64     `col:"VERSION,aol"`
    Name     string    `col:"NAME,+conflict"`
    Integer  int       `col:"INTEGER"`
    Double   float64   `col:"DOUBLE"`
    Bool     bool      `col:"BOOL"`
    Time     time.Time `col:"TIME"`
    JsonRaw  *Sample   `col:"JSON_RAW,json"`
    Bars     []*Baz    `col:"BAZS,links,ID+FOO_ID,ID DESC,0:10"`
    BarsNum  int       `col:"BARS_NUM,vc,SELECT COUNT(1) FROM \"schema\".\"bar_table_name\" WHERE \"FOO_ID\" = \"schema\".\"foo_table_name\".\"ID\"`
}

func (t Foo) TableName() (string, string) {
    return "schema", "foo_table_name"
}
CRUD
postgres.Insert(ctx, &foo{})
postgres.Query(ctx, postgres.NewConditions(postgres.Eq("id", "FOO")), &[]*foo{})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeginTransaction added in v0.12.1

func BeginTransaction(ctx context.Context) (err errors.CodeError)

func CommitTransaction added in v0.12.1

func CommitTransaction(ctx context.Context) (err errors.CodeError)

func Count

func Count(ctx context.Context, cond *Conditions, table Table) (v int, err errors.CodeError)

func Delete

func Delete(ctx context.Context, row interface{}) (err errors.CodeError)

func ExecuteContext added in v0.12.1

func ExecuteContext(ctx context.Context, query string, args ...interface{}) (affected int64, lastInsertId int64, err errors.CodeError)

func Exist

func Exist(ctx context.Context, cond *Conditions, table Table) (has bool, err errors.CodeError)

func Insert

func Insert(ctx context.Context, row interface{}) (err errors.CodeError)

func InsertOrUpdate

func InsertOrUpdate(ctx context.Context, row interface{}) (err errors.CodeError)

func InsertWhenExist

func InsertWhenExist(ctx context.Context, row interface{}, source Select) (err errors.CodeError)

func InsertWhenNotExist

func InsertWhenNotExist(ctx context.Context, row interface{}, source Select) (err errors.CodeError)

func LitValue

func LitValue(v string) *lit

func Modify

func Modify(ctx context.Context, row interface{}) (err errors.CodeError)

func Query

func Query(ctx context.Context, cond *Conditions, rows interface{}) (fetched bool, err errors.CodeError)

func QueryDirect

func QueryDirect(ctx context.Context, rows interface{}, query string, args ...interface{}) (fetched bool, err error)

func QueryOne

func QueryOne(ctx context.Context, cond *Conditions, row interface{}) (fetched bool, err errors.CodeError)

func QueryWithRange

func QueryWithRange(ctx context.Context, cond *Conditions, orders *Orders, rng *Range, rows interface{}) (fetched bool, err errors.CodeError)

func RollbackTransaction added in v0.12.1

func RollbackTransaction(ctx context.Context) (err errors.CodeError)

Types

type Condition

type Condition struct {
	Column    string
	Operation string
	Values    []interface{}
}

func Between

func Between(column string, left interface{}, right interface{}) *Condition

func Eq

func Eq(column string, value interface{}) *Condition

func GT

func GT(column string, value interface{}) *Condition

func GTE

func GTE(column string, value interface{}) *Condition

func IN

func IN(column string, value interface{}) *Condition

func LT

func LT(column string, value interface{}) *Condition

func LTE

func LTE(column string, value interface{}) *Condition

func Like

func Like(column string, value string) *Condition

func LikeLeft

func LikeLeft(column string, value string) *Condition

func LikeRight

func LikeRight(column string, value string) *Condition

func NotDeleted

func NotDeleted(deletedByColumnName string) *Condition

func NotEq

func NotEq(column string, value interface{}) *Condition

func NotIn added in v0.11.2

func NotIn(column string, value interface{}) *Condition

type Conditions

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

func NewConditions

func NewConditions(cond *Condition) *Conditions

func (*Conditions) And

func (c *Conditions) And(v *Condition) *Conditions

func (*Conditions) AndConditions

func (c *Conditions) AndConditions(v *Conditions) *Conditions

func (*Conditions) Or

func (c *Conditions) Or(v *Condition) *Conditions

func (*Conditions) OrConditions

func (c *Conditions) OrConditions(v *Conditions) *Conditions

func (*Conditions) QueryAndArguments

func (c *Conditions) QueryAndArguments() (query string, args []interface{})

type LoadMakeupHook added in v0.10.2

type LoadMakeupHook interface {
	Makeup(ctx context.Context) (err errors.CodeError)
}

type Order

type Order struct {
	Column string
	Desc   bool
}

func Asc

func Asc(column string) *Order

func Desc

func Desc(column string) *Order

type Orders

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

func NewOrders

func NewOrders() *Orders

func (*Orders) Asc

func (o *Orders) Asc(column string) *Orders

func (*Orders) Desc

func (o *Orders) Desc(column string) *Orders

type PageInfo

type PageInfo struct {
	No    int
	Num   int
	Total int
}

func Page

func Page(ctx context.Context, cond *Conditions, orders *Orders, pageNo int, pageSize int, rows interface{}) (page *PageInfo, err errors.CodeError)

func (*PageInfo) Empty

func (p *PageInfo) Empty() bool

type Range

type Range struct {
	Offset int
	Limit  int
}

func NewRange

func NewRange(offset int, limit int) *Range

type Row added in v0.12.1

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

func (*Row) Column added in v0.12.1

func (row *Row) Column(name string, value interface{}) (has bool, err error)

func (*Row) Empty added in v0.12.1

func (row *Row) Empty() (ok bool)

func (*Row) Scan added in v0.12.1

func (row *Row) Scan(ctx context.Context, v interface{}) (err error)

type Rows added in v0.12.1

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

func QueryContext added in v0.12.1

func QueryContext(ctx context.Context, query string, args ...interface{}) (rows *Rows, err errors.CodeError)

func (*Rows) Empty added in v0.12.1

func (rows *Rows) Empty() (ok bool)

func (*Rows) Next added in v0.12.1

func (rows *Rows) Next() (v *Row, has bool)

func (*Rows) Scan added in v0.12.1

func (rows *Rows) Scan(ctx context.Context, v interface{}) (err error)

func (*Rows) Size added in v0.12.1

func (rows *Rows) Size() int

type Select

type Select interface {
	Build(args []interface{}) (query string)
}

func LitSelect

func LitSelect(query string) Select

type Table

type Table interface {
	TableName() (schema string, table string)
}

Jump to

Keyboard shortcuts

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