From 6df582db2a7c9095fdf6824a1812e8bf5ae73635 Mon Sep 17 00:00:00 2001
From: Arun Prakash Jana <engineerarun@gmail.com>
Date: Sun, 8 Nov 2015 23:14:35 +0530
Subject: [PATCH] Better search.

The input keywords, combined as a single tag, are used to match against tags for each bookmark entry. Partial matches (but in same sequence) are accepted.
The same input keywords are also used to search title metadata. Even if a single keyword is found in URL or title metadata the bookmark is shown in results.

Signed-off-by: Arun Prakash Jana <engineerarun@gmail.com>
---
 markit | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/markit b/markit
index 787c71b..dc80f4d 100755
--- a/markit
+++ b/markit
@@ -48,7 +48,7 @@ def usage():
     print("  -i N                       add entry at index N, works with -a, use to fill deleted index")
     print("  -o                         fetch title info from web, works with -a or -u")
     print("  -p                         print all bookmarks, shows real index from database")
-    print("  -s string(s)               search all bookmarks for a (partial) tag or keyword")
+    print("  -s keyword(s)              search all bookmarks for a (partial) tag or keywords")
     print("  -u N                       update entry at index N (from output of -p)")
     print("                             you can either add or update or delete in one instance")
     print("                             any other input shows help and exits markit\n")
@@ -186,18 +186,23 @@ def AddUpdateEntry(conn, cur, keywords, entry):
 
 def searchdb(cur, keywords):
     searchtag = ''
-    searchkey = keywords[0]
     for token in keywords:
         searchtag += token + " "
+    searchtag = searchtag[0:-1]
 
-    searchtag = ',' + searchtag[0:-1] + ','
+    arguments = []
+    arguments.append(searchtag)
+    placeholder = "'%' || ? || '%'"
+    query = "SELECT url, metadata FROM bookmarks WHERE tags LIKE (%s)" % placeholder
+    for token in keywords:
+        query += " OR URL LIKE (%s) OR metadata LIKE (%s)" % (placeholder, placeholder)
+        arguments.append(token)
+        arguments.append(token)
+    print("%s, (%s)" % (query, arguments))
 
     count = 0
     results = []
-    for row in cur.execute("SELECT url, metadata FROM bookmarks WHERE tags LIKE ('%' || ? || '%') \
-                            OR tags LIKE ('%' || ? || '%') \
-                            OR URL LIKE ('%' || ? || '%') \
-                            OR metadata LIKE ('%' || ? || '%')", (searchtag, searchkey, searchkey, searchkey,)):
+    for row in cur.execute(query, arguments):
         results.append(row[0])
         count += 1
         print("\x1B[1m\x1B[93m%d. \x1B[0m\x1B[92m%s\x1B[0m\n\t%s" % (count, row[0], row[1]))