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.
|
|
|
|
|
2019-03-06 21:31:42 +00:00
|
|
|
package keyvalue
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2016-01-21 23:30:51 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
2016-01-24 03:02:34 +00:00
|
|
|
"time"
|
2016-01-21 23:30:51 +00:00
|
|
|
|
2017-05-04 17:21:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2019-03-06 21:31:42 +00:00
|
|
|
"github.com/coreos/clair/database/pgsql/monitoring"
|
|
|
|
"github.com/coreos/clair/database/pgsql/util"
|
2017-01-13 07:08:52 +00:00
|
|
|
"github.com/coreos/clair/pkg/commonerr"
|
2016-01-21 23:30:51 +00:00
|
|
|
)
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2018-09-19 19:38:07 +00:00
|
|
|
const (
|
|
|
|
searchKeyValue = `SELECT value FROM KeyValue WHERE key = $1`
|
|
|
|
upsertKeyValue = `
|
|
|
|
INSERT INTO KeyValue(key, value)
|
|
|
|
VALUES ($1, $2)
|
|
|
|
ON CONFLICT ON CONSTRAINT keyvalue_key_key
|
|
|
|
DO UPDATE SET key=$1, value=$2`
|
|
|
|
)
|
|
|
|
|
2019-03-06 21:31:42 +00:00
|
|
|
func UpdateKeyValue(tx *sql.Tx, key, value string) (err error) {
|
2015-12-28 20:03:29 +00:00
|
|
|
if key == "" || value == "" {
|
|
|
|
log.Warning("could not insert a flag which has an empty name or value")
|
2017-01-13 07:08:52 +00:00
|
|
|
return commonerr.NewBadRequestError("could not insert a flag which has an empty name or value")
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 21:31:42 +00:00
|
|
|
defer monitoring.ObserveQueryTime("PersistKeyValue", "all", time.Now())
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
_, err = tx.Exec(upsertKeyValue, key, value)
|
|
|
|
if err != nil {
|
2019-03-06 21:31:42 +00:00
|
|
|
return util.HandleError("insertKeyValue", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
|
|
|
|
return nil
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 21:31:42 +00:00
|
|
|
func FindKeyValue(tx *sql.Tx, key string) (string, bool, error) {
|
|
|
|
defer monitoring.ObserveQueryTime("FindKeyValue", "all", time.Now())
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2016-01-08 16:17:32 +00:00
|
|
|
var value string
|
2017-07-26 23:23:54 +00:00
|
|
|
err := tx.QueryRow(searchKeyValue, key).Scan(&value)
|
2016-01-21 23:30:51 +00:00
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
2017-07-26 23:23:54 +00:00
|
|
|
return "", false, nil
|
2016-01-21 23:30:51 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
|
2016-01-08 16:17:32 +00:00
|
|
|
if err != nil {
|
2019-03-06 21:31:42 +00:00
|
|
|
return "", false, util.HandleError("searchKeyValue", err)
|
2016-01-08 16:17:32 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
return value, true, nil
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|