Add support for SELECT_JSON statements.

This commit is contained in:
go-jet 2025-02-21 19:55:01 +01:00
parent 7047de44a9
commit 7b16e432ff
46 changed files with 2732 additions and 307 deletions

View file

@ -148,6 +148,11 @@ var NTH_VALUE = jet.NTH_VALUE
//--------------------- String functions ------------------//
// HEX function in MySQL takes an input and returns its equivalent hexadecimal representation
func HEX(expression Expression) StringExpression {
return StringExp(Func("HEX", expression))
}
// BIT_LENGTH returns number of bits in string expression
var BIT_LENGTH = jet.BIT_LENGTH

79
mysql/select_json.go Normal file
View file

@ -0,0 +1,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.JsonProjectionList(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
}

View file

@ -62,12 +62,12 @@ type SelectStatement interface {
// SELECT creates new SelectStatement with list of projections
func SELECT(projection Projection, projections ...Projection) SelectStatement {
return newSelectStatement(nil, append([]Projection{projection}, projections...))
return newSelectStatement(jet.SelectStatementType, nil, append([]Projection{projection}, projections...))
}
func newSelectStatement(table ReadableTable, projections []Projection) SelectStatement {
func newSelectStatement(stmtType jet.StatementType, table ReadableTable, projections []Projection) *selectStatementImpl {
newSelect := &selectStatementImpl{}
newSelect.ExpressionStatement = jet.NewExpressionStatementImpl(Dialect, jet.SelectStatementType, newSelect,
newSelect.ExpressionStatement = jet.NewExpressionStatementImpl(Dialect, stmtType, newSelect,
&newSelect.Select,
&newSelect.From,
&newSelect.Where,

View file

@ -50,7 +50,7 @@ type readableTableInterfaceImpl struct {
// Generates a select query on the current tableName.
func (r readableTableInterfaceImpl) SELECT(projection1 Projection, projections ...Projection) SelectStatement {
return newSelectStatement(r.parent, append([]Projection{projection1}, projections...))
return newSelectStatement(jet.SelectStatementType, r.parent, append([]Projection{projection1}, projections...))
}
// Creates a inner join tableName Expression using onCondition.