postgres

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2024 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateDatabaseIfNotExist

func CreateDatabaseIfNotExist(conf orm.Conf)

func FromDB

func FromDB(db *gorm.DB, conf orm.Conf) orm.DB

Types

type PgDB

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

func NewPgDB

func NewPgDB(conf orm.Conf) (*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{}) orm.DB

func (*PgDB) Association

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

func (*PgDB) Attrs

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

func (*PgDB) AutoMigrate

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

func (*PgDB) Begin

func (db *PgDB) Begin(opts ...*sql.TxOptions) orm.DB

Begin begins a transaction

func (*PgDB) Clauses

func (db *PgDB) Clauses(conds ...clause.Expression) orm.DB

Clauses Add clauses

func (*PgDB) Commit

func (db *PgDB) Commit() orm.DB

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) orm.DB

func (*PgDB) Create

func (db *PgDB) Create(value interface{}) orm.DB

Create insert the value into database

func (*PgDB) CreateInBatches

func (db *PgDB) CreateInBatches(value interface{}, batchSize int) orm.DB

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() orm.DB

Debug start debug mode

func (*PgDB) Delete

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

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{}) orm.DB

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{}) orm.DB

Exec execute raw sql

func (*PgDB) Find

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

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) orm.DB

FindInBatches find records in batches

func (*PgDB) First

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

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

func (*PgDB) FirstOrCreate

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

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{}) orm.DB

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) GetConf

func (db *PgDB) GetConf() orm.Conf

func (*PgDB) GetDB

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

Debug start debug mode

func (*PgDB) Group

func (db *PgDB) Group(name string) orm.DB

Group specify the group method on the find

func (*PgDB) Having

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

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{}) orm.DB

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

func (*PgDB) Joins

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

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{}) orm.DB

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

func (*PgDB) Limit

func (db *PgDB) Limit(limit int) orm.DB

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{}) orm.DB

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{}) orm.DB

Not add NOT conditions

func (*PgDB) Offset

func (db *PgDB) Offset(offset int) orm.DB

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

func (*PgDB) Omit

func (db *PgDB) Omit(columns ...string) orm.DB

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

func (*PgDB) Or

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

Or add OR conditions

func (*PgDB) Order

func (db *PgDB) Order(value interface{}) orm.DB

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{}) orm.DB

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{}) orm.DB

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{}) orm.DB

func (*PgDB) Rollback

func (db *PgDB) Rollback()

Rollback rollback a transaction

func (*PgDB) RollbackTo

func (db *PgDB) RollbackTo(name string) orm.DB

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{}) orm.DB

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) orm.DB

func (*PgDB) Scan

func (db *PgDB) Scan(dest interface{}) orm.DB

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) orm.DB

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{}) orm.DB

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

func (*PgDB) Session

func (db *PgDB) Session(config *gorm.Session) orm.DB

Session create new db session

func (*PgDB) Set

func (db *PgDB) Set(key string, value interface{}) orm.DB

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{}) orm.DB

Table specify the table you would like to run db operations

func (*PgDB) Take

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

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() orm.DB

func (*PgDB) Update

func (db *PgDB) Update(column string, value interface{}) orm.DB

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{}) orm.DB

func (*PgDB) UpdateColumns

func (db *PgDB) UpdateColumns(values interface{}) orm.DB

func (*PgDB) Updates

func (db *PgDB) Updates(values interface{}) orm.DB

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{}) orm.DB

Where add conditions

func (*PgDB) WithContext

func (db *PgDB) WithContext(ctx context.Context) orm.DB

WithContext change current instance db's context to ctx

Jump to

Keyboard shortcuts

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