Fixed sql generation for diff saving

This commit is contained in:
Alexey Chubar 2023-11-07 13:39:48 +03:00
parent 857db14448
commit dc6132f30b
1 changed files with 2 additions and 10 deletions

View File

@ -150,16 +150,11 @@ func Save{{ ctx.s.name }}Diff(ctx context.Context, dbe metadb.Execer, rec {{ ctx
{% endfor %}
{% if ctx.raw_nonpk_fields|length > 0 %}
initUpdateLen := updateBuilder.Len()
{% for f in ctx.raw_nonpk_fields %}
if rec.{{ f|fname }}Changed() {
queryBuilder.WriteString(",`{{ f.name }}`")
valuesBuilder.WriteString(",?")
if updateBuilder.Len() > initUpdateLen {
updateBuilder.WriteRune(',')
}
updateBuilder.WriteString("`{{ f.name }}`=VALUES(`{{ f.name }}`)")
updateBuilder.WriteString(",`{{ f.name }}`=VALUES(`{{ f.name }}`)")
values = append(values, rec.{{ f|fname }})
}
@ -194,10 +189,7 @@ func Save{{ ctx.s.name }}Diff(ctx context.Context, dbe metadb.Execer, rec {{ ctx
queryBuilder.WriteString(",`kv`")
valuesBuilder.WriteString(",?")
if updateBuilder.Len() > initUpdateLen {
updateBuilder.WriteRune(',')
}
updateBuilder.WriteString("`kv`=JSON_SET(`kv`")
updateBuilder.WriteString(",`kv`=JSON_SET(`kv`")
updateBuilder.WriteString(jsonBuilder.String())
updateBuilder.WriteRune(')')