yorm

package module
v0.0.0-...-2e023d5 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2020 License: BSD-3-Clause Imports: 10 Imported by: 2

README

README

yOrm is a simple,lightweight orm , for mysql only now.

Why this project calls yOrm

yOrm is just a name.
thanks [https://github.com/lewgun]

What is this yOrm for?
  • A simple mysql orm to crud

Tags

Now support these types of tag.

column

this tag alias struct name to a real column name. "Id int `yorm:"column(autoId)"`" means this field Id will name autoId in mysql column

pk

this tag allow you to set a primary key where select/delete/update as the where clause "Id int `yorm:"column(autoId);pk"`"

benchmark

select by id with five fields execute 1e5 times (not very accurate)

beegoOrm 13376 milliseconds
xorm 16718 milliseconds
yorm 6759 milliseconds

beegoOrm 14149 milliseconds
xorm 17685 milliseconds
yorm 7568 milliseconds

code is here:




package main

import (
	"beego/orm"
	"fmt"
	"time"

	"github.com/JessonChan/fastfunc"
	"github.com/JessonChan/yorm"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
)

const db = `root:@tcp(127.0.0.1:3306)/yorm_test?charset=utf8`

type ProgramLanguage struct {
	Id        int64
	Name      string
	RankMonth time.Time
	Position  int
	Created   time.Time
}

var engine *xorm.Engine
var o orm.Ormer

func init() {
	orm.RegisterDataBase("default", "mysql", db)
	orm.RegisterModel(new(ProgramLanguage))
	yorm.Register(db)
	engine, _ = xorm.NewEngine("mysql", db)
	o = orm.NewOrm()
}

func main() {
	fastfunc.SetRunTimes(1e5)
	fmt.Println("beegoOrm", fastfunc.Run(beegoOrm)/1e6, "milliseconds")
	fmt.Println("    xorm", fastfunc.Run(xomrTest)/1e6, "milliseconds")
	fmt.Println("    yorm", fastfunc.Run(yormTest)/1e6, "milliseconds")
}

func beegoOrm() {
	p := ProgramLanguage{Id: 1}
	o.Read(&p)
	if p.Name == "" {
		panic(p)
	}
}
func yormTest() {
	p := ProgramLanguage{Id: 1}
	yorm.SelectByPK(&p)
	if p.Name == "" {
		panic(p)
	}
}
func xomrTest() {
	p := ProgramLanguage{Id: 1}
	engine.Get(&p)
	if p.Name == "" {
		panic(p)
	}
}


Documentation

Index

Constants

View Source
const (
	CamelToUnderscore = iota
	FieldName
)

field name

View Source
const (

	//DriverMySQL driver for mysql
	DriverMySQL = "mysql"
)

Variables

View Source
var (
	ErrIllegalParams     = errors.New("illegal parameter(s)")
	ErrNonPtr            = errors.New("must be the pointer to the modified object")
	ErrNonSlice          = errors.New("must be a slice")
	ErrNotSupported      = errors.New("not supported now")
	ErrNilMethodReceiver = errors.New("the method receiver is nil.")
	ErrNilSqlExecutor    = errors.New("yorm not register the db config")
	ErrUpdateBadSql      = errors.New("must be begin with update keyword")
	ErrDuplicatePkColumn = errors.New("duplicate pk column find")
	ErrNonePkColumn      = errors.New("none pk column find")
)
View Source
var (
	Close = ""
	Debug = "Debug"
	Warn  = "Warn"
	Error = "Error"
)
View Source
var (
	// TimeType time's reflect type.
	TimeType   = reflect.TypeOf(time.Time{})
	BoolType   = reflect.TypeOf(true)
	Int64Type  = reflect.TypeOf(int64(0))
	StringType = reflect.TypeOf("")
)

Functions

func Begin

func Begin(name ...string) (sqlExecutor, error)

func Commit

func Commit(e sqlExecutor) error

func Count

func Count(i interface{}, where ...interface{}) int64

func Delete

func Delete(clause string, args ...interface{}) (int64, error)

Delete delete record(s) from a table

func InitLogger

func InitLogger(l *log.Logger)

func Insert

func Insert(i interface{}, args ...string) (int64, error)

Insert insert a record.

func R

func R(i interface{}, cond ...interface{}) error

func Register

func Register(dsn string, driver ...string) error

Register register a database driver.

func RegisterTableFunc

func RegisterTableFunc(fn func(string) string)

table func 可以根据项目需要定制不同的model对应表名,比如添加前缀、后缀;删除model中的前缀等 对于实现了YormTableStruct ( YormTableName() string)则不会去处理

func RegisterWithName

func RegisterWithName(dsn, name string, driver ...string) (err error)

RegisterWithName register a database driver with specific name.

func RollBack

func RollBack(e sqlExecutor) error

func Select

func Select(i interface{}, condition string, args ...interface{}) error

Select query records(s) from a table.

func SelectByPK

func SelectByPK(i interface{}, tableName ...string) error

SelectByPK select by pk.

func SetLoggerLevel

func SetLoggerLevel(s string)

func Update

func Update(clause string, args ...interface{}) (int64, error)

Update update record(s)

func Using

func Using(name string) sqlExecutor

Using using the executor with name, if not exist nilSqlExecutor instead.

Types

type ExecHandler

type ExecHandler func(clause string, args ...interface{}) (sql.Result, error)

type YormTableStruct

type YormTableStruct interface {
	YormTableName() string
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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