Skip to content

Commit

Permalink
src/dict/expression: add dictionary id to all queries
Browse files Browse the repository at this point in the history
Add dictionary ID to all queries. This also shows dictionary IDs to the
user in the form of the id appended to the end of the name in a bracket.
  • Loading branch information
ripose-jp committed Jan 27, 2025
1 parent fae1186 commit b3eb8fa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/dict/databasemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ int DatabaseManager::initCache()
QHash<QString, Tag> &tags = m_tagCache[id];

Tag tag;
tag.dictionaryId = id;
tag.dictionary = m_dictionaryCache[id],
tag.name = (const char *)sqlite3_column_text(stmt, COLUMN_NAME),
tag.category = (const char *)sqlite3_column_text(stmt, COLUMN_CATEGORY),
Expand Down Expand Up @@ -219,10 +220,13 @@ int DatabaseManager::disableDictionaries(const QStringList &dicts)
/* End Dictionary Database Modifiers */
/* Begin Database Getters */

#define QUERY "SELECT title FROM directory;"
#define QUERY "SELECT dic_id, title FROM directory;"

QStringList DatabaseManager::getDictionaries() const
{
constexpr int COLUMN_DIC_ID = 0;
constexpr int COLUMN_TITLE = 1;

m_dbLock.lockForRead();

QStringList dictionaries;
Expand All @@ -235,7 +239,11 @@ QStringList DatabaseManager::getDictionaries() const
}
while ((step = sqlite3_step(stmt)) == SQLITE_ROW)
{
dictionaries.append((const char *)sqlite3_column_text(stmt, 0));
dictionaries.emplaceBack(
QString("%1 [%2]")
.arg((const char *)sqlite3_column_text(stmt, COLUMN_TITLE))
.arg(sqlite3_column_int64(stmt, COLUMN_DIC_ID))
);
}

cleanup:
Expand Down Expand Up @@ -476,6 +484,7 @@ QString DatabaseManager::queryKanji(const QString &query, Kanji &kanji) const
uint64_t id = sqlite3_column_int64(stmt, COLUMN_DIC_ID);

KanjiDefinition def;
def.dictionaryId = id;
def.dictionary = getDictionary(id),
def.onyomi = QString(
(const char *)sqlite3_column_text(stmt, COLUMN_ONYOMI)
Expand Down Expand Up @@ -608,6 +617,7 @@ int DatabaseManager::populateTerms(const QList<SharedTerm> &terms) const
);

TermDefinition def;
def.dictionaryId = id;
def.dictionary = getDictionary(id);
def.glossary = QJsonDocument::fromJson(
(const char *)sqlite3_column_text(stmt, COLUMN_GLOSSARY)
Expand Down Expand Up @@ -809,10 +819,12 @@ int DatabaseManager::addFrequencies(
default:
continue;
}
freq.append(Frequency {
getDictionary(sqlite3_column_int64(stmt, 0)),
freqStr
});

Frequency f;
f.dictionaryId = sqlite3_column_int64(stmt, 0);
f.dictionary = getDictionary(f.dictionaryId);
f.freq = freqStr;
freq.emplaceBack(std::move(f));
}
if (isStepError(step))
{
Expand Down Expand Up @@ -872,7 +884,8 @@ int DatabaseManager::addPitches(Term &term) const
}

Pitch pitch;
pitch.dictionary = getDictionary(sqlite3_column_int64(stmt, 0));
pitch.dictionaryId = sqlite3_column_int64(stmt, 0);
pitch.dictionary = getDictionary(pitch.dictionaryId);

/* Add mora */
QString currentMora;
Expand Down
15 changes: 15 additions & 0 deletions src/dict/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
*/
struct Tag
{
/* The backend ID of the dictionary this entry comes from */
int dictionaryId;

/* The dictionary the tag comes from */
QString dictionary;

Expand Down Expand Up @@ -68,6 +71,9 @@ struct Tag
*/
struct Frequency
{
/* The backend ID of the dictionary this entry comes from */
int dictionaryId;

/* The name of the frequency dictionary. */
QString dictionary;

Expand All @@ -80,6 +86,9 @@ struct Frequency
*/
struct Pitch
{
/* The backend ID of the dictionary this entry comes from */
int dictionaryId;

/* The name of the dictionary this pitch accent belongs to. */
QString dictionary;

Expand All @@ -95,6 +104,9 @@ struct Pitch
*/
struct TermDefinition
{
/* The backend ID of the dictionary this entry comes from */
int dictionaryId;

/* The name of the dictionary this entry comes from. */
QString dictionary;

Expand Down Expand Up @@ -212,6 +224,9 @@ struct Term : public CommonExpFields
*/
struct KanjiDefinition
{
/* The backend ID of the dictionary this entry comes from */
int dictionaryId;

/* The name of the dictionary the definition comes from. */
QString dictionary;

Expand Down

0 comments on commit b3eb8fa

Please sign in to comment.