sqlx

package module
v2.2.5 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2018 License: MIT Imports: 12 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"`
	// 姓名
	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.MySQLTimestamp `db:"F_created_at,default='0'"`
	UpdatedAt datatypes.MySQLTimestamp `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 DBErr

func DBErr(err error) *dbErr

func Scan

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

Types

type DB

type DB struct {
	*Database
	builder.Dialect
	SqlExecutor
}

func (*DB) Begin

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

func (*DB) Commit

func (d *DB) Commit() error

func (*DB) ExecExpr

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

func (*DB) IsTx

func (d *DB) IsTx() bool

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) WithSchema added in v2.2.2

func (d DB) WithSchema(schema string) *DB

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

func (database *Database) Assignments(model builder.Model, zeroFields ...string) builder.Assignments

func (*Database) FieldValuesFromModel

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

func (*Database) Insert

func (database *Database) Insert(model builder.Model, zeroFields []string, additions ...builder.Addition) builder.SqlExpr

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 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 {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
}

type Task

type Task func(db *DB) error

func (Task) Run

func (task Task) Run(db *DB) (err error)

type Tasks

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

func NewTasks

func NewTasks(db *DB) *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