utilized book search, work now as list just with where clauses
This commit is contained in:
parent
8c660ecaee
commit
b50fe04b1f
|
@ -260,9 +260,11 @@ def list_cmd(connection, what):
|
||||||
def search_book(connection, search_strings, search_description=False):
|
def search_book(connection, search_strings, search_description=False):
|
||||||
c = connection.cursor()
|
c = connection.cursor()
|
||||||
if search_description:
|
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:
|
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 = []
|
result_list = []
|
||||||
for s in search_strings:
|
for s in search_strings:
|
||||||
|
@ -273,13 +275,17 @@ def search_book(connection, search_strings, search_description=False):
|
||||||
for i in range(5):
|
for i in range(5):
|
||||||
result_list.append(s)
|
result_list.append(s)
|
||||||
c.execute('SELECT isbn,book.id AS id,title,category, \
|
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 \
|
FROM book LEFT JOIN bookperson ON book.isbn=bookperson.book \
|
||||||
LEFT JOIN person ON person.id=bookperson.person \
|
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):
|
for i in xrange(c.rowcount):
|
||||||
book = fetchone_dict(c)
|
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):
|
def search_person(connection, search_strings):
|
||||||
|
@ -288,7 +294,8 @@ def search_person(connection, search_strings):
|
||||||
for s in search_strings:
|
for s in search_strings:
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
result_strings.append(s)
|
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):
|
for i in xrange(c.rowcount):
|
||||||
person = fetchone_dict(c)
|
person = fetchone_dict(c)
|
||||||
print person['lastname'], ', ', person['firstname'], '\t', person['book']
|
print person['lastname'], ', ', person['firstname'], '\t', person['book']
|
||||||
|
|
Reference in New Issue