From 70721260fea54ccbec651452e224eee56a8a23f7 Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Fri, 23 Aug 2024 11:30:16 +0800 Subject: [PATCH] fix: extend query with metadata field (#21082) Co-authored-by: Qiu Jian --- pkg/cloudcommon/db/metadataresource.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/cloudcommon/db/metadataresource.go b/pkg/cloudcommon/db/metadataresource.go index e8133a84d45..d613f583040 100644 --- a/pkg/cloudcommon/db/metadataresource.go +++ b/pkg/cloudcommon/db/metadataresource.go @@ -82,6 +82,18 @@ func ObjectIdQueryWithTagFilters(ctx context.Context, q *sqlchemy.SQuery, idFiel return q } +func ExtendQueryWithTag(ctx context.Context, q *sqlchemy.SQuery, idField string, modelName string, key string, fieldLabel string) *sqlchemy.SQuery { + manager := GetMetadaManagerInContext(ctx) + metadataQ := manager.Query().Equals("obj_type", modelName).Equals("key", key) + metadataQ = metadataQ.AppendField(metadataQ.Field("value").Label(fieldLabel)) + metadataResQ := metadataQ.SubQuery() + + q = q.LeftJoin(metadataResQ, sqlchemy.Equals(q.Field(idField), metadataResQ.Field("obj_id"))) + q = q.AppendField(metadataResQ.Field(fieldLabel).Label(fieldLabel)) + + return q +} + func objIdQueryWithTags(ctx context.Context, objIdSubQ *sqlchemy.SSubQuery, idField string, modelName string, tagsList []map[string][]string) *sqlchemy.SQuery { manager := GetMetadaManagerInContext(ctx) metadataResQ := manager.Query().Equals("obj_type", modelName).SubQuery()