Skip to content

Commit

Permalink
fmt & replace utf8mb4 default
Browse files Browse the repository at this point in the history
  • Loading branch information
fizzday committed Jul 27, 2020
1 parent c31e4f1 commit adc9313
Show file tree
Hide file tree
Showing 20 changed files with 66 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func init() {
// 如果配置读写分离集群,则使用&gorose.ConfigCluster{}
engin, err = gorose.Open(&gorose.Config{Driver: "sqlite3", Dsn: "./db.sqlite"})
// mysql示例, 记得导入mysql驱动 github.com/go-sql-driver/mysql
// engin, err = gorose.Open(&gorose.Config{Driver: "mysql", Dsn: "root:root@tcp(localhost:3306)/test?charset=utf8&parseTime=true"})
// engin, err = gorose.Open(&gorose.Config{Driver: "mysql", Dsn: "root:root@tcp(localhost:3306)/test?charset=utf8mb4&parseTime=true"})
}
func DB() gorose.IOrm {
return engin.NewOrm()
Expand Down
2 changes: 1 addition & 1 deletion README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func init() {
// If you configure a read-write separation cluster, use & gorose. ConfigCluster {}
engin, err = gorose.Open(&gorose.Config{Driver: "sqlite3", Dsn: "./db.sqlite"})
// mysql demo, remeber import mysql driver of github.com/go-sql-driver/mysql
// engin, err = gorose.Open(&gorose.Config{Driver: "mysql", Dsn: "root:root@tcp(localhost:3306)/test?charset=utf8&parseTime=true"})
// engin, err = gorose.Open(&gorose.Config{Driver: "mysql", Dsn: "root:root@tcp(localhost:3306)/test?charset=utf8mb4&parseTime=true"})
}
func DB() gorose.IOrm {
return engin.NewOrm()
Expand Down
4 changes: 2 additions & 2 deletions binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (o *Binder) BindParse(prefix string) error {
o.parseFields()
// 是否设置了表名
switch dstVal.Kind() {
case reflect.Ptr,reflect.Struct:
case reflect.Ptr, reflect.Struct:
if tn := dstVal.MethodByName("TableName"); tn.IsValid() {
BindName = tn.Call(nil)[0].String()
}
Expand Down Expand Up @@ -177,7 +177,7 @@ func (o *Binder) BindParse(prefix string) error {

// 是否设置了表名
switch dstVal.Kind() {
case reflect.Ptr,reflect.Struct:
case reflect.Ptr, reflect.Struct:
if tn := br.MethodByName("TableName"); tn.IsValid() {
BindName = tn.Call(nil)[0].String()
}
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package gorose
type Config struct {
Driver string `json:"driver"` // 驱动: mysql/sqlite3/oracle/mssql/postgres/clickhouse, 如果集群配置了驱动, 这里可以省略
// mysql 示例:
// root:root@tcp(localhost:3306)/test?charset=utf8&parseTime=true
// root:root@tcp(localhost:3306)/test?charset=utf8mb4&parseTime=true
Dsn string `json:"dsn"` // 数据库链接
SetMaxOpenConns int `json:"setMaxOpenConns"` // (连接池)最大打开的连接数,默认值为0表示不限制
SetMaxIdleConns int `json:"setMaxIdleConns"` // (连接池)闲置的连接数, 默认0
Expand Down
12 changes: 6 additions & 6 deletions engin.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (c *Engin) GetLogger() ILogger {
}

// SetLogger ...
func (c *Engin) SetLogger(lg ILogger) {
func (c *Engin) SetLogger(lg ILogger) {
c.logger = lg
}

Expand All @@ -140,10 +140,10 @@ func (c *Engin) bootCluster() error {
//fmt.Println(len(c.config.Slave))
if len(c.config.Slave) > 0 {
for _, item := range c.config.Slave {
if c.config.Driver!=""{
if c.config.Driver != "" {
item.Driver = c.config.Driver
}
if c.config.Prefix!=""{
if c.config.Prefix != "" {
item.Prefix = c.config.Prefix
}
db, err := c.bootReal(item)
Expand All @@ -161,10 +161,10 @@ func (c *Engin) bootCluster() error {
var pre, dr string
if len(c.config.Master) > 0 {
for _, item := range c.config.Master {
if c.config.Driver!=""{
if c.config.Driver != "" {
item.Driver = c.config.Driver
}
if c.config.Prefix!=""{
if c.config.Prefix != "" {
item.Prefix = c.config.Prefix
}
db, err := c.bootReal(item)
Expand Down Expand Up @@ -201,7 +201,7 @@ func (c *Engin) bootCluster() error {

// boot sql driver
func (c *Engin) bootReal(dbConf Config) (db *sql.DB, err error) {
//db, err = sql.Open("mysql", "root:root@tcp(localhost:3306)/test?charset=utf8")
//db, err = sql.Open("mysql", "root:root@tcp(localhost:3306)/test?charset=utf8mb4")
// 开始驱动
db, err = sql.Open(dbConf.Driver, dbConf.Dsn)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions engin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func (u *UsersMapSlice) TableName() string {
}

type Users struct {
Uid int64 `orm:"uid"`
Uid int64 `orm:"uid"`
Name string `orm:"name"`
Age int64 `orm:"age"`
Age int64 `orm:"age"`
Fi string `orm:"ignore"`
}

Expand Down
4 changes: 2 additions & 2 deletions err.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ type Err struct {
//var gOnce *sync.Once
var gErr *Err

func init() {
func init() {
var tmpLang = make(map[Lang]map[Error]string)
gErr = &Err{err:tmpLang}
gErr = &Err{err: tmpLang}
gErr.lang = CHINESE
gErr.Register(gErr.Default())
}
Expand Down
10 changes: 5 additions & 5 deletions examples/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ func main() {
var config3 = gorose.Config{Dsn: "./db3.sqlite"}
var config4 = gorose.Config{Dsn: "./db4.sqlite"}
var configCluster = &gorose.ConfigCluster{
Master: []gorose.Config{config3, config4},
Slave: []gorose.Config{config1, config2},
Master: []gorose.Config{config3, config4},
Slave: []gorose.Config{config1, config2},
Driver: "sqlite3",
}
engin, err := gorose.Open(configCluster)
if err!=nil {
if err != nil {
panic(err.Error())
}
for i:=0;i<5;i++{
for i := 0; i < 5; i++ {
db := engin.NewOrm()
res,err := db.Table("users").First()
res, err := db.Table("users").First()
fmt.Println(err)
fmt.Println(res)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/dbobj/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func GetMysqlEngin() *gorose.Engin {
var engin *gorose.Engin
engin, err = gorose.Open(&gorose.Config{
Driver: "mysql",
Dsn: "root:123456@tcp(localhost:3306)/novel?charset=utf8mb4",
Dsn: "root:123456@tcp(localhost:3306)/test?charset=utf8mb4",
Prefix: "nv_",
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/error_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
var res interface{}
var err error
db := getdb()
res,err = db.Table("users").Insert(gorose.Data{"uid2":222})
res, err = db.Table("users").Insert(gorose.Data{"uid2": 222})
if err != nil {
fmt.Println("0 - err:", err.Error())
}
Expand Down
6 changes: 4 additions & 2 deletions examples/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import (

var engin *gorose.Engin
var err error

func init() {
engin, err = gorose.Open(&gorose.Config{Prefix: "nv_", Driver: "mysql", Dsn: "root:123456@tcp(localhost:3306)/novel?charset=utf8&parseTime=true"})
if err!=nil {
engin, err = gorose.Open(&gorose.Config{Prefix: "nv_", Driver: "mysql", Dsn: "root:123456@tcp(localhost:3306)/test?charset=utf8mb4&parseTime=true"})
if err != nil {
panic(err.Error())
}
}
func db() gorose.IOrm {
return engin.NewOrm()
}

type Tag struct {
Id int64 `gorose:"id" json:"id"`
TagTitle string `gorose:"tag_title" json:"tag_title"`
Expand Down
4 changes: 2 additions & 2 deletions examples/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func initDB() *gorose.Engin {

return e
}
func main() {
func main() {
db := initDB().NewOrm()
res,err := db.Table("users").First()
res, err := db.Table("users").First()
fmt.Println(err)
fmt.Println(db.LastSql())
fmt.Println(res)
Expand Down
12 changes: 6 additions & 6 deletions examples/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"github.com/gohouse/gorose/v2"
)

func initEngin()*gorose.Engin {
func initEngin() *gorose.Engin {
var err error
var engin *gorose.Engin
engin, err = gorose.Open(&gorose.Config{
Driver: "mysql",
Dsn: "root:123456@tcp(localhost:3306)/novel?charset=utf8mb4",
Dsn: "root:123456@tcp(localhost:3306)/test?charset=utf8mb4",
Prefix: "nv_",
})
if err != nil {
Expand All @@ -31,17 +31,17 @@ func trans() error {
db := engin.NewOrm()
db.Begin()
aff, err = db.Table("logs").Insert(gorose.Data{"username": "xx"})
if err != nil || aff==0 {
if err != nil || aff == 0 {
db.Rollback()
return err
}
aff, err = db.Table("logs").Where("id",2).Update(gorose.Data{"username": "xx1232xx1232xx1232xx1232xx1232xx1232xx1232xx1232"})
if err != nil || aff==0 {
aff, err = db.Table("logs").Where("id", 2).Update(gorose.Data{"username": "xx1232xx1232xx1232xx1232xx1232xx1232xx1232xx1232"})
if err != nil || aff == 0 {
db.Rollback()
return err
}
aff, err = db.Table("logs").Insert(gorose.Data{"username": "xx", "pkid": 1})
if err != nil || aff==0 {
if err != nil || aff == 0 {
db.Rollback()
return err
}
Expand Down
14 changes: 7 additions & 7 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ var logger *Logger
// NewLogger ...
func NewLogger(o *LogOption) *Logger {
//onceLogger.Do(func() {
logger = &Logger{filePath: "./"}
if o.FilePath != "" {
logger.filePath = o.FilePath
}
logger.sqlLog = o.EnableSqlLog
logger.slowLog = o.EnableSlowLog
logger.errLog = o.EnableErrorLog
logger = &Logger{filePath: "./"}
if o.FilePath != "" {
logger.filePath = o.FilePath
}
logger.sqlLog = o.EnableSqlLog
logger.slowLog = o.EnableSlowLog
logger.errLog = o.EnableErrorLog
//})
return logger
}
Expand Down
16 changes: 8 additions & 8 deletions orm_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package gorose

// OrmApi ...
type OrmApi struct {
table string
fields []string
where [][]interface{}
order string
limit int
offset int
join [][]interface{}
distinct bool
table string
fields []string
where [][]interface{}
order string
limit int
offset int
join [][]interface{}
distinct bool
//union string
group string
having string
Expand Down
6 changes: 3 additions & 3 deletions orm_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (dba *Orm) exec(operType string, data ...interface{}) (int64, error) {
rl2 := reflect.Indirect(rl)

switch rl2.Kind() {
case reflect.Struct,reflect.Ptr:
case reflect.Struct, reflect.Ptr:
//return 0, errors.New("传入的结构体必须是对象的地址")
if tn := rl2.MethodByName("TableName"); tn.IsValid() {
dba.Table(dba.GetData())
Expand All @@ -76,7 +76,7 @@ func (dba *Orm) exec(operType string, data ...interface{}) (int64, error) {
r2 := rl2.Type().Elem()
r2val := reflect.New(r2)
switch r2val.Kind() {
case reflect.Struct,reflect.Ptr:
case reflect.Struct, reflect.Ptr:
if tn := r2val.MethodByName("TableName"); tn.IsValid() {
dba.Table(dba.GetData())
}
Expand All @@ -85,7 +85,7 @@ func (dba *Orm) exec(operType string, data ...interface{}) (int64, error) {
dba.Table(dba.GetData())
}
default:
return 0,errors.New("表名有误")
return 0, errors.New("表名有误")
}
}

Expand Down
2 changes: 1 addition & 1 deletion orm_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestOrm_Update2(t *testing.T) {
// Age: 11,
//}}

aff, err := db.Table("users").Where("uid",1).Update()
aff, err := db.Table("users").Where("uid", 1).Update()
if err != nil {
//t.Error(err.Error())
t.Log(err.Error())
Expand Down
5 changes: 3 additions & 2 deletions orm_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

// Select : select one or more rows , relation limit set
func (dba *Orm) Select() error {
switch dba.GetIBinder().GetBindType(){
case OBJECT_STRUCT,OBJECT_MAP,OBJECT_MAP_T:
switch dba.GetIBinder().GetBindType() {
case OBJECT_STRUCT, OBJECT_MAP, OBJECT_MAP_T:
dba.Limit(1)
}
// 构建sql
Expand Down Expand Up @@ -95,6 +95,7 @@ func (dba *Orm) _unionBuild(union, field string) (interface{}, error) {
}
return 0, err
}

//func (dba *Orm) _unionBuild_bak(union, field string) (interface{}, error) {
// var tmp interface{}
//
Expand Down
18 changes: 9 additions & 9 deletions orm_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestOrm_BuildSql2(t *testing.T) {
db := DB()
var u = "age=age+1,num=num+1"
var wheres interface{}
wheres = [][]interface{}{{"a", ">", "b"}, {"a", "b"},{"a is null"}}
wheres = [][]interface{}{{"a", ">", "b"}, {"a", "b"}, {"a is null"}}
sqlstr, a, b := db.Force().Table("users").Data(u).Where(wheres).BuildSql("update")

t.Log(sqlstr, a, b)
Expand All @@ -31,9 +31,9 @@ func TestOrm_BuildSql4(t *testing.T) {
db := DB()
//var wheres interface{}
//wheres = [][]interface{}{{"a", ">", "b"},{"lock",1}}
wheres := Data{"lock":1}
wheres := Data{"lock": 1}
sqlstr, a, b := db.Table(Users{}).Where(wheres).Where(func() {
db.Where("c",2).OrWhere("lock",">",4)
db.Where("c", 2).OrWhere("lock", ">", 4)
}).BuildSql()

t.Log(sqlstr, a, b)
Expand Down Expand Up @@ -71,12 +71,12 @@ func TestOrm_BuildSql6(t *testing.T) {
Name: "2",
Age: 3,
}
res,err := db.Table("xxx").Where("xx","xx").Update(&u)
t.Log(db.LastSql(), res,err)
res, err := db.Table("xxx").Where("xx", "xx").Update(&u)
t.Log(db.LastSql(), res, err)
}

func TestOrm_First(t *testing.T) {
res, err := DB().Table(Users{}).Where("uid",1).First()
res, err := DB().Table(Users{}).Where("uid", 1).First()
if err != nil {
t.Error(err.Error())
}
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestOrm_Pluck(t *testing.T) {
//var u []Users
ormObj := orm.Table("users")
//res,err := ormObj.Pluck("name", "uid")
res, err := ormObj.Limit(5).Pluck("name","uid")
res, err := ormObj.Limit(5).Pluck("name", "uid")
if err != nil {
t.Error(err.Error())
}
Expand Down Expand Up @@ -314,7 +314,7 @@ func TestOrm_Paginate2(t *testing.T) {
db := DB()

var u []Users
res, err := db.Table(&u).Where("uid",">",1).Limit(2).Paginate(3)
res, err := db.Table(&u).Where("uid", ">", 1).Limit(2).Paginate(3)
if err != nil {
t.Error(err.Error())
}
Expand All @@ -327,7 +327,7 @@ func TestOrm_Sum(t *testing.T) {

var u Users
//res, err := db.Table(Users{}).First()
res, err := db.Table(&u).Where(Data{"uid":1}).Sum("age")
res, err := db.Table(&u).Where(Data{"uid": 1}).Sum("age")
if err != nil {
t.Error(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func addQuotes(data interface{}, sep string) string {
func addBackticks(arg string) string {
reg := regexp.MustCompile(`^\w+$`)
if reg.MatchString(arg) {
return fmt.Sprintf("`%s`",arg)
return fmt.Sprintf("`%s`", arg)
}
return arg
}
Expand Down

0 comments on commit adc9313

Please sign in to comment.