crud

package
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 31 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// TableCols table columns map
	TableCols = cmap.NewCMap()
)

Functions

func GetColParamSQL

func GetColParamSQL(p *Parses) (sql string)

GetColParamSQL get col ?

func GetColSQL

func GetColSQL(model any) (sql string)

GetColSQL select * replace

func GetColSQLAlias

func GetColSQLAlias(model any, alias string) (sql string)

GetColSQLAlias select * replace add alias

func GetKeySQL

func GetKeySQL(key string, model any, alias string) (sqlKey string, argsKey []any)

GetKeySQL key search sql

func GetMoreColSQL

func GetMoreColSQL(model any, tables ...string) (sql string)

GetMoreColSQL select * replace select more tables : table name / table alias name first table must main table, like from a inner join b, first table is a

func GetMoreKeySQL

func GetMoreKeySQL(key string, model any, tables ...string) (sqlKey string, argsKey []any)

GetMoreKeySQL more tables key search sql tables [table1:table1_alias] searModel : Model

func ParseTable

func ParseTable(table string) string

ParseTable table parse

func StructWhereSQL

func StructWhereSQL(st any) (sql string, args []any)

StructWhereSQL struct to where sql return key1 = value1 and key2 = value2...

func TableOnly

func TableOnly(table string) string

TableOnly only table name

func TagTables

func TagTables(tag string, tables ...string) (tbs []string)

TagTables return tag tables

func UniqueTagTable

func UniqueTagTable(tag string, tables ...string) string

UniqueTagTable return unique tag table

Types

type Config

type Config struct {
	SlowThreshold time.Duration
	Colorful      bool
	LogLevel      gormLog.LogLevel
}

type Crud

type Crud interface {
	// Init init crud
	Init(*Params)
	// DB db
	DB() *DB
	// Params new/replace param
	// return param
	Params(...Param) Crud

	Count() Crud          // count
	Find(cmap.CMap) Crud  // find data
	FindM(cmap.CMap) Crud // find data more table no search

	// Delete delete by id/ids/slice
	Delete(...any) Crud // delete

	// Update crud and search id
	// json data
	Update() Crud // update
	Create() Crud // create (more), include res insert id

	Select(any, ...any) Crud // select sql
	From(string) Crud        // from sql, if use search, From must only once
	Group(string) Crud       // the last group by
	FindS(cmap.CMap) Crud    // Select origin sql find, params only support Pager and Mock
	Scan() Crud              // no search
	Exec() Crud              // exec insert/update/delete sql
	Error() error            // crud error
	RowsAffected() int64     // inflect rows
	Pager() result.Pager     // search pager
	Begin() Crud             // start a transaction
	Commit() Crud            // commit a transaction
	Rollback() Crud          // rollback a transaction
	SavePoint(string) Crud   // save a point
	RollbackTo(string) Crud  // rollback to point
}

Crud interface

func NewCrud

func NewCrud(params ...Param) (crud Crud)

NewCrud new crud 设计模式--建造者模式

func NewCusCrud

func NewCusCrud(db *gorm.DB, log bool, params ...Param) (crud Crud)

NewCusCrud new your custom db crud

type CrudGo added in v1.2.3

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

CrudGo implement Crud

func (*CrudGo) Begin added in v1.2.3

func (c *CrudGo) Begin() Crud

func (*CrudGo) Commit added in v1.2.3

func (c *CrudGo) Commit() Crud

func (*CrudGo) Count added in v1.2.3

func (c *CrudGo) Count() Crud

func (*CrudGo) Create added in v1.2.3

func (c *CrudGo) Create() Crud

func (*CrudGo) DB added in v1.2.3

func (c *CrudGo) DB() *DB

func (*CrudGo) Delete added in v1.2.3

func (c *CrudGo) Delete(conds ...any) Crud

func (*CrudGo) Error added in v1.2.3

func (c *CrudGo) Error() error

func (*CrudGo) Exec added in v1.2.3

func (c *CrudGo) Exec() Crud

func (*CrudGo) Find added in v1.2.3

func (c *CrudGo) Find(p cmap.CMap) Crud

func (*CrudGo) FindM added in v1.2.3

func (c *CrudGo) FindM(params cmap.CMap) Crud

func (*CrudGo) FindS added in v1.2.3

func (c *CrudGo) FindS(params cmap.CMap) Crud

func (*CrudGo) From added in v1.2.3

func (c *CrudGo) From(query string) Crud

func (*CrudGo) Group added in v1.2.3

func (c *CrudGo) Group(query string) Crud

func (*CrudGo) Init added in v1.2.3

func (c *CrudGo) Init(param *Params)

func (*CrudGo) Pager added in v1.2.3

func (c *CrudGo) Pager() result.Pager

func (*CrudGo) Params added in v1.2.3

func (c *CrudGo) Params(params ...Param) Crud

func (*CrudGo) Rollback added in v1.2.3

func (c *CrudGo) Rollback() Crud

func (*CrudGo) RollbackTo added in v1.2.3

func (c *CrudGo) RollbackTo(name string) Crud

func (*CrudGo) RowsAffected added in v1.2.3

func (c *CrudGo) RowsAffected() int64

func (*CrudGo) SavePoint added in v1.2.3

func (c *CrudGo) SavePoint(name string) Crud

func (*CrudGo) Scan added in v1.2.3

func (c *CrudGo) Scan() Crud

func (*CrudGo) Select added in v1.2.3

func (c *CrudGo) Select(q any, args ...any) Crud

func (*CrudGo) Update added in v1.2.3

func (c *CrudGo) Update() Crud

type DB

type DB struct {
	// db driver
	*gorm.DB
	// contains filtered or unexported fields
}

DB tool

func (*DB) Create

func (db *DB) Create(table string, data any)

Create single/array

func (*DB) CreateMore

func (db *DB) CreateMore(table string, model any, data any)

CreateMore data must array type more data create single table also can use Create array

func (*DB) Delete

func (db *DB) Delete(gt *GT, conds ...any)

func (*DB) Find

func (db *DB) Find(gt *GT) (pager result.Pager)

func (*DB) FindM

func (db *DB) FindM(gt *GT) (pager result.Pager)

FindM no search params: innerTables is inner join tables params: leftTables is left join tables return search info table1 as main table, include other tables_id(foreign key)

func (*DB) FindS

func (db *DB) FindS(gt *GT) (pager result.Pager)

FindS select sql search

func (*DB) InitColumns

func (db *DB) InitColumns(param *Params)

InitColumns init db table columns map

func (*DB) NewDB

func (db *DB) NewDB()

NewDB new db driver

func (*DB) Update

func (db *DB) Update(gt *GT)

type GT

type GT struct {
	*Params
	// CMap
	CMaps cmap.CMap // params

	Select string // select sql
	From   string // only once
	Group  string // the last group
	Args   []any
	// contains filtered or unexported fields
}

GT SQL struct

func (*GT) GetMoreSQL

func (gt *GT) GetMoreSQL()

GetMoreSQL more table params: innerTables is inner join tables, must even number params: leftTables is left join tables return: select sql table1 as main table, include other tables_id(foreign key)

func (*GT) GetSQL

func (gt *GT) GetSQL()

GetSQL get single sql

func (*GT) GetSelectSQL

func (gt *GT) GetSelectSQL()

GetSelectSQL select sql

type Key

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

type Param

type Param func(*Params)

func Data

func Data(Data any) Param

func Distinct

func Distinct(Distinct string) Param

Distinct inner/left support distinct

func Inner

func Inner(InnerTables ...string) Param

func KeyModel

func KeyModel(KeyModel any) Param

func Left

func Left(LeftTable ...string) Param

func Model

func Model(Model any) Param

func SubSQL

func SubSQL(SubSQL ...string) Param

func Table

func Table(Table string) Param

func Valid

func Valid(valid bool) Param

func WhereSQL

func WhereSQL(WhereSQL string, args ...any) Param

WhereSQL where sql and args

func (Param) WhereSQL

func (p Param) WhereSQL(WhereSQL string, args ...any) Param

type Params

type Params struct {
	// attributes
	InnerTable []string // inner join tables
	LeftTable  []string // left join tables
	Table      string   // table name
	Model      any      // table model, like User{}
	KeyModel   any      // key like model
	Data       any      // table model data, like var user User{}, it is 'user', it store real data

	// sub query
	SubSQL string // SubQuery SQL
	// where
	WhereSQL string // Where SQL
	// contains filtered or unexported fields
}

Params crud params

type Parses

type Parses struct {
	Table  string // main table
	Key    string
	Tags   []string
	Vs     []any
	OTags  map[string]string
	TagTb  map[string]string // part: tag->tb
	TagTag map[string]string // part: tag->tag
}

func (*Parses) Marshal

func (r *Parses) Marshal(v string)

func (*Parses) String

func (r *Parses) String() string

Directories

Path Synopsis
dep
tag

Jump to

Keyboard shortcuts

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