fix saving json values

This commit is contained in:
Pavel Merzlyakov 2023-09-07 11:32:50 +03:00
parent d109355790
commit 1a2d4546cf
1 changed files with 29 additions and 18 deletions

View File

@ -86,9 +86,9 @@ func Save{{ s.name }}Diff(ctx context.Context, dbe metadb.Execer, rec {{ s.name
}
var builder strings.Builder
builder.Grow({{ 50 + insert_column_expr|length }})
builder.Grow({{ 56 + table_name|length + insert_column_expr|length + insert_values_expr|length }})
builder.WriteString("INSERT INTO `{{ table_name }}` ({{ insert_column_expr }}) ON DUPLICATE KEY UPDATE `kv`=")
builder.WriteString("INSERT INTO `{{ table_name }}` ({{ insert_column_expr }}) VALUES({{ insert_values_expr }}) ON DUPLICATE KEY UPDATE `kv`=")
if err := rec.WriteJsonSetExpr(&builder); err != nil {
return err
@ -563,7 +563,7 @@ func (v {{ type_name }}) Value() (driver.Value, error) {
return nil, errors.WithStack(err)
}
return jsonBytes, nil
return string(jsonBytes), nil
}
{% endmacro json_sql_impl %}
@ -691,17 +691,11 @@ func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error {
contSize--
}
if !use_mask || (use_mask && s.FieldChanged({{ loop.index0 }})) {
{{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, f.tokens) }}
{% if has_token(s, 'table') %}
s.changedFields.SetChanged("{{ f|alias }}")
{% endif %}
{{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, f.tokens, has_token(s, 'table')) }}
}
{% else %}
contSize--
{{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, f.tokens) }}
{% if has_token(s, 'table') %}
s.changedFields.SetChanged("{{ f|alias }}")
{% endif %}
{{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, f.tokens, has_token(s, 'table')) }}
{% endif %}
{%- endfor -%}
@ -711,15 +705,23 @@ func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error {
{% endmacro meta_read %}
{% macro meta_read_field(type, fname, alias, tokens) %}
{% macro meta_read_field(type, fname, alias, tokens, has_table_token) %}
{% if type is builtin %}
if err := reader.Read{{ type|builtin_type_suffix }}(&{{ fname }}, "{{ alias }}"); err != nil && err != meta.FieldNotFound {
return err
}
if err := reader.Read{{ type|builtin_type_suffix }}(&{{ fname }}, "{{ alias }}"); err != nil {
if err != meta.FieldNotFound {
return err
}
} {% if has_table_token %}else {
s.changedFields.SetChanged("{{ alias }}")
}{% endif ~%}
{% elseif type is enum %}
if err := reader.ReadInt32((*int32)(&{{ fname }}), "{{ alias }}"); err != nil && err != meta.FieldNotFound {
return err
}
if err := reader.ReadInt32((*int32)(&{{ fname }}), "{{ alias }}"); err != nil {
if err != meta.FieldNotFound {
return err
}
} {% if has_table_token %}else {
s.changedFields.SetChanged("{{ alias }}")
}{% endif ~%}
if !{{ fname }}.IsValid() {
return errors.Errorf("bad enum value `%d` for `{{ alias }}`", {{ fname }})
}
@ -731,6 +733,9 @@ func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error {
}
} else {
{{ fname }} = v.(I{{ type.name }})
{% if has_table_token %}
s.changedFields.SetChanged("{{ alias }}")
{% endif %}
}
{% else %}
if err := reader.BeginContainer("{{ alias }}"); err != nil {
@ -744,6 +749,9 @@ func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error {
if err := reader.EndContainer(); err != nil {
return err
}
{% if has_table_token %}
s.changedFields.SetChanged("{{ alias }}")
{% endif %}
}
{% endif %}
{% elseif type is array %}
@ -767,6 +775,9 @@ func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error {
if err := reader.EndContainer(); err != nil {
return err
}
{% if has_table_token %}
s.changedFields.SetChanged("{{ alias }}")
{% endif %}
}
{% else %}
{{ Error('unkown type ' ~ type) }}