199 lines
4.4 KiB
Go
199 lines
4.4 KiB
Go
package sqlite
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.unistack.org/unistack-org/pkgdash/internal/models"
|
|
"git.unistack.org/unistack-org/pkgdash/internal/storage"
|
|
pb "git.unistack.org/unistack-org/pkgdash/proto"
|
|
"github.com/jmoiron/sqlx"
|
|
"go.unistack.org/micro/v4/logger"
|
|
)
|
|
|
|
func init() {
|
|
storage.RegisterStorage("sqlite", NewStorage())
|
|
}
|
|
|
|
var _ storage.Storage = (*Sqlite)(nil)
|
|
|
|
type Sqlite struct {
|
|
db *sqlx.DB
|
|
}
|
|
|
|
func NewStorage() func(*sqlx.DB) interface{} {
|
|
return func(db *sqlx.DB) interface{} {
|
|
return &Sqlite{db: db}
|
|
}
|
|
}
|
|
|
|
func (s *Sqlite) PackagesDelete(ctx context.Context, req *pb.PackagesDeleteReq) error {
|
|
return fmt.Errorf("need implement")
|
|
}
|
|
|
|
func (s *Sqlite) PackagesUpdate(ctx context.Context, req *pb.PackagesUpdateReq) (*models.Package, error) {
|
|
return nil, fmt.Errorf("need implement")
|
|
}
|
|
|
|
func (s *Sqlite) PackagesLookup(ctx context.Context, req *pb.PackagesLookupReq) (*models.Package, error) {
|
|
pkg := &models.Package{}
|
|
|
|
err := s.db.GetContext(ctx, pkg, queryPackagesLookup, req.Id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return pkg, err
|
|
}
|
|
|
|
func (s *Sqlite) PackagesList(ctx context.Context, req *pb.PackagesListReq) ([]*models.Package, error) {
|
|
var packages []*models.Package
|
|
|
|
err := s.db.SelectContext(ctx, &packages, queryPackagesList)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return packages, nil
|
|
}
|
|
|
|
func (s *Sqlite) CommentsDelete(ctx context.Context, req *pb.CommentsDeleteReq) error {
|
|
return nil
|
|
}
|
|
|
|
func (s *Sqlite) CommentsCreate(ctx context.Context, req *pb.CommentsCreateReq) (*models.Comment, error) {
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer func() {
|
|
if err != nil {
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
logger.Errorf(ctx, "AddComment: unable to rollback: %v", rollbackErr)
|
|
}
|
|
} else {
|
|
err = tx.Commit()
|
|
}
|
|
}()
|
|
|
|
if _, err = tx.ExecContext(ctx, queryCommentsCreate, req.Comment, req.PackageId); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
func (s *Sqlite) PackagesProcess(ctx context.Context, td time.Duration) ([]*models.Package, error) {
|
|
var packages []*models.Package
|
|
err := s.db.SelectContext(ctx, &packages, queryPackagesProcess, td.Seconds())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return packages, nil
|
|
}
|
|
|
|
func (s *Sqlite) PackagesCreate(ctx context.Context, req *pb.PackagesCreateReq) (*models.Package, error) {
|
|
pkg := &models.Package{}
|
|
err := s.db.GetContext(ctx, pkg, queryPackagesCreate, req.Name, req.Url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return pkg, nil
|
|
}
|
|
|
|
func (s *Sqlite) PackagesModulesCreate(ctx context.Context, pkg *models.Package, modules []*models.Module) error {
|
|
tx, err := s.db.BeginTxx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, mod := range modules {
|
|
err = tx.GetContext(ctx, mod, queryModulesCreate, mod.Name, mod.Version, mod.LastVersion, mod.Package)
|
|
if err != nil {
|
|
_ = tx.Rollback()
|
|
return err
|
|
}
|
|
}
|
|
|
|
if _, err = tx.ExecContext(ctx, queryPackagesModulesCount, pkg.ID, len(modules)); err != nil {
|
|
_ = tx.Rollback()
|
|
return err
|
|
}
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
_ = tx.Rollback()
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Sqlite) ModulesList(ctx context.Context, req *pb.ModulesListReq) ([]*models.Module, error) {
|
|
var err error
|
|
var modules []*models.Module
|
|
|
|
rows, err := s.db.QueryContext(ctx, queryModulesList)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() {
|
|
if err = rows.Close(); err != nil {
|
|
return
|
|
}
|
|
err = rows.Err()
|
|
}()
|
|
|
|
for ; rows.Err() == nil; rows.Next() {
|
|
mod := &models.Module{}
|
|
if err = rows.Scan(
|
|
&mod.ID,
|
|
&mod.Name,
|
|
&mod.Version,
|
|
&mod.LastVersion,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
modules = append(modules, mod)
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err = rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return modules, nil
|
|
}
|
|
|
|
func (s *Sqlite) CommentsList(ctx context.Context, req *pb.CommentsListReq) ([]*models.Comment, error) {
|
|
var comments []*models.Comment
|
|
|
|
err := s.db.SelectContext(ctx, &comments, queryCommentsList, req.PackageId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return comments, nil
|
|
}
|
|
|
|
func generateQuery(rsp []models.Module) string {
|
|
const pattern = `%c('%s', '%s', '%s')`
|
|
build := strings.Builder{}
|
|
comma := ' '
|
|
for i := range rsp {
|
|
str := fmt.Sprintf(pattern, comma, rsp[i].Name, rsp[i].Version, rsp[i].LastVersion)
|
|
build.WriteString(str)
|
|
comma = ','
|
|
}
|
|
|
|
return fmt.Sprintf(queryInsMsgGetIDs, build.String())
|
|
}
|