3 from __future__ import print_function
5 from sqlobject import SQLObject, StringCol, UnicodeCol, IntCol, BoolCol, \
6 ForeignKey, DateCol, DatabaseIndex, RelatedJoin, \
7 connectionForURI, sqlhub, SQLObjectNotFound, dberrors
8 from .config import get_config
10 __all__ = ['Author', 'Book', 'Extension', 'Genre', 'Language',
11 'AuthorBook', 'BookGenre', 'open_db', 'init_db',
12 'insert_name', 'insert_author', 'update_counters',
16 class Author(SQLObject):
17 surname = UnicodeCol(notNull=True)
18 name = UnicodeCol(notNull=True)
19 misc_name = UnicodeCol(notNull=True)
20 count = IntCol(notNull=True)
21 books = RelatedJoin('Book', otherColumn='book_id',
22 intermediateTable='author_book',
23 createRelatedTable=False)
27 fullnames = filter(None, (self.surname, self.name, self.misc_name))
28 return ' '.join(fullnames)
30 full_name_idx = DatabaseIndex(surname, name, misc_name, unique=True)
31 count_idx = DatabaseIndex(count)
34 class AuthorBook(SQLObject):
37 author = ForeignKey('Author', notNull=True, cascade=True)
38 book = ForeignKey('Book', notNull=True, cascade=True)
39 author_idx = DatabaseIndex(author)
40 book_idx = DatabaseIndex(book)
41 main_idx = DatabaseIndex(author, book, unique=True)
44 class Book(SQLObject):
45 authors = RelatedJoin('Author',
46 intermediateTable='author_book',
47 createRelatedTable=False)
48 genres = RelatedJoin('Genre',
49 intermediateTable='book_genre',
50 createRelatedTable=False)
51 title = UnicodeCol(notNull=True)
52 series = UnicodeCol(notNull=True)
54 archive = StringCol(notNull=True)
55 file = StringCol(notNull=True)
56 size = IntCol(notNull=True)
57 lib_id = StringCol(notNull=True)
58 deleted = BoolCol(notNull=True)
59 extension = ForeignKey('Extension', notNull=True)
60 date = DateCol(notNull=True)
61 language = ForeignKey('Language')
62 title_idx = DatabaseIndex(title)
63 series_idx = DatabaseIndex(series)
64 ser_no_idx = DatabaseIndex(ser_no)
65 archive_idx = DatabaseIndex(archive)
66 archive_file_idx = DatabaseIndex(archive, file, unique=True)
67 file_idx = DatabaseIndex(file)
68 size_idx = DatabaseIndex(size)
69 deleted_idx = DatabaseIndex(deleted)
70 extension_idx = DatabaseIndex(extension)
71 date_idx = DatabaseIndex(date)
72 language_idx = DatabaseIndex(language)
75 class BookGenre(SQLObject):
78 book = ForeignKey('Book', notNull=True, cascade=True)
79 genre = ForeignKey('Genre', notNull=True, cascade=True)
80 book_idx = DatabaseIndex(book)
81 genre_idx = DatabaseIndex(genre)
82 main_idx = DatabaseIndex(book, genre, unique=True)
85 class Extension(SQLObject):
86 name = UnicodeCol(notNull=True, unique=True)
87 count = IntCol(notNull=True)
88 count_idx = DatabaseIndex(count)
91 class Genre(SQLObject):
92 name = UnicodeCol(notNull=True, unique=True)
93 title = UnicodeCol(notNull=True)
94 count = IntCol(notNull=True)
95 books = RelatedJoin('Book', otherColumn='book_id',
96 intermediateTable='book_genre',
97 createRelatedTable=False)
98 title_idx = DatabaseIndex(title)
99 count_idx = DatabaseIndex(count)
102 class Language(SQLObject):
103 name = UnicodeCol(notNull=True, unique=True)
104 count = IntCol(notNull=True)
105 count_idx = DatabaseIndex(count)
108 def _find_sqlite_db_dirs_posix():
110 if 'XDG_CACHE_HOME' in os.environ:
111 db_dirs.append(os.environ['XDG_CACHE_HOME'])
112 home_cache = os.path.expanduser('~/.cache')
113 if home_cache not in db_dirs:
114 db_dirs.append(home_cache)
118 def find_sqlite_db_dirs():
119 if os.name == 'posix':
120 return _find_sqlite_db_dirs_posix()
121 raise OSError("Unknow OS")
124 def find_sqlite_dburi(db_dirs=None):
126 db_dirs = find_sqlite_db_dirs()
128 db_file = os.path.join(d, 'm_librarian.sqlite')
129 if os.path.exists(db_file):
133 # make the database file/directory readable/writeable only by the user
138 except OSError: # Perhaps already exists
140 db_file = os.path.join(db_dir, 'm_librarian.sqlite')
142 return 'sqlite://%s' % db_file.replace(os.sep, '/')
145 def open_db(db_uri=None):
148 db_uri = get_config().get('database', 'URI')
150 db_uri = find_sqlite_dburi()
152 if '://' not in db_uri:
153 db_uri = 'sqlite://' + os.path.abspath(db_uri).replace(os.sep, '/')
155 sqlhub.processConnection = connection = connectionForURI(db_uri)
157 if connection.dbName == 'sqlite':
159 if isinstance(s, basestring):
163 sqlite = connection.module
165 class MLConnection(sqlite.Connection):
166 def __init__(self, *args, **kwargs):
167 super(MLConnection, self).__init__(*args, **kwargs)
168 self.create_function('lower', 1, lower)
170 # This hack must be done at the very beginning, before the first query
171 connection._connOptions['factory'] = MLConnection
173 # Speedup SQLite connection
174 connection.query("PRAGMA synchronous=OFF")
175 connection.query("PRAGMA count_changes=OFF")
176 connection.query("PRAGMA journal_mode=MEMORY")
177 connection.query("PRAGMA temp_store=MEMORY")
183 except IndexError: # Table exists but is empty
185 except dberrors.Error:
186 for table in Author, Extension, Genre, Language, Book, \
187 AuthorBook, BookGenre:
193 def insert_name(table, name, **kw):
195 return table.byName(name)
196 except SQLObjectNotFound:
197 return table(name=name, count=0, **kw)
200 def insert_author(surname, name, misc_name):
202 return Author.full_name_idx.get(
203 surname=surname, name=name, misc_name=misc_name)
204 except SQLObjectNotFound:
205 return Author(surname=surname, name=name, misc_name=misc_name, count=0)
208 def update_counters():
209 for author in Author.select():
210 author.count = AuthorBook.select(AuthorBook.q.author == author).count()
212 for ext in Extension.select():
213 ext.count = Book.select(Book.q.extension == ext.id).count()
215 for genre in Genre.select():
216 genre.count = BookGenre.select(BookGenre.q.genre == genre).count()
218 for language in Language.select():
219 language.count = Book.select(Book.q.language == language.id).count()
223 db_dirs = find_sqlite_db_dirs()
224 print("DB dirs:", db_dirs)
225 print("DB URI:", find_sqlite_dburi())
227 if __name__ == '__main__':