pg_query_set_repository.go
6.6 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package repository
import (
"errors"
"fmt"
"github.com/go-pg/pg/v10"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/snowflake"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg/transform"
)
type QuerySetRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *QuerySetRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
func (repository *QuerySetRepository) Save(querySet *domain.QuerySet) (*domain.QuerySet, error) {
sqlBuildFields := []string{
"query_set_id",
"type",
"flag",
"name",
"pin_name",
"parent_id",
"status",
"query_set_info",
"query_components",
"sort",
"created_at",
"updated_at",
"context",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "query_set_id", "deleted_at"))
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "query_set_id", "deleted_at"))
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "query_set_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if querySet.Identify() == nil {
if _, err := tx.QueryOne(
pg.Scan(
&querySet.QuerySetId,
&querySet.Type,
&querySet.Flag,
&querySet.Name,
&querySet.PinName,
&querySet.ParentId,
&querySet.Status,
&querySet.QuerySetInfo,
&querySet.QueryComponents,
&querySet.Sort,
&querySet.CreatedAt,
&querySet.UpdatedAt,
&querySet.Context,
),
fmt.Sprintf("INSERT INTO metadata.query_sets (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
querySet.Type,
querySet.Flag,
querySet.Name,
querySet.PinName,
querySet.ParentId,
querySet.Status,
querySet.QuerySetInfo,
querySet.QueryComponents,
querySet.Sort,
querySet.CreatedAt,
querySet.UpdatedAt,
querySet.Context,
); err != nil {
return querySet, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&querySet.QuerySetId,
&querySet.Type,
&querySet.Flag,
&querySet.Name,
&querySet.PinName,
&querySet.ParentId,
&querySet.Status,
&querySet.QuerySetInfo,
&querySet.QueryComponents,
&querySet.Sort,
&querySet.CreatedAt,
&querySet.UpdatedAt,
&querySet.Context,
),
fmt.Sprintf("UPDATE metadata.query_sets SET %s WHERE query_set_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
querySet.Type,
querySet.Flag,
querySet.Name,
querySet.PinName,
querySet.ParentId,
querySet.Status,
querySet.QuerySetInfo,
querySet.QueryComponents,
querySet.Sort,
querySet.CreatedAt,
querySet.UpdatedAt,
querySet.Context,
querySet.Identify(),
); err != nil {
return querySet, err
}
}
return querySet, nil
}
func (repository *QuerySetRepository) Remove(querySet *domain.QuerySet) (*domain.QuerySet, error) {
tx := repository.transactionContext.PgTx
querySetModel := new(models.QuerySet)
querySetModel.QuerySetId = querySet.Identify().(int)
if _, err := tx.Model(querySetModel).WherePK().Delete(); err != nil {
return querySet, err
}
return querySet, nil
}
func (repository *QuerySetRepository) FindOne(queryOptions map[string]interface{}) (*domain.QuerySet, error) {
tx := repository.transactionContext.PgTx
querySetModel := new(models.QuerySet)
query := sqlbuilder.BuildQuery(tx.Model(querySetModel), queryOptions)
WhereContext(query, queryOptions)
query.SetWhereByQueryOption("query_set_id = ?", "querySetId")
query.SetWhereByQueryOption("query_set_info->'BindTableId' = '?'", "BindTableId")
query.SetWhereByQueryOption("type = ?", "type")
query.SetWhereByQueryOption("flag = ?", "flag")
query.SetWhereByQueryOption("parent_id = ?", "parentId")
query.SetWhereByQueryOption("name = ?", "name")
if err := query.First(); err != nil {
if errors.Is(err, pg.ErrNoRows) {
return nil, domain.ErrorNotFound
} else {
return nil, err
}
}
if querySetModel.QuerySetId == 0 {
return nil, nil
} else {
return transform.TransformToQuerySetDomainModelFromPgModels(querySetModel)
}
}
func (repository *QuerySetRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.QuerySet, error) {
tx := repository.transactionContext.PgTx
var querySetModels []*models.QuerySet
querySets := make([]*domain.QuerySet, 0)
query := sqlbuilder.BuildQuery(tx.Model(&querySetModels), queryOptions)
WhereContext(query, queryOptions)
query.SetWhereByQueryOption("type = ?", "type")
query.SetWhereByQueryOption("flag = ?", "flag")
query.SetWhereByQueryOption("status = ?", "status")
query.SetWhereByQueryOption(fmt.Sprintf("name like '%%%v%%'", queryOptions["matchName"]), "matchName")
if v, ok := queryOptions["types"]; ok && len(v.([]string)) > 0 {
query.Where("type in (?)", pg.In(v))
}
if v, ok := queryOptions["inParentIds"]; ok && len(v.([]int)) > 0 {
query.Where("parent_id in (?)", pg.In(v))
}
if v, ok := queryOptions["bindTableIds"]; ok && len(v.([]int)) > 0 {
query.Where("query_set_info->'BindTableId' in (?)", pg.In(utils.ToArrayString(v.([]int))))
}
if v, ok := queryOptions["sortByName"]; ok && len(v.(string)) > 0 {
query.SetOrderDirect("pin_name", v.(string))
} else if v, ok := queryOptions["sortByTime"]; ok && len(v.(string)) > 0 {
query.SetOrderDirect("created_at", v.(string))
} else {
query.SetOrderDirect("parent_id", "ASC")
query.SetOrderDirect("sort", "ASC")
}
if count, err := query.SelectAndCount(); err != nil {
return 0, querySets, err
} else {
for _, querySetModel := range querySetModels {
if querySet, err := transform.TransformToQuerySetDomainModelFromPgModels(querySetModel); err != nil {
return 0, querySets, err
} else {
querySets = append(querySets, querySet)
}
}
return int64(count), querySets, nil
}
}
func NewQuerySetRepository(transactionContext *pgTransaction.TransactionContext) (*QuerySetRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &QuerySetRepository{
transactionContext: transactionContext,
}, nil
}
}