From 649128fec841cae9a63d2d170fc0489540b14269 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 11 Apr 2024 12:10:50 +0800 Subject: [PATCH] Fix lint and update format of SQL --- models/packages/package.go | 41 +++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/models/packages/package.go b/models/packages/package.go index 8a91062743..a3e9c739af 100644 --- a/models/packages/package.go +++ b/models/packages/package.go @@ -204,25 +204,30 @@ func TryInsertPackage(ctx context.Context, p *Package) (*Package, error) { } case setting.Database.Type.IsMSSQL(): r := func(s string) string { - return strings.Replace(s, "'", "''", -1) + return strings.ReplaceAll(s, "'", "''") } - sql := fmt.Sprintf(`MERGE INTO package WITH (HOLDLOCK) AS target - USING (SELECT - %d AS owner_id - ,'%s' AS [type] - ,'%s' AS lower_name - ,'%s' AS name - , %s AS semver_compatible) AS source - (owner_id, [type], lower_name, name, semver_compatible) -ON (target.owner_id = source.owner_id - AND target.[type] = source.[type] - AND target.lower_name = source.lower_name) -WHEN MATCHED - THEN UPDATE - SET 1 = 1 -WHEN NOT MATCHED - THEN INSERT (owner_id, [type], lower_name, name, semver_compatible) - VALUES (%d, '%s', '%s', '%s', %s)`, + sql := fmt.Sprintf(` + MERGE INTO package WITH (HOLDLOCK) AS target USING ( + SELECT + %d AS owner_id, + '%s' AS [type], + '%s' AS lower_name, + '%s' AS name, + %s AS semver_compatible + ) AS source ( + owner_id, [type], lower_name, name, semver_compatible + ) ON ( + target.owner_id = source.owner_id + AND target.[type] = source.[type] + AND target.lower_name = source.lower_name + ) WHEN MATCHED + THEN UPDATE SET 1 = 1 + WHEN NOT MATCHED + THEN INSERT ( + owner_id, [type], lower_name, name, semver_compatible + ) VALUES ( + %d, '%s', '%s', '%s', %s + )`, p.OwnerID, r(string(p.Type)), r(p.LowerName), r(p.Name), strconv.FormatBool(p.SemverCompatible), p.OwnerID, r(string(p.Type)), r(p.LowerName), r(p.Name), strconv.FormatBool(p.SemverCompatible), )