Optimise SimpleUpdate.

This commit is contained in:
Azareal 2019-06-11 15:44:31 +10:00
parent 92250fdd8f
commit 7bc2146088
1 changed files with 27 additions and 10 deletions

View File

@ -377,6 +377,7 @@ func (adapter *MysqlAdapter) SimpleUpsert(name string, table string, columns str
return querystr, nil return querystr, nil
} }
var sulen1 = len("UPDATE `` SET ")
func (adapter *MysqlAdapter) SimpleUpdate(up *updatePrebuilder) (string, error) { func (adapter *MysqlAdapter) SimpleUpdate(up *updatePrebuilder) (string, error) {
if up.table == "" { if up.table == "" {
return "", errors.New("You need a name for this table") return "", errors.New("You need a name for this table")
@ -384,31 +385,47 @@ func (adapter *MysqlAdapter) SimpleUpdate(up *updatePrebuilder) (string, error)
if up.set == "" { if up.set == "" {
return "", errors.New("You need to set data in this update statement") return "", errors.New("You need to set data in this update statement")
} }
var sb strings.Builder
sb.Grow(sulen1 + len(up.table))
sb.WriteString("UPDATE `")
sb.WriteString(up.table)
sb.WriteString("` SET ")
var q = "UPDATE `" + up.table + "` SET " set := processSet(up.set)
for _, item := range processSet(up.set) { sb.Grow(len(set) * 6)
q += "`" + item.Column + "` =" for i, item := range set {
if i != 0 {
sb.WriteString(",`")
} else {
sb.WriteString("`")
}
sb.WriteString(item.Column)
sb.WriteString("` =")
for _, token := range item.Expr { for _, token := range item.Expr {
switch token.Type { switch token.Type {
case "function", "operator", "number", "substitute", "or": case "function", "operator", "number", "substitute", "or":
q += " " + token.Contents sb.WriteString(" ")
sb.WriteString(token.Contents)
case "column": case "column":
q += " `" + token.Contents + "`" sb.WriteString(" `")
sb.WriteString(token.Contents)
sb.WriteString("`")
case "string": case "string":
q += " '" + token.Contents + "'" sb.WriteString(" '")
sb.WriteString(token.Contents)
sb.WriteString("'")
} }
} }
q += ","
} }
q = q[0 : len(q)-1]
whereStr, err := adapter.buildFlexiWhere(up.where,up.dateCutoff) whereStr, err := adapter.buildFlexiWhere(up.where,up.dateCutoff)
sb.WriteString(whereStr)
if err != nil { if err != nil {
return q, err return sb.String(), err
} }
q += whereStr
// TODO: Shunt the table name logic and associated stmt list up to the a higher layer to reduce the amount of unnecessary overhead in the builder / accumulator // TODO: Shunt the table name logic and associated stmt list up to the a higher layer to reduce the amount of unnecessary overhead in the builder / accumulator
q := sb.String()
adapter.pushStatement(up.name, "update", q) adapter.pushStatement(up.name, "update", q)
return q, nil return q, nil
} }