Encode json values implicitly in the sql queries according the golang json package spec.

This commit is contained in:
go-jet 2025-03-08 19:01:37 +01:00
parent 9616bb5cfe
commit 17646ca99c
54 changed files with 1446 additions and 744 deletions

View file

@ -6,7 +6,7 @@ import (
"github.com/go-jet/jet/v2/internal/jet"
)
// Dialect is implementation of MySQL dialect for SQL Builder serialisation.
// Dialect is implementation of MySQL dialect for SQL Builder serialization.
var Dialect = newDialect()
func newDialect() jet.Dialect {
@ -34,6 +34,23 @@ func newDialect() jet.Dialect {
ValuesDefaultColumnName: func(index int) string {
return fmt.Sprintf("column_%d", index)
},
JsonValueEncode: func(expr Expression) Expression {
switch e := expr.(type) {
case BlobExpression:
return TO_BASE64(e)
// CustomExpression used bellow (instead DATE_FORMAT function) so that only expr is parametrized
case TimestampExpression:
return CustomExpression(Token("DATE_FORMAT("), e, Token(",'%Y-%m-%dT%H:%i:%s.%fZ')"))
case TimeExpression:
return CustomExpression(Token("CONCAT('0000-01-01T', DATE_FORMAT("), e, Token(",'%H:%i:%s.%fZ'))"))
case DateExpression:
return CustomExpression(Token("CONCAT(DATE_FORMAT("), e, Token(",'%Y-%m-%d')"), Token(", 'T00:00:00Z')"))
case BoolExpression:
return CustomExpression(e, Token(" = 1"))
}
return expr
},
}
return jet.NewDialect(mySQLDialectParams)

View file

@ -98,13 +98,10 @@ func INTERVAL(value interface{}, unitType unitType) Interval {
// INTERVALe creates new temporal interval from expresion and unit type.
func INTERVALe(expr Expression, unitType unitType) Interval {
return jet.NewInterval(jet.ListSerializer{
Serializers: []jet.Serializer{expr, jet.RawWithParent(string(unitType))},
Separator: " ",
})
return jet.IntervalExp(CustomExpression(Token("INTERVAL"), expr, Token(unitType)))
}
// INTERVALd temoral interval from time.Duration
// INTERVALd creates new temporal interval from time.Duration
func INTERVALd(duration time.Duration) Interval {
var sign int64 = 1
if duration < 0 {

View file

@ -43,7 +43,7 @@ func newSelectStatementJson(projections []Projection, statementType jet.Statemen
}
func constructJsonFunc(projections []Projection, statementType jet.StatementType) Expression {
jsonObj := Func("JSON_OBJECT", CustomExpression(jet.JsonProjectionList(projections)))
jsonObj := Func("JSON_OBJECT", CustomExpression(jet.JsonObjProjectionList(projections)))
if statementType == jet.SelectJsonArrStatementType {
return Func("JSON_ARRAYAGG", jsonObj)