Skip to content

Commit 0cef0f7

Browse files
author
Hein
committed
Fixed computed columns
1 parent 006dc4a commit 0cef0f7

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

pkg/common/adapters/database/bun.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ func (b *BunSelectQuery) Column(columns ...string) common.SelectQuery {
119119
return b
120120
}
121121

122+
func (b *BunSelectQuery) ColumnExpr(query string, args ...interface{}) common.SelectQuery {
123+
b.query = b.query.ColumnExpr(query, args)
124+
125+
return b
126+
}
127+
122128
func (b *BunSelectQuery) Where(query string, args ...interface{}) common.SelectQuery {
123129
b.query = b.query.Where(query, args...)
124130
return b

pkg/common/adapters/database/gorm.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ func (g *GormSelectQuery) Column(columns ...string) common.SelectQuery {
105105
return g
106106
}
107107

108+
func (g *GormSelectQuery) ColumnExpr(query string, args ...interface{}) common.SelectQuery {
109+
g.db = g.db.Select(query, args...)
110+
return g
111+
}
112+
108113
func (g *GormSelectQuery) Where(query string, args ...interface{}) common.SelectQuery {
109114
g.db = g.db.Where(query, args...)
110115
return g

pkg/common/interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type SelectQuery interface {
2626
Model(model interface{}) SelectQuery
2727
Table(table string) SelectQuery
2828
Column(columns ...string) SelectQuery
29+
ColumnExpr(query string, args ...interface{}) SelectQuery
2930
Where(query string, args ...interface{}) SelectQuery
3031
WhereOr(query string, args ...interface{}) SelectQuery
3132
Join(query string, args ...interface{}) SelectQuery

pkg/resolvespec/handler.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
196196
query = query.Column(options.Columns...)
197197
}
198198

199+
if len(options.ComputedColumns) > 0 {
200+
for _, cu := range options.ComputedColumns {
201+
logger.Debug("Applying computed column: %s", cu.Name)
202+
query = query.ColumnExpr("(?) AS "+cu.Name, cu.Expression)
203+
}
204+
}
205+
199206
// Apply preloading
200207
if len(options.Preload) > 0 {
201208
query = h.applyPreloads(model, query, options.Preload)

pkg/restheadspec/handler.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,21 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
253253
query = query.Table(tableName)
254254
}
255255

256+
// Apply ComputedQL fields if any
257+
if len(options.ComputedQL) > 0 {
258+
for colName, colExpr := range options.ComputedQL {
259+
logger.Debug("Applying computed column: %s", colName)
260+
query = query.ColumnExpr("(?) AS "+colName, colExpr)
261+
}
262+
}
263+
264+
if len(options.ComputedColumns) > 0 {
265+
for _, cu := range options.ComputedColumns {
266+
logger.Debug("Applying computed column: %s", cu.Name)
267+
query = query.ColumnExpr("(?) AS "+cu.Name, cu.Expression)
268+
}
269+
}
270+
256271
// Apply column selection
257272
if len(options.Columns) > 0 {
258273
logger.Debug("Selecting columns: %v", options.Columns)

0 commit comments

Comments
 (0)