mysql

package module
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// NONE for None
	NONE = 0

	// Serialize Types
	JSON = 2
	YAML = 3

	// Index Types
	INDEX       = 1
	UNIQUE      = 2
	PRIMARY_KEY = 3
)

Variables

View Source
var (
	ErrInvalidData    = errors.New("invalid data")
	ErrNotFound       = errors.New("not found")
	ErrNoDataInfo     = errors.New("dataInfo is missing, call Reflect on Schema first")
	ErrNotReady       = errors.New("stmtInsert is not ready, call Prepare on Schema first")
	ErrNoPrimaryKey   = errors.New("no primary key")
	ErrNoRowsAffected = errors.New("no rows affected")
	ErrDuplicateKey   = errors.New("duplicate key")
)

Functions

func DeserializeField added in v0.1.0

func DeserializeField(t uint8, data string, v *interface{})

func PrepareStmt

func PrepareStmt(db *DB, query string) *sql.Stmt

func SerializeField added in v0.1.0

func SerializeField(t uint8, data interface{}) interface{}

Types

type Config

type Config struct {
	Addr     string `mapstructure:"address" json:"address" yaml:"address"`
	Username string `mapstructure:"username" json:"username" yaml:"username"`
	Password string `mapstructure:"password" json:"password" yaml:"password"`
	DataBase string `mapstructure:"database" json:"database" yaml:"database"`
}

type DB

type DB struct {
	Ctx *sql.DB
	// contains filtered or unexported fields
}

func NewDB

func NewDB(cfg *Config) (*DB, error)

func (*DB) Close

func (db *DB) Close() error

func (*DB) Exec added in v0.1.17

func (db *DB) Exec(ctx context.Context, query string, args ...interface{}) (int64, error)

func (*DB) Tx

func (db *DB) Tx(ctx context.Context, f func(context.Context, *sql.Tx) error) error

type Entity added in v0.1.0

type Entity[T interface{}] struct {
	// contains filtered or unexported fields
}

func GetEntity added in v0.1.0

func GetEntity[T interface{}, S interface{}](schema *Schema[S]) *Entity[T]

func (*Entity[T]) Select added in v0.1.0

func (ent *Entity[T]) Select(ctx context.Context, where string, args ...any) ([]*T, error)

func (*Entity[T]) SelectOne added in v0.1.0

func (ent *Entity[T]) SelectOne(ctx context.Context, where string, args ...any) (*T, error)

func (*Entity[T]) SelectPage added in v0.1.0

func (ent *Entity[T]) SelectPage(ctx context.Context, page_idx, page_size int64, where string, args ...any) ([]*T, int64, int64, int64, int64, error)

SelectPage selects a page of records from the database. page_idx shoud be 1-based. Return: records, current page index, page size, page_count, total count, error

type Field

type Field struct {
	// Basic information
	Name            string // Column name
	Type            string // Column type in SQL format
	IsPrimaryKey    bool
	IsAutoIncrement bool
	IsNullable      bool
	IsUnsigned      bool
	DefaultValue    string // Default value in SQL format
	Comment         string
	SerializeMethod uint8 // json | yaml | none
	Indices         []*FieldIndexDecl

	EntityIndex int // Index in the entity
}

func (*Field) CompleteWithType added in v0.1.0

func (fd *Field) CompleteWithType(structField reflect.StructField)

func (*Field) Equal

func (fd *Field) Equal(other *Field) bool

func (*Field) FromTag added in v0.1.0

func (fd *Field) FromTag(tag string, structField reflect.StructField)

type FieldIndexDecl added in v0.1.0

type FieldIndexDecl struct {
	IndexType uint8  // pk | index | unique
	IndexName string // index name
}

type Index

type Index struct {
	Name    string
	Columns []string
	Primary bool
	Unique  bool
}

func (*Index) Equal

func (idx *Index) Equal(other *Index) bool

type Schema

type Schema[T interface{}] struct {
	Name           string
	Fields         []*Field
	Indices        []*Index
	Engine         string
	Collate        string
	Comment        string
	FieldsByColumn map[string]*Field
	// contains filtered or unexported fields
}

func NewSchema added in v0.1.0

func NewSchema[T interface{}](dbr *DB, dbw *DB, name string) *Schema[T]

func (*Schema[T]) Columns

func (sc *Schema[T]) Columns() []string

func (*Schema[T]) Count

func (sc *Schema[T]) Count(ctx context.Context, where string, args ...any) (int64, error)

Get record count from the database

func (*Schema[T]) Delete added in v0.0.2

func (sc *Schema[T]) Delete(ctx context.Context, where string, args ...any) (int64, error)

func (*Schema[T]) Field

func (sc *Schema[T]) Field(name string) *Field

func (*Schema[T]) Index

func (sc *Schema[T]) Index(name string) *Index

func (*Schema[T]) Insert

func (sc *Schema[T]) Insert(ctx context.Context, data *T) error

func (*Schema[T]) Select

func (sc *Schema[T]) Select(ctx context.Context, where string, args ...any) ([]*T, error)

func (*Schema[T]) SelectOne

func (sc *Schema[T]) SelectOne(ctx context.Context, where string, args ...any) (*T, error)

func (*Schema[T]) SelectPage added in v0.1.2

func (sc *Schema[T]) SelectPage(ctx context.Context, page_idx, page_size int64, where string, args ...any) ([]*T, int64, int64, int64, int64, error)

SelectPage selects a page of records from the database. page_idx shoud be 1-based. Return: records, current page index, page size, page count, total count, error

func (*Schema[T]) Update

func (sc *Schema[T]) Update(ctx context.Context, data *T, columns ...string) (int64, error)

Jump to

Keyboard shortcuts

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