Skip to content

Commit

Permalink
Fix Postgres timestamp sort inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
KinyaElGrande committed Jul 25, 2024
1 parent a56b825 commit 36faf60
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 20 deletions.
4 changes: 3 additions & 1 deletion server/store/adapters/rdbms/drivers/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ type (
// comparison or soring expression
AttributeCast(*dal.Attribute, exp.Expression) (exp.Expression, error)

// TableCodec returns table codec (encodes & decodes data to/from db table)
AttributeExpression(attr *dal.Attribute, modelIdent string, ident string) (expr exp.Expression, err error)

// TableCodec returns table codec (encodes & decodes data to/from db table)
TableCodec(*dal.Model) TableCodec

// TypeWrap returns driver's type implementation for a particular attribute type
Expand Down
4 changes: 4 additions & 0 deletions server/store/adapters/rdbms/drivers/mssql/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ func (mssqlDialect) AttributeCast(attr *dal.Attribute, val exp.Expression) (expr
return
}

func (mssqlDialect) AttributeExpression(attr *dal.Attribute, modelIdent string, ident string) (expr exp.Expression, err error) {
return exp.NewLiteralExpression("?", exp.NewIdentifierExpression("", modelIdent, ident)), nil
}

func (mssqlDialect) AttributeToColumn(attr *dal.Attribute) (col *ddl.Column, err error) {
col = &ddl.Column{
Ident: attr.StoreIdent(),
Expand Down
4 changes: 4 additions & 0 deletions server/store/adapters/rdbms/drivers/mysql/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ func (mysqlDialect) AttributeCast(attr *dal.Attribute, val exp.Expression) (exp.
return exp.NewLiteralExpression("?", c), nil
}

func (mysqlDialect) AttributeExpression(attr *dal.Attribute, modelIdent string, ident string) (expr exp.Expression, err error) {
return exp.NewLiteralExpression("?", exp.NewIdentifierExpression("", modelIdent, ident)), nil
}

func (mysqlDialect) AttributeToColumn(attr *dal.Attribute) (col *ddl.Column, err error) {
col = &ddl.Column{
Ident: attr.StoreIdent(),
Expand Down
38 changes: 25 additions & 13 deletions server/store/adapters/rdbms/drivers/postgres/dialect.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package postgres

import (
"fmt"
"strings"

"github.com/cortezaproject/corteza/server/pkg/dal"
"github.com/cortezaproject/corteza/server/pkg/expr"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/ddl"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/drivers"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/ql"
"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/dialect/postgres"
"github.com/doug-martin/goqu/v9/exp"
"github.com/doug-martin/goqu/v9/sqlgen"
"github.com/spf13/cast"
"fmt"
"strings"

"github.com/cortezaproject/corteza/server/pkg/dal"
"github.com/cortezaproject/corteza/server/pkg/expr"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/ddl"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/drivers"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/ql"
"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/dialect/postgres"
"github.com/doug-martin/goqu/v9/exp"
"github.com/doug-martin/goqu/v9/sqlgen"
"github.com/spf13/cast"
)

type (
Expand Down Expand Up @@ -107,6 +107,18 @@ func (postgresDialect) AttributeCast(attr *dal.Attribute, val exp.Expression) (e
return
}

func (postgresDialect) AttributeExpression(attr *dal.Attribute, modelIdent string, ident string) (expr exp.Expression, err error) {
identExpr := exp.NewIdentifierExpression("", modelIdent, ident)

// truncate timestamp data type to second mark precision
if attr.Type.Type() == dal.AttributeTypeTimestamp {
return exp.NewLiteralExpression("date_trunc(?, ?)", "second", identExpr), nil
}

// using column directly
return exp.NewLiteralExpression("?", identExpr), nil
}

func (postgresDialect) AttributeToColumn(attr *dal.Attribute) (col *ddl.Column, err error) {
col = &ddl.Column{
Ident: attr.StoreIdent(),
Expand Down
4 changes: 4 additions & 0 deletions server/store/adapters/rdbms/drivers/sqlite/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ func (sqliteDialect) AttributeCast(attr *dal.Attribute, val exp.Expression) (exp

}

func (sqliteDialect) AttributeExpression(attr *dal.Attribute, modelIdent string, ident string) (expr exp.Expression, err error) {
return exp.NewLiteralExpression("?", exp.NewIdentifierExpression("", modelIdent, ident)), nil
}

func (sqliteDialect) AttributeToColumn(attr *dal.Attribute) (col *ddl.Column, err error) {

col = &ddl.Column{
Expand Down
10 changes: 4 additions & 6 deletions server/store/adapters/rdbms/drivers/table.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package drivers

import (
"fmt"

"github.com/cortezaproject/corteza/server/pkg/dal"
"github.com/doug-martin/goqu/v9/exp"
"fmt"
"github.com/cortezaproject/corteza/server/pkg/dal"
"github.com/doug-martin/goqu/v9/exp"
)

type (
Expand Down Expand Up @@ -132,8 +131,7 @@ func (t *GenericTableCodec) AttributeExpression(ident string) (exp.Expression, e

switch s := attr.Store.(type) {
case *dal.CodecAlias:
// using column directly
return exp.NewLiteralExpression("?", exp.NewIdentifierExpression("", t.model.Ident, s.Ident)), nil
return t.dialect.AttributeExpression(attr, t.model.Ident, s.Ident)

case *dal.CodecRecordValueSetJSON:
// using JSON to handle embedded values
Expand Down

0 comments on commit 36faf60

Please sign in to comment.