2017-01-13 07:08:52 +00:00
|
|
|
// Copyright 2017 clair authors
|
2016-01-19 20:16:45 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
package pgsql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2016-12-28 01:45:11 +00:00
|
|
|
"strings"
|
2016-01-24 03:02:34 +00:00
|
|
|
"time"
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2016-12-28 01:45:11 +00:00
|
|
|
"github.com/guregu/null/zero"
|
2017-05-04 17:21:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-12-28 01:45:11 +00:00
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2017-01-13 07:08:52 +00:00
|
|
|
"github.com/coreos/clair/pkg/commonerr"
|
2015-12-28 20:03:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (pgSQL *pgSQL) FindLayer(name string, withFeatures, withVulnerabilities bool) (database.Layer, error) {
|
2016-01-24 03:02:34 +00:00
|
|
|
subquery := "all"
|
|
|
|
if withFeatures {
|
|
|
|
subquery += "/features"
|
|
|
|
} else if withVulnerabilities {
|
|
|
|
subquery += "/features+vulnerabilities"
|
|
|
|
}
|
|
|
|
defer observeQueryTime("FindLayer", subquery, time.Now())
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
// Find the layer
|
2016-12-28 01:45:11 +00:00
|
|
|
var (
|
|
|
|
layer database.Layer
|
|
|
|
parentID zero.Int
|
|
|
|
parentName zero.String
|
|
|
|
nsID zero.Int
|
|
|
|
nsName sql.NullString
|
|
|
|
nsVersionFormat sql.NullString
|
|
|
|
)
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2016-01-24 03:02:34 +00:00
|
|
|
t := time.Now()
|
2016-12-28 01:45:11 +00:00
|
|
|
err := pgSQL.QueryRow(searchLayer, name).Scan(
|
|
|
|
&layer.ID,
|
|
|
|
&layer.Name,
|
|
|
|
&layer.EngineVersion,
|
|
|
|
&parentID,
|
|
|
|
&parentName,
|
|
|
|
&nsID,
|
|
|
|
&nsName,
|
|
|
|
&nsVersionFormat,
|
|
|
|
)
|
2016-02-23 00:15:38 +00:00
|
|
|
observeQueryTime("FindLayer", "searchLayer", t)
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
if err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return layer, handleError("searchLayer", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 15:27:30 +00:00
|
|
|
if !parentID.IsZero() {
|
|
|
|
layer.Parent = &database.Layer{
|
|
|
|
Model: database.Model{ID: int(parentID.Int64)},
|
|
|
|
Name: parentName.String,
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
2016-12-28 01:45:11 +00:00
|
|
|
if !nsID.IsZero() {
|
2016-01-08 15:27:30 +00:00
|
|
|
layer.Namespace = &database.Namespace{
|
2016-12-28 01:45:11 +00:00
|
|
|
Model: database.Model{ID: int(nsID.Int64)},
|
|
|
|
Name: nsName.String,
|
|
|
|
VersionFormat: nsVersionFormat.String,
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find its features
|
|
|
|
if withFeatures || withVulnerabilities {
|
2016-02-20 01:55:54 +00:00
|
|
|
// Create a transaction to disable hash/merge joins as our experiments have shown that
|
|
|
|
// PostgreSQL 9.4 makes bad planning decisions about:
|
|
|
|
// - joining the layer tree to feature versions and feature
|
|
|
|
// - joining the feature versions to affected/fixed feature version and vulnerabilities
|
|
|
|
// It would for instance do a merge join between affected feature versions (300 rows, estimated
|
|
|
|
// 3000 rows) and fixed in feature version (100k rows). In this case, it is much more
|
|
|
|
// preferred to use a nested loop.
|
|
|
|
tx, err := pgSQL.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return layer, handleError("FindLayer.Begin()", err)
|
|
|
|
}
|
|
|
|
defer tx.Commit()
|
|
|
|
|
2016-02-23 00:15:38 +00:00
|
|
|
_, err = tx.Exec(disableHashJoin)
|
2016-02-20 01:55:54 +00:00
|
|
|
if err != nil {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithError(err).Warningf("FindLayer: could not disable hash join")
|
2016-02-20 01:55:54 +00:00
|
|
|
}
|
2016-02-23 00:15:38 +00:00
|
|
|
_, err = tx.Exec(disableMergeJoin)
|
2016-02-20 01:55:54 +00:00
|
|
|
if err != nil {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithError(err).Warningf("FindLayer: could not disable merge join")
|
2016-02-20 01:55:54 +00:00
|
|
|
}
|
|
|
|
|
2016-01-24 03:02:34 +00:00
|
|
|
t = time.Now()
|
2016-02-20 01:55:54 +00:00
|
|
|
featureVersions, err := getLayerFeatureVersions(tx, layer.ID)
|
2016-01-24 03:02:34 +00:00
|
|
|
observeQueryTime("FindLayer", "getLayerFeatureVersions", t)
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return layer, err
|
|
|
|
}
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
layer.Features = featureVersions
|
|
|
|
|
|
|
|
if withVulnerabilities {
|
|
|
|
// Load the vulnerabilities that affect the FeatureVersions.
|
2016-01-24 03:02:34 +00:00
|
|
|
t = time.Now()
|
2016-02-20 01:55:54 +00:00
|
|
|
err := loadAffectedBy(tx, layer.Features)
|
2016-01-24 03:02:34 +00:00
|
|
|
observeQueryTime("FindLayer", "loadAffectedBy", t)
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return layer, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return layer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getLayerFeatureVersions returns list of database.FeatureVersion that a database.Layer has.
|
2016-02-20 01:55:54 +00:00
|
|
|
func getLayerFeatureVersions(tx *sql.Tx, layerID int) ([]database.FeatureVersion, error) {
|
2015-12-28 20:03:29 +00:00
|
|
|
var featureVersions []database.FeatureVersion
|
|
|
|
|
2016-01-19 03:03:38 +00:00
|
|
|
// Query.
|
2016-02-23 00:15:38 +00:00
|
|
|
rows, err := tx.Query(searchLayerFeatureVersion, layerID)
|
2016-01-12 15:40:46 +00:00
|
|
|
if err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return featureVersions, handleError("searchLayerFeatureVersion", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2016-01-19 03:03:38 +00:00
|
|
|
// Scan query.
|
2015-12-28 20:03:29 +00:00
|
|
|
var modification string
|
|
|
|
mapFeatureVersions := make(map[int]database.FeatureVersion)
|
|
|
|
for rows.Next() {
|
2016-12-28 01:45:11 +00:00
|
|
|
var fv database.FeatureVersion
|
|
|
|
err = rows.Scan(
|
|
|
|
&fv.ID,
|
|
|
|
&modification,
|
|
|
|
&fv.Feature.Namespace.ID,
|
|
|
|
&fv.Feature.Namespace.Name,
|
|
|
|
&fv.Feature.Namespace.VersionFormat,
|
|
|
|
&fv.Feature.ID,
|
|
|
|
&fv.Feature.Name,
|
|
|
|
&fv.ID,
|
|
|
|
&fv.Version,
|
|
|
|
&fv.AddedBy.ID,
|
|
|
|
&fv.AddedBy.Name,
|
|
|
|
)
|
2016-01-19 03:03:38 +00:00
|
|
|
if err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return featureVersions, handleError("searchLayerFeatureVersion.Scan()", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-19 03:03:38 +00:00
|
|
|
// Do transitive closure.
|
2015-12-28 20:03:29 +00:00
|
|
|
switch modification {
|
|
|
|
case "add":
|
2016-12-28 01:45:11 +00:00
|
|
|
mapFeatureVersions[fv.ID] = fv
|
2015-12-28 20:03:29 +00:00
|
|
|
case "del":
|
2016-12-28 01:45:11 +00:00
|
|
|
delete(mapFeatureVersions, fv.ID)
|
2015-12-28 20:03:29 +00:00
|
|
|
default:
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithField("modification", modification).Warning("unknown Layer_diff_FeatureVersion's modification")
|
2015-12-28 20:03:29 +00:00
|
|
|
return featureVersions, database.ErrInconsistent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return featureVersions, handleError("searchLayerFeatureVersion.Rows()", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-19 03:03:38 +00:00
|
|
|
// Build result by converting our map to a slice.
|
2015-12-28 20:03:29 +00:00
|
|
|
for _, featureVersion := range mapFeatureVersions {
|
|
|
|
featureVersions = append(featureVersions, featureVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
return featureVersions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadAffectedBy returns the list of database.Vulnerability that affect the given
|
|
|
|
// FeatureVersion.
|
2016-02-20 01:55:54 +00:00
|
|
|
func loadAffectedBy(tx *sql.Tx, featureVersions []database.FeatureVersion) error {
|
2015-12-28 20:03:29 +00:00
|
|
|
if len(featureVersions) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct list of FeatureVersion IDs, we will do a single query
|
|
|
|
featureVersionIDs := make([]int, 0, len(featureVersions))
|
|
|
|
for i := 0; i < len(featureVersions); i++ {
|
|
|
|
featureVersionIDs = append(featureVersionIDs, featureVersions[i].ID)
|
|
|
|
}
|
|
|
|
|
2016-02-23 00:15:38 +00:00
|
|
|
rows, err := tx.Query(searchFeatureVersionVulnerability,
|
2015-12-28 20:03:29 +00:00
|
|
|
buildInputArray(featureVersionIDs))
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2016-02-23 00:15:38 +00:00
|
|
|
return handleError("searchFeatureVersionVulnerability", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
vulnerabilities := make(map[int][]database.Vulnerability, len(featureVersions))
|
|
|
|
var featureversionID int
|
|
|
|
for rows.Next() {
|
|
|
|
var vulnerability database.Vulnerability
|
2016-12-28 01:45:11 +00:00
|
|
|
err := rows.Scan(
|
|
|
|
&featureversionID,
|
|
|
|
&vulnerability.ID,
|
|
|
|
&vulnerability.Name,
|
|
|
|
&vulnerability.Description,
|
|
|
|
&vulnerability.Link,
|
|
|
|
&vulnerability.Severity,
|
|
|
|
&vulnerability.Metadata,
|
|
|
|
&vulnerability.Namespace.Name,
|
|
|
|
&vulnerability.Namespace.VersionFormat,
|
|
|
|
&vulnerability.FixedBy,
|
|
|
|
)
|
2015-12-28 20:03:29 +00:00
|
|
|
if err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return handleError("searchFeatureVersionVulnerability.Scan()", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
vulnerabilities[featureversionID] = append(vulnerabilities[featureversionID], vulnerability)
|
|
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return handleError("searchFeatureVersionVulnerability.Rows()", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assign vulnerabilities to every FeatureVersions
|
|
|
|
for i := 0; i < len(featureVersions); i++ {
|
|
|
|
featureVersions[i].AffectedBy = vulnerabilities[featureVersions[i].ID]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internally, only Feature additions/removals are stored for each layer. If a layer has a parent,
|
|
|
|
// the Feature list will be compared to the parent's Feature list and the difference will be stored.
|
|
|
|
// Note that when the Namespace of a layer differs from its parent, it is expected that several
|
|
|
|
// Feature that were already included a parent will have their Namespace updated as well
|
|
|
|
// (happens when Feature detectors relies on the detected layer Namespace). However, if the listed
|
|
|
|
// Feature has the same Name/Version as its parent, InsertLayer considers that the Feature hasn't
|
|
|
|
// been modified.
|
|
|
|
func (pgSQL *pgSQL) InsertLayer(layer database.Layer) error {
|
2016-01-24 03:02:34 +00:00
|
|
|
tf := time.Now()
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
// Verify parameters
|
|
|
|
if layer.Name == "" {
|
|
|
|
log.Warning("could not insert a layer which has an empty Name")
|
2017-01-13 07:08:52 +00:00
|
|
|
return commonerr.NewBadRequestError("could not insert a layer which has an empty Name")
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get a potentially existing layer.
|
|
|
|
existingLayer, err := pgSQL.FindLayer(layer.Name, true, false)
|
2017-01-13 07:08:52 +00:00
|
|
|
if err != nil && err != commonerr.ErrNotFound {
|
2015-12-28 20:03:29 +00:00
|
|
|
return err
|
|
|
|
} else if err == nil {
|
2016-01-12 15:40:46 +00:00
|
|
|
if existingLayer.EngineVersion >= layer.EngineVersion {
|
2016-02-24 23:00:36 +00:00
|
|
|
// The layer exists and has an equal or higher engine version, do nothing.
|
2016-01-12 15:40:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
layer.ID = existingLayer.ID
|
|
|
|
}
|
|
|
|
|
2016-01-24 03:02:34 +00:00
|
|
|
// We do `defer observeQueryTime` here because we don't want to observe existing layers.
|
|
|
|
defer observeQueryTime("InsertLayer", "all", tf)
|
|
|
|
|
2016-01-12 15:40:46 +00:00
|
|
|
// Get parent ID.
|
|
|
|
var parentID zero.Int
|
|
|
|
if layer.Parent != nil {
|
|
|
|
if layer.Parent.ID == 0 {
|
|
|
|
log.Warning("Parent is expected to be retrieved from database when inserting a layer.")
|
2017-01-13 07:08:52 +00:00
|
|
|
return commonerr.NewBadRequestError("Parent is expected to be retrieved from database when inserting a layer.")
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parentID = zero.IntFrom(int64(layer.Parent.ID))
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find or insert namespace if provided.
|
|
|
|
var namespaceID zero.Int
|
|
|
|
if layer.Namespace != nil {
|
|
|
|
n, err := pgSQL.insertNamespace(*layer.Namespace)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
namespaceID = zero.IntFrom(int64(n))
|
2016-01-08 15:27:30 +00:00
|
|
|
} else if layer.Namespace == nil && layer.Parent != nil {
|
|
|
|
// Import the Namespace from the parent if it has one and this layer doesn't specify one.
|
|
|
|
if layer.Parent.Namespace != nil {
|
|
|
|
namespaceID = zero.IntFrom(int64(layer.Parent.Namespace.ID))
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 15:40:46 +00:00
|
|
|
// Begin transaction.
|
|
|
|
tx, err := pgSQL.Begin()
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return handleError("InsertLayer.Begin()", err)
|
|
|
|
}
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
if layer.ID == 0 {
|
|
|
|
// Insert a new layer.
|
2016-02-23 00:15:38 +00:00
|
|
|
err = tx.QueryRow(insertLayer, layer.Name, layer.EngineVersion, parentID, namespaceID).
|
2015-12-28 20:03:29 +00:00
|
|
|
Scan(&layer.ID)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2016-02-05 23:08:23 +00:00
|
|
|
|
|
|
|
if isErrUniqueViolation(err) {
|
|
|
|
// Ignore this error, another process collided.
|
2016-03-08 19:39:53 +00:00
|
|
|
log.Debug("Attempted to insert duplicate layer.")
|
2016-02-05 23:08:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-02-23 00:15:38 +00:00
|
|
|
return handleError("insertLayer", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Update an existing layer.
|
2016-02-23 00:15:38 +00:00
|
|
|
_, err = tx.Exec(updateLayer, layer.ID, layer.EngineVersion, namespaceID)
|
2015-12-28 20:03:29 +00:00
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2016-02-23 00:15:38 +00:00
|
|
|
return handleError("updateLayer", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
2016-01-08 15:27:30 +00:00
|
|
|
|
|
|
|
// Remove all existing Layer_diff_FeatureVersion.
|
2016-02-23 00:15:38 +00:00
|
|
|
_, err = tx.Exec(removeLayerDiffFeatureVersion, layer.ID)
|
2016-01-08 15:27:30 +00:00
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2016-02-23 00:15:38 +00:00
|
|
|
return handleError("removeLayerDiffFeatureVersion", err)
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update Layer_diff_FeatureVersion now.
|
2016-01-08 15:27:30 +00:00
|
|
|
err = pgSQL.updateDiffFeatureVersions(tx, &layer, &existingLayer)
|
|
|
|
if err != nil {
|
2016-01-12 15:40:46 +00:00
|
|
|
tx.Rollback()
|
2016-01-08 15:27:30 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
|
|
|
|
// Commit transaction.
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2016-01-08 16:17:32 +00:00
|
|
|
return handleError("InsertLayer.Commit()", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 15:27:30 +00:00
|
|
|
func (pgSQL *pgSQL) updateDiffFeatureVersions(tx *sql.Tx, layer, existingLayer *database.Layer) error {
|
|
|
|
// add and del are the FeatureVersion diff we should insert.
|
|
|
|
var add []database.FeatureVersion
|
|
|
|
var del []database.FeatureVersion
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2016-01-08 15:27:30 +00:00
|
|
|
if layer.Parent == nil {
|
2015-12-28 20:03:29 +00:00
|
|
|
// There is no parent, every Features are added.
|
2016-01-08 15:27:30 +00:00
|
|
|
add = append(add, layer.Features...)
|
2015-12-28 20:03:29 +00:00
|
|
|
} else if layer.Parent != nil {
|
|
|
|
// There is a parent, we need to diff the Features with it.
|
|
|
|
|
2016-01-12 15:40:46 +00:00
|
|
|
// Build name:version structures.
|
2016-01-08 15:27:30 +00:00
|
|
|
layerFeaturesMapNV, layerFeaturesNV := createNV(layer.Features)
|
|
|
|
parentLayerFeaturesMapNV, parentLayerFeaturesNV := createNV(layer.Parent.Features)
|
|
|
|
|
|
|
|
// Calculate the added and deleted FeatureVersions name:version.
|
2017-01-18 02:40:59 +00:00
|
|
|
addNV := compareStringLists(layerFeaturesNV, parentLayerFeaturesNV)
|
|
|
|
delNV := compareStringLists(parentLayerFeaturesNV, layerFeaturesNV)
|
2016-01-08 15:27:30 +00:00
|
|
|
|
2016-05-11 22:13:00 +00:00
|
|
|
// Fill the structures containing the added and deleted FeatureVersions.
|
2016-01-08 15:27:30 +00:00
|
|
|
for _, nv := range addNV {
|
|
|
|
add = append(add, *layerFeaturesMapNV[nv])
|
|
|
|
}
|
|
|
|
for _, nv := range delNV {
|
|
|
|
del = append(del, *parentLayerFeaturesMapNV[nv])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert FeatureVersions in the database.
|
|
|
|
addIDs, err := pgSQL.insertFeatureVersions(add)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delIDs, err := pgSQL.insertFeatureVersions(del)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert diff in the database.
|
|
|
|
if len(addIDs) > 0 {
|
2016-02-23 00:15:38 +00:00
|
|
|
_, err = tx.Exec(insertLayerDiffFeatureVersion, layer.ID, "add", buildInputArray(addIDs))
|
2016-01-08 15:27:30 +00:00
|
|
|
if err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return handleError("insertLayerDiffFeatureVersion.Add", err)
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(delIDs) > 0 {
|
2016-02-23 00:15:38 +00:00
|
|
|
_, err = tx.Exec(insertLayerDiffFeatureVersion, layer.ID, "del", buildInputArray(delIDs))
|
2016-01-08 15:27:30 +00:00
|
|
|
if err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return handleError("insertLayerDiffFeatureVersion.Del", err)
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createNV(features []database.FeatureVersion) (map[string]*database.FeatureVersion, []string) {
|
|
|
|
mapNV := make(map[string]*database.FeatureVersion, 0)
|
|
|
|
sliceNV := make([]string, 0, len(features))
|
|
|
|
|
|
|
|
for i := 0; i < len(features); i++ {
|
2016-12-28 01:45:11 +00:00
|
|
|
fv := &features[i]
|
|
|
|
nv := strings.Join([]string{fv.Feature.Namespace.Name, fv.Feature.Name, fv.Version}, ":")
|
|
|
|
mapNV[nv] = fv
|
2016-01-08 15:27:30 +00:00
|
|
|
sliceNV = append(sliceNV, nv)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
2016-01-08 15:27:30 +00:00
|
|
|
|
|
|
|
return mapNV, sliceNV
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pgSQL *pgSQL) DeleteLayer(name string) error {
|
2016-01-24 03:02:34 +00:00
|
|
|
defer observeQueryTime("DeleteLayer", "all", time.Now())
|
|
|
|
|
2016-02-23 00:15:38 +00:00
|
|
|
result, err := pgSQL.Exec(removeLayer, name)
|
2016-01-20 18:57:51 +00:00
|
|
|
if err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return handleError("removeLayer", err)
|
2016-01-20 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return handleError("removeLayer.RowsAffected()", err)
|
2016-01-20 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if affected <= 0 {
|
2017-01-13 07:08:52 +00:00
|
|
|
return commonerr.ErrNotFound
|
2016-01-20 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
return nil
|
|
|
|
}
|