woodpecker/store/datastore/files.go

86 lines
2.5 KiB
Go
Raw Normal View History

2018-02-19 22:24:10 +00:00
// Copyright 2018 Drone.IO Inc.
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +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
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// 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.
2017-03-28 08:53:06 +00:00
package datastore
import (
"bytes"
"io"
"io/ioutil"
2019-04-04 18:51:20 +00:00
"github.com/laszlocph/drone-oss-08/model"
"github.com/laszlocph/drone-oss-08/store/datastore/sql"
2017-03-28 08:53:06 +00:00
"github.com/russross/meddler"
)
func (db *datastore) FileList(build *model.Build) ([]*model.File, error) {
2017-03-28 09:13:13 +00:00
stmt := sql.Lookup(db.driver, "files-find-build")
2017-03-28 08:53:06 +00:00
list := []*model.File{}
err := meddler.QueryAll(db, &list, stmt, build.ID)
return list, err
}
func (db *datastore) FileFind(proc *model.Proc, name string) (*model.File, error) {
2017-03-28 09:13:13 +00:00
stmt := sql.Lookup(db.driver, "files-find-proc-name")
2017-03-28 08:53:06 +00:00
file := new(model.File)
err := meddler.QueryRow(db, file, stmt, proc.ID, name)
return file, err
}
func (db *datastore) FileRead(proc *model.Proc, name string) (io.ReadCloser, error) {
2017-03-28 09:13:13 +00:00
stmt := sql.Lookup(db.driver, "files-find-proc-name-data")
2017-03-28 08:53:06 +00:00
file := new(fileData)
err := meddler.QueryRow(db, file, stmt, proc.ID, name)
buf := bytes.NewBuffer(file.Data)
return ioutil.NopCloser(buf), err
}
func (db *datastore) FileCreate(file *model.File, r io.Reader) error {
d, err := ioutil.ReadAll(r)
if err != nil {
return err
}
f := fileData{
ID: file.ID,
BuildID: file.BuildID,
ProcID: file.ProcID,
2017-07-27 17:06:24 +00:00
PID: file.PID,
2017-03-28 08:53:06 +00:00
Name: file.Name,
Size: file.Size,
Mime: file.Mime,
Time: file.Time,
2017-08-02 20:04:00 +00:00
Passed: file.Passed,
Failed: file.Failed,
Skipped: file.Skipped,
2017-03-28 08:53:06 +00:00
Data: d,
}
return meddler.Insert(db, "files", &f)
}
type fileData struct {
ID int64 `meddler:"file_id,pk"`
BuildID int64 `meddler:"file_build_id"`
ProcID int64 `meddler:"file_proc_id"`
2017-07-27 17:06:24 +00:00
PID int `meddler:"file_pid"`
2017-03-28 08:53:06 +00:00
Name string `meddler:"file_name"`
Size int `meddler:"file_size"`
Mime string `meddler:"file_mime"`
Time int64 `meddler:"file_time"`
2017-08-02 20:04:00 +00:00
Passed int `meddler:"file_meta_passed"`
Failed int `meddler:"file_meta_failed"`
Skipped int `meddler:"file_meta_skipped"`
2017-03-28 08:53:06 +00:00
Data []byte `meddler:"file_data"`
}