2025-01-18 16:30:02 +00:00
|
|
|
// ALR - Any Linux Repository
|
|
|
|
// Copyright (C) 2025 Евгений Храмов
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2025-01-14 07:11:17 +00:00
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2025-01-22 13:37:16 +00:00
|
|
|
"log/slog"
|
|
|
|
"os"
|
2025-01-14 07:11:17 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
2025-01-22 13:37:16 +00:00
|
|
|
"github.com/leonelquinteros/gotext"
|
2025-01-18 16:30:02 +00:00
|
|
|
|
2025-01-20 16:58:24 +00:00
|
|
|
"gitea.plemya-x.ru/Plemya-x/ALR/internal/config"
|
2025-01-14 07:11:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DB returns the ALR database.
|
|
|
|
// The first time it's called, it opens the SQLite database file.
|
|
|
|
// Subsequent calls return the same connection.
|
|
|
|
//
|
|
|
|
// Deprecated: use struct method
|
|
|
|
func DB(ctx context.Context) *sqlx.DB {
|
2025-01-14 08:49:42 +00:00
|
|
|
return GetInstance(ctx).GetConn()
|
2025-01-14 07:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the database
|
|
|
|
//
|
|
|
|
// Deprecated: use struct method
|
|
|
|
func Close() error {
|
|
|
|
if database != nil {
|
|
|
|
return database.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty returns true if the database has no packages in it, otherwise it returns false.
|
|
|
|
//
|
|
|
|
// Deprecated: use struct method
|
|
|
|
func IsEmpty(ctx context.Context) bool {
|
2025-01-14 08:49:42 +00:00
|
|
|
return GetInstance(ctx).IsEmpty(ctx)
|
2025-01-14 07:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InsertPackage adds a package to the database
|
|
|
|
//
|
|
|
|
// Deprecated: use struct method
|
|
|
|
func InsertPackage(ctx context.Context, pkg Package) error {
|
2025-01-14 08:49:42 +00:00
|
|
|
return GetInstance(ctx).InsertPackage(ctx, pkg)
|
2025-01-14 07:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPkgs returns a result containing packages that match the where conditions
|
|
|
|
//
|
|
|
|
// Deprecated: use struct method
|
|
|
|
func GetPkgs(ctx context.Context, where string, args ...any) (*sqlx.Rows, error) {
|
2025-01-14 08:49:42 +00:00
|
|
|
return GetInstance(ctx).GetPkgs(ctx, where, args...)
|
2025-01-14 07:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPkg returns a single package that matches the where conditions
|
|
|
|
//
|
|
|
|
// Deprecated: use struct method
|
|
|
|
func GetPkg(ctx context.Context, where string, args ...any) (*Package, error) {
|
2025-01-14 08:49:42 +00:00
|
|
|
return GetInstance(ctx).GetPkg(ctx, where, args...)
|
2025-01-14 07:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePkgs deletes all packages matching the where conditions
|
|
|
|
//
|
|
|
|
// Deprecated: use struct method
|
|
|
|
func DeletePkgs(ctx context.Context, where string, args ...any) error {
|
2025-01-14 08:49:42 +00:00
|
|
|
return GetInstance(ctx).DeletePkgs(ctx, where, args...)
|
2025-01-14 07:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// =======================
|
|
|
|
// FOR LEGACY ONLY
|
|
|
|
// =======================
|
|
|
|
|
|
|
|
var (
|
|
|
|
dbOnce sync.Once
|
|
|
|
database *Database
|
|
|
|
)
|
|
|
|
|
2025-01-14 08:49:42 +00:00
|
|
|
// Deprecated: For legacy only
|
|
|
|
func GetInstance(ctx context.Context) *Database {
|
2025-01-14 07:11:17 +00:00
|
|
|
dbOnce.Do(func() {
|
|
|
|
cfg := config.GetInstance(ctx)
|
|
|
|
database = New(cfg)
|
|
|
|
err := database.Init(ctx)
|
|
|
|
if err != nil {
|
2025-01-22 13:37:16 +00:00
|
|
|
slog.Error(gotext.Get("Error opening database"), "err", err)
|
|
|
|
os.Exit(1)
|
2025-01-14 07:11:17 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return database
|
|
|
|
}
|