3 __all__ = ['Author', 'Book', 'Extension', 'Genre', 'Language',
4 'AuthorBook', 'BookGenre',
5 'init_db', 'insert_name', 'insert_author', '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':
51 sqlite = connection.module
53 class MLConnection(sqlite.Connection):
54 def __init__(self, *args, **kwargs):
55 super(MLConnection, self).__init__(*args, **kwargs)
56 self.create_function('lower', 1, lower)
58 connection._connOptions['factory'] = MLConnection
60 # Speedup SQLite connection
61 connection.query("PRAGMA synchronous=OFF")
62 connection.query("PRAGMA count_changes=OFF")
63 connection.query("PRAGMA journal_mode=MEMORY")
64 connection.query("PRAGMA temp_store=MEMORY")
67 class Author(SQLObject):
68 surname = UnicodeCol(notNull=True)
69 name = UnicodeCol(notNull=True)
70 misc_name = UnicodeCol(notNull=True)
71 count = IntCol(notNull=True)
72 books = RelatedJoin('Book', otherColumn='book_id',
73 intermediateTable='author_book',
74 createRelatedTable=False)
75 full_name_idx = DatabaseIndex(surname, name, misc_name, unique=True)
76 count_idx = DatabaseIndex(count)
79 class AuthorBook(SQLObject):
82 author = ForeignKey('Author', notNull=True, cascade=True)
83 book = ForeignKey('Book', notNull=True, cascade=True)
84 author_idx = DatabaseIndex(author)
85 book_idx = DatabaseIndex(book)
86 main_idx = DatabaseIndex(author, book, unique=True)
89 class Book(SQLObject):
90 authors = RelatedJoin('Author',
91 intermediateTable='author_book',
92 createRelatedTable=False)
93 genres = RelatedJoin('Genre',
94 intermediateTable='book_genre',
95 createRelatedTable=False)
96 title = UnicodeCol(notNull=True)
97 series = UnicodeCol(notNull=True)
99 archive = StringCol(notNull=True)
100 file = StringCol(notNull=True)
101 size = IntCol(notNull=True)
102 lib_id = StringCol(notNull=True)
103 deleted = BoolCol(notNull=True)
104 extension = ForeignKey('Extension', notNull=True)
105 date = DateCol(notNull=True)
106 language = ForeignKey('Language')
107 title_idx = DatabaseIndex(title)
108 series_idx = DatabaseIndex(series)
109 ser_no_idx = DatabaseIndex(ser_no)
110 archive_idx = DatabaseIndex(archive)
111 archive_file_idx = DatabaseIndex(archive, file, unique=True)
112 file_idx = DatabaseIndex(file)
113 size_idx = DatabaseIndex(size)
114 deleted_idx = DatabaseIndex(deleted)
115 extension_idx = DatabaseIndex(extension)
116 date_idx = DatabaseIndex(date)
117 language_idx = DatabaseIndex(language)
120 class BookGenre(SQLObject):
123 book = ForeignKey('Book', notNull=True, cascade=True)
124 genre = ForeignKey('Genre', notNull=True, cascade=True)
125 book_idx = DatabaseIndex(book)
126 genre_idx = DatabaseIndex(genre)
127 main_idx = DatabaseIndex(book, genre, unique=True)
130 class Extension(SQLObject):
131 name = StringCol(notNull=True, unique=True)
132 count = IntCol(notNull=True)
133 count_idx = DatabaseIndex(count)
136 class Genre(SQLObject):
137 name = StringCol(notNull=True, unique=True)
138 title = UnicodeCol(notNull=True)
139 count = IntCol(notNull=True)
140 books = RelatedJoin('Book', otherColumn='book_id',
141 intermediateTable='book_genre',
142 createRelatedTable=False)
143 title_idx = DatabaseIndex(title)
144 count_idx = DatabaseIndex(count)
147 class Language(SQLObject):
148 name = StringCol(notNull=True, unique=True)
149 count = IntCol(notNull=True)
150 count_idx = DatabaseIndex(count)
156 except IndexError: # Table exists but is empty
158 except dberrors.Error:
159 for table in Author, Extension, Genre, Language, Book, \
160 AuthorBook, BookGenre:
166 def insert_name(table, name, **kw):
168 return table.byName(name)
169 except SQLObjectNotFound:
170 return table(name=name, count=0, **kw)
173 def insert_author(surname, name, misc_name):
175 return Author.full_name_idx.get(
176 surname=surname, name=name, misc_name=misc_name)
177 except SQLObjectNotFound:
178 return Author(surname=surname, name=name, misc_name=misc_name, count=0)
181 def update_counters():
182 for author in Author.select():
183 author.count = AuthorBook.select(AuthorBook.q.author == author).count()
185 for ext in Extension.select():
186 ext.count = Book.select(Book.q.extension == ext.name).count()
188 for genre in Genre.select():
189 genre.count = BookGenre.select(BookGenre.q.genre == genre).count()
191 for language in Language.select():
192 language.count = Book.select(Book.q.language == language.name).count()
196 print "DB dirs:", db_dirs
198 print "DB URI:", db_uri
200 if __name__ == '__main__':