3 __all__ = ['Author', 'Book', 'Extension', 'Genre', 'Language',
4 'AuthorBook', 'BookGenre',
5 'init_db', 'insert_name', 'update_counters',
9 from sqlobject import SQLObject, StringCol, UnicodeCol, IntCol, BoolCol, \
10 ForeignKey, DateCol, DatabaseIndex, RelatedJoin, \
11 connectionForURI, sqlhub, SQLObjectNotFound, dberrors
12 from .config import ml_conf
15 db_uri = ml_conf.get('database', 'URI')
21 if 'XDG_CACHE_HOME' in os.environ:
22 db_dirs.append(os.environ['XDG_CACHE_HOME'])
23 home_cache = os.path.expanduser('~/.cache')
24 if home_cache not in db_dirs:
25 db_dirs.append(home_cache)
28 db_file = os.path.join(d, 'm_librarian.sqlite')
29 if os.path.exists(db_file):
33 # make the database file/directory readable/writeable only by the user
38 except OSError: # Perhaps already exists
40 db_file = os.path.join(db_dir, 'm_librarian.sqlite')
42 db_uri = 'sqlite://%s' % db_file.replace(os.sep, '/')
45 sqlhub.processConnection = connection = connectionForURI(db_uri)
47 if connection.dbName == 'sqlite':
48 # Speedup SQLite connection
49 connection.query("PRAGMA synchronous=OFF")
50 connection.query("PRAGMA count_changes=OFF")
51 connection.query("PRAGMA journal_mode=MEMORY")
52 connection.query("PRAGMA temp_store=MEMORY")
55 class Author(SQLObject):
56 name = UnicodeCol(unique=True)
58 count_idx = DatabaseIndex('count')
59 books = RelatedJoin('Book', otherColumn='book_id',
60 intermediateTable='author_book',
61 createRelatedTable=False)
64 class AuthorBook(SQLObject):
67 author = ForeignKey('Author', notNull=True, cascade=True)
68 book = ForeignKey('Book', notNull=True, cascade=True)
69 main_idx = DatabaseIndex(author, book, unique=True)
72 class Book(SQLObject):
73 authors = RelatedJoin('Author',
74 intermediateTable='author_book',
75 createRelatedTable=False)
76 genres = RelatedJoin('Genre',
77 intermediateTable='book_genre',
78 createRelatedTable=False)
87 extension = ForeignKey('Extension')
89 language = ForeignKey('Language')
90 archive_file_idx = DatabaseIndex('archive', 'file', unique=True)
93 class BookGenre(SQLObject):
96 book = ForeignKey('Book', notNull=True, cascade=True)
97 genre = ForeignKey('Genre', notNull=True, cascade=True)
98 main_idx = DatabaseIndex(book, genre, unique=True)
101 class Extension(SQLObject):
102 name = StringCol(unique=True)
104 count_idx = DatabaseIndex('count')
107 class Genre(SQLObject):
108 name = StringCol(unique=True)
111 count_idx = DatabaseIndex('count')
112 books = RelatedJoin('Book', otherColumn='book_id',
113 intermediateTable='book_genre',
114 createRelatedTable=False)
117 class Language(SQLObject):
118 name = StringCol(unique=True)
120 count_idx = DatabaseIndex('count')
126 except IndexError: # Table exists but is empty
128 except dberrors.Error:
129 for table in Author, Extension, Genre, Language, Book, \
130 AuthorBook, BookGenre:
136 def insert_name(table, name, **kw):
138 return table.byName(name)
139 except SQLObjectNotFound:
140 return table(name=name, count=0, **kw)
143 def update_counters():
144 for author in Author.select():
145 author.count = len(author.books)
147 for ext in Extension.select():
148 ext.count = Book.select(Book.q.extension == ext.name).count()
150 for genre in Genre.select():
151 genre.count = len(genre.books)
153 for language in Language.select():
154 language.count = Book.select(Book.q.language == language.name).count()
158 print "DB dirs:", db_dirs
160 print "DB URI:", db_uri
162 if __name__ == '__main__':