Make DAL QL functions consistent and introduce DAY
* date outputs the date without the time part * day outputs the day of the month
This commit is contained in:
@@ -166,6 +166,11 @@ var (
|
||||
return fmt.Sprintf("date(%s)", args[0])
|
||||
},
|
||||
},
|
||||
"day": {
|
||||
Handler: func(args ...string) string {
|
||||
return fmt.Sprintf("day(%s)", args[0])
|
||||
},
|
||||
},
|
||||
"date_format": {
|
||||
Handler: func(args ...string) string {
|
||||
return fmt.Sprintf("strftime(%s, %s)", args[0], args[1])
|
||||
@@ -284,6 +289,7 @@ func newGval(expr string) (gval.Evaluable, error) {
|
||||
gval.Function("month", gvalfnc.Month),
|
||||
gval.Function("strftime", gvalfnc.StrfTime),
|
||||
gval.Function("date", gvalfnc.Date),
|
||||
gval.Function("day", gvalfnc.Day),
|
||||
gval.Function("isNil", gvalfnc.IsNil),
|
||||
gval.Function("float", gvalfnc.CastFloat),
|
||||
gval.Function("int", gvalfnc.CastInt),
|
||||
|
||||
@@ -162,8 +162,8 @@ func TestHandlers(t *testing.T) {
|
||||
out: mnt,
|
||||
},
|
||||
{
|
||||
name: "date",
|
||||
expr: `date(now())`,
|
||||
name: "day",
|
||||
expr: `day(now())`,
|
||||
in: simpleRow{},
|
||||
out: dy,
|
||||
},
|
||||
|
||||
+6
-1
@@ -39,7 +39,12 @@ func Month(in any) (int, error) {
|
||||
return int(t.Month()), nil
|
||||
}
|
||||
|
||||
func Date(in any) (int, error) {
|
||||
func Date(in any) (time.Time, error) {
|
||||
t, _, err := PrepMod(in, 0)
|
||||
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()), err
|
||||
}
|
||||
|
||||
func Day(in any) (int, error) {
|
||||
t, _, err := PrepMod(in, 0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
||||
@@ -47,6 +47,13 @@ var (
|
||||
return exp.NewLiteralExpression("?::DATE", args[0])
|
||||
},
|
||||
},
|
||||
"day": {
|
||||
Handler: func(args ...exp.Expression) exp.Expression {
|
||||
return exp.NewSQLFunctionExpression("EXTRACT",
|
||||
exp.NewLiteralExpression("DAY FROM ?", args[0]),
|
||||
)
|
||||
},
|
||||
},
|
||||
"time": {
|
||||
Handler: func(args ...exp.Expression) exp.Expression {
|
||||
return exp.NewLiteralExpression("DATE_TRUNC('second', ?::TIME)::TIME", args[0])
|
||||
|
||||
@@ -2,10 +2,11 @@ package sqlite
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/cortezaproject/corteza-server/store/adapters/rdbms/ql"
|
||||
"github.com/doug-martin/goqu/v9/exp"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/store/adapters/rdbms/ql"
|
||||
"github.com/doug-martin/goqu/v9/exp"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -47,6 +48,14 @@ var (
|
||||
)
|
||||
},
|
||||
},
|
||||
"day": {
|
||||
Handler: func(args ...exp.Expression) exp.Expression {
|
||||
return exp.NewSQLFunctionExpression("STRFTIME",
|
||||
exp.NewLiteralExpression("'%d'"),
|
||||
args[0],
|
||||
)
|
||||
},
|
||||
},
|
||||
"datetime": {
|
||||
Handler: func(args ...exp.Expression) exp.Expression {
|
||||
return exp.NewSQLFunctionExpression("DATETIME", args[0])
|
||||
|
||||
@@ -180,34 +180,6 @@ var (
|
||||
},
|
||||
},
|
||||
|
||||
// functions
|
||||
// - aggregation
|
||||
//"count": {
|
||||
// Args: collectParams(false, "Any"),
|
||||
// Result: wrapRes("Number"),
|
||||
// Handler: makeGenericAggFncHandler("COUNT"),
|
||||
//},
|
||||
//"sum": {
|
||||
// Args: collectParams(true, "Any"),
|
||||
// Result: wrapRes("Number"),
|
||||
// Handler: makeGenericAggFncHandler("SUM"),
|
||||
//},
|
||||
//"max": {
|
||||
// Args: collectParams(true, "Any"),
|
||||
// Result: wrapRes("Number"),
|
||||
// Handler: makeGenericAggFncHandler("MAX"),
|
||||
//},
|
||||
//"min": {
|
||||
// Args: collectParams(true, "Any"),
|
||||
// Result: wrapRes("Number"),
|
||||
// Handler: makeGenericAggFncHandler("MIN"),
|
||||
//},
|
||||
//"avg": {
|
||||
// Args: collectParams(true, "Any"),
|
||||
// Result: wrapRes("Number"),
|
||||
// Handler: makeGenericAggFncHandler("AVG"),
|
||||
//},
|
||||
|
||||
// - filtering
|
||||
"date_format": {
|
||||
Handler: func(args ...exp.Expression) exp.Expression {
|
||||
@@ -234,11 +206,16 @@ var (
|
||||
return exp.NewSQLFunctionExpression("MONTH", args[0])
|
||||
},
|
||||
},
|
||||
"date": {
|
||||
"day": {
|
||||
Handler: func(args ...exp.Expression) exp.Expression {
|
||||
return exp.NewSQLFunctionExpression("DAY", args[0])
|
||||
},
|
||||
},
|
||||
"date": {
|
||||
Handler: func(args ...exp.Expression) exp.Expression {
|
||||
return exp.NewSQLFunctionExpression("DATE", args[0])
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user