Simplify literal expressions.
This commit is contained in:
parent
4995a90483
commit
0e495a279e
26 changed files with 233 additions and 616 deletions
|
|
@ -1,25 +1,25 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"github.com/go-jet/jet/v2/internal/jet"
|
||||
"strconv"
|
||||
|
||||
"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 {
|
||||
return &cast{
|
||||
expr: expr,
|
||||
}
|
||||
}
|
||||
|
||||
// CAST function converts a expr (of any type) into latter specified datatype.
|
||||
func CAST(expr Expression) *cast {
|
||||
ret := &cast{}
|
||||
ret.Cast = jet.NewCastImpl(expr)
|
||||
|
||||
return ret
|
||||
type cast struct {
|
||||
expr Expression
|
||||
}
|
||||
|
||||
// AS casts expressions to castType
|
||||
func (c *cast) AS(castType string) Expression {
|
||||
return c.Cast.AS(castType)
|
||||
return jet.AtomicCustomExpression(Token("CAST("), c.expr, Token("AS "+castType+")"))
|
||||
}
|
||||
|
||||
// AS_DATETIME cast expression to DATETIME type
|
||||
|
|
|
|||
|
|
@ -12,8 +12,6 @@ var Dialect = newDialect()
|
|||
|
||||
func newDialect() jet.Dialect {
|
||||
operatorSerializeOverrides := map[string]jet.SerializeOverride{}
|
||||
operatorSerializeOverrides[jet.StringRegexpLikeOperator] = mysqlREGEXPLIKEoperator
|
||||
operatorSerializeOverrides[jet.StringNotRegexpLikeOperator] = mysqlNOTREGEXPLIKEoperator
|
||||
operatorSerializeOverrides["IS DISTINCT FROM"] = mysqlISDISTINCTFROM
|
||||
operatorSerializeOverrides["IS NOT DISTINCT FROM"] = mysqlISNOTDISTINCTFROM
|
||||
operatorSerializeOverrides["/"] = mysqlDivision
|
||||
|
|
@ -52,6 +50,7 @@ func newDialect() jet.Dialect {
|
|||
}
|
||||
return expr
|
||||
},
|
||||
RegexpLike: regexpLikeOperator,
|
||||
}
|
||||
|
||||
return jet.NewDialect(mySQLDialectParams)
|
||||
|
|
@ -144,20 +143,12 @@ func mysqlISDISTINCTFROM(expressions ...jet.Serializer) jet.SerializerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func mysqlREGEXPLIKEoperator(expressions ...jet.Serializer) jet.SerializerFunc {
|
||||
func regexpLikeOperator(str StringExpression, not bool, pattern 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...)
|
||||
|
||||
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 {
|
||||
out.WriteString("NOT")
|
||||
}
|
||||
|
||||
out.WriteString("REGEXP")
|
||||
|
|
@ -166,33 +157,7 @@ func mysqlREGEXPLIKEoperator(expressions ...jet.Serializer) jet.SerializerFunc {
|
|||
out.WriteString("BINARY")
|
||||
}
|
||||
|
||||
jet.Serialize(expressions[1], statement, out, options...)
|
||||
}
|
||||
}
|
||||
|
||||
func mysqlNOTREGEXPLIKEoperator(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)
|
||||
}
|
||||
}
|
||||
|
||||
out.WriteString("NOT REGEXP")
|
||||
|
||||
if caseSensitive {
|
||||
out.WriteString("BINARY")
|
||||
}
|
||||
|
||||
jet.Serialize(expressions[1], statement, out, options...)
|
||||
jet.Serialize(pattern, statement, out, options...)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ func TestRawInvalidArguments(t *testing.T) {
|
|||
|
||||
func TestRawType(t *testing.T) {
|
||||
assertSerialize(t, RawBool("table.colInt < :float", RawArgs{":float": 11.22}).IS_FALSE(),
|
||||
"(table.colInt < ?) IS FALSE", 11.22)
|
||||
"((table.colInt < ?) IS FALSE)", 11.22)
|
||||
|
||||
assertSerialize(t, RawFloat("table.colInt + &float", RawArgs{"&float": 11.22}).EQ(Float(3.14)),
|
||||
"((table.colInt + ?) = ?)", 11.22, 3.14)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue