pg_table_repository.go
6.4 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
203
204
205
206
207
208
209
210
211
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"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 TableRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *TableRepository) Save(table *domain.Table) (*domain.Table, error) {
sqlBuildFields := []string{
"table_id",
"table_type",
"name",
"sql_name",
"parent_id",
"data_field_index",
"pk",
"data_fields",
"manual_fields",
"created_at",
"updated_at",
"deleted_at",
"version",
"row_count",
"context",
"table_info",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "table_id", "deleted_at"))
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "table_id", "deleted_at"))
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "table_id", "deleted_at")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if table.Identify() == nil {
if _, err := tx.QueryOne(
pg.Scan(
&table.TableId,
&table.TableType,
&table.Name,
&table.SQLName,
&table.ParentId,
&table.DataFieldIndex,
&table.PK,
&table.DataFields,
&table.ManualFields,
&table.CreatedAt,
&table.UpdatedAt,
&table.DeletedAt,
&table.Version,
&table.RowCount,
&table.Context,
&table.TableInfo,
),
fmt.Sprintf("INSERT INTO metadata.tables (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
table.TableType,
table.Name,
table.SQLName,
table.ParentId,
table.DataFieldIndex,
table.PK,
table.DataFields,
table.ManualFields,
table.CreatedAt,
table.UpdatedAt,
table.Version,
table.RowCount,
table.Context,
table.TableInfo,
); err != nil {
return table, err
}
} else {
oldVersion := table.Version
table.Version += 1
if _, err := tx.QueryOne(
pg.Scan(
&table.TableId,
&table.TableType,
&table.Name,
&table.SQLName,
&table.ParentId,
&table.DataFieldIndex,
&table.PK,
&table.DataFields,
&table.ManualFields,
&table.CreatedAt,
&table.UpdatedAt,
&table.DeletedAt,
&table.Version,
&table.RowCount,
&table.Context,
&table.TableInfo,
),
fmt.Sprintf("UPDATE metadata.tables SET %s WHERE table_id=? and version=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
table.TableType,
table.Name,
table.SQLName,
table.ParentId,
table.DataFieldIndex,
table.PK,
table.DataFields,
table.ManualFields,
table.CreatedAt,
table.UpdatedAt,
table.Version,
table.RowCount,
table.Context,
table.TableInfo,
table.Identify(),
oldVersion,
); err != nil {
return table, err
}
}
return table, nil
}
func (repository *TableRepository) Remove(table *domain.Table) (*domain.Table, error) {
tx := repository.transactionContext.PgTx
tableModel := new(models.Table)
tableModel.TableId = table.Identify().(int)
if _, err := tx.Model(tableModel).WherePK().Delete(); err != nil {
return table, err
}
return table, nil
}
func (repository *TableRepository) FindOne(queryOptions map[string]interface{}) (*domain.Table, error) {
tx := repository.transactionContext.PgTx
tableModel := new(models.Table)
query := sqlbuilder.BuildQuery(tx.Model(tableModel), queryOptions)
WhereContext(query, queryOptions)
query.SetWhereByQueryOption("table_id = ?", "tableId")
query.SetWhereByQueryOption(`table_type = ?`, "tableType")
query.SetWhereByQueryOption("name = ?", "tableName")
query.SetWhereByQueryOption("parent_id = ?", "parentId")
if v, ok := queryOptions["tableTypes"]; ok && len(v.([]string)) > 0 {
query.Where(`table_type in (?)`, pg.In(v.([]string)))
}
if v, ok := queryOptions["dependencyTable"]; ok && v.(int) > 0 {
query.Where(`table_info->'dependencyTables' @> '[?]'`, v.(int))
}
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, domain.ErrorNotFound
} else {
return nil, err
}
}
if tableModel.TableId == 0 {
return nil, nil
} else {
return transform.TransformToTableDomainModelFromPgModels(tableModel)
}
}
func (repository *TableRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Table, error) {
tx := repository.transactionContext.PgTx
var tableModels []*models.Table
tables := make([]*domain.Table, 0)
query := sqlbuilder.BuildQuery(tx.Model(&tableModels), queryOptions)
WhereContext(query, queryOptions)
query.SetWhereByQueryOption(fmt.Sprintf("name like '%%%v%%'", queryOptions["name"]), "name")
query.SetWhereByQueryOption("table_id = ?", "tableId")
query.SetWhereByQueryOption("parent_id = ?", "parentId")
if v, ok := queryOptions["tableIds"]; ok && len(v.([]int)) > 0 {
query.Where(`table_id in (?)`, pg.In(v.([]int)))
}
if v, ok := queryOptions["tableTypes"]; ok && len(v.([]string)) > 0 {
query.Where(`table_type in (?)`, pg.In(v.([]string)))
}
if v, ok := queryOptions["module"]; ok && v.(int) > 0 {
query.Where(`(cast(table_info->>'module' as integer) & ?) >0`, v)
}
if v, ok := queryOptions["dependencyTable"]; ok && v.(int) > 0 {
query.Where(`table_info->'dependencyTables' @> '[?]'`, v.(int))
}
query.SetWhereByQueryOption("created_at >= ?", "beginTime")
query.SetWhereByQueryOption("created_at < ?", "endTime")
//query.SetOffsetAndLimit(20)
query.SetOrderDirect("table_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, tables, err
} else {
for _, tableModel := range tableModels {
if table, err := transform.TransformToTableDomainModelFromPgModels(tableModel); err != nil {
return 0, tables, err
} else {
tables = append(tables, table)
}
}
return int64(count), tables, nil
}
}
func NewTableRepository(transactionContext *pgTransaction.TransactionContext) (*TableRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &TableRepository{
transactionContext: transactionContext,
}, nil
}
}