sqlx

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2024 License: MIT Imports: 11 Imported by: 2

README

Sqlx

GoDoc Widget 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

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

func DBErr

func DBErr(err error) *dbErr

func FieldValuesFromModel

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

func InsertToDB

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

func Scan

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

func UnwrapAll

func UnwrapAll(err error) error

func UnwrapOnce

func UnwrapOnce(err error) (cause 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

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

func (*DB) Commit

func (d *DB) Commit() error

func (*DB) Context

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

func (*DB) D

func (d *DB) D() *Database

func (*DB) Dialect

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

func (*DB) ExecExpr

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

func (*DB) Generate added in v1.0.2

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

func (*DB) IsTx

func (d *DB) IsTx() bool

func (*DB) Migrate

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

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

func (DB) WithSchema

func (d DB) WithSchema(schema string) DBExecutor

type DBExecutor

type DBExecutor interface {
	SqlxExecutor
	TableResolver

	// Dialect of databases
	Dialect() builder.Dialect
	// D return database which is connecting
	D() *Database
	// WithSchema switch database schema
	WithSchema(schema string) DBExecutor

	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

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

type Generator added in v1.0.2

type Generator interface {
	Generate(ctx context.Context, db DBExecutor) error
}

type MaybeTxExecutor

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

type Migrator

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

type ScanIterator

type ScanIterator = scanner.ScanIterator

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

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 TableResolver

type TableResolver interface {
	// T return table of the connecting database
	T(model builder.Model) *builder.Table
}

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