fix sql method generation for structs with pk fields only

This commit is contained in:
Pavel Merzlyakov 2023-11-15 15:43:01 +03:00
parent 33e2e5c3a3
commit 9eb9d71175
1 changed files with 6 additions and 0 deletions

View File

@ -86,7 +86,11 @@ type {{ json_fields_type }} struct{
{% macro table_save(ctx) %}
func Save{{ ctx.s.name }}(ctx context.Context, dbe metadb.Execer, rec {{ ctx.s.name }}) error {
{% if ctx.insert_update_expr|length > 0 %}
query := "INSERT INTO `{{ ctx.table_name }}` ({{ ctx.insert_column_expr }}) VALUES ({{ ctx.insert_values_expr }}) ON DUPLICATE KEY UPDATE {{ ctx.insert_update_expr }}"
{% else %}
query := "INSERT INTO `{{ ctx.table_name }}` ({{ ctx.insert_column_expr }}) VALUES ({{ ctx.insert_values_expr }})"
{% endif %}
_, saveErr := dbe.ExecContext(
ctx,
@ -121,7 +125,9 @@ func Save{{ ctx.s.name }}Collection(ctx context.Context, dbe metadb.Execer, recs
{%~ endfor %}
)
}
{% if ctx.insert_update_expr|length > 0 %}
builder.WriteString(" ON DUPLICATE KEY UPDATE {{ ctx.insert_update_expr }}")
{% endif %}
_, saveErr := dbe.ExecContext(ctx, builder.String(), values...)
return errors.WithStack(saveErr)