Documentation
¶
Overview ¶
Package db provides the model of the configuration for a database.
Index ¶
- Variables
- func IsInitializedFromPath(ctx context.Context, p string) (bool, error)
- func OpenDatabase(path string, cfg *Cfg) (*sqlx.DB, error)
- func Shutdown()
- func TagsList(ctx context.Context, r *SQLite) ([]string, error)
- type Cfg
- type DatabaseSchemas
- type Manager
- type RepoStats
- type SQLite
- func (r *SQLite) AddVisit(ctx context.Context, bID int) error
- func (r *SQLite) All(ctx context.Context) ([]*bookmark.Bookmark, error)
- func (r *SQLite) Backup(ctx context.Context, destRoot string) (string, error)
- func (r *SQLite) ByID(ctx context.Context, bID int) (*bookmark.Bookmark, error)
- func (r *SQLite) ByIDList(ctx context.Context, bIDs []int) ([]*bookmark.Bookmark, error)
- func (r *SQLite) ByOrder(ctx context.Context, column, sortBy string) ([]*bookmark.Bookmark, error)
- func (r *SQLite) ByQuery(ctx context.Context, query string) ([]*bookmark.Bookmark, error)
- func (r *SQLite) ByTag(ctx context.Context, tag string) ([]*bookmark.Bookmark, error)
- func (r *SQLite) ByURL(ctx context.Context, bURL string) (*bookmark.Bookmark, error)
- func (r *SQLite) CheckIntegrity(ctx context.Context) error
- func (r *SQLite) CleanupOrphanTags(ctx context.Context) error
- func (r *SQLite) Close()
- func (r *SQLite) Count(ctx context.Context, table Table) int
- func (r *SQLite) CountFavorites(ctx context.Context) int
- func (r *SQLite) DeleteMany(ctx context.Context, bs []*bookmark.Bookmark) error
- func (r *SQLite) DropSecure(ctx context.Context) error
- func (r *SQLite) FavoritesList(ctx context.Context) ([]*bookmark.Bookmark, error)
- func (r *SQLite) Has(ctx context.Context, bURL string) (*bookmark.Bookmark, bool)
- func (r *SQLite) Init(ctx context.Context) error
- func (r *SQLite) InsertMany(ctx context.Context, bs []*bookmark.Bookmark) error
- func (r *SQLite) InsertOne(ctx context.Context, b *bookmark.Bookmark) (int64, error)
- func (r *SQLite) IsInitialized(ctx context.Context) bool
- func (r *SQLite) MaxID(ctx context.Context) int
- func (r *SQLite) Name() string
- func (r *SQLite) ReorderIDs(ctx context.Context) error
- func (r *SQLite) SetFavorite(ctx context.Context, b *bookmark.Bookmark) error
- func (r *SQLite) Stats(ctx context.Context) *RepoStats
- func (r *SQLite) TagsCounter(ctx context.Context) (map[string]int, error)
- func (r *SQLite) UpdateNotes(ctx context.Context, bID int, notes string) error
- func (r *SQLite) UpdateOne(ctx context.Context, b *bookmark.Bookmark) error
- func (r *SQLite) Vacuum(ctx context.Context) error
- func (r *SQLite) WithTx(ctx context.Context, fn func(tx *sqlx.Tx) error) error
- type Schema
- type Table
Constants ¶
This section is empty.
Variables ¶
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") )
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") )
var ( // backups errs. ErrBackupExists = errors.New("backup already exists") ErrBackupNotFound = errors.New("no backup found") )
var Schemas = DatabaseSchemas{
Main: schemaMain,
Tags: schemaTags,
Relation: schemaRelation,
Temp: schemaTemp,
}
Functions ¶
func IsInitializedFromPath ¶ added in v0.1.30
IsInitializedFromPath checks if the database is initialized.
func OpenDatabase ¶ added in v0.1.21
OpenDatabase opens a SQLite database at the specified path and verifies the connection, returning the database handle or an error.
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
NewSQLiteCfg returns the default settings for the database.
type DatabaseSchemas ¶ added in v0.1.19
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
HasOpenConnections checks if any are open.
func (*Manager) Unregister ¶ added in v0.1.30
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.
type SQLite ¶
type SQLite struct {
DB *sqlx.DB `json:"-"`
Cfg *Cfg `json:"db"`
// contains filtered or unexported fields
}
SQLite implements the Repository interface.
func (*SQLite) Backup ¶
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) CheckIntegrity ¶ added in v0.1.30
CheckIntegrity performs a PRAGMA integrity_check on the SQLite database.
func (*SQLite) CleanupOrphanTags ¶ added in v0.1.22
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) CountFavorites ¶
CountFavorites returns the number of favorite records.
func (*SQLite) DeleteMany ¶
DeleteMany deletes multiple records from the main table.
func (*SQLite) DropSecure ¶
DropSecure removes all records database.
func (*SQLite) FavoritesList ¶
FavoritesList returns the favorite bookmarks.
func (*SQLite) InsertMany ¶
func (*SQLite) IsInitialized ¶
IsInitialized checks if the database is initialized.
func (*SQLite) MaxID ¶
MaxID retrieves the maximum ID from the specified table in the SQLite database.
func (*SQLite) SetFavorite ¶
func (*SQLite) TagsCounter ¶
TagsCounter returns a map with tag as key and count as value.
func (*SQLite) UpdateNotes ¶ added in v0.1.27
UpdateNotes updates the bookmak's notes.