forked from Juniper/contrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_common.tmpl
59 lines (50 loc) · 1.94 KB
/
db_common.tmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package db
import (
"context"
"fmt"
"github.com/Juniper/contrail/pkg/db/basedb"
"github.com/Juniper/contrail/pkg/models/basemodels"
"github.com/Juniper/contrail/pkg/services/baseservices"
"{{ option.PackagePath }}/pkg/services"
)
func (db *Service) initQueryBuilders() {
queryBuilders := map[string]*basedb.QueryBuilder{}
db.QueryBuilders = queryBuilders
{% for schema in schemas %}{% if schema.Type != "abstract" and schema.ID %}
queryBuilders["{{ schema.ID }}"] = basedb.NewQueryBuilder(db.Dialect,
"{{ schema.ID | lower }}",
{{ schema.JSONSchema.GoName }}Fields,
{{ schema.JSONSchema.GoName }}RefFields,
{{ schema.JSONSchema.GoName }}ChildFields,
{{ schema.JSONSchema.GoName }}BackRefFields)
{% endif%}{% endfor %}
}
func (db *Service) dump(ctx context.Context, ow basedb.ObjectWriter) error {
{% for schema in schemas %}{% if schema.Type != "abstract" and schema.ID %}
list{{ schema.JSONSchema.GoName }}Response, err := db.list{{ schema.JSONSchema.GoName }}(
ctx,
&services.List{{ schema.JSONSchema.GoName }}Request{
Spec: &baseservices.ListSpec{
Detail: true,
},
},
)
if err != nil {
return err
}
for _, item := range list{{ schema.JSONSchema.GoName }}Response.{{ schema.JSONSchema.GoName }}s {
if err = ow.WriteObject("{{ schema.ID }}", item.GetUUID(), item); err != nil {
return err
}
}
{% endif%}{% endfor %}
return nil
}
// ScanRow maps row from database table named schemaID into generated Go struct.
func (db *Service) ScanRow(schemaID string, rowData map[string]interface{}) (basemodels.Object, error) {
switch schemaID { {% for schema in schemas %}{% if schema.Type != "abstract" and schema.ID %}
case "{{ schema.ID }}":
return db.scan{{schema.JSONSchema.GoName}}(rowData){% endif%}{% endfor %}
}
return nil, fmt.Errorf("unknown schemaID: %v", schemaID)
}