diff --git a/server/store/adapters/rdbms/drivers/dialect.go b/server/store/adapters/rdbms/drivers/dialect.go index 98ef8851f..0436f4f72 100644 --- a/server/store/adapters/rdbms/drivers/dialect.go +++ b/server/store/adapters/rdbms/drivers/dialect.go @@ -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 diff --git a/server/store/adapters/rdbms/drivers/mssql/dialect.go b/server/store/adapters/rdbms/drivers/mssql/dialect.go index ccd2f53a3..2d24db34a 100644 --- a/server/store/adapters/rdbms/drivers/mssql/dialect.go +++ b/server/store/adapters/rdbms/drivers/mssql/dialect.go @@ -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(), diff --git a/server/store/adapters/rdbms/drivers/mysql/dialect.go b/server/store/adapters/rdbms/drivers/mysql/dialect.go index bff3ec716..28b995b3e 100644 --- a/server/store/adapters/rdbms/drivers/mysql/dialect.go +++ b/server/store/adapters/rdbms/drivers/mysql/dialect.go @@ -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(), diff --git a/server/store/adapters/rdbms/drivers/postgres/dialect.go b/server/store/adapters/rdbms/drivers/postgres/dialect.go index 013dcd848..1ff760dbe 100644 --- a/server/store/adapters/rdbms/drivers/postgres/dialect.go +++ b/server/store/adapters/rdbms/drivers/postgres/dialect.go @@ -1,19 +1,19 @@ package postgres import ( - "fmt" - "strings" + "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" + "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 ( @@ -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(), diff --git a/server/store/adapters/rdbms/drivers/sqlite/dialect.go b/server/store/adapters/rdbms/drivers/sqlite/dialect.go index 9b0861359..3732c7fbb 100644 --- a/server/store/adapters/rdbms/drivers/sqlite/dialect.go +++ b/server/store/adapters/rdbms/drivers/sqlite/dialect.go @@ -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{ diff --git a/server/store/adapters/rdbms/drivers/table.go b/server/store/adapters/rdbms/drivers/table.go index 29b105331..529837b8b 100644 --- a/server/store/adapters/rdbms/drivers/table.go +++ b/server/store/adapters/rdbms/drivers/table.go @@ -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 ( @@ -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