mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-01-02 18:04:25 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
abee5c942f | ||
|
|
2e9a0bd51a |
@@ -228,7 +228,10 @@ func (b *BunSelectQuery) Scan(ctx context.Context, dest interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *BunSelectQuery) Count(ctx context.Context) (int, error) {
|
func (b *BunSelectQuery) Count(ctx context.Context) (int, error) {
|
||||||
count, err := b.query.Count(ctx)
|
// Use ColumnExpr with Scan instead of Count() to avoid requiring a model
|
||||||
|
// This works with just Table() set and avoids "Model(nil)" error
|
||||||
|
var count int
|
||||||
|
err := b.query.ColumnExpr("count(*)").Scan(ctx, &count)
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -171,7 +171,12 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
|
|||||||
|
|
||||||
logger.Info("Reading records from %s.%s", schema, entity)
|
logger.Info("Reading records from %s.%s", schema, entity)
|
||||||
|
|
||||||
// Use Table() with the resolved table name (don't use Model() as it would add the table twice)
|
// Create the model pointer for Scan() operations
|
||||||
|
// We don't set it on the query to avoid table duplication in FROM clause
|
||||||
|
sliceType := reflect.SliceOf(reflect.PointerTo(modelType))
|
||||||
|
modelPtr := reflect.New(sliceType).Interface()
|
||||||
|
|
||||||
|
// Use only Table() - model will be provided to Scan() directly
|
||||||
query := h.db.NewSelect().Table(tableName)
|
query := h.db.NewSelect().Table(tableName)
|
||||||
|
|
||||||
// Apply column selection
|
// Apply column selection
|
||||||
@@ -224,7 +229,7 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
|
|||||||
var result interface{}
|
var result interface{}
|
||||||
if id != "" {
|
if id != "" {
|
||||||
logger.Debug("Querying single record with ID: %s", id)
|
logger.Debug("Querying single record with ID: %s", id)
|
||||||
// Create a pointer to the struct type for scanning - use modelType which is already unwrapped
|
// For single record, create a new pointer to the struct type
|
||||||
singleResult := reflect.New(modelType).Interface()
|
singleResult := reflect.New(modelType).Interface()
|
||||||
query = query.Where("id = ?", id)
|
query = query.Where("id = ?", id)
|
||||||
if err := query.Scan(ctx, singleResult); err != nil {
|
if err := query.Scan(ctx, singleResult); err != nil {
|
||||||
@@ -235,16 +240,13 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
|
|||||||
result = singleResult
|
result = singleResult
|
||||||
} else {
|
} else {
|
||||||
logger.Debug("Querying multiple records")
|
logger.Debug("Querying multiple records")
|
||||||
// Create a slice of pointers to the model type - use modelType which is already unwrapped
|
// Use the modelPtr already created and set on the query
|
||||||
sliceType := reflect.SliceOf(reflect.PointerTo(modelType))
|
if err := query.Scan(ctx, modelPtr); err != nil {
|
||||||
results := reflect.New(sliceType).Interface()
|
|
||||||
|
|
||||||
if err := query.Scan(ctx, results); err != nil {
|
|
||||||
logger.Error("Error querying records: %v", err)
|
logger.Error("Error querying records: %v", err)
|
||||||
h.sendError(w, http.StatusInternalServerError, "query_error", "Error executing query", err)
|
h.sendError(w, http.StatusInternalServerError, "query_error", "Error executing query", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
result = reflect.ValueOf(results).Elem().Interface()
|
result = reflect.ValueOf(modelPtr).Elem().Interface()
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info("Successfully retrieved records")
|
logger.Info("Successfully retrieved records")
|
||||||
|
|||||||
@@ -201,7 +201,8 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
|
|||||||
|
|
||||||
logger.Info("Reading records from %s.%s", schema, entity)
|
logger.Info("Reading records from %s.%s", schema, entity)
|
||||||
|
|
||||||
// Use Table() with the resolved table name (don't use Model() as it would add the table twice)
|
// Use Table() with the resolved table name
|
||||||
|
// Model will be provided to Scan() directly to avoid table duplication in FROM clause
|
||||||
query := h.db.NewSelect().Table(tableName)
|
query := h.db.NewSelect().Table(tableName)
|
||||||
|
|
||||||
// Apply column selection
|
// Apply column selection
|
||||||
|
|||||||
Reference in New Issue
Block a user