Update searchdb to handle special keywords.
This commit is contained in:
parent
1609b5a135
commit
3a39685a1b
101
buku.py
101
buku.py
@ -810,50 +810,56 @@ class BukuDb:
|
|||||||
:return: search results, or None, if no matches
|
:return: search results, or None, if no matches
|
||||||
'''
|
'''
|
||||||
|
|
||||||
arguments = []
|
qry = 'SELECT id, url, metadata, tags, desc FROM bookmarks WHERE'
|
||||||
query = 'SELECT id, url, metadata, tags, desc FROM bookmarks WHERE'
|
|
||||||
# Deep query string
|
# Deep query string
|
||||||
q1 = "(tags LIKE ('%' || ? || '%') OR URL LIKE ('%' || ? || '%') OR \
|
q1 = "(tags LIKE ('%' || ? || '%') OR URL LIKE ('%' || ? || '%') OR \
|
||||||
metadata LIKE ('%' || ? || '%') OR desc LIKE ('%' || ? || '%'))"
|
metadata LIKE ('%' || ? || '%') OR desc LIKE ('%' || ? || '%'))"
|
||||||
# Non-deep query string
|
# Non-deep query string
|
||||||
q2 = '(tags REGEXP ? OR URL REGEXP ? OR metadata REGEXP ? OR desc \
|
q2 = '(tags REGEXP ? OR URL REGEXP ? OR metadata REGEXP ? OR desc \
|
||||||
REGEXP ?)'
|
REGEXP ?)'
|
||||||
|
qargs = []
|
||||||
|
|
||||||
if regex:
|
if regex:
|
||||||
for token in keywords:
|
for token in keywords:
|
||||||
query = '%s %s OR' % (query, q2)
|
qry = '%s %s OR' % (qry, q2)
|
||||||
|
|
||||||
arguments += (token, token, token, token)
|
qargs += (token, token, token, token,)
|
||||||
query = query[:-3]
|
qry = qry[:-3]
|
||||||
elif all_keywords:
|
elif all_keywords:
|
||||||
|
if len(keywords) == 1 and keywords[0] == 'blank':
|
||||||
|
qry = "SELECT * FROM bookmarks WHERE metadata = '' OR tags = ?"
|
||||||
|
qargs += (DELIM,)
|
||||||
|
elif len(keywords) == 1 and keywords[0] == 'immutable':
|
||||||
|
qry = "SELECT * FROM bookmarks WHERE flags & 1 == 1"
|
||||||
|
else:
|
||||||
for token in keywords:
|
for token in keywords:
|
||||||
if deep:
|
if deep:
|
||||||
query = '%s %s AND' % (query, q1)
|
qry = '%s %s AND' % (qry, q1)
|
||||||
else:
|
else:
|
||||||
token = '\\b' + token + '\\b'
|
token = '\\b' + token + '\\b'
|
||||||
query = '%s %s AND' % (query, q2)
|
qry = '%s %s AND' % (qry, q2)
|
||||||
|
|
||||||
arguments += (token, token, token, token)
|
qargs += (token, token, token, token,)
|
||||||
query = query[:-4]
|
qry = qry[:-4]
|
||||||
elif not all_keywords:
|
elif not all_keywords:
|
||||||
for token in keywords:
|
for token in keywords:
|
||||||
if deep:
|
if deep:
|
||||||
query = '%s %s OR' % (query, q1)
|
qry = '%s %s OR' % (qry, q1)
|
||||||
else:
|
else:
|
||||||
token = '\\b' + token + '\\b'
|
token = '\\b' + token + '\\b'
|
||||||
query = '%s %s OR' % (query, q2)
|
qry = '%s %s OR' % (qry, q2)
|
||||||
|
|
||||||
arguments += (token, token, token, token)
|
qargs += (token, token, token, token,)
|
||||||
query = query[:-3]
|
qry = qry[:-3]
|
||||||
else:
|
else:
|
||||||
logger.error('Invalid search option')
|
logger.error('Invalid search option')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
query = '%s ORDER BY id ASC' % query
|
qry = '%s ORDER BY id ASC' % qry
|
||||||
logger.debug('query: "%s", args: %s', query, arguments)
|
logger.debug('query: "%s", args: %s', qry, qargs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.cur.execute(query, arguments)
|
self.cur.execute(qry, qargs)
|
||||||
except sqlite3.OperationalError as e:
|
except sqlite3.OperationalError as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
return None
|
return None
|
||||||
@ -1013,47 +1019,14 @@ class BukuDb:
|
|||||||
print('All bookmarks deleted')
|
print('All bookmarks deleted')
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def print_bm(self, index, empty=False, immutable=False):
|
def print_bm(self, index):
|
||||||
'''Print bookmark details at index or all bookmarks if index is 0
|
'''Print bookmark details at index or all bookmarks if index is 0
|
||||||
Print only bookmarks with blank title or tag if empty is True
|
|
||||||
Note: URL is printed on top because title may be blank
|
Note: URL is printed on top because title may be blank
|
||||||
|
|
||||||
:param index: index to print (0 for all)
|
:param index: index to print, 0 prints all
|
||||||
:param empty: flag to show only bookmarks with no title or tags
|
|
||||||
:param immutable: flag to show only bookmarks with immutable titles
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
if index == 0: # Show all entries
|
if index != 0: # Show record at index
|
||||||
if empty:
|
|
||||||
qry = "SELECT * FROM bookmarks WHERE metadata = '' OR tags = ?"
|
|
||||||
self.cur.execute(qry, (DELIM,))
|
|
||||||
resultset = self.cur.fetchall()
|
|
||||||
print('\x1b[1m%s records found\x1b[21m\n' % len(resultset))
|
|
||||||
elif immutable:
|
|
||||||
qry = "SELECT * FROM bookmarks WHERE flags & 1 == 1"
|
|
||||||
self.cur.execute(qry)
|
|
||||||
resultset = self.cur.fetchall()
|
|
||||||
print('\x1b[1m%s records found\x1b[21m\n' % len(resultset))
|
|
||||||
else:
|
|
||||||
self.cur.execute('SELECT * FROM bookmarks')
|
|
||||||
resultset = self.cur.fetchall()
|
|
||||||
|
|
||||||
if not self.json:
|
|
||||||
if self.field_filter == 0:
|
|
||||||
for row in resultset:
|
|
||||||
print_record(row)
|
|
||||||
elif self.field_filter == 1:
|
|
||||||
for row in resultset:
|
|
||||||
print('%s\t%s' % (row[0], row[1]))
|
|
||||||
elif self.field_filter == 2:
|
|
||||||
for row in resultset:
|
|
||||||
print('%s\t%s\t%s' % (row[0], row[1], row[3][1:-1]))
|
|
||||||
elif self.field_filter == 3:
|
|
||||||
for row in resultset:
|
|
||||||
print('%s\t%s' % (row[0], row[2]))
|
|
||||||
else:
|
|
||||||
print(format_json(resultset, field_filter=self.field_filter))
|
|
||||||
else: # Show record at index
|
|
||||||
try:
|
try:
|
||||||
query = 'SELECT * FROM bookmarks WHERE id = ?'
|
query = 'SELECT * FROM bookmarks WHERE id = ?'
|
||||||
self.cur.execute(query, (index,))
|
self.cur.execute(query, (index,))
|
||||||
@ -1077,6 +1050,25 @@ class BukuDb:
|
|||||||
print('%s\t%s' % (row[0], row[2]))
|
print('%s\t%s' % (row[0], row[2]))
|
||||||
else:
|
else:
|
||||||
print(format_json(results, True, self.field_filter))
|
print(format_json(results, True, self.field_filter))
|
||||||
|
else: # Show all entries
|
||||||
|
self.cur.execute('SELECT * FROM bookmarks')
|
||||||
|
resultset = self.cur.fetchall()
|
||||||
|
|
||||||
|
if not self.json:
|
||||||
|
if self.field_filter == 0:
|
||||||
|
for row in resultset:
|
||||||
|
print_record(row)
|
||||||
|
elif self.field_filter == 1:
|
||||||
|
for row in resultset:
|
||||||
|
print('%s\t%s' % (row[0], row[1]))
|
||||||
|
elif self.field_filter == 2:
|
||||||
|
for row in resultset:
|
||||||
|
print('%s\t%s\t%s' % (row[0], row[1], row[3][1:-1]))
|
||||||
|
elif self.field_filter == 3:
|
||||||
|
for row in resultset:
|
||||||
|
print('%s\t%s' % (row[0], row[2]))
|
||||||
|
else:
|
||||||
|
print(format_json(resultset, field_filter=self.field_filter))
|
||||||
|
|
||||||
def get_all_tags(self):
|
def get_all_tags(self):
|
||||||
'''Get list of tags in DB
|
'''Get list of tags in DB
|
||||||
@ -2422,11 +2414,6 @@ def main():
|
|||||||
# Search URLs, titles, tags with all keywords and delete if wanted
|
# Search URLs, titles, tags with all keywords and delete if wanted
|
||||||
elif args.sall is not None:
|
elif args.sall is not None:
|
||||||
search_opted = True
|
search_opted = True
|
||||||
if args.sall[0] == 'blank' and len(args.sall) == 1:
|
|
||||||
bdb.print_bm(0, True)
|
|
||||||
elif args.sall[0] == 'immutable' and len(args.sall) == 1:
|
|
||||||
bdb.print_bm(0, False, True)
|
|
||||||
else:
|
|
||||||
search_results = bdb.searchdb(args.sall, True, args.deep)
|
search_results = bdb.searchdb(args.sall, True, args.deep)
|
||||||
|
|
||||||
# Run a regular expression search
|
# Run a regular expression search
|
||||||
|
Loading…
Reference in New Issue
Block a user