2023-08-11 20:12:15 +03:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-08-18 23:59:15 +03:00
|
|
|
"time"
|
2023-08-11 20:12:15 +03:00
|
|
|
|
2023-08-16 13:17:42 +03:00
|
|
|
"git.unistack.org/unistack-org/pkgdash/internal/models"
|
2023-08-18 23:59:15 +03:00
|
|
|
"git.unistack.org/unistack-org/pkgdash/internal/storage"
|
2023-08-16 13:17:42 +03:00
|
|
|
pb "git.unistack.org/unistack-org/pkgdash/proto"
|
2023-08-18 23:59:15 +03:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
2023-08-11 20:12:15 +03:00
|
|
|
)
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
func init() {
|
|
|
|
storage.RegisterStorage("sqlite", NewStorage())
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ storage.Storage = (*Sqlite)(nil)
|
2023-08-11 20:12:15 +03:00
|
|
|
|
|
|
|
type Sqlite struct {
|
2023-08-18 23:59:15 +03:00
|
|
|
db *sqlx.DB
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
func NewStorage() func(*sqlx.DB) interface{} {
|
|
|
|
return func(db *sqlx.DB) interface{} {
|
|
|
|
return &Sqlite{db: db}
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-19 16:55:52 +03:00
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = tx.ExecContext(ctx, queryPackagesModulesCreate, pkg.ID, mod.ID)
|
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
func (s *Sqlite) PackagesDelete(ctx context.Context, req *pb.PackagesDeleteReq) error {
|
|
|
|
return fmt.Errorf("need implement")
|
|
|
|
}
|
2023-08-11 20:12:15 +03:00
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
func (s *Sqlite) PackagesUpdate(ctx context.Context, req *pb.PackagesUpdateReq) (*models.Package, error) {
|
|
|
|
return nil, fmt.Errorf("need implement")
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
func (s *Sqlite) PackagesLookup(ctx context.Context, req *pb.PackagesLookupReq) (*models.Package, error) {
|
|
|
|
pkg := &models.Package{}
|
2023-08-11 20:12:15 +03:00
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
err := s.db.GetContext(ctx, pkg, queryPackagesLookup, req.Id)
|
2023-08-11 20:12:15 +03:00
|
|
|
if err != nil {
|
2023-08-18 23:59:15 +03:00
|
|
|
return nil, err
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
return pkg, err
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-16 13:17:42 +03:00
|
|
|
func (s *Sqlite) PackagesList(ctx context.Context, req *pb.PackagesListReq) ([]*models.Package, error) {
|
|
|
|
var packages []*models.Package
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
err := s.db.SelectContext(ctx, &packages, queryPackagesList)
|
2023-08-11 20:12:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
return packages, nil
|
|
|
|
}
|
2023-08-16 13:17:42 +03:00
|
|
|
|
2023-08-19 16:55:52 +03:00
|
|
|
func (s *Sqlite) PackagesModules(ctx context.Context, req *pb.PackagesModulesReq) ([]*models.Module, error) {
|
|
|
|
var modules []*models.Module
|
|
|
|
|
|
|
|
err := s.db.SelectContext(ctx, &modules, queryPackagesModules, req.Package)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return modules, nil
|
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
func (s *Sqlite) CommentsDelete(ctx context.Context, req *pb.CommentsDeleteReq) error {
|
|
|
|
return nil
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
func (s *Sqlite) CommentsCreate(ctx context.Context, req *pb.CommentsCreateReq) (*models.Comment, error) {
|
2023-08-11 20:12:15 +03:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-08-18 23:59:15 +03:00
|
|
|
return nil, err
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
|
|
logger.Errorf(ctx, "AddComment: unable to rollback: %v", rollbackErr)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = tx.Commit()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
if _, err = tx.ExecContext(ctx, queryCommentsCreate, req.Comment, req.PackageId); err != nil {
|
|
|
|
return nil, err
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
2023-08-13 00:09:57 +03:00
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
return nil, nil
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
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())
|
2023-08-11 20:12:15 +03:00
|
|
|
if err != nil {
|
2023-08-18 23:59:15 +03:00
|
|
|
return nil, err
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
return packages, nil
|
|
|
|
}
|
2023-08-11 20:12:15 +03:00
|
|
|
|
2023-08-19 16:55:52 +03:00
|
|
|
func (s *Sqlite) PackagesUpdateLastCheck(ctx context.Context, packages []*models.Package) error {
|
|
|
|
tx, err := s.db.BeginTxx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, pkg := range packages {
|
|
|
|
if _, err = tx.ExecContext(ctx, queryPackagesUpdateLastCheck, pkg.ID); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sqlite) ModulesProcess(ctx context.Context, td time.Duration) ([]*models.Module, error) {
|
|
|
|
var modules []*models.Module
|
|
|
|
err := s.db.SelectContext(ctx, &modules, queryModulesProcess, td.Seconds())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return modules, nil
|
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
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)
|
2023-08-11 20:12:15 +03:00
|
|
|
if err != nil {
|
2023-08-18 23:59:15 +03:00
|
|
|
return nil, err
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
return pkg, nil
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-19 16:55:52 +03:00
|
|
|
func (s *Sqlite) ModulesCreate(ctx context.Context, modules []*models.Module) error {
|
2023-08-18 23:59:15 +03:00
|
|
|
tx, err := s.db.BeginTxx(ctx, nil)
|
2023-08-11 20:12:15 +03:00
|
|
|
if err != nil {
|
2023-08-18 23:59:15 +03:00
|
|
|
return err
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
2023-08-18 23:59:15 +03:00
|
|
|
|
|
|
|
for _, mod := range modules {
|
2023-08-19 16:55:52 +03:00
|
|
|
err = tx.GetContext(ctx, mod, queryModulesCreate, mod.Name, mod.Version)
|
2023-08-11 20:12:15 +03:00
|
|
|
if err != nil {
|
2023-08-18 23:59:15 +03:00
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
return nil
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
func (s *Sqlite) ModulesList(ctx context.Context, req *pb.ModulesListReq) ([]*models.Module, error) {
|
2023-08-16 13:17:42 +03:00
|
|
|
var modules []*models.Module
|
2023-08-11 20:12:15 +03:00
|
|
|
|
2023-08-19 16:55:52 +03:00
|
|
|
err := s.db.SelectContext(ctx, &modules, queryModulesList)
|
2023-08-11 20:12:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-16 13:17:42 +03:00
|
|
|
|
|
|
|
return modules, nil
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-16 13:17:42 +03:00
|
|
|
func (s *Sqlite) CommentsList(ctx context.Context, req *pb.CommentsListReq) ([]*models.Comment, error) {
|
|
|
|
var comments []*models.Comment
|
2023-08-14 14:27:29 +03:00
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
err := s.db.SelectContext(ctx, &comments, queryCommentsList, req.PackageId)
|
2023-08-14 14:27:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-16 13:17:42 +03:00
|
|
|
|
|
|
|
return comments, nil
|
2023-08-14 14:27:29 +03:00
|
|
|
}
|