Compare commits

..

2 Commits

6 changed files with 49 additions and 2 deletions

3
.gitignore vendored
View File

@@ -23,4 +23,5 @@ go.work.sum
# env file
.env
bin/
bin/
test.db

View File

@@ -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
@@ -238,6 +244,10 @@ func (b *BunSelectQuery) Scan(ctx context.Context, dest interface{}) error {
return b.query.Scan(ctx, dest)
}
func (b *BunSelectQuery) ScanModel(ctx context.Context) error {
return b.query.Scan(ctx)
}
func (b *BunSelectQuery) Count(ctx context.Context) (int, error) {
// If Model() was set, use bun's native Count() which works properly
if b.hasModel {

View File

@@ -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
@@ -221,6 +226,13 @@ func (g *GormSelectQuery) Scan(ctx context.Context, dest interface{}) error {
return g.db.WithContext(ctx).Find(dest).Error
}
func (g *GormSelectQuery) ScanModel(ctx context.Context) error {
if g.db.Statement.Model == nil {
return fmt.Errorf("ScanModel requires Model() to be set before scanning")
}
return g.db.WithContext(ctx).Find(g.db.Statement.Model).Error
}
func (g *GormSelectQuery) Count(ctx context.Context) (int, error) {
var count int64
err := g.db.WithContext(ctx).Count(&count).Error

View File

@@ -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
@@ -39,6 +40,7 @@ type SelectQuery interface {
// Execution methods
Scan(ctx context.Context, dest interface{}) error
ScanModel(ctx context.Context) error
Count(ctx context.Context) (int, error)
Exists(ctx context.Context) (bool, error)
}

View File

@@ -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)

View File

@@ -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)
@@ -398,7 +413,7 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
}
// Execute query - modelPtr was already created earlier
if err := query.Scan(ctx, modelPtr); err != nil {
if err := query.ScanModel(ctx); err != nil {
logger.Error("Error executing query: %v", err)
h.sendError(w, http.StatusInternalServerError, "query_error", "Error executing query", err)
return