Workaround for LIKE operator, COLLATION & PgSQL
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package ql
|
||||
|
||||
type (
|
||||
Encoder interface {
|
||||
// CaseInsensitiveLike translates "like" or "not like" op to case-insensitive version
|
||||
// supported by the db
|
||||
CaseInsensitiveLike(neg bool) string
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
// QueryEncoder
|
||||
// This is a temp solution to enable multi-db support in the ql package
|
||||
// that is not aware of the SQL flavour
|
||||
//
|
||||
// We can get away with this kind of solution right now because we are currently
|
||||
// only supporting one single db at the time.
|
||||
//
|
||||
// This will change in the future and so will the pkg/ql logic
|
||||
QueryEncoder Encoder
|
||||
)
|
||||
+1
-1
@@ -103,7 +103,7 @@ func (n Operator) ToSql() (string, []interface{}, error) {
|
||||
switch n.Kind {
|
||||
case "LIKE", "NOT LIKE":
|
||||
// Make sure we are doing case insensitive search
|
||||
op = "COLLATE utf8_general_ci " + n.Kind
|
||||
op = QueryEncoder.CaseInsensitiveLike(n.Kind == "NOT LIKE")
|
||||
}
|
||||
|
||||
return " " + op + " ", nil, nil
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/cortezaproject/corteza-server/pkg/ql"
|
||||
|
||||
type (
|
||||
// QueryEncoder provides query parts encoding rules for MySQL
|
||||
// see ql.QueryEncoder for mor info
|
||||
QueryEncoder struct{}
|
||||
)
|
||||
|
||||
var _ ql.Encoder = &QueryEncoder{}
|
||||
|
||||
func (QueryEncoder) CaseInsensitiveLike(neg bool) string {
|
||||
if neg {
|
||||
return "COLLATE utf8_unicode_ci NOT LIKE"
|
||||
} else {
|
||||
return "COLLATE utf8_unicode_ci LIKE"
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/cortezaproject/corteza-server/pkg/ql"
|
||||
"github.com/cortezaproject/corteza-server/store"
|
||||
"github.com/cortezaproject/corteza-server/store/rdbms"
|
||||
"github.com/cortezaproject/corteza-server/store/rdbms/instrumentation"
|
||||
@@ -53,6 +54,8 @@ func Connect(ctx context.Context, dsn string) (store.Storer, error) {
|
||||
// for details
|
||||
s.DB().ExecContext(ctx, "SET SESSION sql_mode = 'ANSI'")
|
||||
|
||||
ql.QueryEncoder = &QueryEncoder{}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package postgres
|
||||
|
||||
import "github.com/cortezaproject/corteza-server/pkg/ql"
|
||||
|
||||
type (
|
||||
// QueryEncoder provides query parts encoding rules for Postgres
|
||||
// see ql.QueryEncoder for mor info
|
||||
QueryEncoder struct{}
|
||||
)
|
||||
|
||||
var _ ql.Encoder = &QueryEncoder{}
|
||||
|
||||
func (QueryEncoder) CaseInsensitiveLike(neg bool) string {
|
||||
if neg {
|
||||
return "NOT ILIKE"
|
||||
} else {
|
||||
return "ILIKE"
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/cortezaproject/corteza-server/pkg/ql"
|
||||
"github.com/cortezaproject/corteza-server/store"
|
||||
"github.com/cortezaproject/corteza-server/store/rdbms"
|
||||
"github.com/cortezaproject/corteza-server/store/rdbms/instrumentation"
|
||||
@@ -47,6 +48,8 @@ func Connect(ctx context.Context, dsn string) (store.Storer, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ql.QueryEncoder = &QueryEncoder{}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package sqlite3
|
||||
|
||||
import "github.com/cortezaproject/corteza-server/pkg/ql"
|
||||
|
||||
type (
|
||||
// QueryEncoder provides query parts encoding rules for SQLite
|
||||
// see ql.QueryEncoder for mor info
|
||||
QueryEncoder struct{}
|
||||
)
|
||||
|
||||
var _ ql.Encoder = &QueryEncoder{}
|
||||
|
||||
func (QueryEncoder) CaseInsensitiveLike(neg bool) string {
|
||||
if neg {
|
||||
return "COLLATE utf8_unicode_ci NOT LIKE"
|
||||
} else {
|
||||
return "COLLATE utf8_unicode_ci LIKE"
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/cortezaproject/corteza-server/pkg/ql"
|
||||
"github.com/cortezaproject/corteza-server/store"
|
||||
"github.com/cortezaproject/corteza-server/store/rdbms"
|
||||
"github.com/cortezaproject/corteza-server/store/rdbms/instrumentation"
|
||||
@@ -51,6 +52,8 @@ func Connect(ctx context.Context, dsn string) (store.Storer, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ql.QueryEncoder = &QueryEncoder{}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -340,6 +340,16 @@ func testComposeRecords(t *testing.T, s store.ComposeRecords) {
|
||||
req.NoError(err)
|
||||
req.Len(set, 1)
|
||||
})
|
||||
|
||||
t.Run("with like op", func(t *testing.T) {
|
||||
var (
|
||||
req = require.New(t)
|
||||
f = types.RecordFilter{Query: `str3 LIKE '%“2020%'`}
|
||||
|
||||
_, _, err = s.SearchComposeRecords(ctx, mod, f)
|
||||
)
|
||||
req.NoError(err)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("paging and sorting", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user