~ubuntu-branches/debian/sid/calibre/sid

« back to all changes in this revision

Viewing changes to src/calibre/db/write.py

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-05-14 18:17:50 UTC
  • mto: This revision was merged to the branch mainline in revision 75.
  • Revision ID: package-import@ubuntu.com-20140514181750-efj1wymey2vb4cao
Tags: upstream-1.36.0+dfsg
ImportĀ upstreamĀ versionĀ 1.36.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
94
94
            x = True
95
95
        elif x == 'false':
96
96
            x = False
97
 
        elif x == 'none':
 
97
        elif x == 'none' or x == '':
98
98
            x = None
99
99
        else:
100
100
            x = bool(int(x))
174
174
    'Set a one-one field in the books table'
175
175
    if book_id_val_map:
176
176
        sequence = ((sqlite_datetime(v), k) for k, v in book_id_val_map.iteritems())
177
 
        db.conn.executemany(
 
177
        db.executemany(
178
178
            'UPDATE books SET %s=? WHERE id=?'%field.metadata['column'], sequence)
179
179
        field.table.book_col_map.update(book_id_val_map)
180
180
    return set(book_id_val_map)
194
194
    'Set a one-one field in the non-books table, like comments'
195
195
    deleted = tuple((k,) for k, v in book_id_val_map.iteritems() if v is None)
196
196
    if deleted:
197
 
        db.conn.executemany('DELETE FROM %s WHERE book=?'%field.metadata['table'],
 
197
        db.executemany('DELETE FROM %s WHERE book=?'%field.metadata['table'],
198
198
                        deleted)
199
199
        for book_id in deleted:
200
200
            field.table.book_col_map.pop(book_id[0], None)
201
201
    updated = {k:v for k, v in book_id_val_map.iteritems() if v is not None}
202
202
    if updated:
203
 
        db.conn.executemany('INSERT OR REPLACE INTO %s(book,%s) VALUES (?,?)'%(
 
203
        db.executemany('INSERT OR REPLACE INTO %s(book,%s) VALUES (?,?)'%(
204
204
            field.metadata['table'], field.metadata['column']),
205
205
            ((k, sqlite_datetime(v)) for k, v in updated.iteritems()))
206
206
        field.table.book_col_map.update(updated)
217
217
            sequence.append((sidx, book_id, ids[0]))
218
218
        field.table.book_col_map[book_id] = sidx
219
219
    if sequence:
220
 
        db.conn.executemany('UPDATE %s SET %s=? WHERE book=? AND value=?'%(
 
220
        db.executemany('UPDATE %s SET %s=? WHERE book=? AND value=?'%(
221
221
                field.metadata['table'], field.metadata['column']), sequence)
222
222
    return {s[1] for s in sequence}
223
223
# }}}
239
239
    if item_id is None:
240
240
        if is_authors:
241
241
            aus = author_to_author_sort(val)
242
 
            db.conn.execute('INSERT INTO authors(name,sort) VALUES (?,?)',
 
242
            db.execute('INSERT INTO authors(name,sort) VALUES (?,?)',
243
243
                            (val.replace(',', '|'), aus))
244
244
        else:
245
 
            db.conn.execute('INSERT INTO %s(%s) VALUES (?)'%(
 
245
            db.execute('INSERT INTO %s(%s) VALUES (?)'%(
246
246
                m['table'], m['column']), (val,))
247
 
        item_id = rid_map[kval] = db.conn.last_insert_rowid()
 
247
        item_id = rid_map[kval] = db.last_insert_rowid()
248
248
        table.id_map[item_id] = val
249
249
        table.col_book_map[item_id] = set()
250
250
        if is_authors:
260
260
                case_changes.iteritems())
261
261
    else:
262
262
        vals = ((val, item_id) for item_id, val in case_changes.iteritems())
263
 
    db.conn.executemany(
 
263
    db.executemany(
264
264
        'UPDATE %s SET %s=? WHERE id=?'%(m['table'], m['column']), vals)
265
265
    for item_id, val in case_changes.iteritems():
266
266
        table.id_map[item_id] = val
316
316
 
317
317
    # Update the db link table
318
318
    if deleted:
319
 
        db.conn.executemany('DELETE FROM %s WHERE book=?'%table.link_table,
 
319
        db.executemany('DELETE FROM %s WHERE book=?'%table.link_table,
320
320
                            ((k,) for k in deleted))
321
321
    if updated:
322
322
        sql = (
324
324
            if is_custom_series else
325
325
            'DELETE FROM {0} WHERE book=?; INSERT INTO {0}(book,{1}) VALUES(?, ?)'
326
326
        )
327
 
        db.conn.executemany(sql.format(table.link_table, m['link_column']),
 
327
        db.executemany(sql.format(table.link_table, m['link_column']),
328
328
            ((book_id, book_id, item_id) for book_id, item_id in
329
329
                    updated.iteritems()))
330
330
 
332
332
    remove = {item_id for item_id in table.id_map if not
333
333
              table.col_book_map.get(item_id, False)}
334
334
    if remove:
335
 
        db.conn.executemany('DELETE FROM %s WHERE id=?'%m['table'],
 
335
        db.executemany('DELETE FROM %s WHERE id=?'%m['table'],
336
336
            ((item_id,) for item_id in remove))
337
337
        for item_id in remove:
338
338
            del table.id_map[item_id]
413
413
 
414
414
    # Update the db link table
415
415
    if deleted:
416
 
        db.conn.executemany('DELETE FROM %s WHERE book=?'%table.link_table,
 
416
        db.executemany('DELETE FROM %s WHERE book=?'%table.link_table,
417
417
                            ((k,) for k in deleted))
418
418
    if updated:
419
419
        vals = (
420
420
            (book_id, val) for book_id, vals in updated.iteritems()
421
421
            for val in vals
422
422
        )
423
 
        db.conn.executemany('DELETE FROM %s WHERE book=?'%table.link_table,
 
423
        db.executemany('DELETE FROM %s WHERE book=?'%table.link_table,
424
424
                            ((k,) for k in updated))
425
 
        db.conn.executemany('INSERT INTO {0}(book,{1}) VALUES(?, ?)'.format(
 
425
        db.executemany('INSERT INTO {0}(book,{1}) VALUES(?, ?)'.format(
426
426
            table.link_table, m['link_column']), vals)
427
427
        if is_authors:
428
428
            aus_map = {book_id:field.author_sort_for_book(book_id) for book_id
433
433
    remove = {item_id for item_id in table.id_map if not
434
434
              table.col_book_map.get(item_id, False)}
435
435
    if remove:
436
 
        db.conn.executemany('DELETE FROM %s WHERE id=?'%m['table'],
 
436
        db.executemany('DELETE FROM %s WHERE id=?'%m['table'],
437
437
            ((item_id,) for item_id in remove))
438
438
        for item_id in remove:
439
439
            del table.id_map[item_id]
463
463
                table.col_book_map[key] = set()
464
464
            table.col_book_map[key].add(book_id)
465
465
            updates.add((book_id, key, val))
466
 
    db.conn.executemany('DELETE FROM identifiers WHERE book=?',
 
466
    db.executemany('DELETE FROM identifiers WHERE book=?',
467
467
                        ((x,) for x in book_id_val_map))
468
468
    if updates:
469
 
        db.conn.executemany('INSERT OR REPLACE INTO identifiers (book, type, val) VALUES (?, ?, ?)',
 
469
        db.executemany('INSERT OR REPLACE INTO identifiers (book, type, val) VALUES (?, ?, ?)',
470
470
                            tuple(updates))
471
471
    return set(book_id_val_map)
472
472
# }}}