mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2025-12-30 16:24:26 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f1c6b36374 | ||
|
|
abee5c942f |
@@ -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 {
|
||||
|
||||
@@ -171,13 +171,13 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
|
||||
|
||||
logger.Info("Reading records from %s.%s", schema, entity)
|
||||
|
||||
// Create the model pointer early for Bun compatibility
|
||||
// Bun requires Model() to be set for Count() and Scan() operations
|
||||
// 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 Model() and Table() - Bun needs both for proper operation
|
||||
query := h.db.NewSelect().Model(modelPtr).Table(tableName)
|
||||
// Use only Table() - model will be provided to Scan() directly
|
||||
query := h.db.NewSelect().Table(tableName)
|
||||
|
||||
// Apply column selection
|
||||
if len(options.Columns) > 0 {
|
||||
|
||||
@@ -201,9 +201,9 @@ 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 and Model() for Bun compatibility
|
||||
// Bun requires Model() to be set for Count() and Scan() operations
|
||||
query := h.db.NewSelect().Model(modelPtr).Table(tableName)
|
||||
// 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
|
||||
if len(options.Columns) > 0 {
|
||||
|
||||
Reference in New Issue
Block a user