github.com/jtzjtz/kit@v1.0.2/convert/db.go (about)

     1  package convert
     2  
     3  import (
     4  	"github.com/jtzjtz/kit/database"
     5  	"reflect"
     6  	"strings"
     7  )
     8  
     9  func getEqualCondition(name string, value interface{}) (database.SqlCondition, bool) {
    10  	if name == "" || value == nil {
    11  		return database.SqlCondition{}, false
    12  	}
    13  
    14  	return database.SqlCondition{
    15  		QueryName: name,
    16  		Predicate: database.SqlEqualPredicate,
    17  		Value:     value,
    18  	}, true
    19  }
    20  
    21  // map转数据库检索条件数组
    22  func MapToSqlcondition(protoMegMap map[string]interface{}) []database.SqlCondition {
    23  	conditions := []database.SqlCondition{}
    24  	for name, value := range protoMegMap {
    25  		if con, ok := getEqualCondition(name, value); ok {
    26  			conditions = append(conditions, con)
    27  		}
    28  	}
    29  
    30  	return conditions
    31  }
    32  
    33  func GetEntityPk(sct interface{}) (string, bool) {
    34  	t := reflect.TypeOf(sct).Elem()
    35  	for i := 0; i < t.NumField(); i++ {
    36  		js := strings.Split(t.Field(i).Tag.Get("gorm"), ";")
    37  		if len(js) > 1 && strings.TrimSpace(js[1]) == "primary_key" {
    38  			cols := strings.Split(js[0], ":")
    39  			return cols[1], true
    40  		}
    41  	}
    42  	return "", false
    43  }