53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
// Package database define database operation functions
|
|
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"modelRT/orm"
|
|
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
var recursiveSQL = `WITH RECURSIVE recursive_tree as (
|
|
SELECT uuid_from,uuid_to,page_id,flag
|
|
FROM "Topologic"
|
|
WHERE uuid_from is null and page_id = ?
|
|
UNION ALL
|
|
SELECT t.uuid_from,t.uuid_to,t.page_id,t.flag
|
|
FROM "Topologic" t
|
|
JOIN recursive_tree rt ON t.uuid_from = rt.uuid_to
|
|
)
|
|
SELECT * FROM recursive_tree;`
|
|
|
|
// QueryTopologicByPageID return the topologic info of the circuit diagram query by pageID
|
|
func QueryTopologicByPageID(ctx context.Context, logger *zap.Logger, pageID int64) ([]orm.Topologic, error) {
|
|
var topologics []orm.Topologic
|
|
// ctx超时判断
|
|
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
result := _globalPostgresClient.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Raw(recursiveSQL, pageID).Scan(&topologics)
|
|
if result.Error != nil {
|
|
logger.Error("query circuit diagram topologic info by pageID failed", zap.Int64("pageID", pageID), zap.Error(result.Error))
|
|
return nil, result.Error
|
|
}
|
|
return topologics, nil
|
|
}
|
|
|
|
// InitCircuitDiagramTopologic return circuit diagram topologic info from postgres
|
|
func InitCircuitDiagramTopologic(topologicNodes []orm.Topologic) error {
|
|
// find root node
|
|
var rootNode orm.Topologic
|
|
for index, node := range topologicNodes {
|
|
if node.UUIDFrom.IsNil() {
|
|
rootNode = topologicNodes[index]
|
|
}
|
|
}
|
|
|
|
fmt.Println(rootNode)
|
|
return nil
|
|
}
|