// Package database define database operation functions package database import ( "context" "fmt" "time" "modelRT/orm" "github.com/gofrs/uuid" "gorm.io/gorm" "gorm.io/gorm/clause" ) // QueryCircuitDiagramComponentFromDB return the result of query circuit diagram component info order by page id from postgresDB // func QueryCircuitDiagramComponentFromDB(ctx context.Context, tx *gorm.DB, pool *ants.PoolWithFunc) (map[uuid.UUID]string, error) { // var components []orm.Component // // ctx超时判断 // cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) // defer cancel() // result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&components) // if result.Error != nil { // logger.Error(ctx, "query circuit diagram component info failed", "error", result.Error) // return nil, result.Error // } // componentTypeMap := make(map[uuid.UUID]string, len(components)) // for _, component := range components { // pool.Invoke(config.ModelParseConfig{ // ComponentInfo: component, // Ctx: ctx, // }) // componentTypeMap[component.GlobalUUID] = component.GlobalUUID.String() // } // return componentTypeMap, nil // } // QueryComponentByUUID return the result of query circuit diagram component info by uuid from postgresDB func QueryComponentByUUID(ctx context.Context, tx *gorm.DB, uuid uuid.UUID) (orm.Component, error) { var component orm.Component // ctx超时判断 cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() result := tx.WithContext(cancelCtx). Where("global_uuid = ?", uuid). Clauses(clause.Locking{Strength: "UPDATE"}). First(&component) if result.Error != nil { return orm.Component{}, result.Error } return component, nil } // QueryComponentByCompTag return the result of query circuit diagram component info by component tag from postgresDB func QueryComponentByCompTag(ctx context.Context, tx *gorm.DB, tag string) (orm.Component, error) { var component orm.Component result := tx.WithContext(ctx). Where("tag = ?", tag). Clauses(clause.Locking{Strength: "UPDATE"}). First(&component) if result.Error != nil { return orm.Component{}, result.Error } return component, nil } // QueryComponentByCompTags return the result of query circuit diagram component info by components tag from postgresDB func QueryComponentByCompTags(ctx context.Context, tx *gorm.DB, tags []string) (map[string]orm.Component, error) { if len(tags) == 0 { return make(map[string]orm.Component), nil } var results []orm.Component err := tx.WithContext(ctx). Model(orm.Component{}). Select("global_uuid,tag, model_name"). Where("tag IN ?", tags). Find(&results).Error if err != nil { return nil, err } compModelMap := make(map[string]orm.Component, len(results)) for _, result := range results { compModelMap[result.Tag] = result } return compModelMap, nil } // QueryComponentByPageID return the result of query circuit diagram component info by page id from postgresDB func QueryComponentByPageID(ctx context.Context, tx *gorm.DB, uuid uuid.UUID) (orm.Component, error) { var component orm.Component // ctx超时判断 cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() result := tx.WithContext(cancelCtx).Where("page_id = ? ", uuid).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&component) if result.Error != nil { return orm.Component{}, result.Error } return component, nil } // QueryComponentByNSPath return the result of query circuit diagram component info by ns path from postgresDB func QueryComponentByNSPath(ctx context.Context, tx *gorm.DB, nsPath string) (orm.Component, error) { var component orm.Component // ctx超时判断 cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() result := tx.WithContext(cancelCtx).Where("NAME = ? ", nsPath).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&component) if result.Error != nil { return orm.Component{}, result.Error } return component, nil } // QueryLongIdentModelInfoByToken define func to query long identity model info by long token func QueryLongIdentModelInfoByToken(ctx context.Context, tx *gorm.DB, measTag string, condition *orm.Component) (*orm.Component, *orm.Measurement, error) { var resultComp orm.Component var meauserment orm.Measurement // ctx timeout judgment cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).First(&resultComp, &condition) if result.Error != nil { if result.Error == gorm.ErrRecordNotFound { return nil, nil, fmt.Errorf("component record not found by %v:%w", condition, result.Error) } return nil, nil, result.Error } filterMap := map[string]any{"component_uuid": resultComp.GlobalUUID, "tag": measTag} result = tx.WithContext(cancelCtx).Where(filterMap).Clauses(clause.Locking{Strength: "UPDATE"}).First(&meauserment) if result.Error != nil { if result.Error == gorm.ErrRecordNotFound { return nil, nil, fmt.Errorf("measurement record not found by %v:%w", filterMap, result.Error) } return nil, nil, result.Error } return &resultComp, &meauserment, nil } // QueryShortIdentModelInfoByToken define func to query short identity model info by short token func QueryShortIdentModelInfoByToken(ctx context.Context, tx *gorm.DB, measTag string, condition *orm.Component) (*orm.Component, *orm.Measurement, error) { var resultComp orm.Component var meauserment orm.Measurement // ctx timeout judgment cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).First(&resultComp, &condition) if result.Error != nil { if result.Error == gorm.ErrRecordNotFound { return nil, nil, fmt.Errorf("component record not found by %v:%w", condition, result.Error) } return nil, nil, result.Error } filterMap := map[string]any{"component_uuid": resultComp.GlobalUUID, "tag": measTag} result = tx.WithContext(cancelCtx).Where(filterMap).Clauses(clause.Locking{Strength: "UPDATE"}).First(&meauserment) if result.Error != nil { if result.Error == gorm.ErrRecordNotFound { return nil, nil, fmt.Errorf("measurement record not found by %v:%w", filterMap, result.Error) } return nil, nil, result.Error } return &resultComp, &meauserment, nil }