Browse Source

分页插入数据库通用方法 & 中文地名处理更新

marion 5 years ago
parent
commit
35ab463f73
2 changed files with 92 additions and 0 deletions
  1. 87 0
      utils/db_utils.go
  2. 5 0
      utils/string_utils.go

+ 87 - 0
utils/db_utils.go

@@ -0,0 +1,87 @@
+package utils
+
+import (
+	"errors"
+	"github.com/go-xorm/xorm"
+	"reflect"
+)
+
+// 分页插入数据库
+func Insert(db *xorm.Engine, beans ...interface{}) error {
+	return InsertWithSize(db, 500, beans...)
+}
+
+func InsertWithSize(db *xorm.Engine, size int, beans ...interface{}) error {
+	if nil == db {
+		return errors.New(`db can not be nil`)
+	}
+
+	if nil == beans || len(beans) == 0 {
+		return nil
+	}
+
+	trans := db.NewSession()
+	defer trans.Close()
+	_ = trans.Begin()
+
+	err := TransInsertWithSize(trans, size, beans...)
+	if nil != err {
+		_ = trans.Rollback()
+		return err
+	}
+
+	return trans.Commit()
+}
+
+func TransInsert(trans *xorm.Session, beans ...interface{}) error {
+	return TransInsertWithSize(trans, 500, beans...)
+}
+
+func TransInsertWithSize(trans *xorm.Session, size int, beans ...interface{}) error {
+	if nil == trans {
+		return errors.New(`trans can not be nil`)
+	}
+
+	if nil == beans || len(beans) == 0 {
+		return nil
+	}
+
+	if size < 100 {
+		size = 500
+	}
+
+	var objects []interface{}
+	for _, bean := range beans {
+		sliceValue := reflect.Indirect(reflect.ValueOf(bean))
+		if sliceValue.Kind() == reflect.Slice {
+			for i := 0; i < sliceValue.Len(); i++ {
+				v := sliceValue.Index(i)
+				elemValue := v.Interface()
+				objects = append(objects, elemValue)
+			}
+		} else {
+			objects = append(objects, bean)
+		}
+	}
+
+	total := len(objects)
+	var err error
+	if total <= size {
+		_, err = trans.Insert(&objects)
+		return err
+	}
+
+	for i := 0; i < total; i += size {
+		end := i + size
+		if end > total {
+			end = total
+		}
+		arr := objects[i:end]
+		_, err = trans.Insert(&arr)
+		if nil != err {
+			return err
+		}
+	}
+
+	return err
+}

+ 5 - 0
utils/string_utils.go

@@ -184,6 +184,11 @@ func GetCleanLocationName(name string) string {
 		return ""
 	}
 
+	rs := []rune(name)
+	if len(rs) <= 2 {
+		return name
+	}
+
 	b := []byte(name)
 	b = locationPostfixPattern.ReplaceAll(b, emptyBytes)
 	if string(b) != "内蒙古" {