Improved list book command.
Wrote a better query, so I could remove much ugliness from the Python code. Added trimming of book titles that are too long.
This commit is contained in:
parent
6f840f1073
commit
066d3a4364
|
@ -17,11 +17,12 @@ from util import *
|
||||||
# print fetchall_dict(c)
|
# print fetchall_dict(c)
|
||||||
|
|
||||||
q_list_books = \
|
q_list_books = \
|
||||||
'SELECT isbn, book.id AS id, title, category, person.id AS person_id, ' \
|
'SELECT isbn, book.id AS id, title, category, ' \
|
||||||
' lastname, firstname ' \
|
'array_to_string(array_agg(person.lastname || \' (\' || person.id || \')\'), \', \') AS persons ' \
|
||||||
'FROM book ' \
|
'FROM book ' \
|
||||||
'LEFT JOIN bookperson ON book.isbn=bookperson.book ' \
|
'LEFT JOIN bookperson ON book.isbn=bookperson.book ' \
|
||||||
'LEFT JOIN person ON bookperson.person=person.id'
|
'LEFT JOIN person ON bookperson.person=person.id ' \
|
||||||
|
'GROUP BY isbn, book.id, title, category'
|
||||||
q_list_persons = \
|
q_list_persons = \
|
||||||
'SELECT person.id, person.firstname, person.lastname, ' \
|
'SELECT person.id, person.firstname, person.lastname, ' \
|
||||||
' COUNT(bookperson.id) AS num_books ' \
|
' COUNT(bookperson.id) AS num_books ' \
|
||||||
|
@ -223,20 +224,11 @@ def list_books(connection):
|
||||||
c = connection.cursor()
|
c = connection.cursor()
|
||||||
c.execute(q_list_books)
|
c.execute(q_list_books)
|
||||||
#print fetchall_dict(c)
|
#print fetchall_dict(c)
|
||||||
s = ''
|
|
||||||
last_isbn = None
|
|
||||||
for i in xrange(c.rowcount):
|
for i in xrange(c.rowcount):
|
||||||
d = fetchone_dict(c)
|
book = fetchone_dict(c)
|
||||||
if d['isbn'] != last_isbn:
|
print('%-13s %-10s %-60s %s' %
|
||||||
if last_isbn != None:
|
(book['isbn'], str_or_empty(book['id']),
|
||||||
s += '\n'
|
cut_str(book['title'], 60), book['persons']))
|
||||||
s += '%-13s %-10s %-40s' % (d['isbn'], str_or_empty(d['id']), d['title'])
|
|
||||||
if d['person_id']:
|
|
||||||
s += ' ' + d['person_id']
|
|
||||||
else:
|
|
||||||
s += ', ' + d['person_id']
|
|
||||||
last_isbn = d['isbn']
|
|
||||||
print s
|
|
||||||
|
|
||||||
def list_persons(connection):
|
def list_persons(connection):
|
||||||
c = connection.cursor()
|
c = connection.cursor()
|
||||||
|
|
Reference in New Issue