From 0cef0f75d33205b6e0e5417148e753fdf316bdbb Mon Sep 17 00:00:00 2001 From: Hein Date: Tue, 11 Nov 2025 12:28:53 +0200 Subject: [PATCH] Fixed computed columns --- pkg/common/adapters/database/bun.go | 6 ++++++ pkg/common/adapters/database/gorm.go | 5 +++++ pkg/common/interfaces.go | 1 + pkg/resolvespec/handler.go | 7 +++++++ pkg/restheadspec/handler.go | 15 +++++++++++++++ 5 files changed, 34 insertions(+) diff --git a/pkg/common/adapters/database/bun.go b/pkg/common/adapters/database/bun.go index 5ae0c73..d46e4fe 100644 --- a/pkg/common/adapters/database/bun.go +++ b/pkg/common/adapters/database/bun.go @@ -119,6 +119,12 @@ func (b *BunSelectQuery) Column(columns ...string) common.SelectQuery { return b } +func (b *BunSelectQuery) ColumnExpr(query string, args ...interface{}) common.SelectQuery { + b.query = b.query.ColumnExpr(query, args) + + return b +} + func (b *BunSelectQuery) Where(query string, args ...interface{}) common.SelectQuery { b.query = b.query.Where(query, args...) return b diff --git a/pkg/common/adapters/database/gorm.go b/pkg/common/adapters/database/gorm.go index 0c4dc11..6c31fb2 100644 --- a/pkg/common/adapters/database/gorm.go +++ b/pkg/common/adapters/database/gorm.go @@ -105,6 +105,11 @@ func (g *GormSelectQuery) Column(columns ...string) common.SelectQuery { return g } +func (g *GormSelectQuery) ColumnExpr(query string, args ...interface{}) common.SelectQuery { + g.db = g.db.Select(query, args...) + return g +} + func (g *GormSelectQuery) Where(query string, args ...interface{}) common.SelectQuery { g.db = g.db.Where(query, args...) return g diff --git a/pkg/common/interfaces.go b/pkg/common/interfaces.go index 1599ee9..8efc027 100644 --- a/pkg/common/interfaces.go +++ b/pkg/common/interfaces.go @@ -26,6 +26,7 @@ type SelectQuery interface { Model(model interface{}) SelectQuery Table(table string) SelectQuery Column(columns ...string) SelectQuery + ColumnExpr(query string, args ...interface{}) SelectQuery Where(query string, args ...interface{}) SelectQuery WhereOr(query string, args ...interface{}) SelectQuery Join(query string, args ...interface{}) SelectQuery diff --git a/pkg/resolvespec/handler.go b/pkg/resolvespec/handler.go index 96c5d41..00a06b8 100644 --- a/pkg/resolvespec/handler.go +++ b/pkg/resolvespec/handler.go @@ -196,6 +196,13 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st query = query.Column(options.Columns...) } + if len(options.ComputedColumns) > 0 { + for _, cu := range options.ComputedColumns { + logger.Debug("Applying computed column: %s", cu.Name) + query = query.ColumnExpr("(?) AS "+cu.Name, cu.Expression) + } + } + // Apply preloading if len(options.Preload) > 0 { query = h.applyPreloads(model, query, options.Preload) diff --git a/pkg/restheadspec/handler.go b/pkg/restheadspec/handler.go index e01f531..3e0ec1e 100644 --- a/pkg/restheadspec/handler.go +++ b/pkg/restheadspec/handler.go @@ -253,6 +253,21 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st query = query.Table(tableName) } + // Apply ComputedQL fields if any + if len(options.ComputedQL) > 0 { + for colName, colExpr := range options.ComputedQL { + logger.Debug("Applying computed column: %s", colName) + query = query.ColumnExpr("(?) AS "+colName, colExpr) + } + } + + if len(options.ComputedColumns) > 0 { + for _, cu := range options.ComputedColumns { + logger.Debug("Applying computed column: %s", cu.Name) + query = query.ColumnExpr("(?) AS "+cu.Name, cu.Expression) + } + } + // Apply column selection if len(options.Columns) > 0 { logger.Debug("Selecting columns: %v", options.Columns)