diff --git a/cli/worblehat.py b/cli/worblehat.py index c46a9ba..978df01 100644 --- a/cli/worblehat.py +++ b/cli/worblehat.py @@ -48,6 +48,40 @@ q_books_for_category = \ 'FROM book ' \ 'WHERE category=%(id)s' +q_new_person = \ + 'INSERT INTO person (id, lastname, firstname) ' \ + 'VALUES (%(id)s, %(lastname)s, %(firstname)s' +q_edit_person = \ + 'UPDATE person ' \ + 'SET lastname=%(lastname)s, firstname=%(firstname)s ' \ + 'WHERE id=%(id)s' +q_new_book = \ + 'INSERT INTO book ' \ + ' (isbn, id, title, subtitle, category, publisher, ' \ + ' published_year, edition, pages, series, description) ' \ + 'VALUES (%(isbn)s, %(id)s, %(title)s, %(subtitle)s, %(category)s, %(publisher)s, ' \ + ' %(published_year)d, %(edition)s, %(pages)d, %(series)s, %(description)s)' +q_edit_book = \ + 'UPDATE book ' \ + 'SET isbn=%(isbn)s, id=%(id)s, title=%(title)s, ' \ + ' subtitle=%(subtitle)s, category=%(category)s, ' \ + ' publisher=%(publisher)s, published_year=%(published_year)s, ' \ + ' edition=%(edition)s, pages=%(pages)s, series=%(series)s, ' \ + ' description=%(description)s) ' \ + 'WHERE isbn=%(isbn)s' +q_remove_bookpersons = \ + 'DELETE FROM bookperson WHERE book=%(isbn)s' +q_add_bookperson = \ + 'INSERT INTO bookperson (book, person, relation) ' \ + 'VALUES (%(isbn)s, %(person_id)s, %(relation)s)' +q_new_category = \ + 'INSERT INTO category (id, name) ' \ + 'VALUES (%(id)s, %(name)s)' +q_edit_category = \ + 'UPDATE category ' \ + 'SET name=%(name)s ' \ + 'WHERE id=%(id)s' + def connect_to_db(): connection = pgdb.connect(database='oysteini_pbb2', user='oysteini_pbb', @@ -150,6 +184,10 @@ def show_category(cat): def show(connection, ids, commit_format=False, tmp_file=False): objects = map(lambda id: get_by_id(connection, id), ids) for i in range(len(ids)): + if not objects[i]: + objects[i] = 'No object with id %s.\n' % ids[i] + continue + typ = objects[i]['type'] if typ == 'book': persons = get_persons_for_book(connection, objects[i]['isbn']) @@ -161,9 +199,7 @@ def show(connection, ids, commit_format=False, tmp_file=False): elif typ == 'category': books = get_books_for_category(connection, objects[i]['id']) objects[i]['books'] = books - if not objects[i]: - objects[i] = 'No object with id %s.\n' % ids[i] - elif commit_format: + if commit_format: objects[i]['action'] = 'edit-%s' % typ else: show_funs = {'book': show_book, @@ -235,8 +271,50 @@ def search_book(connection, search_strings, search_description=False): def search_person(connection, search_strings): pass +def do_action(connection, action): + c = connection.cursor() + queries = {'new-person': q_new_person, + 'edit-person': q_edit_person, + 'new-book': q_new_book, + 'edit-book': q_edit_book, + 'new-category': q_new_category, + 'edit-category': q_edit_category} + action_type = action['action'] + c.execute(queries[action_type], action) + if action_type in ['new-book', 'edit-book']: + c.execute(q_remove_bookpersons, action['isbn']) + for (relation, personlist) in action['persons']: + for person in personlist: + c.execute(q_add_bookperson, + {'isbn': action['isbn'], + 'person_id': person, + 'relation': relation}) + +def commit_actions(connection, actions): + for action in actions: + try: + do_action(connection, action) + except pgdb.DatabaseError, err: + print>>sys.stderr, 'Error in "%s" action: %s' % (action['action'], err) + print>>sys.stderr, 'Giving up.' + return + connection.commit() + def commit(connection, filename=None): - pass + if filename: + try: + f = file(filename, 'r') + text = f.read() + f.close() + except IOError, e: + print 'commit: Error reading file %s: %s' % (filename, e) + print 'Exiting.' + sys.exit(1) + else: + text = sys.stdin.read() + + actions = read_actionlist(text) + commit_actions(connection, actions) def edit(connection, ids): pass