diff --git a/yomi_base/japanese2/dictionary.py b/yomi_base/japanese2/dictionary.py index 176baaa..9e083e6 100644 --- a/yomi_base/japanese2/dictionary.py +++ b/yomi_base/japanese2/dictionary.py @@ -28,19 +28,17 @@ class Dictionary: def findTerm(self, word, partial=False): + self.requireIndex('Terms', 'expression') + self.requireIndex('Terms', 'reading') + cursor = self.db.cursor() - - if not self.hasIndex('TermIndex'): - cursor.execute('CREATE INDEX TermIndex ON Terms(expression, reading)') - self.db.commit() - cursor.execute('SELECT * FROM Terms WHERE expression {0} ? OR reading=?'.format('LIKE' if partial else '='), (word, word)) return cursor.fetchall() def findCharacter(self, character): cursor = self.db.cursor() - cursor.execute('SELECT * FROM Kanji WHERE character=?', character) + cursor.execute('SELECT * FROM Kanji WHERE character=? LIMIT 1', character) return cursor.fetchone() @@ -60,7 +58,7 @@ class Dictionary: def findRadicalsByCharacter(self, character): cursor = self.db.cursor() - cursor.execute('SELECT radicals FROM Radicals WHERE character=?', character) + cursor.execute('SELECT radicals FROM Radicals WHERE character=? LIMIT 1', character) columns = cursor.fetchone() if columns is None: @@ -80,6 +78,18 @@ class Dictionary: return map(operator.itemgetter(0), columns) + def requireIndex(self, table, column): + name = 'index_{0}_{1}'.format(table, column) + if not self.hasIndex(name): + self.buildIndex(name, table, column) + + + def buildIndex(self, name, table, column): + cursor = self.db.cursor() + cursor.execute('CREATE INDEX {0} ON {1}({2})'.format(name, table, column)) + self.db.commit() + + def hasIndex(self, name): if name in self.indices: return True