fix json diff's saving

This commit is contained in:
Pavel Merzlyakov 2023-09-11 11:17:13 +03:00
parent 2684a2c663
commit 174cf70343
1 changed files with 4 additions and 17 deletions

View File

@ -169,12 +169,14 @@ func Save{{ ctx.s.name }}Diff(ctx context.Context, dbe metadb.Execer, rec {{ ctx
{% if has_token(ctx.s, 'table_json_kv') %}
var jsonBuilder strings.Builder
jsonBuilder.Grow(255)
jsonValues := make([]any, 0, {{ ctx.json_fields|length }})
{% for f in ctx.json_fields %}
{% set fname = f|fname %}
if rec.{{ f|fname }}Changed() {
{%~ if f.type is builtin %}
jsonBuilder.WriteString(",'$.{{ f|alias }}',?")
values = append(values, rec.{{ f|fname }})
jsonValues = append(jsonValues, rec.{{ f|fname }})
{%~ else %}
jsonBuilder.WriteString(",'$.{{ f|alias }}',")
jsonBytes, err := json.Marshal(rec.{{ fname }})
@ -184,22 +186,6 @@ func Save{{ ctx.s.name }}Diff(ctx context.Context, dbe metadb.Execer, rec {{ ctx
jsonBuilder.WriteString("CAST(")
jsonBuilder.WriteString(strconv.Quote(string(jsonBytes)))
jsonBuilder.WriteString(" AS JSON)")
{# {%~ if f.type is string %}
jsonBuilder.WriteString(strconv.Quote(rec.{{ fname }}))
{%~ elseif f.type is bool %}
jsonBuilder.WriteString(strconv.FormatBool(rec.{{ fname }}))
{%~ elseif f.type is uint %}
jsonBuilder.WriteString(strconv.FormatUint(uint64(rec.{{ fname }}), 10))
{%~ elseif f.type is int %}
jsonBuilder.WriteString(strconv.FormatInt(int64(rec.{{ fname }}), 10))
{%~ else %}
jsonBytes, err := json.Marshal(rec.{{ fname }})
if err != nil {
return errors.WithStack(err)
}
jsonBuilder.WriteString("CAST(")
jsonBuilder.WriteString(strconv.Quote(string(jsonBytes)))
jsonBuilder.WriteString(" AS JSON)") #}
{%~ endif %}
}
{% endfor %}
@ -216,6 +202,7 @@ func Save{{ ctx.s.name }}Diff(ctx context.Context, dbe metadb.Execer, rec {{ ctx
updateBuilder.WriteRune(')')
values = append(values, rec.{{ ctx.s.name }}JsonValues)
values = append(values, jsonValues...)
}
{% endif %}