Squash merge feature/fts into master

This commit is contained in:
2025-04-23 14:51:24 +02:00
parent 5ea62a1216
commit 9d39cb09df
3 changed files with 496 additions and 43 deletions

View File

@@ -33,6 +33,29 @@ CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY NOT NULL,
value TEXT NOT NULL
);
-- Create virtual FTS5 table for searching notes content
CREATE VIRTUAL TABLE IF NOT EXISTS notes_fts USING fts5(
content,
date,
content='notes',
content_rowid='id'
);
-- Trigger to keep FTS table in sync with notes table when inserting
CREATE TRIGGER IF NOT EXISTS notes_ai AFTER INSERT ON notes BEGIN
INSERT INTO notes_fts(rowid, content, date) VALUES (new.id, new.content, new.date);
END;
-- Trigger to keep FTS table in sync when deleting notes
CREATE TRIGGER IF NOT EXISTS notes_ad AFTER DELETE ON notes BEGIN
DELETE FROM notes_fts WHERE rowid = old.id;
END;
-- Trigger to keep FTS table in sync when updating notes
CREATE TRIGGER IF NOT EXISTS notes_au AFTER UPDATE ON notes BEGIN
UPDATE notes_fts SET content = new.content, date = new.date WHERE rowid = old.id;
END;
''';
static Future<String> _getDatabasePath() async {
@@ -44,7 +67,7 @@ CREATE TABLE IF NOT EXISTS settings (
if (home == null) {
throw Exception('Could not find home directory');
}
debugPrint('Home directory found: home');
debugPrint('Home directory found: $home');
final dbDir = Directory(path.join(home, settingsDir));
if (!await dbDir.exists()) {
@@ -58,7 +81,7 @@ CREATE TABLE IF NOT EXISTS settings (
} else {
// Default path for other platforms
final databasesPath = await databaseFactoryFfi.getDatabasesPath();
debugPrint('Using default databases path: databasesPath');
debugPrint('Using default databases path: $databasesPath');
return path.join(databasesPath, dbFileName);
}
}
@@ -68,7 +91,7 @@ CREATE TABLE IF NOT EXISTS settings (
sqfliteFfiInit();
final dbPath = await _getDatabasePath();
debugPrint('Database path: dbPath');
debugPrint('Database path: $dbPath');
try {
db = await databaseFactoryFfi.openDatabase(
@@ -84,7 +107,7 @@ CREATE TABLE IF NOT EXISTS settings (
);
debugPrint('Database opened and initialized');
} catch (e) {
debugPrint('Failed to initialize database: e');
debugPrint('Failed to initialize database: $e');
rethrow;
}
}
@@ -104,11 +127,73 @@ CREATE TABLE IF NOT EXISTS settings (
}
static Future<void> setSetting(String key, String value) async {
await db.insert(
'settings',
{'key': key, 'value': value},
conflictAlgorithm: ConflictAlgorithm.replace,
);
await db.insert('settings', {
'key': key,
'value': value,
}, conflictAlgorithm: ConflictAlgorithm.replace);
debugPrint("Setting updated: $key = $value");
}
// Search notes using FTS
static Future<List<Map<String, dynamic>>> searchNotes(String query) async {
try {
if (query.trim().isEmpty) {
return [];
}
// Process the query for partial word matching
// Split into individual terms, filter empty ones
List<String> terms =
query
.trim()
.split(RegExp(r'\s+'))
.where((term) => term.isNotEmpty)
.toList();
if (terms.isEmpty) {
return [];
}
// Add wildcards to each term for prefix matching (e.g., "fuck*" will match "fucked")
// Join terms with AND for all-term matching (results must contain ALL terms)
String ftsQuery = terms
.map((term) {
// Remove any special characters that might break the query
String sanitizedTerm = term.replaceAll(RegExp(r'[^\w]'), '');
if (sanitizedTerm.isEmpty) return '';
// Add wildcard for stemming/prefix matching
return '$sanitizedTerm*';
})
.where((term) => term.isNotEmpty)
.join(' AND ');
if (ftsQuery.isEmpty) {
debugPrint('Query was sanitized to empty string');
return [];
}
debugPrint('FTS query: "$ftsQuery"');
// Execute the FTS query with AND logic
final List<Map<String, dynamic>> results = await db.rawQuery(
'''
SELECT n.id, n.date, n.content, snippet(notes_fts, 0, '<b>', '</b>', '...', 20) as snippet
FROM notes_fts
JOIN notes n ON notes_fts.rowid = n.id
WHERE notes_fts MATCH ?
ORDER BY n.date DESC
LIMIT 100
''',
[ftsQuery],
);
debugPrint('Search query "$ftsQuery" returned ${results.length} results');
return results;
} catch (e) {
debugPrint('Search failed: $e');
// Return empty results rather than crashing on malformed queries
return [];
}
}
}