3
0

Properly support CONCAT in Postgres and SQLite

This commit is contained in:
Denis Arh
2022-10-11 07:38:10 +02:00
committed by Tomaž Jerman
parent 2bd455cab7
commit ef12ab9aaa
3 changed files with 31 additions and 0 deletions

12
pkg/cast2/slice.go Normal file
View 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
}

View File

@@ -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...)
}

View File

@@ -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...)
}