Documentation ¶
Overview ¶
Package store provides the worklog data storage layer.
Index ¶
- Constants
- type DB
- func (db *DB) AmendEvents(ts time.Time, note *worklog.Amendment) (sql.Result, error)
- func (db *DB) BucketID(uid string) string
- func (db *DB) BucketMetadata(bid string) (*worklog.BucketMetadata, error)
- func (db *DB) Buckets() ([]worklog.BucketMetadata, error)
- func (db *DB) Close() error
- func (db *DB) CreateBucket(uid, name, typ, client string, created time.Time, data map[string]any) (m *worklog.BucketMetadata, err error)
- func (db *DB) Dump() ([]worklog.BucketMetadata, error)
- func (db *DB) DumpRange(start, end time.Time) ([]worklog.BucketMetadata, error)
- func (db *DB) Dynamic(q Query) ([]map[string]any, error)
- func (db *DB) Events(bid string) ([]worklog.Event, error)
- func (db *DB) EventsRange(bid string, start, end time.Time, limit int) ([]worklog.Event, error)
- func (db *DB) EventsRangeFunc(bid string, start, end time.Time, limit int, fn func(worklog.Event) error) error
- func (db *DB) InsertEvent(e *worklog.Event) (sql.Result, error)
- func (db *DB) LastEvent(uid string) (*worklog.Event, error)
- func (db *DB) Load(buckets []worklog.BucketMetadata, replace bool) (err error)
- func (db *DB) Name() string
- func (db *DB) UpdateEvent(e *worklog.Event) (sql.Result, error)
- type Query
Constants ¶
const ( EventsRange = `` /* 178-byte string literal not displayed */ EventsRangeUntil = `` /* 161-byte string literal not displayed */ EventsRangeFrom = `` /* 159-byte string literal not displayed */ EventsLimit = `` /* 142-byte string literal not displayed */ )
const AmendEvents = `` /* 806-byte string literal not displayed */
const BucketMetadata = `select id, name, type, client, hostname, created, datastr from buckets where id = ?`
const Buckets = `select id, name, type, client, hostname, created, datastr from buckets`
const CreateBucket = `insert into buckets(id, name, type, client, hostname, created, datastr) values (?, ?, ?, ?, ?, ?, ?)`
const DeleteBucket = `delete from buckets where id = ?`
const DeleteBucketEvents = `delete from events where bucketrow in (
select rowid from buckets where id = ?
)`
const DeleteEvent = `delete from events where bucketrow = (
select rowid from buckets where id = ?
) and id = ?`
const Event = `` /* 131-byte string literal not displayed */
const Events = `select id, starttime, endtime, datastr from events where bucketrow = (
select rowid from buckets where id = ?
)`
const InsertEvent = `insert into events(bucketrow, starttime, endtime, datastr) values ((select rowid from buckets where id = ?), ?, ?, ?)`
const LastEvent = `` /* 245-byte string literal not displayed */
const Schema = `` /* 821-byte string literal not displayed */
Schema is the DB schema.
const UpdateEvent = `` /* 130-byte string literal not displayed */
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a persistent store.
func Open ¶
Open opens an existing DB. See https://pkg.go.dev/modernc.org/sqlite#Driver.Open for name handling details. Open attempts to get the CNAME for the host, which may wait indefinitely, so a timeout context can be provided to fall back to the kernel-provided hostname.
func (*DB) AmendEvents ¶
AmendEvents adds amendment notes to the data for events in the store overlapping the note. On return the note.Replace slice will be sorted.
The SQL command run is AmendEvents.
func (*DB) BucketMetadata ¶
func (db *DB) BucketMetadata(bid string) (*worklog.BucketMetadata, error)
BucketMetadata returns the metadata for the bucket with the provided internal bucket ID. The SQL command run is BucketMetadata.
func (*DB) Buckets ¶
func (db *DB) Buckets() ([]worklog.BucketMetadata, error)
Buckets returns the full set of bucket metadata. The SQL command run is Buckets.
func (*DB) CreateBucket ¶
func (db *DB) CreateBucket(uid, name, typ, client string, created time.Time, data map[string]any) (m *worklog.BucketMetadata, err error)
CreateBucket creates a new entry in the bucket table. If the entry already exists it will return an sqlite.Error with the code sqlite3.SQLITE_CONSTRAINT_UNIQUE. The SQL command run is CreateBucket.
func (*DB) Dump ¶
func (db *DB) Dump() ([]worklog.BucketMetadata, error)
Dump dumps the complete database into a slice of worklog.BucketMetadata.
func (*DB) DumpRange ¶
DumpRange dumps the database spanning the specified time range into a slice of worklog.BucketMetadata.
func (*DB) Events ¶
Buckets returns the full set of events in the bucket with the provided internal bucket ID. The SQL command run is Events.
func (*DB) EventsRange ¶
EventsRange returns the events in the bucket with the provided bucket ID within the specified time range, sorted descending by end time. The SQL command run is EventsRange, EventsRangeUntil, EventsRangeFrom or EventsLimit depending on whether start and end are zero.
func (*DB) EventsRangeFunc ¶
func (db *DB) EventsRangeFunc(bid string, start, end time.Time, limit int, fn func(worklog.Event) error) error
EventsRange calls fn on all the events in the bucket with the provided bucket ID within the specified time range, sorted descending by end time. The SQL command run is EventsRange, EventsRangeUntil, EventsRangeFrom or EventsLimit depending on whether start and end are zero.
func (*DB) InsertEvent ¶
InsertEvent inserts a new event into the events table. The SQL command run is InsertEvent.
func (*DB) LastEvent ¶
LastEvent returns the last event in the named bucket. The SQL command run is LastEvent.
func (*DB) Load ¶
func (db *DB) Load(buckets []worklog.BucketMetadata, replace bool) (err error)
Load loads a complete database from a slice of worklog.BucketMetadata. Event IDs will be regenerated by the backing database and so will not match the input data. If replace is true and a bucket already exists matching the bucket in the provided buckets slice, the existing events will be deleted and replaced. If replace is false, the new events will be added to the existing events in the store.
func (*DB) UpdateEvent ¶
UpdateEvent updates the event in the store corresponding to the provided event. The SQL command run is UpdateEvent.
type Query ¶
type Query struct { // Fields is the result columns to return. // It may be a string, a []string or a []any // containing strings. If fields is nil // the * result is returned. Fields any // From is the table to query from. It must // be "events" or "buckets". From string // Where is the SQL WHERE expression. If it // is empty, the constructed SQL query will // have no WHERE clause. Where map[string]any // Limit is the result limit. If it is nil, // no LIMIT clause will be added to the query. Limit *int }
Query represents an SQL SELECT query.
The SQL expressible with Query is a subset of the SQLite SQL language. Result columns may be literal columns, count(*) or * and the WHERE clause does not support SELECT sub-expressions.
WHERE expressions are represented as a single-entry map. For example, 'a = 1 and b != 2' would be represented as
{"and": [ {"=": {"a": 1}}, {"!=": {"b": 2}} ]}
in JSON.
Language operators that are supported in the WHERE clause are:
- "or" and "and": take an array of sub-expressions
- "not": takes a single sub-expression
- "in" and "not in": takes an array of literals and cannot use columns
- "isnull", "notnull" and "not null": take a single sub-expression
- "=", "!=", "<", ">", "<=", ">=", "is", "is not", "like" and "glob": take a single entry sub-expression where the key is the column name and the value is the comparison argument.
JSON fields ¶
In addition to fields defined within the DB schema, fields within the datastr JSON field are accessible by prefixing the field name with "$.", so to select records where datastr.title is not null the expression in JSON would be
{"notnull": "$.title"}
or to select a set of applications, "terminal" and "firefox" the expression would be
{"in": {"$.app": [ "terminal", "firefox" ]}}