pkgdash/storage/sqlite/storage.go

288 lines
5.8 KiB
Go
Raw Normal View History

2023-08-10 22:16:14 +03:00
package sqlite
import (
"context"
"database/sql"
"embed"
"errors"
"fmt"
"strings"
_ "github.com/mattn/go-sqlite3"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/lib/pq"
"go.unistack.org/micro/v3/logger"
"go.unistack.org/unistack-org/pkgdash/config"
"go.unistack.org/unistack-org/pkgdash/models"
pb "go.unistack.org/unistack-org/pkgdash/proto/go_generate"
)
const (
pathMigration = `migrations/sqlite`
)
2023-08-10 22:35:48 +03:00
type Sqlite struct {
2023-08-10 22:16:14 +03:00
db *sql.DB
fs embed.FS
}
func NewStorage(db *sql.DB) (interface{}, error) {
2023-08-10 22:35:48 +03:00
return &Sqlite{db: db}, nil
2023-08-10 22:16:14 +03:00
}
func NewStorageFS(fs embed.FS) func(*sql.DB) (interface{}, error) {
return func(db *sql.DB) (interface{}, error) {
2023-08-10 22:35:48 +03:00
return &Sqlite{db: db, fs: fs}, nil
2023-08-10 22:16:14 +03:00
}
}
2023-08-10 22:35:48 +03:00
func (s *Sqlite) MigrateUp() error {
2023-08-10 22:16:14 +03:00
driver, err := sqlite.WithInstance(s.db, &sqlite.Config{
MigrationsTable: sqlite.DefaultMigrationsTable,
DatabaseName: config.ServiceName,
})
if err != nil {
return err
}
source, err := iofs.New(s.fs, pathMigration)
if err != nil {
return err
}
// TODO: pass own logger
m, err := migrate.NewWithInstance("fs", source, config.ServiceName, driver)
if err != nil {
return err
}
if err = m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
return err
}
return nil
}
2023-08-10 22:35:48 +03:00
func (s *Sqlite) MigrateDown() error {
2023-08-10 22:16:14 +03:00
driver, err := sqlite.WithInstance(s.db, &sqlite.Config{
MigrationsTable: sqlite.DefaultMigrationsTable,
DatabaseName: config.ServiceName,
})
if err != nil {
return err
}
source, err := iofs.New(s.fs, pathMigration)
if err != nil {
return err
}
// TODO: pass own logger
m, err := migrate.NewWithInstance("fs", source, config.ServiceName, driver)
if err != nil {
return err
}
if err = m.Down(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
return err
}
return nil
}
2023-08-11 08:58:31 +03:00
func (s *Sqlite) UpdatePackage(ctx context.Context, req *pb.UpdatePackageReq) error {
2023-08-10 22:16:14 +03:00
panic("need implement")
}
2023-08-10 22:35:48 +03:00
func (s *Sqlite) ListPackage(ctx context.Context) (models.ListPackage, error) {
2023-08-10 22:16:14 +03:00
rows, err := s.db.QueryContext(ctx, queryListPackage)
if err != nil {
return nil, err
}
defer func() {
if err = rows.Close(); err != nil {
return
}
err = rows.Err()
}()
result := make([]*models.Package, 0)
for rows.Next() {
tmp := &models.Package{}
if err = rows.Scan(
&tmp.ID,
&tmp.Name,
&tmp.URL,
pq.Array(&tmp.Comments),
); err != nil {
return nil, err
}
}
return result, err
}
2023-08-11 08:58:31 +03:00
func (s *Sqlite) AddComment(ctx context.Context, req *pb.AddCommentReq) error {
2023-08-10 22:16:14 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return 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()
}
}()
2023-08-11 08:58:31 +03:00
res, err := tx.ExecContext(ctx, queryAddComment, req.Text, req.IdPackage.Value)
2023-08-10 22:16:14 +03:00
if err != nil {
return err
}
if aff, affErr := res.RowsAffected(); err != nil {
err = affErr
} else if aff == 0 {
err = errors.New("rows affected is 0")
}
return err
}
2023-08-11 08:58:31 +03:00
func (s *Sqlite) AddPackage(ctx context.Context, req *pb.AddPackageReq) error {
2023-08-10 22:16:14 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer func() {
if err != nil {
if rollbackErr := tx.Rollback(); rollbackErr != nil {
logger.Errorf(ctx, "AddPackage: unable to rollback: %v", rollbackErr)
}
} else {
err = tx.Commit()
}
}()
2023-08-11 08:58:31 +03:00
res, err := tx.ExecContext(ctx, queryAddPackage, req.Name.Value, req.Url.Value, pq.Array(req.Modules))
2023-08-10 22:16:14 +03:00
if err != nil {
return err
}
if aff, affErr := res.RowsAffected(); err != nil {
err = affErr
} else if aff == 0 {
err = errors.New("rows affected is 0")
}
return err
}
2023-08-11 08:58:31 +03:00
func (s *Sqlite) InsertButchModules(ctx context.Context, req []models.Module) ([]uint64, error) {
2023-08-10 22:16:14 +03:00
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, "AddPackage: unable to rollback: %v", rollbackErr)
}
} else {
err = tx.Commit()
}
}()
2023-08-11 08:58:31 +03:00
query := generateQuery(req)
2023-08-10 22:16:14 +03:00
rows, err := tx.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer func() {
if err = rows.Close(); err != nil {
return
}
err = rows.Err()
}()
result := make([]uint64, 0)
for rows.Next() {
tmp := uint64(0)
if err = rows.Scan(&tmp); err != nil {
return nil, err
}
result = append(result, tmp)
}
return result, err
}
func (s *Sqlite) GetModule(ctx context.Context, req *pb.GetModuleReq) (result []models.Module, err error) {
query := ""
if len(req.ModulesId) < 1 {
query = fmt.Sprintf(queryGetModule, "() or 1=1")
} else {
query = fmt.Sprintf(queryGetModule, generateArrayIneq(len(req.GetModulesId())))
}
rows, err := s.db.QueryContext(ctx, query, convertSliceUInt(req.ModulesId...)...)
if err != nil {
return nil, err
}
defer func() {
if err = rows.Close(); err != nil {
return
}
err = rows.Err()
}()
for rows.Next() {
tmp := models.Module{}
if err = rows.Scan(
&tmp.ID,
&tmp.Name,
&tmp.Version,
&tmp.LastVersion,
); err != nil {
return nil, err
}
result = append(result, tmp)
}
return result, err
}
func convertSliceUInt(arg ...uint64) []interface{} {
result := make([]interface{}, 0, len(arg))
for i := range arg {
result = append(result, arg[i])
}
return result
}
2023-08-10 22:16:14 +03:00
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())
}
func generateArrayIneq(count int) string {
return "(?" + strings.Repeat(",?", count-1) + ")"
}