postgres

package
v0.0.0-...-dd21589 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2022 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Param

type Param struct {
	Host         string `yaml:"host"`
	Port         uint   `yaml:"port"`
	User         string `yaml:"user"`
	Password     string `yaml:"password"`
	Dbname       string `yaml:"dbname"`
	TablePrefix  string `yaml:"table_prefix"`
	MaxIdleConns int    `yaml:"max_idle_conns"`
	MaxOpenConns int    `yaml:"max_open_conns"`
	Debug        bool   `yaml:"debug"`
}

func (*Param) New

func (c *Param) New(pgdb *PgDB) (*PgDB, error)

type PgDB

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

+ioc:autowire=true +ioc:autowire:type=normal +ioc:autowire:type=singleton +ioc:autowire:paramType=Param +ioc:autowire:constructFunc=New

func GetPgDB

func GetPgDB(p *Param) (*PgDB, error)

func GetPgDBSingleton

func GetPgDBSingleton(p *Param) (*PgDB, error)

func (*PgDB) AddError

func (db *PgDB) AddError(err error) error

AddError add error to db

func (*PgDB) Assign

func (db *PgDB) Assign(attrs ...interface{}) PgDBIOCInterface

func (*PgDB) Association

func (db *PgDB) Association(column string) *gorm.Association

func (*PgDB) Attrs

func (db *PgDB) Attrs(attrs ...interface{}) PgDBIOCInterface

func (*PgDB) AutoMigrate

func (db *PgDB) AutoMigrate(dst ...interface{}) error

func (*PgDB) Begin

func (db *PgDB) Begin(opts ...*sql.TxOptions) PgDBIOCInterface

Begin begins a transaction

func (*PgDB) Clauses

func (db *PgDB) Clauses(conds ...clause.Expression) PgDBIOCInterface

Clauses Add clauses

func (*PgDB) Commit

func (db *PgDB) Commit() PgDBIOCInterface

Commit commit a transaction

func (*PgDB) Connection

func (db *PgDB) Connection(fc func(db *gorm.DB) error) (err error)

Connection use a db conn to execute Multiple commands,this conn will put conn pool after it is executed.

func (*PgDB) Count

func (db *PgDB) Count(count *int64) PgDBIOCInterface

func (*PgDB) Create

func (db *PgDB) Create(value interface{}) PgDBIOCInterface

Create insert the value into database

func (*PgDB) CreateInBatches

func (db *PgDB) CreateInBatches(value interface{}, batchSize int) PgDBIOCInterface

CreateInBatches insert the value in batches into database

func (*PgDB) DB

func (db *PgDB) DB() (*sql.DB, error)

DB returns `*sql.DB`

func (*PgDB) Debug

func (db *PgDB) Debug() PgDBIOCInterface

Debug start debug mode

func (*PgDB) Delete

func (db *PgDB) Delete(value interface{}, conds ...interface{}) PgDBIOCInterface

Delete delete value match given conditions, if the value has primary key, then will including the primary key as condition

func (*PgDB) Distinct

func (db *PgDB) Distinct(args ...interface{}) PgDBIOCInterface

Distinct specify distinct fields that you want querying

func (*PgDB) Error

func (db *PgDB) Error() error

func (*PgDB) Exec

func (db *PgDB) Exec(sql string, values ...interface{}) PgDBIOCInterface

Exec execute raw sql

func (*PgDB) Find

func (db *PgDB) Find(dest interface{}, conds ...interface{}) PgDBIOCInterface

Find find records that match given conditions

func (*PgDB) FindInBatches

func (db *PgDB) FindInBatches(dest interface{}, batchSize int, fc func(tx *gorm.DB, batch int) error) PgDBIOCInterface

FindInBatches find records in batches

func (*PgDB) First

func (db *PgDB) First(dest interface{}, conds ...interface{}) PgDBIOCInterface

First find first record that match given conditions, order by primary key

func (*PgDB) FirstOrCreate

func (db *PgDB) FirstOrCreate(dest interface{}, conds ...interface{}) PgDBIOCInterface

FirstOrCreate gets the first matched record or create a new one with given conditions (only works with struct, map conditions)

func (*PgDB) FirstOrInit

func (db *PgDB) FirstOrInit(dest interface{}, conds ...interface{}) PgDBIOCInterface

FirstOrInit gets the first matched record or initialize a new instance with given conditions (only works with struct or map conditions)

func (*PgDB) Get

func (db *PgDB) Get(key string) (interface{}, bool)

Get get value with key from current db instance's context

func (*PgDB) GetDB

func (db *PgDB) GetDB() *gorm.DB

Debug start debug mode

func (*PgDB) Group

func (db *PgDB) Group(name string) PgDBIOCInterface

Group specify the group method on the find

func (*PgDB) Having

func (db *PgDB) Having(query interface{}, args ...interface{}) PgDBIOCInterface

Having specify HAVING conditions for GROUP BY

func (*PgDB) InstanceGet

func (db *PgDB) InstanceGet(key string) (interface{}, bool)

InstanceGet get value with key from current db instance's context

func (*PgDB) InstanceSet

func (db *PgDB) InstanceSet(key string, value interface{}) PgDBIOCInterface

InstanceSet store value with key into current db instance's context

func (*PgDB) Joins

func (db *PgDB) Joins(query string, args ...interface{}) PgDBIOCInterface

Joins specify Joins conditions

db.Joins("Account").Find(&user)
db.Joins("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "jinzhu@example.org").Find(&user)
db.Joins("Account", DB.Select("id").Where("user_id = users.id AND name = ?", "someName").Model(&Account{}))

func (*PgDB) Last

func (db *PgDB) Last(dest interface{}, conds ...interface{}) PgDBIOCInterface

Last find last record that match given conditions, order by primary key

func (*PgDB) Limit

func (db *PgDB) Limit(limit int) PgDBIOCInterface

Limit specify the number of records to be retrieved

func (*PgDB) Migrator

func (db *PgDB) Migrator() gorm.Migrator

func (*PgDB) Model

func (db *PgDB) Model(value interface{}) PgDBIOCInterface

Model specify the model you would like to run db operations

// update all users's name to `hello`
db.Model(&User{}).Update("name", "hello")
// if user's primary key is non-blank, will use it as condition, then will only update the user's name to `hello`
db.Model(&user).Update("name", "hello")

func (*PgDB) Not

func (db *PgDB) Not(query interface{}, args ...interface{}) PgDBIOCInterface

Not add NOT conditions

func (*PgDB) Offset

func (db *PgDB) Offset(offset int) PgDBIOCInterface

Offset specify the number of records to skip before starting to return the records

func (*PgDB) Omit

func (db *PgDB) Omit(columns ...string) PgDBIOCInterface

Omit specify fields that you want to ignore when creating, updating and querying

func (*PgDB) Or

func (db *PgDB) Or(query interface{}, args ...interface{}) PgDBIOCInterface

Or add OR conditions

func (*PgDB) Order

func (db *PgDB) Order(value interface{}) PgDBIOCInterface

Order specify order when retrieve records from database

db.Order("name DESC")
db.Order(clause.OrderByColumn{Column: clause.Column{Name: "name"}, Desc: true})

func (*PgDB) Pluck

func (db *PgDB) Pluck(column string, dest interface{}) PgDBIOCInterface

Pluck used to query single column from a model as a map

var ages []int64
db.Model(&users).Pluck("age", &ages)

func (*PgDB) Preload

func (db *PgDB) Preload(query string, args ...interface{}) PgDBIOCInterface

Preload preload associations with given conditions

db.Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)

func (*PgDB) Raw

func (db *PgDB) Raw(sql string, values ...interface{}) PgDBIOCInterface

func (*PgDB) Rollback

func (db *PgDB) Rollback()

Rollback rollback a transaction

func (*PgDB) RollbackTo

func (db *PgDB) RollbackTo(name string) PgDBIOCInterface

func (*PgDB) Row

func (db *PgDB) Row() *sql.Row

func (*PgDB) Rows

func (db *PgDB) Rows() (*sql.Rows, error)

func (*PgDB) Save

func (db *PgDB) Save(value interface{}) PgDBIOCInterface

Save update value in database, if the value doesn't have primary key, will insert it

func (*PgDB) SavePoint

func (db *PgDB) SavePoint(name string) PgDBIOCInterface

func (*PgDB) Scan

func (db *PgDB) Scan(dest interface{}) PgDBIOCInterface

Scan scan value to a struct

func (*PgDB) ScanRows

func (db *PgDB) ScanRows(rows *sql.Rows, dest interface{}) error

func (*PgDB) Scopes

func (db *PgDB) Scopes(funcs ...func(db *gorm.DB) *gorm.DB) PgDBIOCInterface

Scopes pass current database connection to arguments `func(DB) DB`, which could be used to add conditions dynamically

func AmountGreaterThan1000(db *gorm.DB) *gorm.DB {
    return db.Where("amount > ?", 1000)
}

func OrderStatus(status []string) func (db *gorm.DB) *gorm.DB {
    return func (db *gorm.DB) *gorm.DB {
        return db.Scopes(AmountGreaterThan1000).Where("status in (?)", status)
    }
}

db.Scopes(AmountGreaterThan1000, OrderStatus([]string{"paid", "shipped"})).Find(&orders)

func (*PgDB) Select

func (db *PgDB) Select(query interface{}, args ...interface{}) PgDBIOCInterface

Select specify fields that you want when querying, creating, updating

func (*PgDB) Session

func (db *PgDB) Session(config *gorm.Session) PgDBIOCInterface

Session create new db session

func (*PgDB) Set

func (db *PgDB) Set(key string, value interface{}) PgDBIOCInterface

Set store value with key into current db instance's context

func (*PgDB) SetupJoinTable

func (db *PgDB) SetupJoinTable(model interface{}, field string, joinTable interface{}) error

SetupJoinTable setup join table schema

func (*PgDB) Table

func (db *PgDB) Table(name string, args ...interface{}) PgDBIOCInterface

Table specify the table you would like to run db operations

func (*PgDB) Take

func (db *PgDB) Take(dest interface{}, conds ...interface{}) PgDBIOCInterface

Take return a record that match given conditions, the order will depend on the database implementation

func (*PgDB) ToSQL

func (db *PgDB) ToSQL(queryFn func(tx *gorm.DB) *gorm.DB) string

func (*PgDB) Transaction

func (db *PgDB) Transaction(fc func(db *gorm.DB) error, opts ...*sql.TxOptions) (err error)

Transaction start a transaction as a block, return error will rollback, otherwise to commit.

func (*PgDB) Unscoped

func (db *PgDB) Unscoped() PgDBIOCInterface

func (*PgDB) Update

func (db *PgDB) Update(column string, value interface{}) PgDBIOCInterface

Update update attributes with callbacks, refer: https://gorm.io/docs/update.html#Update-Changed-Fields

func (*PgDB) UpdateColumn

func (db *PgDB) UpdateColumn(column string, value interface{}) PgDBIOCInterface

func (*PgDB) UpdateColumns

func (db *PgDB) UpdateColumns(values interface{}) PgDBIOCInterface

func (*PgDB) Updates

func (db *PgDB) Updates(values interface{}) PgDBIOCInterface

Updates update attributes with callbacks, refer: https://gorm.io/docs/update.html#Update-Changed-Fields

func (*PgDB) Use

func (db *PgDB) Use(plugin gorm.Plugin) error

Use use plugin

func (*PgDB) Where

func (db *PgDB) Where(query interface{}, args ...interface{}) PgDBIOCInterface

Where add conditions

func (*PgDB) WithContext

func (db *PgDB) WithContext(ctx context.Context) PgDBIOCInterface

WithContext change current instance db's context to ctx

type PgDBIOCInterface

type PgDBIOCInterface interface {
	Session(config *gorm.Session) PgDBIOCInterface
	WithContext(ctx contextx.Context) PgDBIOCInterface
	Debug() PgDBIOCInterface
	GetDB() *gorm.DB
	Set(key string, value interface{}) PgDBIOCInterface
	Get(key string) (interface{}, bool)
	InstanceSet(key string, value interface{}) PgDBIOCInterface
	InstanceGet(key string) (interface{}, bool)
	AddError(err error) error
	DB() (*sql.DB, error)
	SetupJoinTable(model interface{}, field string, joinTable interface{}) error
	Use(plugin gorm.Plugin) error
	ToSQL(queryFn func(tx *gorm.DB) *gorm.DB) string
	Model(value interface{}) PgDBIOCInterface
	Clauses(conds ...clause.Expression) PgDBIOCInterface
	Table(name string, args ...interface{}) PgDBIOCInterface
	Distinct(args ...interface{}) PgDBIOCInterface
	Select(query interface{}, args ...interface{}) PgDBIOCInterface
	Omit(columns ...string) PgDBIOCInterface
	Where(query interface{}, args ...interface{}) PgDBIOCInterface
	Not(query interface{}, args ...interface{}) PgDBIOCInterface
	Or(query interface{}, args ...interface{}) PgDBIOCInterface
	Joins(query string, args ...interface{}) PgDBIOCInterface
	Group(name string) PgDBIOCInterface
	Having(query interface{}, args ...interface{}) PgDBIOCInterface
	Order(value interface{}) PgDBIOCInterface
	Limit(limit int) PgDBIOCInterface
	Offset(offset int) PgDBIOCInterface
	Scopes(funcs ...func(db *gorm.DB) *gorm.DB) PgDBIOCInterface
	Preload(query string, args ...interface{}) PgDBIOCInterface
	Attrs(attrs ...interface{}) PgDBIOCInterface
	Assign(attrs ...interface{}) PgDBIOCInterface
	Unscoped() PgDBIOCInterface
	Raw(sql string, values ...interface{}) PgDBIOCInterface
	Error() error
	Create(value interface{}) PgDBIOCInterface
	CreateInBatches(value interface{}, batchSize int) PgDBIOCInterface
	Save(value interface{}) PgDBIOCInterface
	First(dest interface{}, conds ...interface{}) PgDBIOCInterface
	Take(dest interface{}, conds ...interface{}) PgDBIOCInterface
	Last(dest interface{}, conds ...interface{}) PgDBIOCInterface
	Find(dest interface{}, conds ...interface{}) PgDBIOCInterface
	FindInBatches(dest interface{}, batchSize int, fc func(tx *gorm.DB, batch int) error) PgDBIOCInterface
	FirstOrInit(dest interface{}, conds ...interface{}) PgDBIOCInterface
	FirstOrCreate(dest interface{}, conds ...interface{}) PgDBIOCInterface
	Update(column string, value interface{}) PgDBIOCInterface
	Updates(values interface{}) PgDBIOCInterface
	UpdateColumn(column string, value interface{}) PgDBIOCInterface
	UpdateColumns(values interface{}) PgDBIOCInterface
	Delete(value interface{}, conds ...interface{}) PgDBIOCInterface
	Count(count *int64) PgDBIOCInterface
	Row() *sql.Row
	Rows() (*sql.Rows, error)
	Scan(dest interface{}) PgDBIOCInterface
	Pluck(column string, dest interface{}) PgDBIOCInterface
	ScanRows(rows *sql.Rows, dest interface{}) error
	Connection(fc func(db *gorm.DB) error) (err error)
	Transaction(fc func(db *gorm.DB) error, opts ...*sql.TxOptions) (err error)
	Begin(opts ...*sql.TxOptions) PgDBIOCInterface
	Commit() PgDBIOCInterface
	Rollback()
	SavePoint(name string) PgDBIOCInterface
	RollbackTo(name string) PgDBIOCInterface
	Exec(sql string, values ...interface{}) PgDBIOCInterface
	Migrator() gorm.Migrator
	AutoMigrate(dst ...interface{}) error
	Association(column string) *gorm.Association
}

func GetPgDBIOCInterface

func GetPgDBIOCInterface(p *Param) (PgDBIOCInterface, error)

func GetPgDBIOCInterfaceSingleton

func GetPgDBIOCInterfaceSingleton(p *Param) (PgDBIOCInterface, error)

Jump to

Keyboard shortcuts

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