Skip to content

refactor(db): build alias-specific SQL with functions and share the row loop - #432

Merged
ryanlewis merged 2 commits into
mainfrom
refactor/db-sql
Oct 9, 2026
Merged

ryanlewis merged 2 commits into
mainfrom
refactor/db-sql

Conversation

@ryanlewis

Copy link
Copy Markdown
Owner

Removes duplicated SQL building in internal/db. This is a refactor: the list-query golden file (testdata/list_query_golden.txt) is unchanged, and so is every other SQL fragment touched here.

What changed

  • Alias builders instead of string replacement. closedTodayUnlogged, upcomingScheduled and somedayDeferred were turned into p./c. versions by strings.ReplaceAll on "t." or "t.stopDate". They are now built by closedUnloggedOf(alias), scheduledLaterOf(alias) and deferredUndatedOf(alias). Some fragments that depend on them change from const to var as a result.
  • One placeholder expander. fillRepeating(query, self, parent) fills {{repeating}} and {{repeating_parent}}. It replaces the separate ReplaceAll pairs in probeRepeating and buildListQuery and the NewReplacer in ListProjects.
  • One "open or just closed" rule. openOrUnlogged(fold) is used by openOrJustClosed, ListProjects and AddTarget.
  • Shared title-match decision. GetTask and GetTaskExact both call pickTitleMatch. FindTasksByTitle now uses the named constants untrashedRows, untrashedParent and openRows, which have the same text.
  • Generic row loop. queryAll[T] replaces the query/scan/append loops in collectTasks, ListProjects, ListAreas and ListTags. splitTags replaces the two \x1f splits.

One clause that is not textually identical

AddTarget's title match was status = ? OR <closedTodayUnlogged> with 0 bound. It is now (t.status = 0 OR (t.status IN (2, 3) AND <closedTodayUnlogged>)), the same rule the lists and things projects use. The two differ only for a row whose status is not 0, 2 or 3, or is NULL. Things does not write such rows: on the live database every TMTask row has status 0, 2 or 3.

Not changed

  • GetChecklistItems keeps its own loop. It returns nil when there are no items, and queryAll returns an empty slice, so moving it could change JSON output from null to [].
  • Magic ints in SQL are left as they are.

Checks

  • make test and make lint pass. The golden SQL test passes without regenerating.
  • A snapshot of listGrouping, projectPageOrder, projectOfFoldedTagged, parentNotClosedOrUnlogged, heldInPlace, notHeldInPlace, upcomingScope, closedTodayUnlogged and the assembled task query was byte-identical before and after.
  • Against the real Things database, today, upcoming, anytime, someday, logbook, projects, search, tags, areas and show with --json gave byte-identical output from this branch and from main.

…ow loop

Build closedTodayUnlogged, upcomingScheduled and somedayDeferred for a
given alias instead of string-replacing "t." in their text. Fill the
recurrence placeholders through one fillRepeating, write "open or just
closed" once as openOrUnlogged, share the title-match decision between
GetTask and GetTaskExact, and scan rows through a generic queryAll with
a splitTags helper.
@ryanlewis
ryanlewis merged commit 243c30f into main Oct 9, 2026
10 of 11 checks passed
@ryanlewis
ryanlewis deleted the refactor/db-sql branch October 9, 2026 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant