Properly support CONCAT in Postgres and SQLite
This commit is contained in:
12
pkg/cast2/slice.go
Normal file
12
pkg/cast2/slice.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package cast2
|
||||
|
||||
// Anys converts any kinds of values to a []any slice
|
||||
func Anys[C any](in ...C) (out []any) {
|
||||
out = make([]any, len(in))
|
||||
|
||||
for i := range in {
|
||||
out[i] = in[i]
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/doug-martin/goqu/v9/dialect/postgres"
|
||||
"github.com/doug-martin/goqu/v9/exp"
|
||||
"github.com/spf13/cast"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -185,5 +186,16 @@ func (postgresDialect) AttributeToColumn(attr *dal.Attribute) (col *ddl.Column,
|
||||
}
|
||||
|
||||
func (postgresDialect) ExprHandler(n *ql.ASTNode, args ...exp.Expression) (exp.Expression, error) {
|
||||
switch strings.ToLower(n.Ref) {
|
||||
case "concat":
|
||||
// need to force text type on all arguments
|
||||
aa := make([]any, len(args))
|
||||
for a := range args {
|
||||
aa[a] = exp.NewCastExpression(exp.NewLiteralExpression("?", args[a]), "TEXT")
|
||||
}
|
||||
|
||||
return exp.NewSQLFunctionExpression("CONCAT", aa...), nil
|
||||
}
|
||||
|
||||
return ref2exp.RefHandler(n, args...)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package sqlite
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/cortezaproject/corteza-server/pkg/cast2"
|
||||
"github.com/cortezaproject/corteza-server/pkg/dal"
|
||||
"github.com/cortezaproject/corteza-server/store/adapters/rdbms/ddl"
|
||||
"github.com/cortezaproject/corteza-server/store/adapters/rdbms/drivers"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
"github.com/doug-martin/goqu/v9"
|
||||
"github.com/doug-martin/goqu/v9/dialect/sqlite3"
|
||||
"github.com/doug-martin/goqu/v9/exp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -158,5 +160,10 @@ func (sqliteDialect) AttributeToColumn(attr *dal.Attribute) (col *ddl.Column, er
|
||||
}
|
||||
|
||||
func (sqliteDialect) ExprHandler(n *ql.ASTNode, args ...exp.Expression) (exp.Expression, error) {
|
||||
switch strings.ToLower(n.Ref) {
|
||||
case "concat":
|
||||
return exp.NewLiteralExpression("?"+strings.Repeat(" || ?", len(args)-1), cast2.Anys(args...)...), nil
|
||||
}
|
||||
|
||||
return ref2exp.RefHandler(n, args...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user