gen code for loading @data_root struct

This commit is contained in:
Pavel Merzlyakov 2023-07-26 17:49:47 +03:00
parent 6bc05545b1
commit f20ab00a7b
1 changed files with 21 additions and 0 deletions

View File

@ -14,6 +14,7 @@
{{ _self.root_delete(s) }}
{{ _self.root_save_diff(s) }}
{{ _self.root_delete_diff(s) }}
{{ _self.root_load(s) }}
{% endif %}
{% endmacro struct %}
@ -259,6 +260,26 @@ func Delete{{ s.name }}Diff(ctx context.Context, dbe metadb.Execer, ids {{ s.nam
{% endmacro root_delete_diff %}
{% macro root_load(s) %}
{% set load_by = meta_field(token(s, 'data_root')) %}
func Load{{ s.name }}(ctx context.Context, dbq metadb.Querier, {{ load_by|varname }} {{ load_by.type|go_type }}) ({{ s.name }}, error) {
var s {{ s.name }}
{%~ for f in s.fields %}
{%~ if f.type is array %}
{{ f|varname }}, err := Load{{ f.type.value.name }}Collection(ctx, dbq, {{ load_by|varname }})
{%~ else %}
{{ f|varname }}, err := Load{{ f.type.name }}(ctx, dbq, {{ load_by|varname }})
{%~ endif %}
if err != nil {
return {{ s.name }}{}, err
}
s.{{ f|fname }} = {{ f|varname }}
{%~ endfor %}
return s, nil
}
{% endmacro root_load %}
{% macro table_save(ctx) %}
func Save{{ ctx.s.name }}(ctx context.Context, dbe metadb.Execer, rec {{ ctx.s.name }}) error {
query := "INSERT INTO `{{ ctx.table_name }}` ({{ ctx.insert_column_expr }}) VALUES ({{ ctx.insert_values_expr }}) ON DUPLICATE KEY UPDATE {{ ctx.insert_update_expr }}"