Remove old 'Deep JSON' leftover code
This commit is contained in:
@@ -7,27 +7,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func JsonPath(asJSON bool, jsonDoc exp.Expression, pp ...any) (_ exp.LiteralExpression, err error) {
|
||||
var (
|
||||
sql strings.Builder
|
||||
path string
|
||||
)
|
||||
|
||||
if path, err = jsonPath(pp...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sql.WriteString(`?->'`)
|
||||
if !asJSON {
|
||||
// interested in un-encoded value
|
||||
sql.WriteString(`>'`)
|
||||
}
|
||||
sql.WriteString(path)
|
||||
sql.WriteString(`'`)
|
||||
|
||||
return exp.NewLiteralExpression(sql.String(), jsonDoc), nil
|
||||
}
|
||||
|
||||
func jsonPathExpr(pp ...any) (exp.LiteralExpression, error) {
|
||||
if path, err := jsonPath(pp...); err != nil {
|
||||
return nil, err
|
||||
@@ -59,37 +38,3 @@ func jsonPath(pp ...any) (string, error) {
|
||||
|
||||
return sql.String(), nil
|
||||
}
|
||||
|
||||
// JSONArrayContains prepares MySQL compatible comparison of value and JSON array
|
||||
//
|
||||
// # literal value = multi-value field / plain
|
||||
// # multi-value field = single-value field / plain
|
||||
// JSON_CONTAINS(v, JSON_EXTRACT(V, '$.f3'), '$.f2')
|
||||
//
|
||||
// # single-value field = multi-value field / plain
|
||||
// # multi-value field = single-value field / plain
|
||||
// JSON_CONTAINS(v, '"aaa"', '$.f2')
|
||||
//
|
||||
// This approach is not optimal, but it is the only way to make it work
|
||||
func JSONArrayContains(left exp.Expression, ident exp.IdentifierExpression, pp ...any) (jc exp.Expression, err error) {
|
||||
var (
|
||||
pathAux string
|
||||
path exp.Expression
|
||||
)
|
||||
|
||||
// this is the least painful way how to encode
|
||||
// an unknown value as JSON
|
||||
left = exp.NewSQLFunctionExpression(
|
||||
"JSON_EXTRACT",
|
||||
exp.NewSQLFunctionExpression("JSON_ARRAY", left),
|
||||
exp.NewLiteralExpression(`'$[0]'`),
|
||||
)
|
||||
|
||||
if pathAux, err = jsonPath(pp...); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
path = exp.NewLiteralExpression(`'` + pathAux + `'`)
|
||||
}
|
||||
|
||||
return exp.NewSQLFunctionExpression("JSON_CONTAINS", ident, left, path), nil
|
||||
}
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// test deep ident expression generator
|
||||
func Test_DeepIdentJSON(t *testing.T) {
|
||||
var (
|
||||
cc = []struct {
|
||||
input []interface{}
|
||||
sql string
|
||||
}{
|
||||
{
|
||||
input: []interface{}{"one"},
|
||||
sql: `?->>"$.one"`,
|
||||
},
|
||||
{
|
||||
input: []interface{}{"one", "two"},
|
||||
sql: `?->>"$.one.two"`,
|
||||
},
|
||||
{
|
||||
input: []interface{}{"one", 2, "three"},
|
||||
sql: `?->>"$.one[2].three"`,
|
||||
},
|
||||
{
|
||||
input: []interface{}{"one", "two", 3},
|
||||
sql: `?->>"$.one.two[3]"`,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
for _, c := range cc {
|
||||
t.Run(c.sql, func(t *testing.T) {
|
||||
var (
|
||||
r = require.New(t)
|
||||
l, err = JSONPath(nil, c.input...)
|
||||
)
|
||||
|
||||
r.NoError(err)
|
||||
r.Equal(c.sql, l.Literal())
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user