Skip to content

Commit

Permalink
pgsql: Use booleans instead of varchar to return creation status
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-M committed Nov 12, 2016
1 parent cd23262 commit ec0aad9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
9 changes: 5 additions & 4 deletions database/pgsql/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,21 @@ func (pgSQL *pgSQL) insertFeatureVersion(featureVersion database.FeatureVersion)
}

// Find or create FeatureVersion.
var newOrExisting string
var created bool

t = time.Now()
err = tx.QueryRow(soiFeatureVersion, featureID, &featureVersion.Version).
Scan(&newOrExisting, &featureVersion.ID)
Scan(&created, &featureVersion.ID)
observeQueryTime("insertFeatureVersion", "soiFeatureVersion", t)

if err != nil {
tx.Rollback()
return 0, handleError("soiFeatureVersion", err)
}

if newOrExisting == "exi" {
// That featureVersion already exists, return its id.
if !created {
// The featureVersion already existed, no need to link it to
// vulnerabilities.
tx.Commit()

if pgSQL.cache != nil {
Expand Down
8 changes: 4 additions & 4 deletions database/pgsql/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ const (
WHERE NOT EXISTS (SELECT id FROM FeatureVersion WHERE feature_id = $1 AND version = $2)
RETURNING id
)
SELECT 'exi', id FROM FeatureVersion WHERE feature_id = $1 AND version = $2
SELECT false, id FROM FeatureVersion WHERE feature_id = $1 AND version = $2
UNION
SELECT 'new', id FROM new_featureversion`
SELECT true, id FROM new_featureversion`

searchVulnerabilityFixedInFeature = `
SELECT id, vulnerability_id, version FROM Vulnerability_FixedIn_Feature
Expand Down Expand Up @@ -167,9 +167,9 @@ const (
WHERE NOT EXISTS (SELECT id FROM Vulnerability_FixedIn_Feature WHERE vulnerability_id = $1 AND feature_id = $2)
RETURNING id
)
SELECT 'exi', id FROM Vulnerability_FixedIn_Feature WHERE vulnerability_id = $1 AND feature_id = $2
SELECT false, id FROM Vulnerability_FixedIn_Feature WHERE vulnerability_id = $1 AND feature_id = $2
UNION
SELECT 'new', id FROM new_fixedinfeature`
SELECT true, id FROM new_fixedinfeature`

searchFeatureVersionByFeature = `SELECT id, version FROM FeatureVersion WHERE feature_id = $1`

Expand Down
8 changes: 4 additions & 4 deletions database/pgsql/vulnerability.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,22 +433,22 @@ func (pgSQL *pgSQL) insertVulnerabilityFixedInFeatureVersions(tx *sql.Tx, vulner

for _, fv := range fixedIn {
var fixedInID int
var newOrExisting string
var created bool

// Find or create entry in Vulnerability_FixedIn_Feature.
err = tx.QueryRow(
soiVulnerabilityFixedInFeature,
vulnerabilityID, fv.Feature.ID,
&fv.Version,
).Scan(&newOrExisting, &fixedInID)
).Scan(&created, &fixedInID)

if err != nil {
return handleError("insertVulnerabilityFixedInFeature", err)
}

if newOrExisting == "exi" {
if !created {
// The relationship between the feature and the vulnerability already
// exists, there's no need to update Vulnerability_Affects_FeatureVersion.
// existed, no need to update Vulnerability_Affects_FeatureVersion.
continue
}

Expand Down

0 comments on commit ec0aad9

Please sign in to comment.