-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathselect_json.go
132 lines (101 loc) · 3.65 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
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
package postgres
import (
"github.com/go-jet/jet/v2/internal/jet"
"strings"
)
// SELECT_JSON_ARR creates a new SelectJsonStatement with a list of projections.
func SELECT_JSON_ARR(projections ...Projection) SelectStatement {
return newSelectStatementJson(projections, jet.SelectJsonArrStatementType)
}
// SELECT_JSON_OBJ creates a new SelectJsonStatement with a list of projections.
func SELECT_JSON_OBJ(projections ...Projection) SelectStatement {
return newSelectStatementJson(projections, jet.SelectJsonObjStatementType)
}
type selectJsonStatement struct {
*selectStatementImpl
subQuery *selectStatementImpl
statementType jet.StatementType
}
func (s *selectJsonStatement) AS(alias string) Projection {
s.setSubQueryAlias(strings.ToLower(alias) + "_")
return s.selectStatementImpl.AS(alias)
}
func (s *selectJsonStatement) FROM(table ...ReadableTable) SelectStatement {
s.subQuery.From.Tables = readableTablesToSerializerList(table)
return s
}
func (s *selectJsonStatement) DISTINCT(on ...jet.ColumnExpression) SelectStatement {
s.subQuery.Select.Distinct = true
s.subQuery.Select.DistinctOnColumns = on
return s
}
func (s *selectJsonStatement) WHERE(condition BoolExpression) SelectStatement {
s.subQuery.Where.Condition = condition
return s
}
func (s *selectJsonStatement) GROUP_BY(groupByClauses ...GroupByClause) SelectStatement {
s.subQuery.GroupBy.List = groupByClauses
return s
}
func (s *selectJsonStatement) HAVING(boolExpression BoolExpression) SelectStatement {
s.subQuery.Having.Condition = boolExpression
return s
}
func (s *selectJsonStatement) WINDOW(name string) windowExpand {
s.subQuery.Window.Definitions = append(s.subQuery.Window.Definitions, jet.WindowDefinition{Name: name})
return windowExpand{
selectStatement: s.subQuery,
rootStmt: s,
}
}
func (s *selectJsonStatement) ORDER_BY(orderByClauses ...OrderByClause) SelectStatement {
s.subQuery.OrderBy.List = orderByClauses
return s
}
func (s *selectJsonStatement) LIMIT(limit int64) SelectStatement {
s.subQuery.Limit.Count = limit
return s
}
func (s *selectJsonStatement) OFFSET(offset int64) SelectStatement {
s.subQuery.Offset.Count = Int(offset)
return s
}
func (s *selectJsonStatement) OFFSET_e(offset IntegerExpression) SelectStatement {
s.subQuery.Offset.Count = offset
return s
}
func (s *selectJsonStatement) FETCH_FIRST(count IntegerExpression) fetchExpand {
s.subQuery.Fetch.Count = count
return fetchExpand{
selectStatement: s.subQuery,
rootStmt: s,
}
}
func (s *selectJsonStatement) FOR(lock RowLock) SelectStatement {
s.subQuery.For.Lock = lock
return s
}
func newSelectStatementJson(projections []Projection, statementType jet.StatementType) SelectStatement {
newSelectJson := &selectJsonStatement{
selectStatementImpl: newSelectStatement(statementType, nil, nil),
subQuery: newSelectStatement(statementType, nil, projections),
statementType: statementType,
}
newSelectJson.setOperatorsImpl.stmtRoot = newSelectJson
newSelectJson.subQuery.Select.IsForRowToJson = true
newSelectJson.setSubQueryAlias("")
return newSelectJson
}
func (s *selectJsonStatement) setSubQueryAlias(alias string) {
subQueryAlias := alias + "records"
jsonAlias := alias + "json"
s.Select.ProjectionList = ProjectionList{constructJsonFunc(s.statementType, subQueryAlias).AS(jsonAlias)}
s.From.Tables = []jet.Serializer{newSelectTable(s.subQuery, subQueryAlias, nil)}
}
func constructJsonFunc(statementType jet.StatementType, subQueryAlias string) Expression {
rowToJson := Func("row_to_json", CustomExpression(Token(subQueryAlias)))
if statementType == jet.SelectJsonArrStatementType {
return Func("json_agg", rowToJson)
}
return rowToJson
}