modelRT/database/query_topologic.go

53 lines
1.5 KiB
Go
Raw Permalink Normal View History

// Package database define database operation functions
package database
import (
"context"
"time"
"modelRT/logger"
"modelRT/orm"
"modelRT/sql"
"github.com/gofrs/uuid"
2025-01-09 15:56:40 +08:00
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// QueryTopologic return the topologic info of the circuit diagram
func QueryTopologic(ctx context.Context, tx *gorm.DB) ([]orm.Topologic, error) {
var topologics []orm.Topologic
// ctx超时判断
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
2025-01-09 15:56:40 +08:00
result := tx.WithContext(cancelCtx).
Clauses(clause.Locking{Strength: "UPDATE"}).
Find(&topologics)
if result.Error != nil {
logger.Error(ctx, "query circuit diagram topologic info failed", "error", result.Error)
return nil, result.Error
}
return topologics, nil
}
// QueryTopologicByStartUUID returns all directed edges reachable from startUUID.
// It is used by point-to-point topology reachability checks and intentionally
// does not depend on the legacy all-zero UUID virtual root.
func QueryTopologicByStartUUID(ctx context.Context, tx *gorm.DB, startUUID uuid.UUID) ([]orm.Topologic, error) {
var topologics []orm.Topologic
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
result := tx.WithContext(cancelCtx).
Clauses(clause.Locking{Strength: "UPDATE"}).
Raw(sql.RecursiveTopologicByStartSQL, startUUID).
Scan(&topologics)
if result.Error != nil {
logger.Error(ctx, "query topologic by start uuid failed", "start_uuid", startUUID, "error", result.Error)
return nil, result.Error
}
return topologics, nil
}