Projects/worblehat-old
Projects
/
worblehat-old
Archived
12
0
Fork 0

utilized book search, work now as list just with where clauses

This commit is contained in:
Péter Henrik Gombos 2011-10-09 18:56:19 +00:00
parent 8c660ecaee
commit b50fe04b1f
1 changed files with 13 additions and 6 deletions

View File

@ -260,9 +260,11 @@ def list_cmd(connection, what):
def search_book(connection, search_strings, search_description=False):
c = connection.cursor()
if search_description:
where_clauses = ['book.title ILIKE %s OR book.subtitle ILIKE %s OR book.series ILIKE %s OR person.lastname ILIKE %s OR person.firstname ILIKE %s OR book.description ILIKE %s']*len(search_strings)
where_clauses = ['book.title ILIKE %s OR book.subtitle ILIKE %s OR book.series ILIKE %s \
OR person.lastname ILIKE %s OR person.firstname ILIKE %s OR book.description ILIKE %s']*len(search_strings)
else:
where_clauses = ['book.title ILIKE %s OR book.subtitle ILIKE %s OR book.series ILIKE %s OR person.lastname ILIKE %s OR person.firstname ILIKE %s']*len(search_strings)
where_clauses = ['book.title ILIKE %s OR book.subtitle ILIKE %s OR book.series ILIKE %s \
OR person.lastname ILIKE %s OR person.firstname ILIKE %s']*len(search_strings)
result_list = []
for s in search_strings:
@ -273,13 +275,17 @@ def search_book(connection, search_strings, search_description=False):
for i in range(5):
result_list.append(s)
c.execute('SELECT isbn,book.id AS id,title,category, \
array_to_string(array_agg(person.lastname || \' (\' || person.id || \')\', \', \') AS persons \
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 person.id=bookperson.person \
WHERE ' + ' OR '.join(where_clauses), map(lambda s:'%' + s + '%',result_list))
WHERE ' + ' OR '.join(where_clauses) + '\
GROUP BY isbn, book.id, title, category \
', map(lambda s:'%' + s + '%',result_list))
for i in xrange(c.rowcount):
book = fetchone_dict(c)
print book['isbn'], book['title'], book['person']
print('%-13s %-10s %-60s %s' %
(book['isbn'], str_or_empty(book['id']),
cut_str(book['title'], 60), book['persons']))
def search_person(connection, search_strings):
@ -288,7 +294,8 @@ def search_person(connection, search_strings):
for s in search_strings:
for i in range(3):
result_strings.append(s)
c.execute('SELECT * FROM person LEFT JOIN bookperson ON person.id=bookperson.person WHERE person.lastname ILIKE %s or person.firstname ILIKE %s OR person.id ILIKE %s', result_strings)
c.execute('SELECT * FROM person LEFT JOIN bookperson ON person.id=bookperson.person \
WHERE person.lastname ILIKE %s or person.firstname ILIKE %s OR person.id ILIKE %s', result_strings)
for i in xrange(c.rowcount):
person = fetchone_dict(c)
print person['lastname'], ', ', person['firstname'], '\t', person['book']