orm

package
v0.0.0-...-5df1a6f Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2018 License: MIT Imports: 15 Imported by: 0

README

ORM

=== 简化sql操作,提供基于schema,model,query组合

初始化

  • 使用sql.DB,内部不处理数据链接
executor := NewExecutor()
executor.Conn(conn)
  • 使用事务
executor.Session(func(conn Conn) error {
    executor.Query().Session(conn).Select()
})

or

tx, err := executor.Begin()
executor.Query().Session(tx).Select()
tx.Commit()
tx.Rollback()

基础查询

  • 基于Query,提供链式查询
query := executor.Query().Table().Field().Where().SelectString()
query := executor.Query().Sql("select * from table where name=?", []interface{}{"t"})
  • 便捷操作
query.Order().Asc().Desc().Limit(0,1).Page(1,20)
  • 增删改
query.Insert(data) // data可以是map[string]类型或者struct
query.Update(data) // data可以是map[string]类型或者struct
query.Delete()
  • 绑定结构体
query.Find(struct)  // struct为单条查询
query.Find(slice) // slice为多条查询
  • 参数绑定
type ttt struct{
    Name string
}
t := &ttt{
    Name: "t"
}
t := map[string]interface{}{Name:"t"}
query.Sql("select * from table where a = :Name", t).SelectString()

表结构

  • 根据struct确定表结构
model, err := NewModel("user", &Users{}, nil)

或者

schema, err := NewSchema(&Users{}, nil)
model := NewModelWithSchema("user", schema, nil)
  • model绑定数据链接,schema绑定表结构
  • 自定义表结构
schema.SetAutoIncrement("id")
schema.SoftDelete() // 开启软删除
schema.SetTimeStamps() // 开启时间更新
schema.SetPrimary() // 设置主键
  • 绑定事件
schema.On("beforeInsert", func)
schema.On("afterInsert", func)

模型查询

  • 绑定主键
model.PK() // 需要和schema中的Primary长度一致
  • 链式查询,支持Query的接口
model.PK().Get() // 查询主键记录
model.Query().Session(conn) // 绑定事务
model.Query().Save(data) // 实例
model.PK().SoftDelete() // 软删除
model.PK().Recovery() // 恢复删除
  • 绑定查询
query := executor.Query().Session(conn)
// 判断软删除
model.Find(query)
model.Count(query)
model.Update(query, data)
model.Delete(query)

model.FindByQuery(query)
model.CountQuery(query)
model.UpdateQuery(query, data)
model.DeleteQuery(query)

Documentation

Index

Constants

View Source
const (
	POSTGRES = "postgres"
	SQLITE   = "sqlite3"
	MYSQL    = "mysql"
	MSSQL    = "mssql"
	ORACLE   = "oracle"
)
View Source
const (
	UNKNOW_TYPE = iota
	TEXT_TYPE
	BLOB_TYPE
	TIME_TYPE
	NUMERIC_TYPE
)

Variables

View Source
var (
	Bit       = "BIT"
	TinyInt   = "TINYINT"
	SmallInt  = "SMALLINT"
	MediumInt = "MEDIUMINT"
	Int       = "INT"
	Integer   = "INTEGER"
	BigInt    = "BIGINT"

	Enum = "ENUM"
	Set  = "SET"

	Char       = "CHAR"
	Varchar    = "VARCHAR"
	NVarchar   = "NVARCHAR"
	TinyText   = "TINYTEXT"
	Text       = "TEXT"
	Clob       = "CLOB"
	MediumText = "MEDIUMTEXT"
	LongText   = "LONGTEXT"
	Uuid       = "UUID"

	Date       = "DATE"
	DateTime   = "DATETIME"
	Time       = "TIME"
	TimeStamp  = "TIMESTAMP"
	TimeStampz = "TIMESTAMPZ"

	Decimal = "DECIMAL"
	Numeric = "NUMERIC"

	Real   = "REAL"
	Float  = "FLOAT"
	Double = "DOUBLE"

	Binary     = "BINARY"
	VarBinary  = "VARBINARY"
	TinyBlob   = "TINYBLOB"
	Blob       = "BLOB"
	MediumBlob = "MEDIUMBLOB"
	LongBlob   = "LONGBLOB"
	Bytea      = "BYTEA"

	Bool    = "BOOL"
	Boolean = "BOOLEAN"

	Serial    = "SERIAL"
	BigSerial = "BIGSERIAL"

	Json  = "JSON"
	Jsonb = "JSONB"

	SqlTypes = map[string]int{
		Bit:       NUMERIC_TYPE,
		TinyInt:   NUMERIC_TYPE,
		SmallInt:  NUMERIC_TYPE,
		MediumInt: NUMERIC_TYPE,
		Int:       NUMERIC_TYPE,
		Integer:   NUMERIC_TYPE,
		BigInt:    NUMERIC_TYPE,

		Enum:  TEXT_TYPE,
		Set:   TEXT_TYPE,
		Json:  TEXT_TYPE,
		Jsonb: TEXT_TYPE,

		Char:       TEXT_TYPE,
		Varchar:    TEXT_TYPE,
		NVarchar:   TEXT_TYPE,
		TinyText:   TEXT_TYPE,
		Text:       TEXT_TYPE,
		MediumText: TEXT_TYPE,
		LongText:   TEXT_TYPE,
		Uuid:       TEXT_TYPE,
		Clob:       TEXT_TYPE,

		Date:       TIME_TYPE,
		DateTime:   TIME_TYPE,
		Time:       TIME_TYPE,
		TimeStamp:  TIME_TYPE,
		TimeStampz: TIME_TYPE,

		Decimal: NUMERIC_TYPE,
		Numeric: NUMERIC_TYPE,
		Real:    NUMERIC_TYPE,
		Float:   NUMERIC_TYPE,
		Double:  NUMERIC_TYPE,

		Binary:    BLOB_TYPE,
		VarBinary: BLOB_TYPE,

		TinyBlob:   BLOB_TYPE,
		Blob:       BLOB_TYPE,
		MediumBlob: BLOB_TYPE,
		LongBlob:   BLOB_TYPE,
		Bytea:      BLOB_TYPE,

		Bool: NUMERIC_TYPE,

		Serial:    NUMERIC_TYPE,
		BigSerial: NUMERIC_TYPE,
	}
)
View Source
var (
	IntType   = reflect.TypeOf(c_INT_DEFAULT)
	Int8Type  = reflect.TypeOf(c_INT8_DEFAULT)
	Int16Type = reflect.TypeOf(c_INT16_DEFAULT)
	Int32Type = reflect.TypeOf(c_INT32_DEFAULT)
	Int64Type = reflect.TypeOf(c_INT64_DEFAULT)

	UintType   = reflect.TypeOf(c_UINT_DEFAULT)
	Uint8Type  = reflect.TypeOf(c_UINT8_DEFAULT)
	Uint16Type = reflect.TypeOf(c_UINT16_DEFAULT)
	Uint32Type = reflect.TypeOf(c_UINT32_DEFAULT)
	Uint64Type = reflect.TypeOf(c_UINT64_DEFAULT)

	Float32Type = reflect.TypeOf(c_FLOAT32_DEFAULT)
	Float64Type = reflect.TypeOf(c_FLOAT64_DEFAULT)

	Complex64Type  = reflect.TypeOf(c_COMPLEX64_DEFAULT)
	Complex128Type = reflect.TypeOf(c_COMPLEX128_DEFAULT)

	StringType = reflect.TypeOf(c_EMPTY_STRING)
	BoolType   = reflect.TypeOf(c_BOOL_DEFAULT)
	ByteType   = reflect.TypeOf(c_BYTE_DEFAULT)
	BytesType  = reflect.SliceOf(ByteType)

	TimeType = reflect.TypeOf(c_TIME_DEFAULT)
)
View Source
var (
	PtrIntType   = reflect.PtrTo(IntType)
	PtrInt8Type  = reflect.PtrTo(Int8Type)
	PtrInt16Type = reflect.PtrTo(Int16Type)
	PtrInt32Type = reflect.PtrTo(Int32Type)
	PtrInt64Type = reflect.PtrTo(Int64Type)

	PtrUintType   = reflect.PtrTo(UintType)
	PtrUint8Type  = reflect.PtrTo(Uint8Type)
	PtrUint16Type = reflect.PtrTo(Uint16Type)
	PtrUint32Type = reflect.PtrTo(Uint32Type)
	PtrUint64Type = reflect.PtrTo(Uint64Type)

	PtrFloat32Type = reflect.PtrTo(Float32Type)
	PtrFloat64Type = reflect.PtrTo(Float64Type)

	PtrComplex64Type  = reflect.PtrTo(Complex64Type)
	PtrComplex128Type = reflect.PtrTo(Complex128Type)

	PtrStringType = reflect.PtrTo(StringType)
	PtrBoolType   = reflect.PtrTo(BoolType)
	PtrByteType   = reflect.PtrTo(ByteType)

	PtrTimeType = reflect.PtrTo(TimeType)
)

Functions

func MapToSlice

func MapToSlice(query string, mp interface{}) (string, []interface{}, error)

func StructToSlice

func StructToSlice(query string, st interface{}) (string, []interface{}, error)

Types

type CondQuery

type CondQuery interface {
	Table(table string) Query
	TableAlias(table string, alias string) Query
	Field(field string) Query
	FieldAlias(field string, alias string) Query
	Where(field string, operate string, value interface{}) Query
	Limit(start int, offset int) Query
	Page(page int, size int) Query
	Order(field string) Query
	Asc(field string) Query
	Desc(field string) Query
	Sql(query string, args interface{}) Query
}

type Conn

type Conn interface {
	Query(query string, args ...interface{}) (*sql.Rows, error)
	Exec(query string, args ...interface{}) (sql.Result, error)
}

type Conversion

type Conversion interface {
	FromDB([]byte) error
	ToDB() ([]byte, error)
}

type ExecQuery

type ExecQuery interface {
	Insert(data ...map[string]interface{}) (int64, error)
	Update(data map[string]interface{}) (int64, error)
	//InsertOrUpdate(update interface{}, data ...interface{}) (int64, error)
	Delete() (int64, error)

	Id(field string) Query
}

type Executor

type Executor interface {
	Conn(conn *sql.DB)
	ConnWithDriver(conn *sql.DB, driver string)
	Query() Query
	Session(f func(conn Conn) error) error
	Begin() (Transaction, error)
}

func DefaultExecutor

func DefaultExecutor() Executor

func NewExecutor

func NewExecutor() Executor

type Logger

type Logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Error(args ...interface{})
}

type Model

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

func NewModel

func NewModel(name string, record interface{}, executor *executor) (*Model, error)

func NewModelWithSchema

func NewModelWithSchema(name string, schema *Schema, executor *executor) *Model

func (*Model) Bind

func (m *Model) Bind(query Query) Query

func (*Model) Count

func (m *Model) Count(query Query) (int64, error)

func (*Model) CountByQuery

func (m *Model) CountByQuery(query Query) (int64, error)

func (*Model) Delete

func (m *Model) Delete(query Query) (int64, error)

func (*Model) DeleteByQuery

func (m *Model) DeleteByQuery(query Query) (int64, error)

func (*Model) Find

func (m *Model) Find(query Query) ([]interface{}, error)

func (*Model) FindByQuery

func (m *Model) FindByQuery(query Query) ([]interface{}, error)

func (*Model) Insert

func (m *Model) Insert(record interface{}) (int64, error)

func (*Model) One

func (m *Model) One(query Query) (interface{}, error)

func (*Model) PK

func (m *Model) PK(key ...interface{}) ModelQuery

func (*Model) Query

func (m *Model) Query() ModelQuery

func (*Model) Rows

func (m *Model) Rows(query Query) (Rows, error)

func (*Model) Update

func (m *Model) Update(query Query, record interface{}) (int64, error)

func (*Model) UpdateByQuery

func (m *Model) UpdateByQuery(query Query, record interface{}) (int64, error)

type ModelQuery

type ModelQuery interface {
	Query
	Get() (interface{}, error)
	SoftDelete() (int64, error)
	Recovery() (int64, error)
	Save(record interface{}) (int64, error)
}

type Query

type Query interface {
	Session(conn Conn) Query

	CondQuery
	ExecQuery
	SelectQuery
}

type RawBytes

type RawBytes []byte

type Rows

type Rows interface {
	Next() bool
	Scan(row interface{}) error
	With(schema *Schema)
	Close() error
}

type Schema

type Schema struct {
	AutoIncrement string
	Primary       []string
	// contains filtered or unexported fields
}

func NewSchema

func NewSchema(record interface{}) (*Schema, error)

func (*Schema) Default

func (s *Schema) Default(field string, value interface{}) *Schema

func (*Schema) Emit

func (s *Schema) Emit(event string, record interface{}) error

func (*Schema) Get

func (s *Schema) Get(record interface{}, field string) (interface{}, error)

func (*Schema) On

func (s *Schema) On(event string, f func(record interface{}) error) error

func (*Schema) Set

func (s *Schema) Set(record interface{}, field string, v interface{}) error

func (*Schema) SetAutoIncrement

func (s *Schema) SetAutoIncrement(field string) *Schema

func (*Schema) SetCreateTime

func (s *Schema) SetCreateTime(createTime string) *Schema

func (*Schema) SetPrimary

func (s *Schema) SetPrimary(primary ...string) *Schema

func (*Schema) SetTime

func (s *Schema) SetTime(createTime string, updateTime string, deleteTime string) *Schema

func (*Schema) SetUpdateTime

func (s *Schema) SetUpdateTime(updateTime string) *Schema

func (*Schema) SoftDelete

func (s *Schema) SoftDelete(deleteTime string) *Schema

func (*Schema) With

func (s *Schema) With(record interface{}) error

type SelectQuery

type SelectQuery interface {
	SelectString() ([]map[string]string, error)
	OneString() (map[string]string, error)

	SelectInterface() ([]map[string]interface{}, error)
	OneInterface() (map[string]interface{}, error)

	Find(result interface{}) error
	Rows() (Rows, error)

	Count() (int64, error)
}

type Transaction

type Transaction interface {
	Conn
	Rollback() error
	Commit() error
}

Jump to

Keyboard shortcuts

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