2021-09-24 09:06:26 +00:00
|
|
|
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Drone Non-Commercial License
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
// +build !oss
|
|
|
|
|
|
|
|
package card
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/drone/drone/core"
|
|
|
|
"github.com/drone/drone/store/shared/db"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a new card database store.
|
|
|
|
func New(db *db.DB) core.CardStore {
|
|
|
|
return &cardStore{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type cardStore struct {
|
|
|
|
db *db.DB
|
|
|
|
}
|
|
|
|
|
2021-10-26 07:10:01 +00:00
|
|
|
type card struct {
|
|
|
|
Id int64 `json:"id,omitempty"`
|
|
|
|
Data []byte `json:"card_data"`
|
2021-09-24 09:06:26 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 07:10:01 +00:00
|
|
|
func (c cardStore) Find(ctx context.Context, step int64) (io.ReadCloser, error) {
|
|
|
|
out := &card{Id: step}
|
2021-09-24 09:06:26 +00:00
|
|
|
err := c.db.View(func(queryer db.Queryer, binder db.Binder) error {
|
|
|
|
params, err := toParams(out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-26 07:10:01 +00:00
|
|
|
query, args, err := binder.BindNamed(queryKey, params)
|
2021-09-24 09:06:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
row := queryer.QueryRow(query, args...)
|
|
|
|
return scanRow(row, out)
|
|
|
|
})
|
|
|
|
|
|
|
|
return ioutil.NopCloser(
|
|
|
|
bytes.NewBuffer(out.Data),
|
|
|
|
), err
|
|
|
|
}
|
|
|
|
|
2021-10-26 07:10:01 +00:00
|
|
|
func (c cardStore) Create(ctx context.Context, step int64, r io.Reader) error {
|
|
|
|
data, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-09-24 09:06:26 +00:00
|
|
|
}
|
|
|
|
return c.db.Lock(func(execer db.Execer, binder db.Binder) error {
|
2021-10-26 07:10:01 +00:00
|
|
|
in := &card{
|
|
|
|
Id: step,
|
|
|
|
Data: data,
|
2021-09-24 09:06:26 +00:00
|
|
|
}
|
2021-10-26 07:10:01 +00:00
|
|
|
params, err := toParams(in)
|
2021-09-24 09:06:26 +00:00
|
|
|
stmt, args, err := binder.BindNamed(stmtInsert, params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-26 07:10:01 +00:00
|
|
|
_, err = execer.Exec(stmt, args...)
|
2021-09-24 09:06:26 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-26 07:10:01 +00:00
|
|
|
func (c *cardStore) Update(ctx context.Context, step int64, r io.Reader) error {
|
|
|
|
data, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-24 09:06:26 +00:00
|
|
|
return c.db.Lock(func(execer db.Execer, binder db.Binder) error {
|
2021-10-26 07:10:01 +00:00
|
|
|
card := &card{
|
|
|
|
Id: step,
|
|
|
|
Data: data,
|
2021-09-29 14:03:02 +00:00
|
|
|
}
|
2021-10-26 07:10:01 +00:00
|
|
|
params, err := toParams(card)
|
2021-09-24 09:06:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-26 07:10:01 +00:00
|
|
|
stmt, args, err := binder.BindNamed(stmtUpdate, params)
|
2021-09-24 09:06:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-26 07:10:01 +00:00
|
|
|
_, err = execer.Exec(stmt, args...)
|
|
|
|
return err
|
2021-09-24 09:06:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-26 07:10:01 +00:00
|
|
|
func (c cardStore) Delete(ctx context.Context, step int64) error {
|
2021-09-24 09:06:26 +00:00
|
|
|
return c.db.Lock(func(execer db.Execer, binder db.Binder) error {
|
2021-10-26 07:10:01 +00:00
|
|
|
params := &card{
|
|
|
|
Id: step,
|
2021-09-24 09:06:26 +00:00
|
|
|
}
|
|
|
|
stmt, args, err := binder.BindNamed(stmtDelete, params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = execer.Exec(stmt, args...)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const queryBase = `
|
|
|
|
SELECT
|
|
|
|
card_id
|
|
|
|
,card_data
|
|
|
|
`
|
|
|
|
|
2021-10-26 07:10:01 +00:00
|
|
|
const queryKey = queryBase + `
|
2021-09-24 09:06:26 +00:00
|
|
|
FROM cards
|
|
|
|
WHERE card_id = :card_id
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
const stmtInsert = `
|
|
|
|
INSERT INTO cards (
|
2021-10-26 07:10:01 +00:00
|
|
|
card_id
|
2021-09-24 09:06:26 +00:00
|
|
|
,card_data
|
|
|
|
) VALUES (
|
2021-10-26 07:10:01 +00:00
|
|
|
:card_id
|
2021-09-24 09:06:26 +00:00
|
|
|
,:card_data
|
|
|
|
)
|
|
|
|
`
|
|
|
|
|
2021-10-26 07:10:01 +00:00
|
|
|
const stmtUpdate = `
|
|
|
|
UPDATE cards
|
|
|
|
SET card_data = :card_data
|
|
|
|
WHERE card_id = :card_id
|
|
|
|
`
|
|
|
|
|
2021-09-24 09:06:26 +00:00
|
|
|
const stmtDelete = `
|
|
|
|
DELETE FROM cards
|
|
|
|
WHERE card_id = :card_id
|
|
|
|
`
|
|
|
|
|
|
|
|
const stmtInsertPostgres = stmtInsert + `
|
|
|
|
RETURNING card_id
|
|
|
|
`
|