Add --orphan filtering flag

pull/6/head
Mickaël Menu 3 years ago
parent f7a5c04c6e
commit e4e734f3dd
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -410,6 +410,11 @@ func (d *NoteDAO) findRows(opts note.FinderOpts) (*sql.Rows, error) {
whereExprs = append(whereExprs, expr)
case note.OrphanFilter:
whereExprs = append(whereExprs, `n.id NOT IN (
SELECT target_id FROM links WHERE target_id IS NOT NULL
)`)
case note.DateFilter:
value := "?"
field := "n." + dateField(filter)

@ -527,6 +527,15 @@ func TestNoteDAOFindNotLinkingTo(t *testing.T) {
)
}
func TestNoteDAOFindOrphan(t *testing.T) {
testNoteDAOFindPaths(t,
note.FinderOpts{
Filters: []note.Filter{note.OrphanFilter{}},
},
[]string{"ref/test/b.md", "f39c8.md", "log/2021-02-04.md", "index.md"},
)
}
func TestNoteDAOFindCreatedOn(t *testing.T) {
testNoteDAOFindPaths(t,
note.FinderOpts{

@ -23,6 +23,7 @@ type Filtering struct {
LinkingTo []string `help:"Only the notes linking to the given notes" placeholder:"<path>" short:"L"`
NotLinkedBy []string `help:"Only the notes not linked by the given notes" placeholder:"<path>"`
NotLinkingTo []string `help:"Only the notes not linking to the given notes" placeholder:"<path>"`
Orphan bool `help:"Only the notes which don't have any other note linking to them"`
Exclude []string `help:"Excludes notes matching the given file path pattern from the list" short:"x" placeholder:"<glob>"`
Interactive bool `help:"Further filter the list of notes interactively" short:"i"`
}
@ -154,6 +155,10 @@ func NewFinderOpts(zk *zk.Zk, filtering Filtering, sorting Sorting) (*note.Finde
})
}
if filtering.Orphan {
filters = append(filters, note.OrphanFilter{})
}
if filtering.Interactive {
filters = append(filters, note.InteractiveFilter(true))
}

@ -52,6 +52,9 @@ type LinkingToFilter struct {
Negate bool
}
// OrphanFilter is a note filter used to select notes having no other notes linking to them.
type OrphanFilter struct{}
// DateFilter can be used to filter notes created or modified before, after or on a given date.
type DateFilter struct {
Date time.Time
@ -67,6 +70,7 @@ func (f PathFilter) sealed() {}
func (f ExcludePathFilter) sealed() {}
func (f LinkedByFilter) sealed() {}
func (f LinkingToFilter) sealed() {}
func (f OrphanFilter) sealed() {}
func (f DateFilter) sealed() {}
func (f InteractiveFilter) sealed() {}

Loading…
Cancel
Save