mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2025-12-31 00:34:25 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0899ba5029 | ||
|
|
c84dd7dc91 | ||
|
|
f1c6b36374 | ||
|
|
abee5c942f | ||
|
|
2e9a0bd51a |
@@ -22,7 +22,10 @@ func NewBunAdapter(db *bun.DB) *BunAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *BunAdapter) NewSelect() common.SelectQuery {
|
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 {
|
func (b *BunAdapter) NewInsert() common.InsertQuery {
|
||||||
@@ -78,13 +81,16 @@ func (b *BunAdapter) RunInTransaction(ctx context.Context, fn func(common.Databa
|
|||||||
// BunSelectQuery implements SelectQuery for Bun
|
// BunSelectQuery implements SelectQuery for Bun
|
||||||
type BunSelectQuery struct {
|
type BunSelectQuery struct {
|
||||||
query *bun.SelectQuery
|
query *bun.SelectQuery
|
||||||
schema string // Separated schema name
|
db bun.IDB // Store DB connection for count queries
|
||||||
tableName string // Just the table name, without schema
|
hasModel bool // Track if Model() was called
|
||||||
|
schema string // Separated schema name
|
||||||
|
tableName string // Just the table name, without schema
|
||||||
tableAlias string
|
tableAlias string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BunSelectQuery) Model(model interface{}) common.SelectQuery {
|
func (b *BunSelectQuery) Model(model interface{}) common.SelectQuery {
|
||||||
b.query = b.query.Model(model)
|
b.query = b.query.Model(model)
|
||||||
|
b.hasModel = true // Mark that we have a model
|
||||||
|
|
||||||
// Try to get table name from model if it implements TableNameProvider
|
// Try to get table name from model if it implements TableNameProvider
|
||||||
if provider, ok := model.(common.TableNameProvider); ok {
|
if provider, ok := model.(common.TableNameProvider); ok {
|
||||||
@@ -228,7 +234,19 @@ 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)
|
// If Model() was set, use bun's native Count() which works properly
|
||||||
|
if b.hasModel {
|
||||||
|
count, err := b.query.Count(ctx)
|
||||||
|
return count, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, wrap as subquery to avoid "Model(nil)" error
|
||||||
|
// This is needed when only Table() is set without a model
|
||||||
|
var count int
|
||||||
|
err := b.db.NewSelect().
|
||||||
|
TableExpr("(?) AS subquery", b.query).
|
||||||
|
ColumnExpr("COUNT(*)").
|
||||||
|
Scan(ctx, &count)
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -378,7 +396,10 @@ type BunTxAdapter struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *BunTxAdapter) NewSelect() common.SelectQuery {
|
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 {
|
func (b *BunTxAdapter) NewInsert() common.InsertQuery {
|
||||||
|
|||||||
@@ -171,8 +171,20 @@ 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
|
||||||
query := h.db.NewSelect().Table(tableName)
|
sliceType := reflect.SliceOf(reflect.PointerTo(modelType))
|
||||||
|
modelPtr := reflect.New(sliceType).Interface()
|
||||||
|
|
||||||
|
// Start with Model() using the slice pointer to avoid "Model(nil)" errors in Count()
|
||||||
|
// Bun's Model() accepts both single pointers and slice pointers
|
||||||
|
query := h.db.NewSelect().Model(modelPtr)
|
||||||
|
|
||||||
|
// Only set Table() if the model doesn't provide a table name via the underlying type
|
||||||
|
// Create a temporary instance to check for TableNameProvider
|
||||||
|
tempInstance := reflect.New(modelType).Interface()
|
||||||
|
if provider, ok := tempInstance.(common.TableNameProvider); !ok || provider.TableName() == "" {
|
||||||
|
query = query.Table(tableName)
|
||||||
|
}
|
||||||
|
|
||||||
// Apply column selection
|
// Apply column selection
|
||||||
if len(options.Columns) > 0 {
|
if len(options.Columns) > 0 {
|
||||||
@@ -224,7 +236,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 +247,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,8 +201,16 @@ 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)
|
// Start with Model() using the slice pointer to avoid "Model(nil)" errors in Count()
|
||||||
query := h.db.NewSelect().Table(tableName)
|
// Bun's Model() accepts both single pointers and slice pointers
|
||||||
|
query := h.db.NewSelect().Model(modelPtr)
|
||||||
|
|
||||||
|
// Only set Table() if the model doesn't provide a table name via the underlying type
|
||||||
|
// Create a temporary instance to check for TableNameProvider
|
||||||
|
tempInstance := reflect.New(modelType).Interface()
|
||||||
|
if provider, ok := tempInstance.(common.TableNameProvider); !ok || provider.TableName() == "" {
|
||||||
|
query = query.Table(tableName)
|
||||||
|
}
|
||||||
|
|
||||||
// Apply column selection
|
// Apply column selection
|
||||||
if len(options.Columns) > 0 {
|
if len(options.Columns) > 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user