~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/library/database.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730124941-qjdsmri25zt8zocn
Tags: 0.6.3+dfsg-0ubuntu1
* New upstream release. Please see http://calibre.kovidgoyal.net/new_in_6/
  for the list of new features and changes.
* remove_postinstall.patch: Update for new version.
* build_debug.patch: Does not apply any more, disable for now. Might not be
  necessary any more.
* debian/copyright: Fix reference to versionless GPL.
* debian/rules: Drop obsolete dh_desktop call.
* debian/rules: Add workaround for weird Python 2.6 setuptools behaviour of
  putting compiled .so files into src/calibre/plugins/calibre/plugins
  instead of src/calibre/plugins.
* debian/rules: Drop hal fdi moving, new upstream version does not use hal
  any more. Drop hal dependency, too.
* debian/rules: Install udev rules into /lib/udev/rules.d.
* Add debian/calibre.preinst: Remove unmodified
  /etc/udev/rules.d/95-calibre.rules on upgrade.
* debian/control: Bump Python dependencies to 2.6, since upstream needs
  it now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 
10
10
from calibre.ebooks.metadata import MetaInformation
11
11
from calibre.web.feeds.recipes import migrate_automatic_profile_to_automatic_recipe
 
12
from calibre.ebooks.metadata import string_to_authors
12
13
 
13
14
class Concatenate(object):
14
15
    '''String concatenation aggregator for sqlite'''
27
28
            return self.ans[:-len(self.sep)]
28
29
        return self.ans
29
30
class Connection(sqlite.Connection):
30
 
    
 
31
 
31
32
    def get(self, *args, **kw):
32
33
        ans = self.execute(*args)
33
34
        if not kw.get('all', True):
97
98
            obj = conn.execute('INSERT INTO books(title, timestamp, author_sort) VALUES (?,?,?)',
98
99
                               (book['title'], book['timestamp'], authors))
99
100
            id = obj.lastrowid
100
 
            authors = authors.split('&')
 
101
            authors = string_to_authors(authors)
101
102
            for a in authors:
102
103
                author = conn.execute('SELECT id from authors WHERE name=?', (a,)).fetchone()
103
104
                if author:
785
786
    FROM books;
786
787
''')
787
788
        conn.execute('pragma user_version=12')
788
 
        conn.commit()    
789
 
        
 
789
        conn.commit()
 
790
 
790
791
    def __init__(self, dbpath, row_factory=False):
791
792
        self.dbpath = dbpath
792
793
        self.conn = _connect(dbpath)
814
815
#        _lock_file = None
815
816
        self.conn.close()
816
817
 
817
 
    @apply
818
 
    def user_version():
 
818
    @dynamic_property
 
819
    def user_version(self):
819
820
        doc = 'The user version of this database'
820
821
        def fget(self):
821
822
            return self.conn.get('pragma user_version;', all=False)
901
902
 
902
903
    def id(self, index):
903
904
        return self.data[index][0]
904
 
    
 
905
 
905
906
    def row(self, id):
906
907
        for r, record in enumerate(self.data):
907
908
            if record[0] == id:
916
917
            return _('Unknown')
917
918
 
918
919
    def authors(self, index, index_is_id=False):
919
 
        ''' 
920
 
        Authors as a comma separated list or None. 
 
920
        '''
 
921
        Authors as a comma separated list or None.
921
922
        In the comma separated list, commas in author names are replaced by | symbols
922
923
        '''
923
924
        if not index_is_id:
927
928
        except:
928
929
            pass
929
930
 
 
931
    def author_id(self, index, index_is_id=False):
 
932
        id  = index if index_is_id else self.id(index)
 
933
        return self.conn.get('SELECT author from books_authors_link WHERE book=?', (id,), all=False)
 
934
 
930
935
    def isbn(self, idx, index_is_id=False):
931
936
        id = idx if index_is_id else self.id(idx)
932
937
        return self.conn.get('SELECT isbn FROM books WHERE id=?',(id,), all=False)
939
944
        if index_is_id:
940
945
            return self.conn.get('SELECT publisher FROM meta WHERE id=?', (index,), all=False)
941
946
        return self.data[index][3]
942
 
    
 
947
 
943
948
    def publisher_id(self, index, index_is_id=False):
944
949
        id  = index if index_is_id else self.id(index)
945
950
        return self.conn.get('SELECT publisher from books_publishers_link WHERE book=?', (id,), all=False)
946
 
    
 
951
 
947
952
    def rating(self, index, index_is_id=False):
948
953
        if index_is_id:
949
954
            return self.conn.get('SELECT rating FROM meta WHERE id=?', (index,), all=False)
983
988
    def series(self, index, index_is_id=False):
984
989
        id = self.series_id(index, index_is_id)
985
990
        return self.conn.get('SELECT name from series WHERE id=?', (id,), all=False)
986
 
        
 
991
 
987
992
    def series_index(self, index, index_is_id=False):
988
993
        ans = None
989
994
        if not index_is_id:
991
996
        else:
992
997
            ans = self.conn.get('SELECT series_index FROM books WHERE id=?', (index,), all=False)
993
998
        try:
994
 
            return int(ans)
 
999
            return float(ans)
995
1000
        except:
996
 
            return 1
 
1001
            return 1.0
997
1002
 
998
1003
    def books_in_series(self, series_id):
999
1004
        '''
1010
1015
 
1011
1016
    def books_in_series_of(self, index, index_is_id=False):
1012
1017
        '''
1013
 
        Return an ordered list of all books in the series that the book indetified by index belongs to.
 
1018
        Return an ordered list of all books in the series that the book identified by index belongs to.
1014
1019
        If the book does not belong to a series return an empty list. The list contains book ids.
1015
1020
        '''
1016
1021
        series_id = self.series_id(index, index_is_id=index_is_id)
1021
1026
        '''Comments as string or None'''
1022
1027
        id = index if index_is_id else self.id(index)
1023
1028
        return self.conn.get('SELECT text FROM comments WHERE book=?', (id,), all=False)
1024
 
        
 
1029
 
1025
1030
    def formats(self, index, index_is_id=False):
1026
1031
        ''' Return available formats as a comma separated list '''
1027
1032
        id = index if index_is_id else self.id(index)
1041
1046
    def all_series(self):
1042
1047
        return [ (i[0], i[1]) for i in \
1043
1048
                self.conn.get('SELECT id, name FROM series')]
1044
 
        
 
1049
 
1045
1050
    def all_authors(self):
1046
1051
        return [ (i[0], i[1]) for i in \
1047
1052
                self.conn.get('SELECT id, name FROM authors')]
1048
 
    
 
1053
 
1049
1054
    def all_publishers(self):
1050
1055
        return [ (i[0], i[1]) for i in \
1051
1056
                self.conn.get('SELECT id, name FROM publishers')]
1103
1108
                item[col] = val
1104
1109
                break
1105
1110
        if column == 'authors':
1106
 
            val = val.split('&,')
 
1111
            val = string_to_authors(val)
1107
1112
            self.set_authors(id, val)
1108
1113
        elif column == 'title':
1109
1114
            self.set_title(id, val)
1266
1271
                mi.authors = ['Unknown']
1267
1272
        authors = []
1268
1273
        for a in mi.authors:
1269
 
            authors += a.split('&')
 
1274
            authors += string_to_authors(a)
1270
1275
        self.set_authors(id, authors)
1271
1276
        if mi.author_sort:
1272
1277
            self.set_author_sort(id, mi.author_sort)
1278
1283
            self.set_series(id, mi.series)
1279
1284
        if mi.cover_data[1] is not None:
1280
1285
            self.set_cover(id, mi.cover_data[1])
1281
 
    
1282
 
        
1283
 
            
 
1286
 
 
1287
 
 
1288
 
1284
1289
 
1285
1290
    def add_books(self, paths, formats, metadata, uris=[], add_duplicates=True):
1286
1291
        '''
1385
1390
    def all_ids(self):
1386
1391
        return [i[0] for i in self.conn.get('SELECT id FROM books')]
1387
1392
 
1388
 
                         
1389
 
 
1390
 
 
1391
 
    
 
1393
 
 
1394
 
 
1395
 
 
1396
 
1392
1397
 
1393
1398
    def has_id(self, id):
1394
1399
        return self.conn.get('SELECT id FROM books where id=?', (id,), all=False) is not None
1395
1400
 
1396
 
    
1397
 
    
 
1401
 
 
1402
 
1398
1403
 
1399
1404
class SearchToken(object):
1400
1405