-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdatabase.py
More file actions
344 lines (290 loc) Β· 11.1 KB
/
database.py
File metadata and controls
344 lines (290 loc) Β· 11.1 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
"""SQLite storage for notes, appointments and reminders."""
import json
import os
import sqlite3
import threading
from datetime import datetime, timedelta
_DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "writher.db")
_lock = threading.Lock()
def _conn() -> sqlite3.Connection:
c = sqlite3.connect(_DB_PATH, check_same_thread=False)
c.row_factory = sqlite3.Row
c.execute("PRAGMA journal_mode=WAL")
return c
def init():
"""Create tables if they don't exist and run safe migrations."""
with _lock:
c = _conn()
c.executescript("""
CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL DEFAULT '',
content TEXT NOT NULL DEFAULT '',
category TEXT NOT NULL DEFAULT 'general',
note_type TEXT NOT NULL DEFAULT 'text',
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS appointments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
dt TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
notified INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS reminders (
id INTEGER PRIMARY KEY AUTOINCREMENT,
message TEXT NOT NULL,
remind_at TEXT NOT NULL,
notified INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL
);
""")
# Safe migration: add notified column to appointments if missing
cols = [row[1] for row in c.execute("PRAGMA table_info(appointments)")]
if "notified" not in cols:
c.execute("ALTER TABLE appointments ADD COLUMN notified INTEGER NOT NULL DEFAULT 0")
# Settings key/value store
c.execute("""
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
)
""")
c.commit()
c.close()
def _now() -> str:
return datetime.now().isoformat(timespec="seconds")
# ββ Notes βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def save_note(content: str, category: str = "general", title: str = "") -> int:
"""Save a free-text note. Returns the new note id."""
now = _now()
with _lock:
c = _conn()
cur = c.execute(
"INSERT INTO notes (title,content,category,note_type,created_at,updated_at)"
" VALUES (?,?,?,?,?,?)",
(title, content, category, "text", now, now),
)
c.commit()
nid = cur.lastrowid
c.close()
return nid
def save_list(title: str, items: list[str], category: str = "general") -> int:
"""Save a list note (shopping, todo β¦). Returns the new note id."""
now = _now()
data = json.dumps([{"item": it, "checked": False} for it in items],
ensure_ascii=False)
with _lock:
c = _conn()
cur = c.execute(
"INSERT INTO notes (title,content,category,note_type,created_at,updated_at)"
" VALUES (?,?,?,?,?,?)",
(title, data, category, "list", now, now),
)
c.commit()
nid = cur.lastrowid
c.close()
return nid
def add_to_list(note_id: int, items: list[str]) -> bool:
"""Append items to an existing list note."""
with _lock:
c = _conn()
row = c.execute("SELECT content, note_type FROM notes WHERE id=?",
(note_id,)).fetchone()
if not row or row["note_type"] != "list":
c.close()
return False
current = json.loads(row["content"])
current.extend({"item": it, "checked": False} for it in items)
c.execute("UPDATE notes SET content=?, updated_at=? WHERE id=?",
(json.dumps(current, ensure_ascii=False), _now(), note_id))
c.commit()
c.close()
return True
def check_item(note_id: int, item_text: str) -> bool:
"""Toggle checked state for an item in a list note (fuzzy match)."""
with _lock:
c = _conn()
row = c.execute("SELECT content, note_type FROM notes WHERE id=?",
(note_id,)).fetchone()
if not row or row["note_type"] != "list":
c.close()
return False
current = json.loads(row["content"])
target = item_text.strip().lower()
found = False
for entry in current:
if entry["item"].strip().lower() == target:
entry["checked"] = not entry["checked"]
found = True
break
if found:
c.execute("UPDATE notes SET content=?, updated_at=? WHERE id=?",
(json.dumps(current, ensure_ascii=False), _now(), note_id))
c.commit()
c.close()
return found
def find_list_by_title(title: str) -> dict | None:
"""Find a list note by fuzzy title match. Returns dict or None."""
with _lock:
c = _conn()
rows = c.execute(
"SELECT * FROM notes WHERE note_type='list' ORDER BY updated_at DESC"
).fetchall()
c.close()
target = title.strip().lower()
for r in rows:
if target in r["title"].strip().lower():
return dict(r)
return None
def get_all_notes() -> list[dict]:
with _lock:
c = _conn()
rows = c.execute("SELECT * FROM notes ORDER BY updated_at DESC").fetchall()
c.close()
return [dict(r) for r in rows]
def delete_note(note_id: int):
with _lock:
c = _conn()
c.execute("DELETE FROM notes WHERE id=?", (note_id,))
c.commit()
c.close()
# ββ Appointments ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def create_appointment(title: str, dt: str, description: str = "") -> int:
"""Create a calendar appointment. *dt* is ISO datetime string."""
with _lock:
c = _conn()
cur = c.execute(
"INSERT INTO appointments (title,dt,description,created_at)"
" VALUES (?,?,?,?)",
(title, dt, description, _now()),
)
c.commit()
aid = cur.lastrowid
c.close()
return aid
def get_appointments(from_dt: str | None = None, to_dt: str | None = None) -> list[dict]:
"""Return appointments optionally filtered by date range."""
with _lock:
c = _conn()
q = "SELECT * FROM appointments"
params: list = []
clauses = []
if from_dt:
clauses.append("dt >= ?")
params.append(from_dt)
if to_dt:
clauses.append("dt <= ?")
params.append(to_dt)
if clauses:
q += " WHERE " + " AND ".join(clauses)
q += " ORDER BY dt ASC"
rows = c.execute(q, params).fetchall()
c.close()
return [dict(r) for r in rows]
def delete_appointment(aid: int):
with _lock:
c = _conn()
c.execute("DELETE FROM appointments WHERE id=?", (aid,))
c.commit()
c.close()
def get_upcoming_appointments(within_minutes: int) -> list[dict]:
"""Return appointments due within *within_minutes* that haven't been notified yet."""
now = datetime.now()
cutoff = (now + timedelta(minutes=within_minutes)).isoformat(timespec="seconds")
now_str = now.isoformat(timespec="seconds")
with _lock:
c = _conn()
rows = c.execute(
"SELECT * FROM appointments WHERE notified=0 AND dt<=? AND dt>=?"
" ORDER BY dt ASC",
(cutoff, now_str),
).fetchall()
c.close()
return [dict(r) for r in rows]
def get_past_unnotified_appointments() -> list[dict]:
"""Return appointments whose time has passed but were never notified."""
now_str = datetime.now().isoformat(timespec="seconds")
with _lock:
c = _conn()
rows = c.execute(
"SELECT * FROM appointments WHERE notified=0 AND dt<=?"
" ORDER BY dt ASC",
(now_str,),
).fetchall()
c.close()
return [dict(r) for r in rows]
def mark_appointment_notified(aid: int):
with _lock:
c = _conn()
c.execute("UPDATE appointments SET notified=1 WHERE id=?", (aid,))
c.commit()
c.close()
# ββ Reminders βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def set_reminder(message: str, remind_at: str) -> int:
"""Create a reminder. *remind_at* is ISO datetime string."""
with _lock:
c = _conn()
cur = c.execute(
"INSERT INTO reminders (message,remind_at,notified,created_at)"
" VALUES (?,?,0,?)",
(message, remind_at, _now()),
)
c.commit()
rid = cur.lastrowid
c.close()
return rid
def get_pending_reminders() -> list[dict]:
"""Return reminders that are due and not yet notified."""
now = _now()
with _lock:
c = _conn()
rows = c.execute(
"SELECT * FROM reminders WHERE notified=0 AND remind_at<=?"
" ORDER BY remind_at ASC", (now,)
).fetchall()
c.close()
return [dict(r) for r in rows]
def mark_reminder_notified(rid: int):
with _lock:
c = _conn()
c.execute("UPDATE reminders SET notified=1 WHERE id=?", (rid,))
c.commit()
c.close()
def get_all_reminders(include_notified: bool = False) -> list[dict]:
with _lock:
c = _conn()
q = "SELECT * FROM reminders"
if not include_notified:
q += " WHERE notified=0"
q += " ORDER BY remind_at ASC"
rows = c.execute(q).fetchall()
c.close()
return [dict(r) for r in rows]
def delete_reminder(rid: int):
with _lock:
c = _conn()
c.execute("DELETE FROM reminders WHERE id=?", (rid,))
c.commit()
c.close()
# ββ Settings ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def get_setting(key: str, default: str = "") -> str:
"""Return a setting value, or *default* if not found."""
with _lock:
c = _conn()
row = c.execute("SELECT value FROM settings WHERE key=?", (key,)).fetchone()
c.close()
return row["value"] if row else default
def save_setting(key: str, value: str):
"""Insert or update a setting."""
with _lock:
c = _conn()
c.execute(
"INSERT INTO settings (key, value) VALUES (?, ?)"
" ON CONFLICT(key) DO UPDATE SET value=excluded.value",
(key, value),
)
c.commit()
c.close()