sqlx

package module
v2.20.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2020 License: MIT Imports: 13 Imported by: 6

README

Sqlx

GoDoc Widget Build Status codecov Go Report Card

Sql helpers just for mysql(5.7+)/postgres(10+) and mysql/postgres-compatibility db.

// @def primary ID
// @def index I_nickname/BTREE Nickname
// @def index I_username Username
// @def index I_geom/SPATIAL Geom
// @def unique_index I_name Name
type User struct {
	ID uint64 `db:"f_id,autoincrement"`
	// 姓名
	NameNeedToDrop   string                   `db:"f_name_need_to_drop,deprecated"`
	OldName   string                          `db:"f_old_name,deprecated=f_name"`
	Name      string                          `db:"f_name,default=''"`
	Username  string                          `db:"f_username,default=''"`
	Nickname  string                          `db:"f_nickname,default=''"`
	Gender    Gender                          `db:"f_gender,default='0'"`
	Boolean   bool                            `db:"f_boolean,default=false"`
	Geom      GeomString                      `db:"f_geom"`
	CreatedAt datatypes.Timestamp             `db:"f_created_at,default='0'"`
	UpdatedAt datatypes.Timestamp             `db:"f_updated_at,default='0'"`
	Enabled   datatypes.Bool                  `db:"f_enabled,default='0'"`
}

type GeomString struct {
	V string
}

func (g GeomString) Value() (driver.Value, error) {
	return g.V, nil
}

func (g *GeomString) Scan(src interface{}) error {
	return nil
}

func (GeomString) DataType(driverName string) string {
	if driverName == "mysql" {
		return "geometry"
	}
	return "geometry(Point)"
}

func (GeomString) ValueEx() string {
	return "ST_GeomFromText(?)"
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DuplicateEntryErrNumber uint16 = 1062
View Source
var ErrNotDB = errors.New("db is not *sql.DB")
View Source
var ErrNotTx = errors.New("db is not *sql.Tx")

Functions

func AsAssignments added in v2.6.0

func AsAssignments(db DBExecutor, model builder.Model, zeroFields ...string) builder.Assignments

func DBErr

func DBErr(err error) *dbErr

func FieldValuesFromModel added in v2.6.0

func FieldValuesFromModel(table *builder.Table, model builder.Model, zeroFields ...string) builder.FieldValues

func InsertToDB added in v2.6.0

func InsertToDB(db DBExecutor, model builder.Model, zeroFields []string, additions ...builder.Addition) builder.SqlExpr

func Scan

func Scan(rows *sql.Rows, v interface{}) error

Types

type DB

type DB struct {
	*Database
	SqlExecutor
	// contains filtered or unexported fields
}

func (*DB) Begin

func (d *DB) Begin() (DBExecutor, error)

func (*DB) BeginTx added in v2.7.0

func (d *DB) BeginTx(opt *sql.TxOptions) (DBExecutor, error)

func (*DB) Commit

func (d *DB) Commit() error

func (*DB) Context added in v2.7.0

func (d *DB) Context() context.Context

func (*DB) D added in v2.6.0

func (d *DB) D() *Database

func (*DB) Dialect added in v2.6.0

func (d *DB) Dialect() builder.Dialect

func (*DB) ExecExpr

func (d *DB) ExecExpr(expr builder.SqlExpr) (sql.Result, error)

func (*DB) IsTx

func (d *DB) IsTx() bool

func (*DB) Migrate added in v2.6.0

func (d *DB) Migrate(ctx context.Context, db DBExecutor) error

func (*DB) QueryExpr

func (d *DB) QueryExpr(expr builder.SqlExpr) (*sql.Rows, error)

func (*DB) QueryExprAndScan

func (d *DB) QueryExprAndScan(expr builder.SqlExpr, v interface{}) error

func (*DB) Rollback

func (d *DB) Rollback() error

func (*DB) SetConnMaxLifetime

func (d *DB) SetConnMaxLifetime(t time.Duration)

func (*DB) SetMaxIdleConns

func (d *DB) SetMaxIdleConns(n int)

func (*DB) SetMaxOpenConns

func (d *DB) SetMaxOpenConns(n int)

func (*DB) WithContext added in v2.7.0

func (d *DB) WithContext(ctx context.Context) DBExecutor

func (DB) WithSchema added in v2.2.2

func (d DB) WithSchema(schema string) DBExecutor

type DBExecutor added in v2.6.0

type DBExecutor interface {
	SqlxExecutor

	// dialect of databases
	Dialect() builder.Dialect
	// return database which is connecting
	D() *Database
	// switch database schema
	WithSchema(schema string) DBExecutor
	// return table of the connecting database
	T(model builder.Model) *builder.Table

	Context() context.Context
	WithContext(ctx context.Context) DBExecutor
}

type DBNameBinder

type DBNameBinder interface {
	WithDBName(dbName string) driver.Connector
}

type Database

type Database struct {
	Name   string
	Schema string
	Tables builder.Tables
}

func NewDatabase

func NewDatabase(name string) *Database

func NewFeatureDatabase

func NewFeatureDatabase(name string) *Database

func (*Database) AddTable

func (database *Database) AddTable(table *builder.Table)

func (*Database) OpenDB

func (database *Database) OpenDB(connector driver.Connector) *DB

func (*Database) Register

func (database *Database) Register(model builder.Model) *builder.Table

func (*Database) T

func (database *Database) T(model builder.Model) *builder.Table

func (*Database) Table

func (database *Database) Table(tableName string) *builder.Table

func (Database) WithSchema added in v2.2.2

func (database Database) WithSchema(schema string) *Database

type MaybeTxExecutor added in v2.6.0

type MaybeTxExecutor interface {
	IsTx() bool
	BeginTx(*sql.TxOptions) (DBExecutor, error)
	Begin() (DBExecutor, error)
	Commit() error
	Rollback() error
}

type Migrator added in v2.6.0

type Migrator interface {
	Migrate(ctx context.Context, db DBExecutor) error
}

type ScanIterator added in v2.9.0

type ScanIterator interface {
	// new a ptr value for scan
	New() interface{}
	// for receive scanned value
	Next(v interface{}) error
}

type SqlError

type SqlError struct {
	Type sqlErrType
	Msg  string
}

func NewSqlError

func NewSqlError(tpe sqlErrType, msg string) *SqlError

func (*SqlError) Error

func (e *SqlError) Error() string

type SqlExecutor

type SqlExecutor interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}

type SqlxExecutor added in v2.6.0

type SqlxExecutor interface {
	SqlExecutor
	ExecExpr(expr builder.SqlExpr) (sql.Result, error)
	QueryExpr(expr builder.SqlExpr) (*sql.Rows, error)

	QueryExprAndScan(expr builder.SqlExpr, v interface{}) error
}

type Task

type Task func(db DBExecutor) error

func (Task) Run

func (task Task) Run(db DBExecutor) (err error)

type Tasks

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

func NewTasks

func NewTasks(db DBExecutor) *Tasks

func (*Tasks) Do

func (tasks *Tasks) Do() (err error)

func (Tasks) With

func (tasks Tasks) With(task ...Task) *Tasks

Jump to

Keyboard shortcuts

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