Compare commits

..

3 Commits

Author SHA1 Message Date
Hein
f1c6b36374 Bun Adaptor updates 2025-11-07 14:03:40 +02:00
Hein
abee5c942f Count Fixes 2025-11-07 13:54:24 +02:00
Hein
2e9a0bd51a Better model pointers 2025-11-07 13:45:08 +02:00
3 changed files with 26 additions and 13 deletions

View File

@@ -22,7 +22,10 @@ func NewBunAdapter(db *bun.DB) *BunAdapter {
}
func (b *BunAdapter) NewSelect() common.SelectQuery {
return &BunSelectQuery{query: b.db.NewSelect()}
return &BunSelectQuery{
query: b.db.NewSelect(),
db: b.db,
}
}
func (b *BunAdapter) NewInsert() common.InsertQuery {
@@ -78,8 +81,9 @@ func (b *BunAdapter) RunInTransaction(ctx context.Context, fn func(common.Databa
// BunSelectQuery implements SelectQuery for Bun
type BunSelectQuery struct {
query *bun.SelectQuery
schema string // Separated schema name
tableName string // Just the table name, without schema
db bun.IDB // Store DB connection for count queries
schema string // Separated schema name
tableName string // Just the table name, without schema
tableAlias string
}
@@ -228,6 +232,9 @@ func (b *BunSelectQuery) Scan(ctx context.Context, dest interface{}) error {
}
func (b *BunSelectQuery) Count(ctx context.Context) (int, error) {
// Bun's Count() method works properly and handles column selections automatically
// It builds: SELECT COUNT(*) FROM (original query) AS subquery
// This works with both Model() and Table() set
count, err := b.query.Count(ctx)
return count, err
}
@@ -378,7 +385,10 @@ type BunTxAdapter struct {
}
func (b *BunTxAdapter) NewSelect() common.SelectQuery {
return &BunSelectQuery{query: b.tx.NewSelect()}
return &BunSelectQuery{
query: b.tx.NewSelect(),
db: b.tx,
}
}
func (b *BunTxAdapter) NewInsert() common.InsertQuery {

View File

@@ -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)
// 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)
// Apply column selection
@@ -224,7 +229,7 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
var result interface{}
if 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()
query = query.Where("id = ?", id)
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
} else {
logger.Debug("Querying multiple records")
// Create a slice of pointers to the model type - use modelType which is already unwrapped
sliceType := reflect.SliceOf(reflect.PointerTo(modelType))
results := reflect.New(sliceType).Interface()
if err := query.Scan(ctx, results); err != nil {
// Use the modelPtr already created and set on the query
if err := query.Scan(ctx, modelPtr); err != nil {
logger.Error("Error querying records: %v", err)
h.sendError(w, http.StatusInternalServerError, "query_error", "Error executing query", err)
return
}
result = reflect.ValueOf(results).Elem().Interface()
result = reflect.ValueOf(modelPtr).Elem().Interface()
}
logger.Info("Successfully retrieved records")

View File

@@ -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)
// 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)
// Apply column selection