This repository has been archived by the owner on Jul 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_unmatched_repo_impl.go
126 lines (114 loc) · 3.37 KB
/
metrics_unmatched_repo_impl.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
package repository
import (
"context"
"database/sql"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/typical-go/typical-rest-server/pkg/dbkit"
"go.uber.org/dig"
)
// MetricsUnmatchedRepoImpl is implementation metrics_unmatched repository
type MetricsUnmatchedRepoImpl struct {
dig.In
*sql.DB
}
// Find metrics_unmatched
func (r *MetricsUnmatchedRepoImpl) Find(ctx context.Context, id int64) (e *MetricsUnmatched, err error) {
var rows *sql.Rows
builder := sq.
Select("id", "request_path", "created_at", "updated_at").
From("metrics_unmatched").
Where(sq.Eq{"id": id}).
PlaceholderFormat(sq.Dollar).RunWith(dbkit.TxCtx(ctx, r))
if rows, err = builder.QueryContext(ctx); err != nil {
return
}
if rows.Next() {
e = new(MetricsUnmatched)
if err = rows.Scan(&e.ID, &e.RequestPath, &e.CreatedAt, &e.UpdatedAt); err != nil {
return nil, err
}
}
return
}
// List metrics_unmatched
func (r *MetricsUnmatchedRepoImpl) List(ctx context.Context) (list []*MetricsUnmatched, err error) {
var rows *sql.Rows
builder := sq.
Select("id", "request_path", "created_at", "updated_at").
From("metrics_unmatched").
PlaceholderFormat(sq.Dollar).RunWith(dbkit.TxCtx(ctx, r))
if rows, err = builder.QueryContext(ctx); err != nil {
return
}
list = make([]*MetricsUnmatched, 0)
for rows.Next() {
var e0 MetricsUnmatched
if err = rows.Scan(&e0.ID, &e0.RequestPath, &e0.CreatedAt, &e0.UpdatedAt); err != nil {
return
}
list = append(list, &e0)
}
return
}
func (r *MetricsUnmatchedRepoImpl) ListCount(ctx context.Context) (list []*MetricsUnmatchedCount, err error) {
var rows *sql.Rows
subQuery := sq.
Select("request_path").
Column("count(request_path)").
Column(sq.Alias(sq.Expr("max(created_at)"), "since")).
From("metrics_unmatched").
GroupBy("request_path").
PlaceholderFormat(sq.Dollar)
builder := sq.
Select("request_path", "count", "since").
FromSelect(subQuery, "u").
OrderBy("u.since desc", "u.count desc").
PlaceholderFormat(sq.Dollar).RunWith(dbkit.TxCtx(ctx, r))
if rows, err = builder.QueryContext(ctx); err != nil {
return
}
list = make([]*MetricsUnmatchedCount, 0)
for rows.Next() {
var e0 MetricsUnmatchedCount
if err = rows.Scan(&e0.RequestPath, &e0.Count, &e0.Since); err != nil {
return
}
list = append(list, &e0)
}
return
}
// Insert metrics_unmatched
func (r *MetricsUnmatchedRepoImpl) Insert(ctx context.Context, e MetricsUnmatched) (lastInsertID int64, err error) {
builder := sq.
Insert("metrics_unmatched").
Columns("request_path").
Values(e.RequestPath).
Suffix("RETURNING \"id\"").
PlaceholderFormat(sq.Dollar).RunWith(dbkit.TxCtx(ctx, r))
if err = builder.QueryRowContext(ctx).Scan(&e.ID); err != nil {
return
}
lastInsertID = e.ID
return
}
// Delete metrics_unmatched
func (r *MetricsUnmatchedRepoImpl) Delete(ctx context.Context, id int64) (err error) {
builder := sq.
Delete("metrics_unmatched").
Where(sq.Eq{"id": id}).
PlaceholderFormat(sq.Dollar).RunWith(dbkit.TxCtx(ctx, r))
_, err = builder.ExecContext(ctx)
return
}
// Update metrics_unmatched
func (r *MetricsUnmatchedRepoImpl) Update(ctx context.Context, e MetricsUnmatched) (err error) {
builder := sq.
Update("metrics_unmatched").
Set("request_path", e.RequestPath).
Set("updated_at", time.Now()).
Where(sq.Eq{"id": e.ID}).
PlaceholderFormat(sq.Dollar).RunWith(dbkit.TxCtx(ctx, r))
_, err = builder.ExecContext(ctx)
return
}