Fixed computed columns

This commit is contained in:
Hein 2025-11-11 12:28:53 +02:00
parent 006dc4a2b2
commit 0cef0f75d3
5 changed files with 34 additions and 0 deletions

View File

@ -119,6 +119,12 @@ func (b *BunSelectQuery) Column(columns ...string) common.SelectQuery {
return b 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 { func (b *BunSelectQuery) Where(query string, args ...interface{}) common.SelectQuery {
b.query = b.query.Where(query, args...) b.query = b.query.Where(query, args...)
return b return b

View File

@ -105,6 +105,11 @@ func (g *GormSelectQuery) Column(columns ...string) common.SelectQuery {
return g 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 { func (g *GormSelectQuery) Where(query string, args ...interface{}) common.SelectQuery {
g.db = g.db.Where(query, args...) g.db = g.db.Where(query, args...)
return g return g

View File

@ -26,6 +26,7 @@ type SelectQuery interface {
Model(model interface{}) SelectQuery Model(model interface{}) SelectQuery
Table(table string) SelectQuery Table(table string) SelectQuery
Column(columns ...string) SelectQuery Column(columns ...string) SelectQuery
ColumnExpr(query string, args ...interface{}) SelectQuery
Where(query string, args ...interface{}) SelectQuery Where(query string, args ...interface{}) SelectQuery
WhereOr(query string, args ...interface{}) SelectQuery WhereOr(query string, args ...interface{}) SelectQuery
Join(query string, args ...interface{}) SelectQuery Join(query string, args ...interface{}) SelectQuery

View File

@ -196,6 +196,13 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
query = query.Column(options.Columns...) 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 // Apply preloading
if len(options.Preload) > 0 { if len(options.Preload) > 0 {
query = h.applyPreloads(model, query, options.Preload) query = h.applyPreloads(model, query, options.Preload)

View File

@ -253,6 +253,21 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
query = query.Table(tableName) 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 // Apply column selection
if len(options.Columns) > 0 { if len(options.Columns) > 0 {
logger.Debug("Selecting columns: %v", options.Columns) logger.Debug("Selecting columns: %v", options.Columns)