sql

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IDatabase

type IDatabase interface {
	dbx.IDatabase

	// 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)
	// Refer https://jinzhu.github.io/gorm/crud.html#scopes
	Scopes(funcs ...func(IDatabase) IDatabase) IDatabase

	// Unscoped return all record including deleted record, refer Soft Delete https://jinzhu.github.io/gorm/crud.html#soft-delete
	Unscoped() IDatabase

	// 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")
	Model(value interface{}) ITable

	// Table specifies the table you would like to run db operations. Alias 'T'
	Table(name string) ITable

	// T specifies the table you would like to run db operations. Alias 'Table'
	T(name string) ITable

	// Debug starts debug mode
	Debug()

	// AddForeignKey Add foreign key to the given scope, e.g:
	//     db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT")
	AddForeignKey(field string, dest string, onDelete string, onUpdate string) error

	// RemoveForeignKey Remove foreign key from the given scope, e.g:
	//     db.Model(&User{}).RemoveForeignKey("city_id", "cities(id)")
	RemoveForeignKey(field string, dest string) error

	Db() *gorm.DB
}

func Dial

func Dial(cfg *dbx.DbConfig) (IDatabase, error)

Dial establishes a new session to the cluster identified by the given seed server(s). The session will enable communication with all of the servers in the cluster, so the seed servers are used only to find out about the cluster topology.

func FromDB

func FromDB(db *gorm.DB, isClone bool) IDatabase

type IQuery

type IQuery interface {
	dbx.IQuery

	// Group specify the group method on the find
	Group(query string) IQuery

	// Having specify HAVING conditions for GROUP BY
	Having(query interface{}, values ...interface{}) IQuery

	// Joins specify Joins conditions
	//     db.Joins("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "jinzhu@example.org").Find(&user)
	Joins(query string, args ...interface{}) IQuery

	// Attrs initialize struct with argument if record not found with `FirstOrInit` https://jinzhu.github.io/gorm/crud.html#firstorinit or `FirstOrCreate` https://jinzhu.github.io/gorm/crud.html#firstorcreate
	Attrs(attrs ...interface{}) IQuery

	Assign(attrs ...interface{}) IQuery

	// Take return a record that match given conditions, the order will depend on the database implementation
	Take(out interface{}, where ...interface{}) IQuery

	// Scan scan value to a struct
	Scan(dest interface{}) IQuery

	// FirstOrInit find first matched record or initialize a new one with given conditions (only works with struct, map conditions)
	// https://jinzhu.github.io/gorm/crud.html#firstorinit
	FirstOrInit(out interface{}, where ...interface{}) IQuery

	// FirstOrCreate find first matched record or create a new one with given conditions (only works with struct, map conditions)
	// https://jinzhu.github.io/gorm/crud.html#firstorcreate
	FirstOrCreate(out interface{}, where ...interface{}) IQuery

	// UpdateColumn update attributes without callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
	UpdateColumn(attrs ...interface{}) IQuery

	// UpdateColumns update attributes without callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
	UpdateColumns(values interface{}) IQuery

	// Row return `*sql.Row` with given conditions
	Row() *sql.Row

	// Rows return `*sql.Rows` with given conditions
	Rows() (*sql.Rows, error)

	Error() error
}

type ITable

type ITable interface {
	dbx.IRepository
	// Omit specify fields that you want to ignore when saving to database for creating, updating
	Omit(columns ...string) ITable

	// ScanRows scan `*sql.Rows` to give struct
	ScanRows(rows *sql.Rows, result interface{}) error

	// Pluck used to query single column from a model as a map
	//     var ages []int64
	//     db.Find(&users).Pluck("age", &ages)
	Pluck(column string, value interface{}) ITable

	// Related get related associations
	Related(foreignKeys ...string) ITable

	// Save update value in database, if the value doesn't have primary key, will insert it
	Save(value interface{}) error

	// ModifyColumn modify column to type
	ModifyColumn(column string, typ string) error

	// DropColumn drop a column
	DropColumn(column string) error

	// Association start `Association Mode` to handler relations things easir in that mode, refer: https://jinzhu.github.io/gorm/associations.html#association-mode
	Association(column string) *gorm.Association

	// Preload preload associations with given conditions
	//    db.Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
	Preload(column string, conditions ...interface{}) ITable
}

Jump to

Keyboard shortcuts

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