diff --git a/tpl/macros_struct.twig b/tpl/macros_struct.twig index e2c50e4..c8f1847 100644 --- a/tpl/macros_struct.twig +++ b/tpl/macros_struct.twig @@ -73,6 +73,7 @@ type {{ json_fields_type }} struct{ {% set delete_by = select_by %} {% set delete_where_expr = select_where_expr %} {% set pkey_column_expr = pkey|map(f => "`#{f.name}`")|join(',') %} +{% set pkey_update_expr = pkey|map(f => "`#{f.name}`=VALUES(`#{f.name}`)")|join(',') %} {% set pkey_values_expr = arr_fill('?', pkey|length)|join(',') %} {{ _self.changed_fields_methods(s.name, nopkey) }} @@ -140,10 +141,10 @@ func Save{{ ctx.s.name }}Diff(ctx context.Context, dbe metadb.Execer, rec {{ ctx valuesBuilder.WriteString(" VALUES ({{ ctx.pkey_values_expr }}") var updateBuilder strings.Builder - updateBuilder.Grow({{ 25 + ctx.insert_update_expr|length }}) - updateBuilder.WriteString(" ON DUPLICATE KEY UPDATE ") + updateBuilder.Grow({{ 25 + ctx.pkey_update_expr|length + ctx.insert_update_expr|length }}) + updateBuilder.WriteString(" ON DUPLICATE KEY UPDATE {{ ctx.pkey_update_expr }}") - values := make([]any, 0, 10) + values := make([]any, 0, {{ ctx.insert_fields|length }}) {% for f in ctx.pkey %} values = append(values, rec.{{ f|fname }}) {% endfor %} @@ -166,7 +167,7 @@ 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) + jsonBuilder.Grow(255) // TODO: is it possible calc grow length? jsonValues := make([]any, 0, {{ ctx.json_fields|length }}) {% for f in ctx.json_fields %}