Skip to content

Commit

Permalink
log.Fatal change to log.Panicf
Browse files Browse the repository at this point in the history
  • Loading branch information
fifsky committed May 15, 2020
1 parent 06df785 commit f5bb8cc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
3 changes: 1 addition & 2 deletions hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gosql

import (
"errors"
"fmt"
"log"
"reflect"
"strings"
Expand Down Expand Up @@ -36,7 +35,7 @@ func (h *Hook) callMethod(methodName string, reflectValue reflect.Value) {
case func(db *DB) error:
h.Err(method(h.db))
default:
log.Fatal(fmt.Errorf("unsupported function %v", methodName))
log.Panicf("unsupported function %v", methodName)
}
}
}
Expand Down
48 changes: 24 additions & 24 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var (
mapper = NewReflectMapper("db")
//Insert database automatically updates fields
// Insert database automatically updates fields
AUTO_CREATE_TIME_FIELDS = []string{
"create_time",
"create_at",
Expand All @@ -18,15 +18,15 @@ var (
"update_at",
"updated_at",
}
//Update database automatically updates fields
// Update database automatically updates fields
AUTO_UPDATE_TIME_FIELDS = []string{
"update_time",
"update_at",
"updated_at",
}
)

//Model interface
// Model interface
type IModel interface {
TableName() string
PK() string
Expand Down Expand Up @@ -63,11 +63,11 @@ func (b *Builder) initModel() {
} else {
value := reflect.ValueOf(b.model)
if value.Kind() != reflect.Ptr {
log.Fatalf("model argument must pass a pointer, not a value %#v", b.model)
log.Panicf("model argument must pass a pointer, not a value %#v", b.model)
}

if value.IsNil() {
log.Fatalf("model argument cannot be nil pointer passed")
log.Panicf("model argument cannot be nil pointer passed")
}

tp := reflect.Indirect(value).Type()
Expand All @@ -86,12 +86,12 @@ func (b *Builder) initModel() {
}

if tp.Kind() != reflect.Slice {
log.Fatalf("model argument must be slice, but get %#v", b.model)
log.Panicf("model argument must slice, but get %#v", b.model)
}

tpEl := tp.Elem()

//Compatible with []*Struct or []Struct
// Compatible with []*Struct or []Struct
if tpEl.Kind() == reflect.Ptr {
tpEl = tpEl.Elem()
}
Expand All @@ -102,24 +102,24 @@ func (b *Builder) initModel() {
b.modelReflectValue = reflect.ValueOf(m)
b.dialect = newDialect(b.db.DriverName())
} else {
log.Fatalf("model argument must implementation IModel interface or slice []IModel and pointer,but get %#v", b.model)
log.Panicf("model argument must implementation IModel interface or slice []IModel and pointer,but get %#v", b.model)
}
}
}

//Hint is set TDDL "/*+TDDL:slave()*/"
// Hint is set TDDL "/*+TDDL:slave()*/"
func (b *Builder) Hint(hint string) *Builder {
b.hint = hint
return b
}

//ForceIndex
// ForceIndex
func (b *Builder) ForceIndex(i string) *Builder {
b.forceIndex = i
return b
}

//Where for example Where("id = ? and name = ?",1,"test")
// Where for example Where("id = ? and name = ?",1,"test")
func (b *Builder) Where(str string, args ...interface{}) *Builder {
b.SQLBuilder.Where(str, args...)
return b
Expand All @@ -131,19 +131,19 @@ func (b *Builder) Select(fields string) *Builder {
return b
}

//Limit
// Limit
func (b *Builder) Limit(i int) *Builder {
b.limit = strconv.Itoa(i)
return b
}

//Offset
// Offset
func (b *Builder) Offset(i int) *Builder {
b.offset = strconv.Itoa(i)
return b
}

//OrderBy for example "id desc"
// OrderBy for example "id desc"
func (b *Builder) OrderBy(str string) *Builder {
b.order = str
return b
Expand All @@ -166,23 +166,23 @@ func (b *Builder) Relation(fieldName string, fn BuilderChainFunc) *Builder {
return b
}

//All get data row from to Struct
// All get data row from to Struct
func (b *Builder) Get(zeroValues ...string) (err error) {
b.initModel()
m := zeroValueFilter(b.reflectModel(nil), zeroValues)
//If where is empty, the primary key where condition is generated automatically
// If where is empty, the primary key where condition is generated automatically
b.generateWhere(m)

return b.db.Get(b.model, b.queryString(), b.args...)
}

//All get data rows from to Struct
// All get data rows from to Struct
func (b *Builder) All() (err error) {
b.initModel()
return b.db.Select(b.model, b.queryString(), b.args...)
}

//Create data from to Struct
// Create data from to Struct
func (b *Builder) Create() (lastInsertId int64, err error) {
b.initModel()
hook := NewHook(b.db)
Expand Down Expand Up @@ -235,7 +235,7 @@ func (b *Builder) generateWhereForPK(m map[string]interface{}) {
}
}

//gosql.Model(&User{Id:1,Status:0}).Update("status")
// gosql.Model(&User{Id:1,Status:0}).Update("status")
func (b *Builder) Update(zeroValues ...string) (affected int64, err error) {
b.initModel()
hook := NewHook(b.db)
Expand All @@ -248,7 +248,7 @@ func (b *Builder) Update(zeroValues ...string) (affected int64, err error) {
fields := b.reflectModel(AUTO_UPDATE_TIME_FIELDS)
m := zeroValueFilter(fields, zeroValues)

//If where is empty, the primary key where condition is generated automatically
// If where is empty, the primary key where condition is generated automatically
b.generateWhereForPK(m)

result, err := b.db.Exec(b.updateString(m), b.args...)
Expand All @@ -266,7 +266,7 @@ func (b *Builder) Update(zeroValues ...string) (affected int64, err error) {
return result.RowsAffected()
}

//gosql.Model(&User{Id:1}).Delete()
// gosql.Model(&User{Id:1}).Delete()
func (b *Builder) Delete(zeroValues ...string) (affected int64, err error) {
b.initModel()
hook := NewHook(b.db)
Expand All @@ -277,7 +277,7 @@ func (b *Builder) Delete(zeroValues ...string) (affected int64, err error) {
}

m := zeroValueFilter(b.reflectModel(nil), zeroValues)
//If where is empty, the primary key where condition is generated automatically
// If where is empty, the primary key where condition is generated automatically
b.generateWhere(m)

result, err := b.db.Exec(b.deleteString(), b.args...)
Expand All @@ -295,12 +295,12 @@ func (b *Builder) Delete(zeroValues ...string) (affected int64, err error) {
return result.RowsAffected()
}

//gosql.Model(&User{}).Where("status = 0").Count()
// gosql.Model(&User{}).Where("status = 0").Count()
func (b *Builder) Count(zeroValues ...string) (num int64, err error) {
b.initModel()

m := zeroValueFilter(b.reflectModel(nil), zeroValues)
//If where is empty, the primary key where condition is generated automatically
// If where is empty, the primary key where condition is generated automatically
b.generateWhere(m)

err = b.db.Get(&num, b.countString(), b.args...)
Expand Down

0 comments on commit f5bb8cc

Please sign in to comment.