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:
Øystein Ingmar Skartsæterhagen 2011-10-09 13:45:15 +00:00
parent 6f840f1073
commit 066d3a4364

View File

@ -17,11 +17,12 @@ from util import *
# print fetchall_dict(c)
q_list_books = \
'SELECT isbn, book.id AS id, title, category, person.id AS person_id, ' \
' lastname, firstname ' \
'SELECT isbn, book.id AS id, title, category, ' \
'array_to_string(array_agg(person.lastname || \' (\' || person.id || \')\'), \', \') AS persons ' \
'FROM 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 = \
'SELECT person.id, person.firstname, person.lastname, ' \
' COUNT(bookperson.id) AS num_books ' \
@ -223,20 +224,11 @@ def list_books(connection):
c = connection.cursor()
c.execute(q_list_books)
#print fetchall_dict(c)
s = ''
last_isbn = None
for i in xrange(c.rowcount):
d = fetchone_dict(c)
if d['isbn'] != last_isbn:
if last_isbn != None:
s += '\n'
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
book = fetchone_dict(c)
print('%-13s %-10s %-60s %s' %
(book['isbn'], str_or_empty(book['id']),
cut_str(book['title'], 60), book['persons']))
def list_persons(connection):
c = connection.cursor()