Beispiel #1
0
func Update(db *sql.DB, entity IEntity) (sql.Result, error) {
	structInfo := structs.New(entity)
	fieldNames := structs.Names(entity)

	whereClause := ""
	var idValue interface{}
	setValueClause := ""
	values := make([]interface{}, 0)
	i := 0
	for _, fieldName := range fieldNames {
		field := structInfo.Field(fieldName)
		if field.IsExported() { // 公有变量作为数据库字段
			isPk := false
			tags := strings.Split(strings.Trim(field.Tag("db"), " "), ",")
			if len(tags) == 2 && tags[1] == "pk" {
				isPk = true
			}
			tag := tags[0]
			if isPk { // 更新时,只认主键作为更新条件
				whereClause = fmt.Sprintf("WHERE %s = ?", tag)
				idValue = field.Value()
			} else {
				if !field.IsZero() { // 零值字段不更新
					if i == 0 {
						setValueClause += fmt.Sprintf(" %s=? ", tag)
					} else {
						setValueClause += fmt.Sprintf(", %s=? ", tag)
					}
					values = append(values, field.Value())
					i++
				}
			}
		}
	}
	// 检查主键值
	if idValue == nil || idValue == 0 {
		return nil, fmt.Errorf("the value of pk must be set")
	}

	values = append(values, idValue)
	updateSql := fmt.Sprintf("UPDATE %s SET %s %s", entity.TableName(), setValueClause, whereClause)

	stmt, err := db.Prepare(updateSql)
	if err != nil {
		return nil, err
	}

	res, err := stmt.Exec(values...)
	if err != nil {
		return nil, err
	}

	return res, nil
}
Beispiel #2
0
func Save(db *sql.DB, entity IEntity) (sql.Result, error) {
	structInfo := structs.New(entity)
	fieldNames := structs.Names(entity)

	values := make([]interface{}, 0)
	sqlFieldStr := ""
	sqlFieldPre := ""
	i := 0
	for _, fieldName := range fieldNames {
		field := structInfo.Field(fieldName)
		if field.IsExported() {
			tags := strings.Split(strings.Trim(field.Tag("db"), " "), ",")
			isPk := false
			if len(tags) == 2 && tags[1] == "pk" {
				isPk = true
			}
			tag := tags[0]
			if !isPk {
				values = append(values, field.Value())
				if i == 0 {
					sqlFieldStr += "(" + strings.ToLower(tag)
					sqlFieldPre += "(?"
				} else {
					sqlFieldStr += "," + strings.ToLower(tag)
					sqlFieldPre += ",?"
				}
				i++
			}
		}
	}
	if len(values) > 1 {
		sqlFieldStr += ")"
		sqlFieldPre += ")"
	}

	sql := fmt.Sprintf("INSERT INTO %s %s VALUES %s", entity.TableName(), sqlFieldStr, sqlFieldPre)
	stmt, err := db.Prepare(sql)
	if err != nil {
		return nil, err
	}
	res, err := stmt.Exec(values...)
	if err != nil {
		return nil, err
	}

	return res, nil
}
Beispiel #3
0
func otherReflect(ui IEntity) {
	structName := structs.Name(ui)
	log.Println("structName", structName)

	fieldNames := structs.Names(ui)

	structInfo := structs.New(ui)

	values := make([]interface{}, 0)
	sqlFieldStr := ""
	sqlFieldPre := ""
	i := 0
	for _, fieldName := range fieldNames {
		field := structInfo.Field(fieldName)
		if field.IsExported() {
			tags := strings.Split(strings.Trim(field.Tag("db"), " "), ",")
			isPk := false
			if len(tags) == 2 && tags[1] == "pk" {
				isPk = true
			}
			tag := tags[0]
			if !isPk {
				values = append(values, field.Value())
				if i == 0 {
					sqlFieldStr += "(" + strings.ToLower(tag)
					sqlFieldPre += "(?"
				} else {
					sqlFieldStr += "," + strings.ToLower(tag)
					sqlFieldPre += ",?"
				}
				i++
			}
		}
	}
	if len(values) > 1 {
		sqlFieldStr += ")"
		sqlFieldPre += ")"
	}

	sql := fmt.Sprintf("INSERT INTO %s %s VALUES %s", ui.TableName(), sqlFieldStr, sqlFieldPre)
	fmt.Println(sql)
	fmt.Println(values)
}
	Host       string                 `valid:"ipv4"`
	Loc        string                 `valid:"url"`
	Image      string                 `valid:"url"`
	Video      string                 `valid:"url"`
	Tag        string                 `valid:""`
	Geo        string                 `valid:"latitude,longitude"`
	News       string                 `valid:"-"`
	Mobile     bool                   `valid:"-"`
	Alternate  string                 `valid:"-"`
	Alternates map[string]interface{} `valid:"-"`
	Pagemap    map[string]interface{} `valid:"-"`
}

// fieldnames []string{"priority" "changefreq" "lastmod" "expires" "host" "images"
// "video" "geo" "news" "videos" "mobile" "alternate" "alternates" "pagemap"}
var fieldnames = ToLowerString(structs.Names(&URLModel{}))

// NewSitemapURL returns the created the SitemapURL's pointer
// and it validates URL types error.
func NewSitemapURL(url URL) (SitemapURL, error) {
	smu := &sitemapURL{data: url}
	err := smu.validate()
	return smu, err
}

// sitemapURL provides xml validator and xml builder.
type sitemapURL struct {
	data URL
}

// validate is checking correct keys and checks the existence.
Beispiel #5
0
func (s Subscriber) fields() []string {
	return structs.Names(s)
}