-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.go
141 lines (133 loc) · 3.98 KB
/
repository.go
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
package repository
import (
"context"
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"reflect"
q "github.com/core-go/sql"
)
type Repository[T any, K any] struct {
*Writer[*T]
Map map[string]int
Fields string
IdMap bool
}
func NewRepository[T any, K any](db *sql.DB, tableName string, opts ...func(int) string) (*Repository[T, K], error) {
return NewRepositoryWithVersionAndArray[T, K](db, tableName, "", nil, opts...)
}
func NewRepositoryWithVersion[T any, K any](db *sql.DB, tableName string, versionField string, opts ...func(int) string) (*Repository[T, K], error) {
return NewRepositoryWithVersionAndArray[T, K](db, tableName, versionField, nil, opts...)
}
func NewRepositoryWithArray[T any, K any](db *sql.DB, tableName string, toArray func(interface{}) interface {
driver.Valuer
sql.Scanner
}, opts ...func(int) string) (*Repository[T, K], error) {
return NewRepositoryWithVersionAndArray[T, K](db, tableName, "", toArray, opts...)
}
func NewRepositoryWithVersionAndArray[T any, K any](db *sql.DB, tableName string, versionField string, toArray func(interface{}) interface {
driver.Valuer
sql.Scanner
}, opts ...func(int) string) (*Repository[T, K], error) {
repository, err := NewSqlWriterWithVersionAndArray[*T](db, tableName, versionField, toArray, opts...)
if err != nil {
return nil, err
}
var t T
modelType := reflect.TypeOf(t)
if modelType.Kind() != reflect.Struct {
return nil, errors.New("T must be a struct")
}
var k K
kType := reflect.TypeOf(k)
idMap := false
if len(repository.Keys) > 1 {
if kType.Kind() == reflect.Map {
idMap = true
} else if kType.Kind() != reflect.Struct {
return nil, errors.New("for composite keys, K must be a struct or a map")
}
}
fieldsIndex, err := q.GetColumnIndexes(modelType)
if err != nil {
return nil, err
}
fields := q.BuildFieldsBySchema(repository.Schema)
return &Repository[T, K]{repository, fieldsIndex, fields, idMap}, nil
}
func (a *Repository[T, K]) All(ctx context.Context) ([]T, error) {
var objs []T
query := fmt.Sprintf("select %s from %s", a.Fields, a.Table)
tx := q.GetExec(ctx, a.DB, a.TxKey)
err := q.Query(ctx, tx, a.Map, &objs, query)
return objs, err
}
func toMap(obj interface{}) (map[string]interface{}, error) {
b, err := json.Marshal(obj)
if err != nil {
return nil, err
}
im := make(map[string]interface{})
er2 := json.Unmarshal(b, &im)
return im, er2
}
func (a *Repository[T, K]) getId(k K) (interface{}, error) {
if len(a.Keys) >= 2 && !a.IdMap {
ri, err := toMap(k)
return ri, err
} else {
return k, nil
}
}
func (a *Repository[T, K]) Load(ctx context.Context, id K) (*T, error) {
ip, er0 := a.getId(id)
if er0 != nil {
return nil, er0
}
var objs []T
query := fmt.Sprintf("select %s from %s ", a.Fields, a.Table)
query1, args := q.BuildFindByIdWithDB(a.DB, query, ip, a.JsonColumnMap, a.Keys, a.BuildParam)
tx := q.GetExec(ctx, a.DB, a.TxKey)
err := q.QueryWithArray(ctx, tx, a.Map, &objs, a.ToArray, query1, args...)
if err != nil {
return nil, err
}
if len(objs) > 0 {
return &objs[0], nil
}
return nil, nil
}
func (a *Repository[T, K]) Exist(ctx context.Context, id K) (bool, error) {
ip, er0 := a.getId(id)
if er0 != nil {
return false, er0
}
query := fmt.Sprintf("select %s from %s ", a.Schema.SColumns[0], a.Table)
query1, args := q.BuildFindByIdWithDB(a.DB, query, ip, a.JsonColumnMap, a.Keys, a.BuildParam)
tx := q.GetExec(ctx, a.DB, a.TxKey)
rows, err := tx.QueryContext(ctx, query1, args...)
if err != nil {
return false, err
}
defer rows.Close()
for rows.Next() {
return true, nil
}
return false, nil
}
func (a *Repository[T, K]) Delete(ctx context.Context, id K) (int64, error) {
ip, er0 := a.getId(id)
if er0 != nil {
return -1, er0
}
query := fmt.Sprintf("delete from %s ", a.Table)
query1, args := q.BuildFindByIdWithDB(a.DB, query, ip, a.JsonColumnMap, a.Keys, a.BuildParam)
tx := q.GetExec(ctx, a.DB, a.TxKey)
res, err := tx.ExecContext(ctx, query1, args...)
if err != nil {
return -1, err
}
return res.RowsAffected()
}