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. 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> <br>
<p align="center"> <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 ...]] .B buku [OPTIONS] [KEYWORD [KEYWORD ...]]
.SH DESCRIPTION .SH DESCRIPTION
.B buku .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 .PP
.B Features .B Features
.PP .PP

58
buku.py
View File

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