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 (
|
2017-07-26 23:23:54 +00:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"sort"
|
2016-01-24 03:02:34 +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
|
|
|
)
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
var (
|
|
|
|
errNamespaceNotFound = errors.New("Requested Namespace is not in database")
|
|
|
|
)
|
|
|
|
|
|
|
|
// PersistNamespaces soi namespaces into database.
|
|
|
|
func (tx *pgSession) PersistNamespaces(namespaces []database.Namespace) error {
|
|
|
|
if len(namespaces) == 0 {
|
|
|
|
return nil
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
// Sorting is needed before inserting into database to prevent deadlock.
|
|
|
|
sort.Slice(namespaces, func(i, j int) bool {
|
|
|
|
return namespaces[i].Name < namespaces[j].Name &&
|
|
|
|
namespaces[i].VersionFormat < namespaces[j].VersionFormat
|
|
|
|
})
|
|
|
|
|
|
|
|
keys := make([]interface{}, len(namespaces)*2)
|
|
|
|
for i, ns := range namespaces {
|
|
|
|
if ns.Name == "" || ns.VersionFormat == "" {
|
|
|
|
return commonerr.NewBadRequestError("Empty namespace name or version format is not allowed")
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
keys[i*2] = ns.Name
|
|
|
|
keys[i*2+1] = ns.VersionFormat
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
_, err := tx.Exec(queryPersistNamespace(len(namespaces)), keys...)
|
2016-01-08 16:17:32 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return handleError("queryPersistNamespace", err)
|
2016-01-08 16:17:32 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func (tx *pgSession) findNamespaceIDs(namespaces []database.Namespace) ([]sql.NullInt64, error) {
|
|
|
|
if len(namespaces) == 0 {
|
|
|
|
return nil, nil
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
keys := make([]interface{}, len(namespaces)*2)
|
|
|
|
nsMap := map[database.Namespace]sql.NullInt64{}
|
|
|
|
for i, n := range namespaces {
|
|
|
|
keys[i*2] = n.Name
|
|
|
|
keys[i*2+1] = n.VersionFormat
|
|
|
|
nsMap[n] = sql.NullInt64{}
|
|
|
|
}
|
2016-01-28 16:26:34 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
rows, err := tx.Query(querySearchNamespace(len(namespaces)), keys...)
|
2016-01-28 16:26:34 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return nil, handleError("searchNamespace", err)
|
2016-01-28 16:26:34 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
|
2016-01-28 16:26:34 +00:00
|
|
|
defer rows.Close()
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
var (
|
|
|
|
id sql.NullInt64
|
|
|
|
ns database.Namespace
|
|
|
|
)
|
2016-01-28 16:26:34 +00:00
|
|
|
for rows.Next() {
|
2017-07-26 23:23:54 +00:00
|
|
|
err := rows.Scan(&id, &ns.Name, &ns.VersionFormat)
|
2016-01-28 16:26:34 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return nil, handleError("searchNamespace", err)
|
2016-01-28 16:26:34 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
nsMap[ns] = id
|
2016-01-28 16:26:34 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
|
|
|
|
ids := make([]sql.NullInt64, len(namespaces))
|
|
|
|
for i, ns := range namespaces {
|
|
|
|
ids[i] = nsMap[ns]
|
2016-01-28 16:26:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
return ids, nil
|
2016-01-28 16:26:34 +00:00
|
|
|
}
|