From 1745097256926956c05b75d2eab97adbdc5fb0b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Menu?= Date: Sat, 17 Sep 2022 21:22:54 +0200 Subject: [PATCH] Fix error when pairing `--link-to` and `--linked-by` (#255) --- CHANGELOG.md | 1 + internal/adapter/sqlite/note_dao.go | 22 +++++++++++----------- tests/fixtures/issue-254/.zk/config.toml | 1 + tests/fixtures/issue-254/index.md | 4 ++++ tests/fixtures/issue-254/note1.md | 3 +++ tests/fixtures/issue-254/note2.md | 3 +++ tests/issue-254.tesh | 8 ++++++++ 7 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 tests/fixtures/issue-254/.zk/config.toml create mode 100644 tests/fixtures/issue-254/index.md create mode 100644 tests/fixtures/issue-254/note1.md create mode 100644 tests/fixtures/issue-254/note2.md create mode 100644 tests/issue-254.tesh diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d4420e..cf55d3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. ### Fixed * [#243](https://github.com/mickael-menu/zk/issues/243) LSP: Fixed finding backlink references for notes in a folder. +* [#254](https://github.com/mickael-menu/zk/issues/254) Fixed SQL error when pairing `--link-to` and `--linked-by`. ## 0.11.1 diff --git a/internal/adapter/sqlite/note_dao.go b/internal/adapter/sqlite/note_dao.go index 81f3d40..b63f9df 100644 --- a/internal/adapter/sqlite/note_dao.go +++ b/internal/adapter/sqlite/note_dao.go @@ -448,7 +448,7 @@ func (d *NoteDAO) findRows(opts core.NoteFindOpts, selection noteSelection) (*sq transitiveClosure := false maxDistance := 0 - setupLinkFilter := func(hrefs []string, direction int, negate, recursive bool) error { + setupLinkFilter := func(tableAlias string, hrefs []string, direction int, negate, recursive bool) error { ids, err := d.findIdsByHrefs(hrefs, true /* allowPartialHrefs */) if err != nil { return err @@ -463,27 +463,29 @@ func (d *NoteDAO) findRows(opts core.NoteFindOpts, selection noteSelection) (*sq if recursive { transitiveClosure = true linksSrc = "transitive_closure" + additionalOrderTerms = append(additionalOrderTerms, tableAlias+".distance") } if !negate { if direction != 0 { - snippetCol = "GROUP_CONCAT(REPLACE(l.snippet, l.title, '' || l.title || ''), '\x01')" + snippetCol = fmt.Sprintf("GROUP_CONCAT(REPLACE(%s.snippet, %[1]s.title, '' || %[1]s.title || ''), '\x01')", tableAlias) } joinOns := make([]string, 0) if direction <= 0 { joinOns = append(joinOns, fmt.Sprintf( - "(n.id = l.target_id AND l.source_id IN %s)", idsList, + "(n.id = %[1]s.target_id AND %[1]s.source_id IN %[2]s)", tableAlias, idsList, )) } if direction >= 0 { joinOns = append(joinOns, fmt.Sprintf( - "(n.id = l.source_id AND l.target_id IN %s)", idsList, + "(n.id = %[1]s.source_id AND %[1]s.target_id IN %[2]s)", tableAlias, idsList, )) } joinClauses = append(joinClauses, fmt.Sprintf( - "LEFT JOIN %s l ON %s", + "LEFT JOIN %[2]s %[1]s ON %[3]s", + tableAlias, linksSrc, strings.Join(joinOns, " OR "), )) @@ -618,7 +620,7 @@ WHERE collection_id IN (SELECT id FROM collections t WHERE kind = '%s' AND (%s)) if opts.LinkedBy != nil { filter := opts.LinkedBy maxDistance = filter.MaxDistance - err := setupLinkFilter(filter.Hrefs, -1, filter.Negate, filter.Recursive) + err := setupLinkFilter("l_by", filter.Hrefs, -1, filter.Negate, filter.Recursive) if err != nil { return nil, err } @@ -627,7 +629,7 @@ WHERE collection_id IN (SELECT id FROM collections t WHERE kind = '%s' AND (%s)) if opts.LinkTo != nil { filter := opts.LinkTo maxDistance = filter.MaxDistance - err := setupLinkFilter(filter.Hrefs, 1, filter.Negate, filter.Recursive) + err := setupLinkFilter("l_to", filter.Hrefs, 1, filter.Negate, filter.Recursive) if err != nil { return nil, err } @@ -635,11 +637,11 @@ WHERE collection_id IN (SELECT id FROM collections t WHERE kind = '%s' AND (%s)) if opts.Related != nil { maxDistance = 2 - err := setupLinkFilter(opts.Related, 0, false, true) + err := setupLinkFilter("l_rel", opts.Related, 0, false, true) if err != nil { return nil, err } - groupBy += " HAVING MIN(l.distance) = 2" + groupBy += " HAVING MIN(l_rel.distance) = 2" } if opts.Orphan { @@ -687,8 +689,6 @@ WHERE collection_id IN (SELECT id FROM collections t WHERE kind = '%s' AND (%s)) // Credit to https://inviqa.com/blog/storing-graphs-database-sql-meets-social-network if transitiveClosure { - orderTerms = append([]string{"l.distance"}, orderTerms...) - query += `WITH RECURSIVE transitive_closure(source_id, target_id, title, snippet, distance, path) AS ( SELECT source_id, target_id, title, snippet, 1 AS distance, diff --git a/tests/fixtures/issue-254/.zk/config.toml b/tests/fixtures/issue-254/.zk/config.toml new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/tests/fixtures/issue-254/.zk/config.toml @@ -0,0 +1 @@ + diff --git a/tests/fixtures/issue-254/index.md b/tests/fixtures/issue-254/index.md new file mode 100644 index 0000000..558a35c --- /dev/null +++ b/tests/fixtures/issue-254/index.md @@ -0,0 +1,4 @@ +# Index + +[Note 1](note1) +[Note 2](note2) diff --git a/tests/fixtures/issue-254/note1.md b/tests/fixtures/issue-254/note1.md new file mode 100644 index 0000000..7951a4a --- /dev/null +++ b/tests/fixtures/issue-254/note1.md @@ -0,0 +1,3 @@ +# Note 1 + +[Index](index) diff --git a/tests/fixtures/issue-254/note2.md b/tests/fixtures/issue-254/note2.md new file mode 100644 index 0000000..82919f4 --- /dev/null +++ b/tests/fixtures/issue-254/note2.md @@ -0,0 +1,3 @@ +# Note 2 + +[Note 1](note1) diff --git a/tests/issue-254.tesh b/tests/issue-254.tesh new file mode 100644 index 0000000..b243c8a --- /dev/null +++ b/tests/issue-254.tesh @@ -0,0 +1,8 @@ +# Unable to list by link and backlink +# https://github.com/mickael-menu/zk/issues/254 + +$ cd issue-254 + +$ zk list -qfpath --link-to index.md --linked-by index.md +>note1.md +