]> git.phdru.name Git - m_librarian.git/blobdiff - m_librarian/db.py
Add unicode-aware function 'lower()' to SQLite
[m_librarian.git] / m_librarian / db.py
index 9985eb96bc79e6f897f32cf01e5dc1567d6c10b8..cd2f6442582a99e60f46bbd84969f3063be053ef 100755 (executable)
@@ -2,7 +2,7 @@
 
 __all__ = ['Author', 'Book', 'Extension', 'Genre', 'Language',
            'AuthorBook', 'BookGenre',
-           'init_db', 'insert_name', 'update_counters',
+           'init_db', 'insert_name', 'insert_author', 'update_counters',
            ]
 
 import os
@@ -45,6 +45,18 @@ if not db_uri:
 sqlhub.processConnection = connection = connectionForURI(db_uri)
 
 if connection.dbName == 'sqlite':
+    def lower(s):
+        return s.lower()
+
+    sqlite = connection.module
+
+    class MLConnection(sqlite.Connection):
+        def __init__(self, *args, **kwargs):
+            super(MLConnection, self).__init__(*args, **kwargs)
+            self.create_function('lower', 1, lower)
+
+    connection._connOptions['factory'] = MLConnection
+
     # Speedup SQLite connection
     connection.query("PRAGMA synchronous=OFF")
     connection.query("PRAGMA count_changes=OFF")
@@ -53,11 +65,14 @@ if connection.dbName == 'sqlite':
 
 
 class Author(SQLObject):
-    name = UnicodeCol(unique=True)
-    count = IntCol()
+    surname = UnicodeCol(notNull=True)
+    name = UnicodeCol(notNull=True)
+    misc_name = UnicodeCol(notNull=True)
+    count = IntCol(notNull=True)
     books = RelatedJoin('Book', otherColumn='book_id',
                         intermediateTable='author_book',
                         createRelatedTable=False)
+    full_name_idx = DatabaseIndex(surname, name, misc_name, unique=True)
     count_idx = DatabaseIndex(count)
 
 
@@ -78,20 +93,21 @@ class Book(SQLObject):
     genres = RelatedJoin('Genre',
                          intermediateTable='book_genre',
                          createRelatedTable=False)
-    title = UnicodeCol()
-    series = UnicodeCol()
+    title = UnicodeCol(notNull=True)
+    series = UnicodeCol(notNull=True)
     ser_no = IntCol()
-    archive = StringCol()
-    file = StringCol()
-    size = IntCol()
-    lib_id = StringCol()
-    deleted = BoolCol()
-    extension = ForeignKey('Extension')
-    date = DateCol()
+    archive = StringCol(notNull=True)
+    file = StringCol(notNull=True)
+    size = IntCol(notNull=True)
+    lib_id = StringCol(notNull=True)
+    deleted = BoolCol(notNull=True)
+    extension = ForeignKey('Extension', notNull=True)
+    date = DateCol(notNull=True)
     language = ForeignKey('Language')
     title_idx = DatabaseIndex(title)
     series_idx = DatabaseIndex(series)
     ser_no_idx = DatabaseIndex(ser_no)
+    archive_idx = DatabaseIndex(archive)
     archive_file_idx = DatabaseIndex(archive, file, unique=True)
     file_idx = DatabaseIndex(file)
     size_idx = DatabaseIndex(size)
@@ -112,15 +128,15 @@ class BookGenre(SQLObject):
 
 
 class Extension(SQLObject):
-    name = StringCol(unique=True)
-    count = IntCol()
+    name = StringCol(notNull=True, unique=True)
+    count = IntCol(notNull=True)
     count_idx = DatabaseIndex(count)
 
 
 class Genre(SQLObject):
-    name = StringCol(unique=True)
-    title = UnicodeCol()
-    count = IntCol()
+    name = StringCol(notNull=True, unique=True)
+    title = UnicodeCol(notNull=True)
+    count = IntCol(notNull=True)
     books = RelatedJoin('Book', otherColumn='book_id',
                         intermediateTable='book_genre',
                         createRelatedTable=False)
@@ -129,8 +145,8 @@ class Genre(SQLObject):
 
 
 class Language(SQLObject):
-    name = StringCol(unique=True)
-    count = IntCol()
+    name = StringCol(notNull=True, unique=True)
+    count = IntCol(notNull=True)
     count_idx = DatabaseIndex(count)
 
 
@@ -154,15 +170,23 @@ def insert_name(table, name, **kw):
         return table(name=name, count=0, **kw)
 
 
+def insert_author(surname, name, misc_name):
+    try:
+        return Author.full_name_idx.get(
+            surname=surname, name=name, misc_name=misc_name)
+    except SQLObjectNotFound:
+        return Author(surname=surname, name=name, misc_name=misc_name, count=0)
+
+
 def update_counters():
     for author in Author.select():
-        author.count = len(author.books)
+        author.count = AuthorBook.select(AuthorBook.q.author == author).count()
 
     for ext in Extension.select():
         ext.count = Book.select(Book.q.extension == ext.name).count()
 
     for genre in Genre.select():
-        genre.count = len(genre.books)
+        genre.count = BookGenre.select(BookGenre.q.genre == genre).count()
 
     for language in Language.select():
         language.count = Book.select(Book.q.language == language.name).count()