db

package
v0.1.31 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2026 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package db provides the model of the configuration for a database.

Index

Constants

This section is empty.

Variables

View Source
var (
	// database errs.
	ErrDBExists             = errors.New("database exists")
	ErrDBAlreadyInitialized = errors.New("already initialized")
	ErrDBExistsAndInit      = errors.New("database exists and initialized")
	ErrDBEmpty              = errors.New("database is empty")
	ErrDBNotFound           = errors.New("database not found")
	ErrDBsNotFound          = errors.New("no database/s found")
	ErrDBCorrupted          = errors.New("database corrupted")
	ErrDBLocked             = errors.New("database is locked")
	ErrDBUnlockFirst        = errors.New("unlock database first")
	ErrDBMainNameReserved   = errors.New("name reserved")
	ErrDBMainNotFound       = errors.New("main database not found")
)
View Source
var (
	// records errs.
	ErrRecordDuplicate        = errors.New("record already exists")
	ErrRecordIDNotProvided    = errors.New("no id provided")
	ErrCommit                 = errors.New("commit error")
	ErrRecordNoMatch          = errors.New("no match found")
	ErrRecordNotFound         = errors.New("no record found")
	ErrRecordQueryNotProvided = errors.New("no id or query provided")
	ErrRecordScan             = errors.New("scan record")
	ErrInvalidSortBy          = errors.New("invalid sort")
	ErrTagsEmpty              = errors.New("tags cannot be empty")
	ErrURLEmpty               = errors.New("URL cannot be empty")
	ErrChecksumEmpty          = errors.New("checksum cannot be empty")
	ErrChecksumEqual          = errors.New("checksum: equal")
)
View Source
var (
	// backups errs.
	ErrBackupExists   = errors.New("backup already exists")
	ErrBackupNotFound = errors.New("no backup found")
)
View Source
var Schemas = DatabaseSchemas{
	Main:     schemaMain,
	Tags:     schemaTags,
	Relation: schemaRelation,
	Temp:     schemaTemp,
}

Functions

func IsInitializedFromPath added in v0.1.30

func IsInitializedFromPath(ctx context.Context, p string) (bool, error)

IsInitializedFromPath checks if the database is initialized.

func OpenDatabase added in v0.1.21

func OpenDatabase(path string, cfg *Cfg) (*sqlx.DB, error)

OpenDatabase opens a SQLite database at the specified path and verifies the connection, returning the database handle or an error.

func Shutdown added in v0.1.30

func Shutdown()

Shutdown closes all open connections and logs warnings if any were left open.

func TagsList

func TagsList(ctx context.Context, r *SQLite) ([]string, error)

TagsList returns the list of tags.

Types

type Cfg

type Cfg struct {
	Name            string `json:"name"` // Name of the SQLite database
	Path            string `json:"path"` // Path to the SQLite database
	MaxOpenConns    int
	MaxIdleConns    int
	MaxLifetimeConn time.Duration
}

Cfg represents the configuration for a SQLite database.

func NewSQLiteCfg added in v0.1.21

func NewSQLiteCfg(p string) (*Cfg, error)

NewSQLiteCfg returns the default settings for the database.

func (*Cfg) Exists

func (c *Cfg) Exists() bool

Exists returns true if the SQLite database exists.

func (*Cfg) Fullpath

func (c *Cfg) Fullpath() string

Fullpath returns the full path to the SQLite database.

type DatabaseSchemas added in v0.1.19

type DatabaseSchemas struct {
	Main     Schema
	Tags     Schema
	Relation Schema
	Temp     Schema
}

type Manager added in v0.1.30

type Manager struct {
	// contains filtered or unexported fields
}

Manager tracks and manages open SQLite connections.

func NewManager added in v0.1.30

func NewManager() *Manager

NewManager creates a new database manager.

func (*Manager) CloseAll added in v0.1.30

func (m *Manager) CloseAll()

CloseAll closes all tracked connections.

func (*Manager) HasOpenConnections added in v0.1.30

func (m *Manager) HasOpenConnections() bool

HasOpenConnections checks if any are open.

func (*Manager) Register added in v0.1.30

func (m *Manager) Register(s string, conn *SQLite)

Register adds a connection to the manager.

func (*Manager) Unregister added in v0.1.30

func (m *Manager) Unregister(s string)

Unregister removes a connection from the manager.

type RepoStats added in v0.1.31

type RepoStats struct {
	Name      string `json:"dbname"`     // Name is the database base name.
	Bookmarks int    `json:"bookmarks"`  // Bookmarks is the count of bookmarks.
	Tags      int    `json:"tags"`       // Tags is the count of tags.
	Favorites int    `json:"favorites"`  // Favorites is the count of favorite bookmarks.
	Size      string `json:"size_bytes"` // Size in bytes
}

RepoStats holds statistics about a bookmark repository.

func NewStats added in v0.1.31

func NewStats(ctx context.Context, dbPath string) (*RepoStats, error)

func (*RepoStats) String added in v0.1.31

func (rs *RepoStats) String() string

String returns a string representation of the repo summary.

type SQLite

type SQLite struct {
	DB  *sqlx.DB `json:"-"`
	Cfg *Cfg     `json:"db"`
	// contains filtered or unexported fields
}

SQLite implements the Repository interface.

func Init

func Init(p string) (*SQLite, error)

Init initializes a new SQLiteRepository at the provided path.

func New

func New(p string) (*SQLite, error)

New returns a new SQLiteRepository from an existing database path.

func (*SQLite) AddVisit added in v0.1.21

func (r *SQLite) AddVisit(ctx context.Context, bID int) error

func (*SQLite) All

func (r *SQLite) All(ctx context.Context) ([]*bookmark.Bookmark, error)

All returns all bookmarks.

func (*SQLite) Backup

func (r *SQLite) Backup(ctx context.Context, destRoot string) (string, error)

Backup creates a timestamped backup of the SQLite database at the specified destination. The backup filename follows the format: YYYYMMDD-HHMMSS_dbname.db.

func (*SQLite) ByID

func (r *SQLite) ByID(ctx context.Context, bID int) (*bookmark.Bookmark, error)

ByID returns a record by its ID in the give table.

func (*SQLite) ByIDList

func (r *SQLite) ByIDList(ctx context.Context, bIDs []int) ([]*bookmark.Bookmark, error)

ByIDList returns a list of records by their IDs in the give table.

func (*SQLite) ByOrder

func (r *SQLite) ByOrder(ctx context.Context, column, sortBy string) ([]*bookmark.Bookmark, error)

func (*SQLite) ByQuery

func (r *SQLite) ByQuery(ctx context.Context, query string) ([]*bookmark.Bookmark, error)

ByQuery returns records by query in the give table.

func (*SQLite) ByTag

func (r *SQLite) ByTag(ctx context.Context, tag string) ([]*bookmark.Bookmark, error)

ByTag returns records filtered by tag, including all associated tags.

func (*SQLite) ByURL

func (r *SQLite) ByURL(ctx context.Context, bURL string) (*bookmark.Bookmark, error)

ByURL returns a record by its URL in the give table.

func (*SQLite) CheckIntegrity added in v0.1.30

func (r *SQLite) CheckIntegrity(ctx context.Context) error

CheckIntegrity performs a PRAGMA integrity_check on the SQLite database.

func (*SQLite) CleanupOrphanTags added in v0.1.22

func (r *SQLite) CleanupOrphanTags(ctx context.Context) error

CleanupOrphanTags elimina todos los tags que no estén asociados a ningún bookmark.

func (*SQLite) Close

func (r *SQLite) Close()

Close closes the SQLite database connection and logs any errors encountered.

func (*SQLite) Count added in v0.1.21

func (r *SQLite) Count(ctx context.Context, table Table) int

Count returns the number of records in the given table.

func (*SQLite) CountFavorites

func (r *SQLite) CountFavorites(ctx context.Context) int

CountFavorites returns the number of favorite records.

func (*SQLite) DeleteMany

func (r *SQLite) DeleteMany(ctx context.Context, bs []*bookmark.Bookmark) error

DeleteMany deletes multiple records from the main table.

func (*SQLite) DropSecure

func (r *SQLite) DropSecure(ctx context.Context) error

DropSecure removes all records database.

func (*SQLite) FavoritesList

func (r *SQLite) FavoritesList(ctx context.Context) ([]*bookmark.Bookmark, error)

FavoritesList returns the favorite bookmarks.

func (*SQLite) Has

func (r *SQLite) Has(ctx context.Context, bURL string) (*bookmark.Bookmark, bool)

Has checks if a record exists in the main table.

func (*SQLite) Init

func (r *SQLite) Init(ctx context.Context) error

Init initializes a new database and creates the required tables.

func (*SQLite) InsertMany

func (r *SQLite) InsertMany(ctx context.Context, bs []*bookmark.Bookmark) error

func (*SQLite) InsertOne

func (r *SQLite) InsertOne(ctx context.Context, b *bookmark.Bookmark) (int64, error)

InsertOne creates a new record in the main table.

func (*SQLite) IsInitialized

func (r *SQLite) IsInitialized(ctx context.Context) bool

IsInitialized checks if the database is initialized.

func (*SQLite) MaxID

func (r *SQLite) MaxID(ctx context.Context) int

MaxID retrieves the maximum ID from the specified table in the SQLite database.

func (*SQLite) Name

func (r *SQLite) Name() string

Name returns the name of the SQLite database.

func (*SQLite) ReorderIDs

func (r *SQLite) ReorderIDs(ctx context.Context) error

func (*SQLite) SetFavorite

func (r *SQLite) SetFavorite(ctx context.Context, b *bookmark.Bookmark) error

func (*SQLite) Stats added in v0.1.31

func (r *SQLite) Stats(ctx context.Context) *RepoStats

func (*SQLite) TagsCounter

func (r *SQLite) TagsCounter(ctx context.Context) (map[string]int, error)

TagsCounter returns a map with tag as key and count as value.

func (*SQLite) UpdateNotes added in v0.1.27

func (r *SQLite) UpdateNotes(ctx context.Context, bID int, notes string) error

UpdateNotes updates the bookmak's notes.

func (*SQLite) UpdateOne added in v0.1.22

func (r *SQLite) UpdateOne(ctx context.Context, b *bookmark.Bookmark) error

UpdateOne updates an existing bookmark by ID (or URL).

func (*SQLite) Vacuum

func (r *SQLite) Vacuum(ctx context.Context) error

Vacuum rebuilds the database file, repacking it into a minimal amount of disk space.

func (*SQLite) WithTx added in v0.1.19

func (r *SQLite) WithTx(ctx context.Context, fn func(tx *sqlx.Tx) error) error

WithTx executes a function within a transaction.

type Schema added in v0.1.19

type Schema struct {
	Name    Table
	SQL     string
	Trigger []string
	Index   string
}

type Table

type Table string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL