Add API get_max_id(), -p -1 shows max index

This commit is contained in:
Arun Prakash Jana 2017-03-06 02:26:40 +05:30
parent a532a78a1f
commit 2650bd74e9
No known key found for this signature in database
GPG Key ID: A75979F35C080412
3 changed files with 38 additions and 16 deletions

View File

@ -198,6 +198,7 @@ POWER TOYS:
-m, --merge file add bookmarks from another buku DB file -m, --merge file add bookmarks from another buku DB file
-p, --print [...] show record details by indices, ranges -p, --print [...] show record details by indices, ranges
print all bookmarks, if no arguments print all bookmarks, if no arguments
-1 shows the bookmark with highest index
-f, --format N limit fields in -p or Json search output -f, --format N limit fields in -p or Json search output
N=1: URL, N=2: URL and tag, N=3: title N=1: URL, N=2: URL and tag, N=3: title
-r, --replace oldtag [newtag ...] -r, --replace oldtag [newtag ...]

2
buku.1
View File

@ -175,7 +175,7 @@ is considered Markdown (compliant with --export format) if it has '.md' extensio
Add bookmarks from another Buku database file. Add bookmarks from another Buku database file.
.TP .TP
.BI \-p " " \--print " [...]" .BI \-p " " \--print " [...]"
Show details (DB index, URL, title, tags and comment) of bookmark record by DB index. If no arguments, all records with actual index from DB are shown. Accepts hyphenated ranges and space-separated indices. Show details (DB index, URL, title, tags and comment) of bookmark record by DB index. If no arguments, all records with actual index from DB are shown. Accepts hyphenated ranges and space-separated indices. Special index -1 (introduced for convenience) shows the details of the bookmark with the highest index.
.TP .TP
.BI \-f " " \--format " N" .BI \-f " " \--format " N"
Show selective monochrome output with specific fields. Works with --print. Search results honour the option when used along with --json. Useful for creating batch scripts. Show selective monochrome output with specific fields. Works with --print. Search results honour the option when used along with --json. Useful for creating batch scripts.

51
buku.py
View File

@ -472,6 +472,19 @@ class BukuDb:
return -1 return -1
def get_max_id(self):
'''Fetch the ID of the last record
:return: ID if any record exists, else -1
'''
self.cur.execute('SELECT MAX(id) from bookmarks')
resultset = self.cur.fetchall()
if resultset[0][0] is None:
return -1
return resultset[0][0]
def add_rec(self, url, title_in=None, tags_in=None, desc=None, immutable=0, def add_rec(self, url, title_in=None, tags_in=None, desc=None, immutable=0,
delay_commit=False): delay_commit=False):
'''Add a new bookmark '''Add a new bookmark
@ -989,10 +1002,9 @@ class BukuDb:
:param delay_commit: do not commit to DB, caller's responsibility :param delay_commit: do not commit to DB, caller's responsibility
''' '''
self.cur.execute('SELECT MAX(id) from bookmarks')
results = self.cur.fetchall()
# Return if the last index left in DB was just deleted # Return if the last index left in DB was just deleted
if results[0][0] is None: max_id = self.get_max_id()
if max_id == -1:
return return
query1 = ('SELECT id, URL, metadata, tags, desc FROM bookmarks ' query1 = ('SELECT id, URL, metadata, tags, desc FROM bookmarks '
@ -1001,18 +1013,17 @@ class BukuDb:
query3 = ('INSERT INTO bookmarks(id, URL, metadata, tags, desc) ' query3 = ('INSERT INTO bookmarks(id, URL, metadata, tags, desc) '
'VALUES (?, ?, ?, ?, ?)') 'VALUES (?, ?, ?, ?, ?)')
for row in results: if max_id > index:
if row[0] > index: self.cur.execute(query1, (max_id,))
self.cur.execute(query1, (row[0],)) results = self.cur.fetchall()
results = self.cur.fetchall() for row in results:
for row in results: self.cur.execute(query2, (row[0],))
self.cur.execute(query2, (row[0],)) self.cur.execute(query3,
self.cur.execute(query3, (index, row[1], row[2], row[3], row[4],))
(index, row[1], row[2], row[3], row[4],)) if not delay_commit:
if not delay_commit: self.conn.commit()
self.conn.commit() if self.chatty:
if self.chatty: print('Index %d moved to %d' % (row[0], index))
print('Index %d moved to %d' % (row[0], index))
def delete_rec(self, index, low=0, high=0, is_range=False, def delete_rec(self, index, low=0, high=0, is_range=False,
delay_commit=False): delay_commit=False):
@ -1137,6 +1148,13 @@ class BukuDb:
''' '''
if index != 0: # Show record at index if index != 0: # Show record at index
# Show the last record if -1 is passed
if index == -1:
index = self.get_max_id()
if index == -1:
logerr('Empty database')
return
try: try:
query = 'SELECT * FROM bookmarks WHERE id = ? LIMIT 1' query = 'SELECT * FROM bookmarks WHERE id = ? LIMIT 1'
self.cur.execute(query, (index,)) self.cur.execute(query, (index,))
@ -2553,6 +2571,7 @@ POSITIONAL ARGUMENTS:
-m, --merge file add bookmarks from another buku DB file -m, --merge file add bookmarks from another buku DB file
-p, --print [...] show record details by indices, ranges -p, --print [...] show record details by indices, ranges
print all bookmarks, if no arguments print all bookmarks, if no arguments
-1 shows the bookmark with highest index
-f, --format N limit fields in -p or Json search output -f, --format N limit fields in -p or Json search output
N=1: URL, N=2: URL and tag, N=3: title N=1: URL, N=2: URL and tag, N=3: title
-r, --replace oldtag [newtag ...] -r, --replace oldtag [newtag ...]
@ -2907,6 +2926,8 @@ POSITIONAL ARGUMENTS:
lower, upper = upper, lower lower, upper = upper, lower
for _id in range(lower, upper + 1): for _id in range(lower, upper + 1):
bdb.print_rec(_id) bdb.print_rec(_id)
elif idx == '-1':
bdb.print_rec(idx)
else: else:
logerr('Invalid index or range to print') logerr('Invalid index or range to print')
bdb.close_quit(1) bdb.close_quit(1)