REGEXP_LIKE refactor.
This commit is contained in:
parent
91dc633b45
commit
bf3ec27f68
12 changed files with 151 additions and 36 deletions
|
|
@ -3,7 +3,6 @@ package postgres
|
|||
import (
|
||||
"github.com/go-jet/jet/internal/jet"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var Dialect = NewDialect()
|
||||
|
|
@ -11,7 +10,8 @@ var Dialect = NewDialect()
|
|||
func NewDialect() jet.Dialect {
|
||||
|
||||
serializeOverrides := map[string]jet.SerializeOverride{}
|
||||
serializeOverrides["REGEXP_LIKE"] = postgres_REGEXP_LIKE_function
|
||||
serializeOverrides[jet.StringRegexpLikeOperator] = postgres_REGEXP_LIKE_operator
|
||||
serializeOverrides[jet.StringNotRegexpLikeOperator] = postgres_NOT_REGEXP_LIKE_operator
|
||||
serializeOverrides["CAST"] = postgresCAST
|
||||
|
||||
dialectParams := jet.DialectParams{
|
||||
|
|
@ -53,7 +53,7 @@ func postgresCAST(expressions ...jet.Expression) jet.SerializeFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func postgres_REGEXP_LIKE_function(expressions ...jet.Expression) jet.SerializeFunc {
|
||||
func postgres_REGEXP_LIKE_operator(expressions ...jet.Expression) jet.SerializeFunc {
|
||||
return func(statement jet.StatementType, out *jet.SqlBuilder, options ...jet.SerializeOption) {
|
||||
if len(expressions) < 2 {
|
||||
panic("jet: invalid number of expressions for operator")
|
||||
|
|
@ -65,9 +65,7 @@ func postgres_REGEXP_LIKE_function(expressions ...jet.Expression) jet.SerializeF
|
|||
|
||||
if len(expressions) >= 3 {
|
||||
if stringLiteral, ok := expressions[2].(jet.LiteralExpression); ok {
|
||||
matchType := stringLiteral.Value().(string)
|
||||
|
||||
caseSensitive = !strings.Contains(matchType, "i")
|
||||
caseSensitive = stringLiteral.Value().(bool)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -80,3 +78,29 @@ func postgres_REGEXP_LIKE_function(expressions ...jet.Expression) jet.SerializeF
|
|||
jet.Serialize(expressions[1], statement, out, options...)
|
||||
}
|
||||
}
|
||||
|
||||
func postgres_NOT_REGEXP_LIKE_operator(expressions ...jet.Expression) jet.SerializeFunc {
|
||||
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...)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,15 +3,17 @@ package postgres
|
|||
import "testing"
|
||||
|
||||
func TestString_REGEXP_LIKE_operator(t *testing.T) {
|
||||
assertClauseSerialize(t, table3StrCol.REGEXP_LIKE(table2ColStr), "table3.col2 ~* table2.col_str")
|
||||
assertClauseSerialize(t, table3StrCol.REGEXP_LIKE(String("JOHN"), "c"), "table3.col2 ~ $1", "JOHN")
|
||||
assertClauseSerialize(t, table3StrCol.REGEXP_LIKE(String("JOHN"), "i"), "table3.col2 ~* $1", "JOHN")
|
||||
assertClauseSerialize(t, table3StrCol.REGEXP_LIKE(table2ColStr), "(table3.col2 ~* table2.col_str)")
|
||||
assertClauseSerialize(t, table3StrCol.REGEXP_LIKE(String("JOHN")), "(table3.col2 ~* $1)", "JOHN")
|
||||
assertClauseSerialize(t, table3StrCol.REGEXP_LIKE(String("JOHN"), false), "(table3.col2 ~* $1)", "JOHN")
|
||||
assertClauseSerialize(t, table3StrCol.REGEXP_LIKE(String("JOHN"), true), "(table3.col2 ~ $1)", "JOHN")
|
||||
}
|
||||
|
||||
func TestString_REGEXP_LIKE_function(t *testing.T) {
|
||||
assertClauseSerialize(t, REGEXP_LIKE(table3StrCol, table2ColStr), "table3.col2 ~* table2.col_str")
|
||||
assertClauseSerialize(t, REGEXP_LIKE(table3StrCol, String("JOHN"), "c"), "table3.col2 ~ $1", "JOHN")
|
||||
assertClauseSerialize(t, REGEXP_LIKE(table3StrCol, String("JOHN"), "i"), "table3.col2 ~* $1", "JOHN")
|
||||
func TestString_NOT_REGEXP_LIKE_operator(t *testing.T) {
|
||||
assertClauseSerialize(t, table3StrCol.NOT_REGEXP_LIKE(table2ColStr), "(table3.col2 !~* table2.col_str)")
|
||||
assertClauseSerialize(t, table3StrCol.NOT_REGEXP_LIKE(String("JOHN")), "(table3.col2 !~* $1)", "JOHN")
|
||||
assertClauseSerialize(t, table3StrCol.NOT_REGEXP_LIKE(String("JOHN"), false), "(table3.col2 !~* $1)", "JOHN")
|
||||
assertClauseSerialize(t, table3StrCol.NOT_REGEXP_LIKE(String("JOHN"), true), "(table3.col2 !~ $1)", "JOHN")
|
||||
}
|
||||
|
||||
func TestExists(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -38,7 +38,6 @@ var SUMi = jet.SUMi
|
|||
|
||||
//--------------------- String functions ------------------//
|
||||
|
||||
var REGEXP_LIKE = jet.REGEXP_LIKE
|
||||
var BIT_LENGTH = jet.BIT_LENGTH
|
||||
var CHAR_LENGTH = jet.CHAR_LENGTH
|
||||
var OCTET_LENGTH = jet.OCTET_LENGTH
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue