Simplify literal expressions.
This commit is contained in:
parent
4995a90483
commit
0e495a279e
26 changed files with 233 additions and 616 deletions
|
|
@ -7,21 +7,19 @@ import (
|
|||
"github.com/go-jet/jet/v2/internal/jet"
|
||||
)
|
||||
|
||||
type cast struct {
|
||||
jet.Cast
|
||||
}
|
||||
|
||||
// CAST function converts an expr (of any type) into later specified datatype.
|
||||
func CAST(expr Expression) *cast {
|
||||
ret := &cast{}
|
||||
ret.Cast = jet.NewCastImpl(expr)
|
||||
|
||||
return ret
|
||||
return &cast{
|
||||
expr: expr,
|
||||
}
|
||||
}
|
||||
|
||||
type cast struct {
|
||||
expr Expression
|
||||
}
|
||||
|
||||
// AS casts expression as castType
|
||||
func (b *cast) AS(castType string) Expression {
|
||||
return b.Cast.AS(castType)
|
||||
return jet.AtomicCustomExpression(b.expr, Token("::"+castType))
|
||||
}
|
||||
|
||||
// AS_BOOL casts expression as bool type
|
||||
|
|
|
|||
|
|
@ -13,15 +13,10 @@ var Dialect = newDialect()
|
|||
|
||||
func newDialect() jet.Dialect {
|
||||
|
||||
operatorSerializeOverrides := map[string]jet.SerializeOverride{}
|
||||
operatorSerializeOverrides[jet.StringRegexpLikeOperator] = postgresREGEXPLIKEoperator
|
||||
operatorSerializeOverrides[jet.StringNotRegexpLikeOperator] = postgresNOTREGEXPLIKEoperator
|
||||
operatorSerializeOverrides["CAST"] = postgresCAST
|
||||
|
||||
dialectParams := jet.DialectParams{
|
||||
Name: "PostgreSQL",
|
||||
PackageName: "postgres",
|
||||
OperatorSerializeOverrides: operatorSerializeOverrides,
|
||||
OperatorSerializeOverrides: nil,
|
||||
AliasQuoteChar: '"',
|
||||
IdentifierQuoteChar: '"',
|
||||
ArgumentPlaceholder: func(ord int) string {
|
||||
|
|
@ -49,6 +44,7 @@ func newDialect() jet.Dialect {
|
|||
}
|
||||
return expr
|
||||
},
|
||||
RegexpLike: regexpLike,
|
||||
}
|
||||
|
||||
return jet.NewDialect(dialectParams)
|
||||
|
|
@ -63,80 +59,23 @@ func argumentToString(value any) (string, bool) {
|
|||
return "", false
|
||||
}
|
||||
|
||||
func postgresCAST(expressions ...jet.Serializer) jet.SerializerFunc {
|
||||
func regexpLike(str jet.StringExpression, not bool, pattern jet.StringExpression, caseSensitive bool) jet.SerializerFunc {
|
||||
return func(statement jet.StatementType, out *jet.SQLBuilder, options ...jet.SerializeOption) {
|
||||
if len(expressions) < 2 {
|
||||
panic("jet: invalid number of expressions for operator")
|
||||
}
|
||||
jet.Serialize(str, statement, out, options...)
|
||||
|
||||
expression := expressions[0]
|
||||
var notOperator string
|
||||
|
||||
litExpr, ok := expressions[1].(jet.LiteralExpression)
|
||||
|
||||
if !ok {
|
||||
panic("jet: cast invalid cast type")
|
||||
}
|
||||
|
||||
castType, ok := litExpr.Value().(string)
|
||||
|
||||
if !ok {
|
||||
panic("jet: cast type is not string")
|
||||
}
|
||||
|
||||
jet.Serialize(expression, statement, out, options...)
|
||||
out.WriteString("::" + castType)
|
||||
}
|
||||
}
|
||||
|
||||
func postgresREGEXPLIKEoperator(expressions ...jet.Serializer) jet.SerializerFunc {
|
||||
return func(statement jet.StatementType, out *jet.SQLBuilder, options ...jet.SerializeOption) {
|
||||
if len(expressions) < 2 {
|
||||
panic("jet: invalid number of expressions for operator")
|
||||
}
|
||||
|
||||
jet.Serialize(expressions[0], statement, out, options...)
|
||||
|
||||
caseSensitive := false
|
||||
|
||||
if len(expressions) >= 3 {
|
||||
if stringLiteral, ok := expressions[2].(jet.LiteralExpression); ok {
|
||||
caseSensitive = stringLiteral.Value().(bool)
|
||||
}
|
||||
if not {
|
||||
notOperator = "!"
|
||||
}
|
||||
|
||||
if caseSensitive {
|
||||
out.WriteString("~")
|
||||
out.WriteString(notOperator + "~")
|
||||
} else {
|
||||
out.WriteString("~*")
|
||||
out.WriteString(notOperator + "~*")
|
||||
}
|
||||
|
||||
jet.Serialize(expressions[1], statement, out, options...)
|
||||
}
|
||||
}
|
||||
|
||||
func postgresNOTREGEXPLIKEoperator(expressions ...jet.Serializer) jet.SerializerFunc {
|
||||
return func(statement jet.StatementType, out *jet.SQLBuilder, options ...jet.SerializeOption) {
|
||||
if len(expressions) < 2 {
|
||||
panic("jet: invalid number of expressions for operator")
|
||||
}
|
||||
|
||||
jet.Serialize(expressions[0], statement, out, options...)
|
||||
|
||||
caseSensitive := false
|
||||
|
||||
if len(expressions) >= 3 {
|
||||
if stringLiteral, ok := expressions[2].(jet.LiteralExpression); ok {
|
||||
caseSensitive = stringLiteral.Value().(bool)
|
||||
}
|
||||
}
|
||||
|
||||
if caseSensitive {
|
||||
out.WriteString("!~")
|
||||
} else {
|
||||
out.WriteString("!~*")
|
||||
}
|
||||
|
||||
jet.Serialize(expressions[1], statement, out, options...)
|
||||
jet.Serialize(pattern, statement, out, options...)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ func TestRawInvalidArguments(t *testing.T) {
|
|||
|
||||
func TestRawHelperMethods(t *testing.T) {
|
||||
assertSerialize(t, RawBool("table.colInt < :float", RawArgs{":float": 11.22}).IS_FALSE(),
|
||||
"(table.colInt < $1) IS FALSE", 11.22)
|
||||
"((table.colInt < $1) IS FALSE)", 11.22)
|
||||
|
||||
assertSerialize(t, RawFloat("table.colInt + :float", RawArgs{":float": 11.22}).EQ(Float(3.14)),
|
||||
"((table.colInt + $1) = $2)", 11.22, 3.14)
|
||||
|
|
|
|||
|
|
@ -184,12 +184,12 @@ var CHR = jet.CHR
|
|||
|
||||
// CONCAT adds two or more expressions together
|
||||
var CONCAT = func(expressions ...Expression) StringExpression {
|
||||
return jet.CONCAT(explicitLiteralCasts(expressions...)...)
|
||||
return jet.CONCAT(expressions...)
|
||||
}
|
||||
|
||||
// CONCAT_WS adds two or more expressions together with a separator.
|
||||
func CONCAT_WS(separator Expression, expressions ...Expression) StringExpression {
|
||||
return jet.CONCAT_WS(explicitLiteralCast(separator), explicitLiteralCasts(expressions...)...)
|
||||
return jet.CONCAT_WS(separator, expressions...)
|
||||
}
|
||||
|
||||
// Character encodings for CONVERT, CONVERT_FROM and CONVERT_TO functions
|
||||
|
|
@ -239,7 +239,7 @@ var DECODE = jet.DECODE
|
|||
|
||||
// FORMAT formats the arguments according to a format string. This function is similar to the C function sprintf.
|
||||
func FORMAT(formatStr StringExpression, formatArgs ...Expression) StringExpression {
|
||||
return jet.FORMAT(formatStr, explicitLiteralCasts(formatArgs...)...)
|
||||
return jet.FORMAT(formatStr, formatArgs...)
|
||||
}
|
||||
|
||||
// INITCAP converts the first letter of each word to upper case
|
||||
|
|
@ -578,55 +578,19 @@ var EXISTS = jet.EXISTS
|
|||
// CASE create CASE operator with optional list of expressions
|
||||
var CASE = jet.CASE
|
||||
|
||||
func explicitLiteralCasts(expressions ...Expression) []jet.Expression {
|
||||
ret := []jet.Expression{}
|
||||
|
||||
for _, exp := range expressions {
|
||||
ret = append(ret, explicitLiteralCast(exp))
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func explicitLiteralCast(expresion Expression) jet.Expression {
|
||||
if _, ok := expresion.(jet.LiteralExpression); !ok {
|
||||
return expresion
|
||||
}
|
||||
|
||||
switch expresion.(type) {
|
||||
case jet.BoolExpression:
|
||||
return CAST(expresion).AS_BOOL()
|
||||
case jet.IntegerExpression:
|
||||
return CAST(expresion).AS_INTEGER()
|
||||
case jet.FloatExpression:
|
||||
return CAST(expresion).AS_NUMERIC()
|
||||
case jet.StringExpression:
|
||||
return CAST(expresion).AS_TEXT()
|
||||
}
|
||||
|
||||
return expresion
|
||||
}
|
||||
|
||||
// MODE computes the most frequent value of the aggregated argument
|
||||
var MODE = jet.MODE
|
||||
|
||||
// PERCENTILE_CONT computes a value corresponding to the specified fraction within the ordered set of
|
||||
// aggregated argument values. This will interpolate between adjacent input items if needed.
|
||||
func PERCENTILE_CONT(fraction FloatExpression) *jet.OrderSetAggregateFunc {
|
||||
return jet.PERCENTILE_CONT(castFloatLiteral(fraction))
|
||||
return jet.PERCENTILE_CONT(fraction)
|
||||
}
|
||||
|
||||
// PERCENTILE_DISC computes the first value within the ordered set of aggregated argument values whose position
|
||||
// in the ordering equals or exceeds the specified fraction. The aggregated argument must be of a sortable type.
|
||||
func PERCENTILE_DISC(fraction FloatExpression) *jet.OrderSetAggregateFunc {
|
||||
return jet.PERCENTILE_DISC(castFloatLiteral(fraction))
|
||||
}
|
||||
|
||||
func castFloatLiteral(fraction FloatExpression) FloatExpression {
|
||||
if _, ok := fraction.(jet.LiteralExpression); ok {
|
||||
return CAST(fraction).AS_DOUBLE() // to make postgres aware of the type
|
||||
}
|
||||
return fraction
|
||||
return jet.PERCENTILE_DISC(fraction)
|
||||
}
|
||||
|
||||
// ----------------- Group By operators --------------------------//
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ func TestINTERVAL(t *testing.T) {
|
|||
assertSerialize(t, INTERVAL(1, YEAR, 10, MONTH, 20, DAY), "INTERVAL '1 YEAR 10 MONTH 20 DAY'")
|
||||
assertSerialize(t, INTERVAL(1, YEAR, 10, MONTH, 20, DAY, 3, HOUR), "INTERVAL '1 YEAR 10 MONTH 20 DAY 3 HOUR'")
|
||||
|
||||
assertSerialize(t, INTERVAL(1, YEAR).IS_NOT_NULL(), "INTERVAL '1 YEAR' IS NOT NULL")
|
||||
assertSerialize(t, INTERVAL(1, YEAR).IS_NOT_NULL(), "(INTERVAL '1 YEAR' IS NOT NULL)")
|
||||
assertProjectionSerialize(t, INTERVAL(1, YEAR).AS("one year"), `INTERVAL '1 YEAR' AS "one year"`)
|
||||
|
||||
f := 5.2
|
||||
|
|
|
|||
|
|
@ -2,9 +2,10 @@ package postgres
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/lib/pq"
|
||||
"time"
|
||||
|
||||
"github.com/lib/pq"
|
||||
|
||||
"github.com/go-jet/jet/v2/internal/jet"
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue