-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathselect_json.go
79 lines (60 loc) · 2.24 KB
/
select_json.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
package mysql
import (
"github.com/go-jet/jet/v2/internal/jet"
)
// SelectJsonStatement is an interface for MySQL statements that generate JSON on the server.
type SelectJsonStatement interface {
Statement
jet.Serializer
AS(alias string) Projection
FROM(table ReadableTable) SelectJsonStatement
WHERE(condition BoolExpression) SelectJsonStatement
ORDER_BY(orderByClauses ...OrderByClause) SelectJsonStatement
LIMIT(limit int64) SelectJsonStatement
OFFSET(offset int64) SelectJsonStatement
}
// SELECT_JSON_ARR creates a new SelectJsonStatement with a list of projections.
func SELECT_JSON_ARR(projections ...Projection) SelectJsonStatement {
return newSelectStatementJson(projections, jet.SelectJsonArrStatementType)
}
// SELECT_JSON_OBJ creates a new SelectJsonStatement with a list of projections.
func SELECT_JSON_OBJ(projections ...Projection) SelectJsonStatement {
return newSelectStatementJson(projections, jet.SelectJsonObjStatementType)
}
type selectJsonStatement struct {
*selectStatementImpl
}
func newSelectStatementJson(projections []Projection, statementType jet.StatementType) SelectJsonStatement {
newSelect := &selectJsonStatement{
selectStatementImpl: newSelectStatement(statementType, nil, nil),
}
newSelect.Select.ProjectionList = ProjectionList{constructJsonFunc(projections, statementType).AS("json")}
return newSelect
}
func constructJsonFunc(projections []Projection, statementType jet.StatementType) Expression {
jsonObj := Func("JSON_OBJECT", CustomExpression(jet.JsonObjProjectionList(projections)))
if statementType == jet.SelectJsonArrStatementType {
return Func("JSON_ARRAYAGG", jsonObj)
}
return jsonObj
}
func (s *selectJsonStatement) FROM(table ReadableTable) SelectJsonStatement {
s.From.Tables = []jet.Serializer{table}
return s
}
func (s *selectJsonStatement) WHERE(condition BoolExpression) SelectJsonStatement {
s.Where.Condition = condition
return s
}
func (s *selectJsonStatement) ORDER_BY(orderByClauses ...OrderByClause) SelectJsonStatement {
s.OrderBy.List = orderByClauses
return s
}
func (s *selectJsonStatement) LIMIT(limit int64) SelectJsonStatement {
s.Limit.Count = limit
return s
}
func (s *selectJsonStatement) OFFSET(offset int64) SelectJsonStatement {
s.Offset.Count = Int(offset)
return s
}