Optimize tag delete and replace APIs.

This commit is contained in:
Arun Prakash Jana 2016-11-28 00:10:14 +05:30
parent 8bbc199709
commit c9f8b124b1
3 changed files with 24 additions and 38 deletions

View File

@ -18,7 +18,7 @@ With tagging and multiple options to search bookmarks, including regex and a dee
Though a terminal utility, it's possible to add bookmarks to `buku` without touching the terminal! Refer to the section on [GUI integration](#gui-integration). If you prefer the terminal, thanks to the shell completion scripts, you don't need to memorize any of the options. There's an Easter egg to revisit random forgotten bookmarks too.
`buku` doesn't track you - no history, obsolete records, usage analytics or homing.
*Buku* is too busy to track you - no history, obsolete records, usage analytics or homing.
<br>
<p align="center">

2
buku.1
View File

@ -5,7 +5,7 @@ buku \- A powerful command-line bookmark manager. Your mini web!
.B buku [OPTIONS] [KEYWORD [KEYWORD ...]]
.SH DESCRIPTION
.B buku
is a command-line tool to save, tag and search bookmarks.
is a command-line tool to store, tag, search and manage bookmarks.
.PP
.B Features
.PP

58
buku.py
View File

@ -594,27 +594,22 @@ class BukuDb:
tags_to_delete = tags_in.strip(DELIM).split(DELIM)
if index == 0:
resp = input('Delete specified tags from ALL bookmarks? (y/n): ')
resp = input('Delete specified tag(s) from ALL bookmarks? (y/n): ')
if resp != 'y':
return False
query1 = "SELECT id, tags FROM bookmarks WHERE tags \
LIKE '%' || ? || '%' ORDER BY id ASC"
query2 = 'UPDATE bookmarks SET tags = ? WHERE id = ?'
count = 0
match = "'%' || ? || '%'"
for tag in tags_to_delete:
self.cur.execute(query1, (DELIM + tag + DELIM,))
resultset = self.cur.fetchall()
q = "UPDATE bookmarks SET tags = replace(tags, '%s%s%s', '%s')\
WHERE tags LIKE %s" % (DELIM, tag, DELIM, DELIM, match)
self.cur.execute(q, (DELIM + tag + DELIM,))
count += self.cur.rowcount
for row in resultset:
tags = row[1]
tags = tags.replace('%s%s%s' % (DELIM, tag, DELIM,), DELIM)
self.cur.execute(query2, (parse_tags([tags]), row[0],))
if self.chatty:
self.print_bm(row[0])
if len(resultset):
self.conn.commit()
if count:
self.conn.commit()
if self.chatty:
print('%d records updated' % count)
else:
query = 'SELECT id, tags FROM bookmarks WHERE id = ?'
self.cur.execute(query, (index,))
@ -1104,51 +1099,42 @@ class BukuDb:
return unique_tags, dic
def replace_tag(self, orig, new=None):
'''Replace orig tags with new tags in DB for all records.
Remove orig tag if new tag is empty.
'''Replace original tag by new tags in all records.
Remove original tag if new tag is empty.
:param orig: original tags
:param new: replacement tags
:param orig: original tag as string
:param new: replacement tags as list
:return: True on success, False on failure
'''
update = False
delete = False
newtags = DELIM
orig = '%s%s%s' % (DELIM, orig, DELIM)
if new is None:
delete = True
else:
if new is not None:
newtags = parse_tags(new)
if newtags == DELIM:
delete = True
if orig == newtags:
print('Tags are same.')
return False
if newtags == DELIM:
return self.delete_tag_at_index(0, orig)
query = 'SELECT id, tags FROM bookmarks WHERE tags LIKE ?'
self.cur.execute(query, ('%' + orig + '%',))
results = self.cur.fetchall()
query = 'UPDATE bookmarks SET tags = ? WHERE id = ?'
for row in results:
if not delete:
# Check if tag newtags is already added
if row[1].find(newtags) >= 0:
newtags = DELIM
tags = row[1].replace(orig, newtags)
tags = parse_tags([tags])
self.cur.execute(query, (tags, row[0],))
print('Index %d updated' % row[0])
update = True
if update:
if len(results):
self.conn.commit()
return update
return True
def browse_by_index(self, index):
'''Open URL at index in browser
@ -2513,7 +2499,7 @@ def main():
# Replace a tag in DB
if args.replace is not None:
if len(args.replace) == 1:
bdb.replace_tag(args.replace[0])
bdb.delete_tag_at_index(0, args.replace[0])
else:
bdb.replace_tag(args.replace[0], args.replace[1:])