-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathdatabase.py
More file actions
executable file
·1192 lines (974 loc) · 38.5 KB
/
database.py
File metadata and controls
executable file
·1192 lines (974 loc) · 38.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Read from the Things SQLite database using SQL queries."""
# pylint: disable=C0302
import datetime
import glob
import os
import plistlib
import re
import sqlite3
from textwrap import dedent
from typing import Optional, Union
# --------------------------------------------------
# Core constants
# --------------------------------------------------
# Database filepath with glob pattern for version 3.15.16+
DEFAULT_FILEPATH_31616502 = (
"~/Library/Group Containers/JLMPQHK86H.com.culturedcode.ThingsMac"
"/ThingsData-*/Things Database.thingsdatabase/main.sqlite"
)
DEFAULT_FILEPATH_31516502 = (
"~/Library/Group Containers/JLMPQHK86H.com.culturedcode.ThingsMac"
"/Things Database.thingsdatabase/main.sqlite"
)
try:
DEFAULT_FILEPATH = next(glob.iglob(os.path.expanduser(DEFAULT_FILEPATH_31616502)))
except StopIteration:
DEFAULT_FILEPATH = os.path.expanduser(DEFAULT_FILEPATH_31516502)
ENVIRONMENT_VARIABLE_WITH_FILEPATH = "THINGSDB"
# Translate app language to database language
START_TO_FILTER = {
"Inbox": "start = 0",
"Anytime": "start = 1",
"Someday": "start = 2",
}
STATUS_TO_FILTER = {
"incomplete": "status = 0",
"canceled": "status = 2",
"completed": "status = 3",
}
TRASHED_TO_FILTER = {True: "trashed = 1", False: "trashed = 0"}
TYPE_TO_FILTER = {
"to-do": "type = 0",
"project": "type = 1",
"heading": "type = 2",
}
# Dates
DATES = ("future", "past", True, False)
# Indices
INDICES = ("index", "todayIndex")
# Response modification
COLUMNS_TO_OMIT_IF_NONE = (
"area",
"area_title",
"checklist",
"heading",
"heading_title",
"project",
"project_title",
"trashed",
"tags",
)
COLUMNS_TO_TRANSFORM_TO_BOOL = ("checklist", "tags", "trashed")
# --------------------------------------------------
# Table names
# --------------------------------------------------
TABLE_AREA = "TMArea"
TABLE_AREATAG = "TMAreaTag"
TABLE_CHECKLIST_ITEM = "TMChecklistItem"
TABLE_META = "Meta"
TABLE_TAG = "TMTag"
TABLE_TASK = "TMTask"
TABLE_TASKTAG = "TMTaskTag"
TABLE_SETTINGS = "TMSettings"
# --------------------------------------------------
# Date Columns
# --------------------------------------------------
# Note that the columns below -- contrary to their names -- seem to
# store the full UTC datetime, not just the date.
DATE_CREATED = "creationDate" # REAL: Unix date & time, UTC
DATE_MODIFIED = "userModificationDate" # REAL: Unix date & time, UTC
DATE_STOP = "stopDate" # REAL: Unix date & time, UTC
# These are stored in a Things date format.
# See `convert_isodate_sql_expression_to_thingsdate` for details.
DATE_DEADLINE = "deadline" # INTEGER: YYYYYYYYYYYMMMMDDDDD0000000, in binary
DATE_START = "startDate" # INTEGER: YYYYYYYYYYYMMMMDDDDD0000000, in binary
# --------------------------------------------------
# Various filters
# --------------------------------------------------
# Type
IS_TODO = TYPE_TO_FILTER["to-do"]
IS_PROJECT = TYPE_TO_FILTER["project"]
IS_HEADING = TYPE_TO_FILTER["heading"]
# Status
IS_INCOMPLETE = STATUS_TO_FILTER["incomplete"]
IS_CANCELED = STATUS_TO_FILTER["canceled"]
IS_COMPLETED = STATUS_TO_FILTER["completed"]
# Start
IS_INBOX = START_TO_FILTER["Inbox"]
IS_ANYTIME = START_TO_FILTER["Anytime"]
IS_SOMEDAY = START_TO_FILTER["Someday"]
# Repeats
IS_NOT_RECURRING = "rt1_recurrenceRule IS NULL"
# Trash
IS_TRASHED = TRASHED_TO_FILTER[True]
# --------------------------------------------------
# Fields and filters not yet used in the implementation.
# This information might be of relevance in the future.
# --------------------------------------------------
#
# IS_SCHEDULED = f"{DATE_START} IS NOT NULL"
# IS_NOT_SCHEDULED = f"{DATE_START} IS NULL"
# IS_DEADLINE = f"{DATE_DEADLINE} IS NOT NULL"
# RECURRING_IS_NOT_PAUSED = "rt1_instanceCreationPaused = 0"
# IS_RECURRING = "rt1_recurrenceRule IS NOT NULL"
# RECURRING_HAS_NEXT_STARTDATE = ("rt1_nextInstanceStartDate IS NOT NULL")
# IS_NOT_TRASHED = TRASHED_TO_FILTER[False]
# pylint: disable=R0904,R0902
class Database:
"""
Access Things SQL database.
Parameters
----------
filepath : str, optional
Any valid path of a SQLite database file generated by the Things app.
If the environment variable `THINGSDB` is set, then use that path.
Otherwise, access the default database path.
print_sql : bool, default False
Print every SQL query performed. Some may contain '?' and ':'
characters which correspond to SQLite parameter tokens.
See https://www.sqlite.org/lang_expr.html#varparam
:raises AssertionError: If the database version is too old.
"""
debug = False
connection: sqlite3.Connection
# pylint: disable=R0913
def __init__(self, filepath=None, print_sql=False):
"""Set up the database."""
self.filepath = (
filepath
or os.getenv(ENVIRONMENT_VARIABLE_WITH_FILEPATH)
or DEFAULT_FILEPATH
)
self.print_sql = print_sql
if self.print_sql:
self.execute_query_count = 0
# "ro" means read-only
# See: https://sqlite.org/uri.html#recognized_query_parameters
uri = f"file:{self.filepath}?mode=ro"
self.connection = sqlite3.connect(uri, uri=True) # pylint: disable=E1101
# Test for migrated database in Things 3.15.16+
# --------------------------------
assert self.get_version() > 21, (
"Your database is in an older format. "
"Run 'pip install things.py==0.0.14' to downgrade to an older "
"version of this library."
)
# Automated migration to new database location in Things 3.12.6/3.13.1
# --------------------------------
try:
with open(self.filepath, encoding="utf-8") as file:
if "Your database file has been moved there" in file.readline():
self.filepath = DEFAULT_FILEPATH
except (UnicodeDecodeError, FileNotFoundError, PermissionError):
pass # binary file (old database) or doesn't exist
# --------------------------------
# Core methods
def get_tasks( # pylint: disable=R0914
self,
uuid: Optional[str] = None,
type: Optional[str] = None, # pylint: disable=W0622
status: Optional[str] = None,
start: Optional[str] = None,
area: Optional[Union[str, bool]] = None,
project: Optional[Union[str, bool]] = None,
heading: Optional[str] = None,
tag: Optional[Union[str, bool]] = None,
start_date: Optional[Union[str, bool]] = None,
stop_date: Optional[Union[str, bool]] = None,
deadline: Optional[Union[str, bool]] = None,
deadline_suppressed: Optional[bool] = None,
trashed: Optional[bool] = False,
context_trashed: Optional[bool] = False,
last: Optional[str] = None,
search_query: Optional[str] = None,
index: str = "index",
count_only: bool = False,
):
"""Get tasks. See `things.api.tasks` for details on parameters."""
if uuid:
return self.get_task_by_uuid(uuid, count_only=count_only)
# Overwrites
start = start and start.title()
# Validation
validate_date("deadline", deadline)
validate("deadline_suppressed", deadline_suppressed, [None, True, False])
validate("start", start, [None] + list(START_TO_FILTER))
validate_date("start_date", start_date)
validate_date("stop_date", stop_date)
validate("status", status, [None] + list(STATUS_TO_FILTER))
validate("trashed", trashed, [None] + list(TRASHED_TO_FILTER))
validate("type", type, [None] + list(TYPE_TO_FILTER))
validate("context_trashed", context_trashed, [None, True, False])
validate("index", index, list(INDICES))
validate_offset("last", last)
if tag is not None:
valid_tags = self.get_tags(titles_only=True)
validate("tag", tag, [None] + list(valid_tags))
# Query
# TK: might consider executing SQL with parameters instead.
# See: https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.execute
start_filter: str = START_TO_FILTER.get(start, "") # type: ignore
status_filter: str = STATUS_TO_FILTER.get(status, "") # type: ignore
trashed_filter: str = TRASHED_TO_FILTER.get(trashed, "") # type: ignore
type_filter: str = TYPE_TO_FILTER.get(type, "") # type: ignore
# Sometimes a task is _not_ set to trashed, but its context
# (project or heading it is contained within) is set to trashed.
# In those cases, the task wouldn't show up in any app view
# except for "Trash".
project_trashed_filter = make_truthy_filter("PROJECT.trashed", context_trashed)
project_of_heading_trashed_filter = make_truthy_filter(
"PROJECT_OF_HEADING.trashed", context_trashed
)
# As a task assigned to a heading is not directly assigned to a project anymore,
# we need to check if the heading is assigned to a project.
# See, e.g. https://github.com/thingsapi/things.py/issues/94
project_filter = make_or_filter(
make_filter("TASK.project", project),
make_filter("PROJECT_OF_HEADING.uuid", project),
)
where_predicate = f"""
TASK.{IS_NOT_RECURRING}
{trashed_filter and f"AND TASK.{trashed_filter}"}
{project_trashed_filter}
{project_of_heading_trashed_filter}
{type_filter and f"AND TASK.{type_filter}"}
{start_filter and f"AND TASK.{start_filter}"}
{status_filter and f"AND TASK.{status_filter}"}
{make_filter('TASK.uuid', uuid)}
{make_filter("TASK.area", area)}
{project_filter}
{make_filter("TASK.heading", heading)}
{make_filter("TASK.deadlineSuppressionDate", deadline_suppressed)}
{make_filter("TAG.title", tag)}
{make_thingsdate_filter(f"TASK.{DATE_START}", start_date)}
{make_unixtime_filter(f"TASK.{DATE_STOP}", stop_date)}
{make_thingsdate_filter(f"TASK.{DATE_DEADLINE}", deadline)}
{make_unixtime_range_filter(f"TASK.{DATE_CREATED}", last)}
{make_search_filter(search_query)}
"""
order_predicate = f'TASK."{index}"'
sql_query = make_tasks_sql_query(where_predicate, order_predicate)
if count_only:
return self.get_count(sql_query)
return self.execute_query(sql_query)
def get_task_by_uuid(self, uuid, count_only=False):
"""Get a task by uuid. Raise `ValueError` if not found."""
where_predicate = "TASK.uuid = ?"
sql_query = make_tasks_sql_query(where_predicate)
parameters = (uuid,)
if count_only:
return self.get_count(sql_query, parameters)
result = self.execute_query(sql_query, parameters)
if not result:
raise ValueError(f"No such task uuid found: {uuid!r}")
return result
def get_areas(self, uuid=None, tag=None, count_only=False):
"""Get areas. See `api.areas` for details on parameters."""
# Validation
if tag is not None:
valid_tags = self.get_tags(titles_only=True)
validate("tag", tag, [None] + list(valid_tags))
if (
uuid
and count_only is False
and not self.get_areas(uuid=uuid, count_only=True)
):
raise ValueError(f"No such area uuid found: {uuid!r}")
# Query
sql_query = f"""
SELECT DISTINCT
AREA.uuid,
'area' as type,
AREA.title,
CASE
WHEN AREA_TAG.areas IS NOT NULL THEN 1
END AS tags
FROM
{TABLE_AREA} AS AREA
LEFT OUTER JOIN
{TABLE_AREATAG} AREA_TAG ON AREA_TAG.areas = AREA.uuid
LEFT OUTER JOIN
{TABLE_TAG} TAG ON TAG.uuid = AREA_TAG.tags
WHERE
TRUE
{make_filter('TAG.title', tag)}
{make_filter('AREA.uuid', uuid)}
ORDER BY AREA."index"
"""
if count_only:
return self.get_count(sql_query)
return self.execute_query(sql_query)
def get_checklist_items(self, todo_uuid=None):
"""Get checklist items."""
sql_query = f"""
SELECT
CHECKLIST_ITEM.title,
CASE
WHEN CHECKLIST_ITEM.{IS_INCOMPLETE} THEN 'incomplete'
WHEN CHECKLIST_ITEM.{IS_CANCELED} THEN 'canceled'
WHEN CHECKLIST_ITEM.{IS_COMPLETED} THEN 'completed'
END AS status,
date(CHECKLIST_ITEM.stopDate, "unixepoch", "localtime") AS stop_date,
'checklist-item' as type,
CHECKLIST_ITEM.uuid,
datetime(
CHECKLIST_ITEM.{DATE_MODIFIED}, "unixepoch", "localtime"
) AS created,
datetime(
CHECKLIST_ITEM.{DATE_MODIFIED}, "unixepoch", "localtime"
) AS modified
FROM
{TABLE_CHECKLIST_ITEM} AS CHECKLIST_ITEM
WHERE
CHECKLIST_ITEM.task = ?
ORDER BY CHECKLIST_ITEM."index"
"""
return self.execute_query(sql_query, (todo_uuid,))
def get_tags(self, title=None, area=None, task=None, titles_only=False):
"""Get tags. See `api.tags` for details on parameters."""
# Validation
if title is not None:
valid_titles = self.get_tags(titles_only=True)
validate("title", title, [None] + list(valid_titles))
# Query
if task:
return self.get_tags_of_task(task)
if area:
return self.get_tags_of_area(area)
if titles_only:
sql_query = f'SELECT title FROM {TABLE_TAG} ORDER BY "index"'
return self.execute_query(sql_query, row_factory=list_factory)
sql_query = f"""
SELECT
uuid, 'tag' AS type, title, shortcut
FROM
{TABLE_TAG}
WHERE
TRUE
{make_filter('title', title)}
ORDER BY "index"
"""
return self.execute_query(sql_query)
def get_tags_of_task(self, task_uuid):
"""Get tag titles of task."""
sql_query = f"""
SELECT
TAG.title
FROM
{TABLE_TASKTAG} AS TASK_TAG
LEFT OUTER JOIN
{TABLE_TAG} TAG ON TAG.uuid = TASK_TAG.tags
WHERE
TASK_TAG.tasks = ?
ORDER BY TAG."index"
"""
return self.execute_query(
sql_query, parameters=(task_uuid,), row_factory=list_factory
)
def get_tags_of_area(self, area_uuid):
"""Get tag titles for area."""
sql_query = f"""
SELECT
AREA.title
FROM
{TABLE_AREATAG} AS AREA_TAG
LEFT OUTER JOIN
{TABLE_TAG} AREA ON AREA.uuid = AREA_TAG.tags
WHERE
AREA_TAG.areas = ?
ORDER BY AREA."index"
"""
return self.execute_query(
sql_query, parameters=(area_uuid,), row_factory=list_factory
)
def get_version(self):
"""Get Things Database version."""
sql_query = f"SELECT value FROM {TABLE_META} WHERE key = 'databaseVersion'"
result = self.execute_query(sql_query, row_factory=list_factory)
plist_bytes = result[0].encode()
return plistlib.loads(plist_bytes)
# pylint: disable=R1710
def get_url_scheme_auth_token(self):
"""Get the Things URL scheme authentication token."""
sql_query = f"""
SELECT
uriSchemeAuthenticationToken
FROM
{TABLE_SETTINGS}
WHERE
uuid = 'RhAzEf6qDxCD5PmnZVtBZR'
"""
rows = self.execute_query(sql_query, row_factory=list_factory)
return rows[0]
def get_count(self, sql_query, parameters=()):
"""Count number of results."""
count_sql_query = f"""SELECT COUNT(uuid) FROM (\n{sql_query}\n)"""
rows = self.execute_query(
count_sql_query, row_factory=list_factory, parameters=parameters
)
return rows[0]
# noqa todo: add type hinting for resutl (List[Tuple[str, Any]]?)
def execute_query(self, sql_query, parameters=(), row_factory=None):
"""Run the actual SQL query."""
if self.print_sql or self.debug:
if not hasattr(self, "execute_query_count"):
# This is needed for historical `self.debug`.
# TK: might consider removing `debug` flag.
self.execute_query_count = 0
self.execute_query_count += 1
if self.debug:
print(f"/* Filepath {self.filepath!r} */")
print(f"/* Query {self.execute_query_count} */")
if parameters:
print(f"/* Parameters: {parameters!r} */")
print()
print(prettify_sql(sql_query))
print()
with self.connection:
# Using context manager to keep queries in separate transactions,
# see https://docs.python.org/3/library/sqlite3.html#sqlite3-connection-context-manager
self.connection.row_factory = row_factory or dict_factory
cursor = self.connection.cursor()
cursor.execute(sql_query, parameters)
return cursor.fetchall()
# Helper functions
def make_tasks_sql_query(where_predicate=None, order_predicate=None):
"""Make SQL query for Task table."""
where_predicate = where_predicate or "TRUE"
order_predicate = order_predicate or 'TASK."index"'
start_date_expression = convert_thingsdate_sql_expression_to_isodate(
f"TASK.{DATE_START}"
)
deadline_expression = convert_thingsdate_sql_expression_to_isodate(
f"TASK.{DATE_DEADLINE}"
)
return f"""
SELECT DISTINCT
TASK.uuid,
CASE
WHEN TASK.{IS_TODO} THEN 'to-do'
WHEN TASK.{IS_PROJECT} THEN 'project'
WHEN TASK.{IS_HEADING} THEN 'heading'
END AS type,
CASE
WHEN TASK.{IS_TRASHED} THEN 1
END AS trashed,
TASK.title,
CASE
WHEN TASK.{IS_INCOMPLETE} THEN 'incomplete'
WHEN TASK.{IS_CANCELED} THEN 'canceled'
WHEN TASK.{IS_COMPLETED} THEN 'completed'
END AS status,
CASE
WHEN AREA.uuid IS NOT NULL THEN AREA.uuid
END AS area,
CASE
WHEN AREA.uuid IS NOT NULL THEN AREA.title
END AS area_title,
CASE
WHEN PROJECT.uuid IS NOT NULL THEN PROJECT.uuid
END AS project,
CASE
WHEN PROJECT.uuid IS NOT NULL THEN PROJECT.title
END AS project_title,
CASE
WHEN HEADING.uuid IS NOT NULL THEN HEADING.uuid
END AS heading,
CASE
WHEN HEADING.uuid IS NOT NULL THEN HEADING.title
END AS heading_title,
TASK.notes,
CASE
WHEN TAG.uuid IS NOT NULL THEN 1
END AS tags,
CASE
WHEN TASK.{IS_INBOX} THEN 'Inbox'
WHEN TASK.{IS_ANYTIME} THEN 'Anytime'
WHEN TASK.{IS_SOMEDAY} THEN 'Someday'
END AS start,
CASE
WHEN CHECKLIST_ITEM.uuid IS NOT NULL THEN 1
END AS checklist,
date({start_date_expression}) AS start_date,
date({deadline_expression}) AS deadline,
datetime(TASK.{DATE_STOP}, "unixepoch", "localtime") AS "stop_date",
datetime(TASK.{DATE_CREATED}, "unixepoch", "localtime") AS created,
datetime(TASK.{DATE_MODIFIED}, "unixepoch", "localtime") AS modified,
TASK.'index',
TASK.todayIndex AS today_index
FROM
{TABLE_TASK} AS TASK
LEFT OUTER JOIN
{TABLE_TASK} PROJECT ON TASK.project = PROJECT.uuid
LEFT OUTER JOIN
{TABLE_AREA} AREA ON TASK.area = AREA.uuid
LEFT OUTER JOIN
{TABLE_TASK} HEADING ON TASK.heading = HEADING.uuid
LEFT OUTER JOIN
{TABLE_TASK} PROJECT_OF_HEADING
ON HEADING.project = PROJECT_OF_HEADING.uuid
LEFT OUTER JOIN
{TABLE_TASKTAG} TAGS ON TASK.uuid = TAGS.tasks
LEFT OUTER JOIN
{TABLE_TAG} TAG ON TAGS.tags = TAG.uuid
LEFT OUTER JOIN
{TABLE_CHECKLIST_ITEM} CHECKLIST_ITEM
ON TASK.uuid = CHECKLIST_ITEM.task
WHERE
{where_predicate}
ORDER BY
{order_predicate}
"""
# In alphabetical order from here...
def convert_isodate_sql_expression_to_thingsdate(sql_expression, null_possible=True):
"""
Return a SQL expression of an isodate converted into a "Things date".
A _Things date_ is an integer where the binary digits are
YYYYYYYYYYYMMMMDDDDD0000000; Y is year, M is month, and D is day.
For example, the ISO 8601 date '2021-03-28' corresponds to the Things
date 132464128 as integer; in binary that is:
111111001010011111000000000
YYYYYYYYYYYMMMMDDDDD0000000
2021 3 28
Parameters
----------
sql_expression : str
A sql expression evaluating to an ISO 8601 date str.
null_possible : bool
Can the input `sql_expression` evaluate to NULL?
Returns
-------
str
A sql expression representing a "Things date" as integer.
Example
-------
>>> convert_isodate_sql_expression_to_thingsdate("date('now', 'localtime')")
"(CASE WHEN date('now', 'localtime') THEN \
((strftime('%Y', date('now', 'localtime')) << 16) \
| (strftime('%m', date('now', 'localtime')) << 12) \
| (strftime('%d', date('now', 'localtime')) << 7)) \
ELSE date('now', 'localtime') END)"
>>> convert_isodate_sql_expression_to_thingsdate("'2023-05-22'")
"(CASE WHEN '2023-05-22' THEN \
((strftime('%Y', '2023-05-22') << 16) \
| (strftime('%m', '2023-05-22') << 12) \
| (strftime('%d', '2023-05-22') << 7)) \
ELSE '2023-05-22' END)"
"""
isodate = sql_expression
year = f"strftime('%Y', {isodate}) << 16"
month = f"strftime('%m', {isodate}) << 12"
day = f"strftime('%d', {isodate}) << 7"
thingsdate = f"(({year}) | ({month}) | ({day}))"
if null_possible:
# when isodate is NULL, return isodate as-is
return f"(CASE WHEN {isodate} THEN {thingsdate} ELSE {isodate} END)"
return thingsdate
def convert_thingsdate_sql_expression_to_isodate(sql_expression):
"""
Return SQL expression as string.
Parameters
----------
sql_expression : str
A sql expression pointing to a "Things date" integer in
format YYYYYYYYYYYMMMMDDDDD0000000, in binary.
See: `convert_isodate_sql_expression_to_thingsdate` for details.
Example
-------
>>> convert_thingsdate_sql_expression_to_isodate('132464128')
"CASE WHEN 132464128 THEN \
printf('%d-%02d-%02d', (132464128 & 134152192) >> 16, \
(132464128 & 61440) >> 12, (132464128 & 3968) >> 7) ELSE 132464128 END"
>>> convert_thingsdate_sql_expression_to_isodate('startDate')
"CASE WHEN startDate THEN \
printf('%d-%02d-%02d', (startDate & 134152192) >> 16, \
(startDate & 61440) >> 12, (startDate & 3968) >> 7) ELSE startDate END"
"""
y_mask = 0b111111111110000000000000000
m_mask = 0b000000000001111000000000000
d_mask = 0b000000000000000111110000000
thingsdate = sql_expression
year = f"({thingsdate} & {y_mask}) >> 16"
month = f"({thingsdate} & {m_mask}) >> 12"
day = f"({thingsdate} & {d_mask}) >> 7"
isodate = f"printf('%d-%02d-%02d', {year}, {month}, {day})"
# when thingsdate is NULL, return thingsdate as-is
return f"CASE WHEN {thingsdate} THEN {isodate} ELSE {thingsdate} END"
def dict_factory(cursor, row):
"""
Convert SQL result into a dictionary.
See also:
https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.row_factory
"""
result = {}
for index, column in enumerate(cursor.description):
key, value = column[0], row[index]
if value is None and key in COLUMNS_TO_OMIT_IF_NONE:
continue
if value and key in COLUMNS_TO_TRANSFORM_TO_BOOL:
value = bool(value)
result[key] = value
return result
def escape_string(string):
r"""
Escape SQLite string literal.
Three notes:
1. A single quote within a SQLite string can be encoded by putting
two single quotes in a row. Escapes using the backslash character
are not supported in SQLite.
2. Null characters '\0' within strings can lead to surprising
behavior. However, `cursor.execute` will already throw a `ValueError`
if it finds a null character in the query, so we let it handle
this case for us.
3. Eventually we might want to make use of parameters instead of
manually escaping. Since this will require some refactoring,
we are going with the easiest solution for now.
See: https://www.sqlite.org/lang_expr.html#literal_values_constants_
"""
return string.replace("'", "''")
def isodate_to_yyyyyyyyyyymmmmddddd(value: str):
"""
Return integer, in binary YYYYYYYYYYYMMMMDDDDD0000000.
Y is year, M is month, D is day as binary.
See also `convert_isodate_sql_expression_to_thingsdate`.
Parameters
----------
value : str
ISO 8601 date str
Example
-------
>>> isodate_to_yyyyyyyyyyymmmmddddd('2021-03-28')
132464128
"""
year, month, day = map(int, value.split("-"))
return year << 16 | month << 12 | day << 7
def list_factory(_cursor, row):
"""Convert SQL selects of one column into a list."""
return row[0]
def make_filter(column, value):
"""
Return SQL filter 'AND {column} = "{value}"'.
Special handling if `value` is `bool` or `None`.
Examples
--------
>>> make_filter('title', 'Important')
"AND title = 'Important'"
>>> make_filter('startDate', True)
'AND startDate IS NOT NULL'
>>> make_filter('startDate', False)
'AND startDate IS NULL'
>>> make_filter('title', None)
''
"""
default = f"AND {column} = '{escape_string(str(value))}'"
return {
None: "",
False: f"AND {column} IS NULL",
True: f"AND {column} IS NOT NULL",
}.get(value, default)
def make_or_filter(*filters):
"""Join filters with OR."""
filters = filter(None, filters) # type: ignore
filters = [remove_prefix(filter, "AND ") for filter in filters] # type: ignore
filters = " OR ".join(filters) # type: ignore
return f"AND ({filters})" if filters else ""
def make_search_filter(query: Optional[str]) -> str:
"""
Return a SQL filter to search tasks by a string query.
Example:
--------
>>> make_search_filter('dinner')
"AND (TASK.title LIKE '%dinner%' OR TASK.notes LIKE '%dinner%' OR \
AREA.title LIKE '%dinner%')"
"""
if not query:
return ""
query = escape_string(query)
# noqa todo 'TMChecklistItem.title'
columns = ["TASK.title", "TASK.notes", "AREA.title"]
sub_searches = (f"{column} LIKE '%{query}%'" for column in columns)
return f"AND ({' OR '.join(sub_searches)})"
def make_thingsdate_filter(date_column: str, value) -> str:
"""
Return a SQL filter for "Things date" columns.
Parameters
----------
date_column : str
Name of the column that has date information on a task
stored as an INTEGER in "Things date" format.
See `convert_isodate_sql_expression_to_thingsdate` for details
on Things dates.
value : bool, 'future', 'past', ISO 8601 date str, or None
`True` or `False` indicates whether a date is set or not.
`'future'` or `'past'` indicates a date in the future or past.
ISO 8601 date str is in the format "YYYY-MM-DD", possibly
prefixed with an operator such as ">YYYY-MM-DD",
"=YYYY-MM-DD", "<=YYYY-MM-DD", etc.
`None` indicates any value.
Returns
-------
str
A date filter for the SQL query. If `value == None`, then
return the empty string.
Examples
--------
>>> make_thingsdate_filter('startDate', True)
'AND startDate IS NOT NULL'
>>> make_thingsdate_filter('startDate', False)
'AND startDate IS NULL'
>>> make_thingsdate_filter('startDate', 'future')
"AND startDate > ((strftime('%Y', date('now', 'localtime')) << 16) \
| (strftime('%m', date('now', 'localtime')) << 12) \
| (strftime('%d', date('now', 'localtime')) << 7))"
>>> make_thingsdate_filter('deadline', '2021-03-28')
'AND deadline == 132464128'
>>> make_thingsdate_filter('deadline', '=2021-03-28')
'AND deadline = 132464128'
>>> make_thingsdate_filter('deadline', '<=2021-03-28')
'AND deadline <= 132464128'
>>> make_thingsdate_filter('deadline', None)
''
"""
if value is None:
return ""
if isinstance(value, bool):
return make_filter(date_column, value)
# Check for ISO 8601 date str + optional operator
match = match_date(value)
if match:
comparator, isodate = match.groups()
if not comparator:
comparator = "=="
thingsdate = isodate_to_yyyyyyyyyyymmmmddddd(isodate)
threshold = str(thingsdate)
else:
# "future" or "past"
validate("value", value, ["future", "past"])
threshold = convert_isodate_sql_expression_to_thingsdate(
"date('now', 'localtime')", null_possible=False
)
comparator = ">" if value == "future" else "<="
return f"AND {date_column} {comparator} {threshold}"
def make_truthy_filter(column: str, value) -> str:
"""
Return a SQL filter that matches if a column is truthy or falsy.
Truthy means TRUE. Falsy means FALSE or NULL. This is akin
to how Python defines it natively.
Passing in `value == None` returns the empty string.
Examples
--------
>>> make_truthy_filter('PROJECT.trashed', True)
'AND PROJECT.trashed'
>>> make_truthy_filter('PROJECT.trashed', False)
'AND NOT IFNULL(PROJECT.trashed, 0)'
>>> make_truthy_filter('PROJECT.trashed', None)
''
"""
if value is None:
return ""
if value:
return f"AND {column}"
return f"AND NOT IFNULL({column}, 0)"
def make_unixtime_filter(date_column: str, value) -> str:
"""
Return a SQL filter for UNIX time columns.
Parameters
----------
date_column : str
Name of the column that has datetime information on a task
stored in UNIX time, that is, number of seconds since
1970-01-01 00:00 UTC.
value : bool, 'future', 'past', ISO 8601 date str, or None
`True` or `False` indicates whether a date is set or not.
`'future'` or `'past'` indicates a date in the future or past.
ISO 8601 date str is in the format "YYYY-MM-DD", possibly
prefixed with an operator such as ">YYYY-MM-DD",
"=YYYY-MM-DD", "<=YYYY-MM-DD", etc.
`None` indicates any value.
Returns
-------
str
A date filter for the SQL query. If `value == None`, then
return the empty string.
Examples
--------
>>> make_unixtime_filter('stopDate', True)
'AND stopDate IS NOT NULL'
>>> make_unixtime_filter('stopDate', False)
'AND stopDate IS NULL'
>>> make_unixtime_filter('stopDate', 'future')
"AND date(stopDate, 'unixepoch', 'localtime') > date('now', 'localtime')"
>>> make_unixtime_filter('creationDate', '2021-03-28')
"AND date(creationDate, 'unixepoch', 'localtime') == date('2021-03-28')"
>>> make_unixtime_filter('creationDate', '=2021-03-28')
"AND date(creationDate, 'unixepoch', 'localtime') = date('2021-03-28')"
>>> make_unixtime_filter('creationDate', '<=2021-03-28')
"AND date(creationDate, 'unixepoch', 'localtime') <= date('2021-03-28')"
>>> make_unixtime_filter('creationDate', None)
''
"""
if value is None:
return ""
if isinstance(value, bool):
return make_filter(date_column, value)
# Check for ISO 8601 date str + optional operator
match = match_date(value)
if match:
comparator, isodate = match.groups()
if not comparator:
comparator = "=="
threshold = f"date('{isodate}')"
else:
# "future" or "past"
validate("value", value, ["future", "past"])