-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlWordSource.cpp
More file actions
72 lines (59 loc) · 1.65 KB
/
SqlWordSource.cpp
File metadata and controls
72 lines (59 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "SqlWordSource.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QVariant>
#include <QDebug>
SqlWordSource::SqlWordSource(const QSqlDatabase& db,
const QString& language)
: m_db(db)
, m_language(language)
{
}
QString SqlWordSource::getWord() const
{
if (!m_db.isOpen()) {
qWarning() << "SqlWordSource::getWord: database is not open";
return {};
}
QSqlQuery query(m_db);
// SQLite için basit ve yeterli: rastgele bir kelime seç
query.prepare(
"SELECT text "
"FROM words "
"WHERE length = :len AND language = :lang AND is_answer = 1 "
"ORDER BY RANDOM() "
"LIMIT 1"
);
query.bindValue(":len", GameConfig::WORD_LENGTH);
query.bindValue(":lang", m_language);
if (!query.exec()) {
qWarning() << "SqlWordSource::getWord query failed:" << query.lastError().text();
return {};
}
if (query.next()) {
return query.value(0).toString();
}
// Uygun kayıt yoksa boş döndür.
return {};
}
bool SqlWordSource::contains(const QString& word) const
{
if (!m_db.isOpen()) {
qWarning() << "SqlWordSource::contains: database is not open";
return false;
}
QSqlQuery query(m_db);
query.prepare(
"SELECT 1 "
"FROM words "
"WHERE text = :text AND language = :lang "
"LIMIT 1"
);
query.bindValue(":text", word.toUpper());
query.bindValue(":lang", m_language);
if (!query.exec()) {
qWarning() << "SqlWordSource::contains query failed:" << query.lastError().text();
return false;
}
return query.next();
}