2025-02-21 19:55:01 +01:00
|
|
|
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
|
2026-01-16 14:11:22 +01:00
|
|
|
|
|
|
|
|
projections []Projection
|
|
|
|
|
statementType jet.StatementType
|
|
|
|
|
|
|
|
|
|
// SELECT_JSON_ARR internal clauses
|
|
|
|
|
arrOrderBy *jet.ClauseOrderBy
|
|
|
|
|
arrLimit *jet.ClauseLimit
|
|
|
|
|
arrOffset *jet.ClauseOffset
|
2025-02-21 19:55:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newSelectStatementJson(projections []Projection, statementType jet.StatementType) SelectJsonStatement {
|
2026-01-16 14:11:22 +01:00
|
|
|
newSelectJson := &selectJsonStatement{
|
2025-02-21 19:55:01 +01:00
|
|
|
selectStatementImpl: newSelectStatement(statementType, nil, nil),
|
2026-01-16 14:11:22 +01:00
|
|
|
|
|
|
|
|
projections: projections,
|
|
|
|
|
statementType: statementType,
|
|
|
|
|
|
|
|
|
|
arrOrderBy: &jet.ClauseOrderBy{},
|
|
|
|
|
arrLimit: &jet.ClauseLimit{Count: -1},
|
|
|
|
|
arrOffset: &jet.ClauseOffset{},
|
2025-02-21 19:55:01 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 14:11:22 +01:00
|
|
|
newSelectJson.constructProjectionList()
|
2025-02-21 19:55:01 +01:00
|
|
|
|
2026-01-16 14:11:22 +01:00
|
|
|
return newSelectJson
|
2025-02-21 19:55:01 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 14:11:22 +01:00
|
|
|
func (s *selectJsonStatement) constructProjectionList() {
|
|
|
|
|
jsonProjection := Func("JSON_OBJECT", CustomExpression(jet.JsonObjProjectionList(s.projections)))
|
2025-02-21 19:55:01 +01:00
|
|
|
|
2026-01-16 14:11:22 +01:00
|
|
|
if s.statementType == jet.SelectJsonArrStatementType {
|
|
|
|
|
jsonProjection = Func("JSON_ARRAYAGG", CustomExpression(
|
|
|
|
|
jsonProjection,
|
|
|
|
|
s.arrOrderBy,
|
|
|
|
|
s.arrLimit,
|
|
|
|
|
s.arrOffset,
|
|
|
|
|
))
|
2025-02-21 19:55:01 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 14:11:22 +01:00
|
|
|
s.Select.ProjectionList = ProjectionList{jsonProjection.AS("json")}
|
2025-02-21 19:55:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 14:11:22 +01:00
|
|
|
func (s *selectJsonStatement) ORDER_BY(orderBy ...OrderByClause) SelectJsonStatement {
|
|
|
|
|
if s.statementType == jet.SelectJsonArrStatementType {
|
|
|
|
|
s.arrOrderBy.List = orderBy
|
|
|
|
|
} else {
|
|
|
|
|
s.OrderBy.List = orderBy
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *selectJsonStatement) LIMIT(limit int64) SelectJsonStatement {
|
2026-01-16 14:11:22 +01:00
|
|
|
if s.statementType == jet.SelectJsonArrStatementType {
|
|
|
|
|
s.arrLimit.Count = limit
|
|
|
|
|
} else {
|
|
|
|
|
s.Limit.Count = limit
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *selectJsonStatement) OFFSET(offset int64) SelectJsonStatement {
|
2026-01-16 14:11:22 +01:00
|
|
|
if s.statementType == jet.SelectJsonArrStatementType {
|
|
|
|
|
s.arrOffset.Count = Int(offset)
|
|
|
|
|
} else {
|
|
|
|
|
s.Offset.Count = Int(offset)
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
return s
|
|
|
|
|
}
|