2019-03-14 13:36:18 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package readdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/db"
|
2022-02-22 14:01:29 +00:00
|
|
|
"agola.io/agola/internal/errors"
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
"agola.io/agola/services/configstore/types"
|
2019-03-14 13:36:18 +00:00
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
variableSelect = sb.Select("id", "data").From("variable")
|
2019-07-09 08:16:45 +00:00
|
|
|
variableInsert = sb.Insert("variable").Columns("id", "name", "parentid", "parenttype", "data")
|
2019-03-14 13:36:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r *ReadDB) insertVariable(tx *db.Tx, data []byte) error {
|
|
|
|
variable := types.Variable{}
|
|
|
|
if err := json.Unmarshal(data, &variable); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "failed to unmarshal variable")
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
// poor man insert or update...
|
|
|
|
if err := r.deleteVariable(tx, variable.ID); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
2019-07-09 08:16:45 +00:00
|
|
|
q, args, err := variableInsert.Values(variable.ID, variable.Name, variable.Parent.ID, variable.Parent.Type, data).ToSql()
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "failed to build query")
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
2019-05-23 09:23:14 +00:00
|
|
|
if _, err = tx.Exec(q, args...); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "failed to insert variable")
|
2019-05-23 09:23:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ReadDB) deleteVariable(tx *db.Tx, id string) error {
|
|
|
|
// poor man insert or update...
|
|
|
|
if _, err := tx.Exec("delete from variable where id = $1", id); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "failed to delete variable")
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ReadDB) GetVariableByID(tx *db.Tx, variableID string) (*types.Variable, error) {
|
|
|
|
q, args, err := variableSelect.Where(sq.Eq{"id": variableID}).ToSql()
|
2022-02-21 17:07:58 +00:00
|
|
|
r.log.Debug().Msgf("q: %s, args: %s", q, util.Dump(args))
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to build query")
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
variables, _, err := fetchVariables(tx, q, args...)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
if len(variables) > 1 {
|
|
|
|
return nil, errors.Errorf("too many rows returned")
|
|
|
|
}
|
|
|
|
if len(variables) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return variables[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ReadDB) GetVariableByName(tx *db.Tx, parentID, name string) (*types.Variable, error) {
|
|
|
|
q, args, err := variableSelect.Where(sq.Eq{"parentid": parentID, "name": name}).ToSql()
|
2022-02-21 17:07:58 +00:00
|
|
|
r.log.Debug().Msgf("q: %s, args: %s", q, util.Dump(args))
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to build query")
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
variables, _, err := fetchVariables(tx, q, args...)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
if len(variables) > 1 {
|
|
|
|
return nil, errors.Errorf("too many rows returned")
|
|
|
|
}
|
|
|
|
if len(variables) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return variables[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ReadDB) GetVariables(tx *db.Tx, parentID string) ([]*types.Variable, error) {
|
|
|
|
q, args, err := variableSelect.Where(sq.Eq{"parentid": parentID}).ToSql()
|
2022-02-21 17:07:58 +00:00
|
|
|
r.log.Debug().Msgf("q: %s, args: %s", q, util.Dump(args))
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to build query")
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
variables, _, err := fetchVariables(tx, q, args...)
|
2022-02-22 14:01:29 +00:00
|
|
|
return variables, errors.WithStack(err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ReadDB) GetVariablesTree(tx *db.Tx, parentType types.ConfigType, parentID string) ([]*types.Variable, error) {
|
|
|
|
allVariables := []*types.Variable{}
|
|
|
|
|
|
|
|
for parentType == types.ConfigTypeProjectGroup || parentType == types.ConfigTypeProject {
|
|
|
|
vars, err := r.GetVariables(tx, parentID)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to get variables for %s %q", parentType, parentID)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
allVariables = append(allVariables, vars...)
|
|
|
|
|
|
|
|
switch parentType {
|
|
|
|
case types.ConfigTypeProjectGroup:
|
|
|
|
projectGroup, err := r.GetProjectGroup(tx, parentID)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
if projectGroup == nil {
|
|
|
|
return nil, errors.Errorf("projectgroup with id %q doesn't exist", parentID)
|
|
|
|
}
|
|
|
|
parentType = projectGroup.Parent.Type
|
|
|
|
parentID = projectGroup.Parent.ID
|
|
|
|
case types.ConfigTypeProject:
|
|
|
|
project, err := r.GetProject(tx, parentID)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
if project == nil {
|
|
|
|
return nil, errors.Errorf("project with id %q doesn't exist", parentID)
|
|
|
|
}
|
|
|
|
parentType = project.Parent.Type
|
|
|
|
parentID = project.Parent.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return allVariables, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchVariables(tx *db.Tx, q string, args ...interface{}) ([]*types.Variable, []string, error) {
|
|
|
|
rows, err := tx.Query(q, args...)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, nil, errors.WithStack(err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
return scanVariables(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func scanVariable(rows *sql.Rows, additionalFields ...interface{}) (*types.Variable, string, error) {
|
|
|
|
var id string
|
|
|
|
var data []byte
|
|
|
|
if err := rows.Scan(&id, &data); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, "", errors.Wrapf(err, "failed to scan rows")
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
variable := types.Variable{}
|
|
|
|
if len(data) > 0 {
|
|
|
|
if err := json.Unmarshal(data, &variable); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, "", errors.Wrapf(err, "failed to unmarshal variable")
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &variable, id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func scanVariables(rows *sql.Rows) ([]*types.Variable, []string, error) {
|
|
|
|
variables := []*types.Variable{}
|
|
|
|
ids := []string{}
|
|
|
|
for rows.Next() {
|
|
|
|
p, id, err := scanVariable(rows)
|
|
|
|
if err != nil {
|
|
|
|
rows.Close()
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, nil, errors.WithStack(err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
variables = append(variables, p)
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, nil, errors.WithStack(err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
return variables, ids, nil
|
|
|
|
}
|