2023-08-11 20:12:15 +03:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-23 21:16:26 +03:00
|
|
|
"database/sql"
|
2023-08-11 20:12:15 +03:00
|
|
|
"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-20 14:19:57 +03:00
|
|
|
func (s *Sqlite) PackageModulesCreate(ctx context.Context, pkg *models.Package, modules []*models.Module) error {
|
2023-08-19 16:55:52 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-23 21:16:26 +03:00
|
|
|
_, err = tx.ExecContext(ctx, queryPackageModulesCount, pkg.ID, len(modules))
|
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-19 16:55:52 +03:00
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-20 14:19:57 +03:00
|
|
|
func (s *Sqlite) PackageDelete(ctx context.Context, req *pb.PackageDeleteReq) error {
|
2024-04-03 17:29:57 +03:00
|
|
|
return fmt.Errorf("need implement")
|
2023-08-18 23:59:15 +03:00
|
|
|
}
|
2023-08-11 20:12:15 +03:00
|
|
|
|
2023-08-20 14:19:57 +03:00
|
|
|
func (s *Sqlite) PackageUpdate(ctx context.Context, req *pb.PackageUpdateReq) (*models.Package, error) {
|
2024-04-03 17:29:57 +03:00
|
|
|
return nil, fmt.Errorf("need implement")
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-20 14:19:57 +03:00
|
|
|
func (s *Sqlite) PackageLookup(ctx context.Context, req *pb.PackageLookupReq) (*models.Package, error) {
|
2023-08-18 23:59:15 +03:00
|
|
|
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-09-23 21:16:26 +03:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, 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-20 14:19:57 +03:00
|
|
|
func (s *Sqlite) PackageList(ctx context.Context, req *pb.PackageListReq) ([]*models.Package, error) {
|
2023-08-16 13:17:42 +03:00
|
|
|
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 {
|
2023-09-23 21:16:26 +03:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-08-11 20:12:15 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
return packages, nil
|
|
|
|
}
|
2023-08-16 13:17:42 +03:00
|
|
|
|
2023-08-20 14:19:57 +03:00
|
|
|
func (s *Sqlite) PackageModules(ctx context.Context, req *pb.PackageModulesReq) ([]*models.Module, error) {
|
2023-08-19 16:55:52 +03:00
|
|
|
var modules []*models.Module
|
|
|
|
|
|
|
|
err := s.db.SelectContext(ctx, &modules, queryPackagesModules, req.Package)
|
|
|
|
if err != nil {
|
2023-09-23 21:16:26 +03:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-08-19 16:55:52 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return modules, nil
|
|
|
|
}
|
|
|
|
|
2023-08-20 14:19:57 +03:00
|
|
|
func (s *Sqlite) CommentDelete(ctx context.Context, req *pb.CommentDeleteReq) error {
|
2023-08-18 23:59:15 +03:00
|
|
|
return nil
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-20 14:19:57 +03:00
|
|
|
func (s *Sqlite) CommentCreate(ctx context.Context, req *pb.CommentCreateReq) (*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 {
|
2024-03-24 20:52:32 +03:00
|
|
|
logger.Error(ctx, "AddComment: unable to rollback: %v", rollbackErr)
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
} 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-20 14:19:57 +03:00
|
|
|
func (s *Sqlite) PackageCreate(ctx context.Context, req *pb.PackageCreateReq) (*models.Package, error) {
|
2023-08-18 23:59:15 +03:00
|
|
|
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-20 14:19:57 +03:00
|
|
|
func (s *Sqlite) ModuleCreate(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-20 14:19:57 +03:00
|
|
|
func (s *Sqlite) ModuleList(ctx context.Context, req *pb.ModuleListReq) ([]*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 {
|
2023-09-23 21:16:26 +03:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-08-11 20:12:15 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-16 13:17:42 +03:00
|
|
|
|
|
|
|
return modules, nil
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-20 14:19:57 +03:00
|
|
|
func (s *Sqlite) CommentList(ctx context.Context, req *pb.CommentListReq) ([]*models.Comment, error) {
|
2023-08-16 13:17:42 +03:00
|
|
|
var comments []*models.Comment
|
2023-08-14 14:27:29 +03:00
|
|
|
|
2023-09-23 21:16:26 +03:00
|
|
|
err := s.db.SelectContext(ctx, &comments, queryCommentsList, req.Package)
|
2023-08-14 14:27:29 +03:00
|
|
|
if err != nil {
|
2023-09-23 21:16:26 +03:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-08-14 14:27:29 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-16 13:17:42 +03:00
|
|
|
|
|
|
|
return comments, nil
|
2023-08-14 14:27:29 +03:00
|
|
|
}
|
2023-09-23 21:16:26 +03:00
|
|
|
|
|
|
|
func (s *Sqlite) HandlerList(ctx context.Context, req *pb.HandlerListReq) ([]*models.Handler, error) {
|
|
|
|
var handlers []*models.Handler
|
|
|
|
|
|
|
|
err := s.db.SelectContext(ctx, &handlers, queryHandlersList, req.Package)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return handlers, nil
|
|
|
|
}
|